Fixing pointer addition (word*~) bsearch16u::$7 ← (word*) bsearch16u::items + (byte~) bsearch16u::$6
Fixing pointer addition (word*~) bsearch16u::$15 ← (word*) bsearch16u::pivot + (number) 1
Fixing pointer addition (word*~) bsearch16u::$1 ← (word*) bsearch16u::items - (number) 1
Fixing pointer array-indexing *((word*) utoa::digit_values + (byte) utoa::digit)
Fixing pointer array-indexing *((dword*) ultoa::digit_values + (byte) ultoa::digit)
Warning! Adding boolean cast to non-boolean condition *((byte*) strcpy::src)
Warning! Adding boolean cast to non-boolean condition *((byte*) strlen::str)
Identified constant variable (byte*) HEAP_TOP
Identified constant variable (byte*) main::screen
Culled Empty Block (label) @1
Culled Empty Block (label) @2
Culled Empty Block (label) @3
Culled Empty Block (label) @4
Culled Empty Block (label) malloc::@1
Culled Empty Block (label) @6
Culled Empty Block (label) @7
Culled Empty Block (label) @8
Culled Empty Block (label) @9
Culled Empty Block (label) @10
Culled Empty Block (label) @11
Culled Empty Block (label) @12
Culled Empty Block (label) @13

CONTROL FLOW GRAPH SSA
@begin: scope:[]  from
  to:@5
@5: scope:[]  from @begin
  (byte*) heap_head#0 ← (const byte*) HEAP_TOP
  to:@14

(void*()) malloc((word) malloc::size)
malloc: scope:[malloc]  from main main::@3
  (word) malloc::size#2 ← phi( main/(word) malloc::size#0 main::@3/(word) malloc::size#1 )
  (byte*) heap_head#7 ← phi( main/(byte*) heap_head#13 main::@3/(byte*) heap_head#3 )
  (byte*~) malloc::$0 ← (byte*) heap_head#7 - (word) malloc::size#2
  (byte*) malloc::mem#0 ← (byte*~) malloc::$0
  (byte*) heap_head#1 ← (byte*) malloc::mem#0
  (void*) malloc::return#0 ← ((void*)) (byte*) malloc::mem#0
  to:malloc::@return
malloc::@return: scope:[malloc]  from malloc
  (byte*) heap_head#8 ← phi( malloc/(byte*) heap_head#1 )
  (void*) malloc::return#4 ← phi( malloc/(void*) malloc::return#0 )
  (void*) malloc::return#1 ← (void*) malloc::return#4
  (byte*) heap_head#2 ← (byte*) heap_head#8
  return 
  to:@return

(void()) free((void*) free::ptr)
free: scope:[free]  from main::@2 main::@5
  to:free::@return
free::@return: scope:[free]  from free
  return 
  to:@return

(void()) main()
main: scope:[main]  from @14
  (byte*) heap_head#13 ← phi( @14/(byte*) heap_head#15 )
  (word) malloc::size#0 ← (number) $64
  call malloc 
  (void*) malloc::return#2 ← (void*) malloc::return#1
  to:main::@3
main::@3: scope:[main]  from main
  (byte*) heap_head#9 ← phi( main/(byte*) heap_head#2 )
  (void*) malloc::return#5 ← phi( main/(void*) malloc::return#2 )
  (void*~) main::$0 ← (void*) malloc::return#5
  (byte*) heap_head#3 ← (byte*) heap_head#9
  (byte*) main::buf1#0 ← ((byte*)) (void*~) main::$0
  (word) malloc::size#1 ← (number) $64
  call malloc 
  (void*) malloc::return#3 ← (void*) malloc::return#1
  to:main::@4
main::@4: scope:[main]  from main::@3
  (byte*) main::buf1#4 ← phi( main::@3/(byte*) main::buf1#0 )
  (byte*) heap_head#10 ← phi( main::@3/(byte*) heap_head#2 )
  (void*) malloc::return#6 ← phi( main::@3/(void*) malloc::return#3 )
  (void*~) main::$1 ← (void*) malloc::return#6
  (byte*) heap_head#4 ← (byte*) heap_head#10
  (byte*) main::buf2#0 ← ((byte*)) (void*~) main::$1
  (byte) main::i#0 ← (byte) 0
  to:main::@1
main::@1: scope:[main]  from main::@1 main::@4
  (byte*) heap_head#18 ← phi( main::@1/(byte*) heap_head#18 main::@4/(byte*) heap_head#4 )
  (byte*) main::buf2#1 ← phi( main::@1/(byte*) main::buf2#1 main::@4/(byte*) main::buf2#0 )
  (byte*) main::buf1#1 ← phi( main::@1/(byte*) main::buf1#1 main::@4/(byte*) main::buf1#4 )
  (byte) main::i#2 ← phi( main::@1/(byte) main::i#1 main::@4/(byte) main::i#0 )
  *((byte*) main::buf1#1 + (byte) main::i#2) ← (byte) main::i#2
  (number~) main::$4 ← (number) $ff - (byte) main::i#2
  *((byte*) main::buf2#1 + (byte) main::i#2) ← (number~) main::$4
  (byte) main::i#1 ← (byte) main::i#2 + rangenext(0,$63)
  (bool~) main::$5 ← (byte) main::i#1 != rangelast(0,$63)
  if((bool~) main::$5) goto main::@1
  to:main::@2
main::@2: scope:[main]  from main::@1
  (byte*) heap_head#17 ← phi( main::@1/(byte*) heap_head#18 )
  (byte*) main::buf2#4 ← phi( main::@1/(byte*) main::buf2#1 )
  (byte*) main::buf1#2 ← phi( main::@1/(byte*) main::buf1#1 )
  (void*) free::ptr#0 ← (void*)(byte*) main::buf1#2
  call free 
  to:main::@5
main::@5: scope:[main]  from main::@2
  (byte*) heap_head#16 ← phi( main::@2/(byte*) heap_head#17 )
  (byte*) main::buf1#5 ← phi( main::@2/(byte*) main::buf1#2 )
  (byte*) main::buf2#2 ← phi( main::@2/(byte*) main::buf2#4 )
  (void*) free::ptr#1 ← (void*)(byte*) main::buf2#2
  call free 
  to:main::@6
main::@6: scope:[main]  from main::@5
  (byte*) heap_head#14 ← phi( main::@5/(byte*) heap_head#16 )
  (byte*) main::buf2#3 ← phi( main::@5/(byte*) main::buf2#2 )
  (byte*) main::buf1#3 ← phi( main::@5/(byte*) main::buf1#5 )
  *((const byte*) main::screen + (number) 0) ← *((byte*) main::buf1#3)
  *((const byte*) main::screen + (number) 1) ← *((byte*) main::buf2#3)
  to:main::@return
main::@return: scope:[main]  from main::@6
  (byte*) heap_head#11 ← phi( main::@6/(byte*) heap_head#14 )
  (byte*) heap_head#5 ← (byte*) heap_head#11
  return 
  to:@return
@14: scope:[]  from @5
  (byte*) heap_head#15 ← phi( @5/(byte*) heap_head#0 )
  call main 
  to:@15
@15: scope:[]  from @14
  (byte*) heap_head#12 ← phi( @14/(byte*) heap_head#5 )
  (byte*) heap_head#6 ← (byte*) heap_head#12
  to:@end
@end: scope:[]  from @15

SYMBOL TABLE SSA
(label) @14
(label) @15
(label) @5
(label) @begin
(label) @end
(const byte*) HEAP_TOP = (byte*)(number) $a000
(const byte) RADIX::BINARY = (number) 2
(const byte) RADIX::DECIMAL = (number) $a
(const byte) RADIX::HEXADECIMAL = (number) $10
(const byte) RADIX::OCTAL = (number) 8
(void()) free((void*) free::ptr)
(label) free::@return
(void*) free::ptr
(void*) free::ptr#0
(void*) free::ptr#1
(byte*) heap_head
(byte*) heap_head#0
(byte*) heap_head#1
(byte*) heap_head#10
(byte*) heap_head#11
(byte*) heap_head#12
(byte*) heap_head#13
(byte*) heap_head#14
(byte*) heap_head#15
(byte*) heap_head#16
(byte*) heap_head#17
(byte*) heap_head#18
(byte*) heap_head#2
(byte*) heap_head#3
(byte*) heap_head#4
(byte*) heap_head#5
(byte*) heap_head#6
(byte*) heap_head#7
(byte*) heap_head#8
(byte*) heap_head#9
(void()) main()
(void*~) main::$0
(void*~) main::$1
(number~) main::$4
(bool~) main::$5
(label) main::@1
(label) main::@2
(label) main::@3
(label) main::@4
(label) main::@5
(label) main::@6
(label) main::@return
(byte*) main::buf1
(byte*) main::buf1#0
(byte*) main::buf1#1
(byte*) main::buf1#2
(byte*) main::buf1#3
(byte*) main::buf1#4
(byte*) main::buf1#5
(byte*) main::buf2
(byte*) main::buf2#0
(byte*) main::buf2#1
(byte*) main::buf2#2
(byte*) main::buf2#3
(byte*) main::buf2#4
(byte) main::i
(byte) main::i#0
(byte) main::i#1
(byte) main::i#2
(const byte*) main::screen = (byte*)(number) $400
(void*()) malloc((word) malloc::size)
(byte*~) malloc::$0
(label) malloc::@return
(byte*) malloc::mem
(byte*) malloc::mem#0
(void*) malloc::return
(void*) malloc::return#0
(void*) malloc::return#1
(void*) malloc::return#2
(void*) malloc::return#3
(void*) malloc::return#4
(void*) malloc::return#5
(void*) malloc::return#6
(word) malloc::size
(word) malloc::size#0
(word) malloc::size#1
(word) malloc::size#2

Adding number conversion cast (unumber) $64 in (word) malloc::size#0 ← (number) $64
Adding number conversion cast (unumber) $64 in (word) malloc::size#1 ← (number) $64
Adding number conversion cast (unumber) $ff in (number~) main::$4 ← (number) $ff - (byte) main::i#2
Adding number conversion cast (unumber) main::$4 in (number~) main::$4 ← (unumber)(number) $ff - (byte) main::i#2
Adding number conversion cast (unumber) 0 in *((const byte*) main::screen + (number) 0) ← *((byte*) main::buf1#3)
Adding number conversion cast (unumber) 1 in *((const byte*) main::screen + (number) 1) ← *((byte*) main::buf2#3)
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast (void*) malloc::return#0 ← (void*)(byte*) malloc::mem#0
Inlining cast (word) malloc::size#0 ← (unumber)(number) $64
Inlining cast (byte*) main::buf1#0 ← (byte*)(void*~) main::$0
Inlining cast (word) malloc::size#1 ← (unumber)(number) $64
Inlining cast (byte*) main::buf2#0 ← (byte*)(void*~) main::$1
Successful SSA optimization Pass2InlineCast
Simplifying constant pointer cast (byte*) 1024
Simplifying constant pointer cast (byte*) 40960
Simplifying constant integer cast $64
Simplifying constant integer cast $64
Simplifying constant integer cast $ff
Simplifying constant integer cast 0
Simplifying constant integer cast 1
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) $64
Finalized unsigned number type (byte) $64
Finalized unsigned number type (byte) $ff
Finalized unsigned number type (byte) 0
Finalized unsigned number type (byte) 1
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inferred type updated to byte in (unumber~) main::$4 ← (byte) $ff - (byte) main::i#2
Alias (byte*) malloc::mem#0 = (byte*~) malloc::$0 
Alias (void*) malloc::return#0 = (void*) malloc::return#4 (void*) malloc::return#1 
Alias (byte*) heap_head#1 = (byte*) heap_head#8 (byte*) heap_head#2 
Alias (void*) malloc::return#2 = (void*) malloc::return#5 
Alias (byte*) heap_head#3 = (byte*) heap_head#9 
Alias (void*) malloc::return#3 = (void*) malloc::return#6 
Alias (byte*) main::buf1#0 = (byte*) main::buf1#4 
Alias (byte*) heap_head#10 = (byte*) heap_head#4 
Alias (byte*) main::buf1#1 = (byte*) main::buf1#2 (byte*) main::buf1#5 (byte*) main::buf1#3 
Alias (byte*) main::buf2#1 = (byte*) main::buf2#4 (byte*) main::buf2#2 (byte*) main::buf2#3 
Alias (byte*) heap_head#11 = (byte*) heap_head#17 (byte*) heap_head#18 (byte*) heap_head#16 (byte*) heap_head#14 (byte*) heap_head#5 
Alias (byte*) heap_head#0 = (byte*) heap_head#15 
Alias (byte*) heap_head#12 = (byte*) heap_head#6 
Successful SSA optimization Pass2AliasElimination
Identical Phi Values (byte*) heap_head#13 (byte*) heap_head#0
Identical Phi Values (byte*) heap_head#3 (byte*) heap_head#1
Identical Phi Values (byte*) heap_head#10 (byte*) heap_head#1
Identical Phi Values (byte*) main::buf1#1 (byte*) main::buf1#0
Identical Phi Values (byte*) main::buf2#1 (byte*) main::buf2#0
Identical Phi Values (byte*) heap_head#11 (byte*) heap_head#10
Identical Phi Values (byte*) heap_head#12 (byte*) heap_head#11
Successful SSA optimization Pass2IdenticalPhiElimination
Simple Condition (bool~) main::$5 [27] if((byte) main::i#1!=rangelast(0,$63)) goto main::@1
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant (const byte*) heap_head#0 = HEAP_TOP
Constant (const word) malloc::size#0 = $64
Constant (const word) malloc::size#1 = $64
Constant (const byte) main::i#0 = 0
Successful SSA optimization Pass2ConstantIdentification
Resolved ranged next value [25] main::i#1 ← ++ main::i#2 to ++
Resolved ranged comparison value [27] if(main::i#1!=rangelast(0,$63)) goto main::@1 to (number) $64
Simplifying expression containing zero main::screen in [32] *((const byte*) main::screen + (byte) 0) ← *((byte*) main::buf1#0)
Successful SSA optimization PassNSimplifyExpressionWithZero
Eliminating unused variable (void*) free::ptr#0 and assignment [20] (void*) free::ptr#0 ← (void*)(byte*) main::buf1#0
Eliminating unused variable (void*) free::ptr#1 and assignment [22] (void*) free::ptr#1 ← (void*)(byte*) main::buf2#0
Successful SSA optimization PassNEliminateUnusedVars
Adding number conversion cast (unumber) $64 in if((byte) main::i#1!=(number) $64) goto main::@1
Successful SSA optimization PassNAddNumberTypeConversions
Simplifying constant integer cast $64
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) $64
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inlining Noop Cast [3] (void*) malloc::return#0 ← (void*)(byte*) malloc::mem#0 keeping malloc::mem#0
Successful SSA optimization Pass2NopCastInlining
Inlining Noop Cast [7] (void*) malloc::return#2 ← (void*)(byte*) malloc::mem#0 keeping malloc::mem#0
Inlining Noop Cast [11] (void*) malloc::return#3 ← (void*)(byte*) malloc::mem#0 keeping malloc::mem#0
Successful SSA optimization Pass2NopCastInlining
Inlining Noop Cast [9] (byte*) main::buf1#0 ← (byte*)(void*~) main::$0 keeping main::buf1#0
Inlining Noop Cast [13] (byte*) main::buf2#0 ← (byte*)(void*~) main::$1 keeping main::buf2#0
Successful SSA optimization Pass2NopCastInlining
Inlining constant with var siblings (const word) malloc::size#0
Inlining constant with var siblings (const word) malloc::size#1
Inlining constant with var siblings (const byte) main::i#0
Inlining constant with var siblings (const byte*) heap_head#0
Constant inlined heap_head#0 = (const byte*) HEAP_TOP
Constant inlined main::i#0 = (byte) 0
Constant inlined malloc::size#1 = (byte) $64
Constant inlined malloc::size#0 = (byte) $64
Successful SSA optimization Pass2ConstantInlining
Consolidated array index constant in *(main::screen+1)
Successful SSA optimization Pass2ConstantAdditionElimination
Identical Phi Values (word) malloc::size#2 (byte) $64
Successful SSA optimization Pass2IdenticalPhiElimination
Added new block during phi lifting main::@7(between main::@1 and main::@1)
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @5
Adding NOP phi() at start of @14
Adding NOP phi() at start of @15
Adding NOP phi() at start of @end
Adding NOP phi() at start of main
Adding NOP phi() at start of main::@2
Adding NOP phi() at start of main::@5
Adding NOP phi() at start of free
CALL GRAPH
Calls in [] to main:3 
Calls in [main] to malloc:7 malloc:10 free:19 free:21 

Created 2 initial phi equivalence classes
Coalesced [9] heap_head#19 ← heap_head#1
Coalesced [25] main::i#3 ← main::i#1
Not coalescing [30] heap_head#1 ← malloc::mem#0
Coalesced down to 3 phi equivalence classes
Culled Empty Block (label) @5
Culled Empty Block (label) @15
Culled Empty Block (label) main::@7
Renumbering block @14 to @1
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @1
Adding NOP phi() at start of @end
Adding NOP phi() at start of main
Adding NOP phi() at start of main::@2
Adding NOP phi() at start of main::@5
Adding NOP phi() at start of free

FINAL CONTROL FLOW GRAPH
@begin: scope:[]  from
  [0] phi()
  to:@1
@1: scope:[]  from @begin
  [1] phi()
  [2] call main 
  to:@end
@end: scope:[]  from @1
  [3] phi()

(void()) main()
main: scope:[main]  from @1
  [4] phi()
  [5] call malloc 
  to:main::@3
main::@3: scope:[main]  from main
  [6] (void*) main::buf1#0 ← (void*)(byte*) malloc::mem#0
  [7] call malloc 
  to:main::@4
main::@4: scope:[main]  from main::@3
  [8] (void*) main::buf2#0 ← (void*)(byte*) malloc::mem#0
  to:main::@1
main::@1: scope:[main]  from main::@1 main::@4
  [9] (byte) main::i#2 ← phi( main::@1/(byte) main::i#1 main::@4/(byte) 0 )
  [10] *((byte*)(void*) main::buf1#0 + (byte) main::i#2) ← (byte) main::i#2
  [11] (byte~) main::$4 ← (byte) $ff - (byte) main::i#2
  [12] *((byte*)(void*) main::buf2#0 + (byte) main::i#2) ← (byte~) main::$4
  [13] (byte) main::i#1 ← ++ (byte) main::i#2
  [14] if((byte) main::i#1!=(byte) $64) goto main::@1
  to:main::@2
main::@2: scope:[main]  from main::@1
  [15] phi()
  [16] call free 
  to:main::@5
main::@5: scope:[main]  from main::@2
  [17] phi()
  [18] call free 
  to:main::@6
main::@6: scope:[main]  from main::@5
  [19] *((const byte*) main::screen) ← *((byte*)(void*) main::buf1#0)
  [20] *((const byte*) main::screen+(byte) 1) ← *((byte*)(void*) main::buf2#0)
  to:main::@return
main::@return: scope:[main]  from main::@6
  [21] return 
  to:@return

(void()) free((void*) free::ptr)
free: scope:[free]  from main::@2 main::@5
  [22] phi()
  to:free::@return
free::@return: scope:[free]  from free
  [23] return 
  to:@return

(void*()) malloc((word) malloc::size)
malloc: scope:[malloc]  from main main::@3
  [24] (byte*) heap_head#7 ← phi( main/(const byte*) HEAP_TOP main::@3/(byte*) heap_head#1 )
  [25] (byte*) malloc::mem#0 ← (byte*) heap_head#7 - (byte) $64
  [26] (byte*) heap_head#1 ← (byte*) malloc::mem#0
  to:malloc::@return
malloc::@return: scope:[malloc]  from malloc
  [27] return 
  to:@return


VARIABLE REGISTER WEIGHTS
(void()) free((void*) free::ptr)
(void*) free::ptr
(byte*) heap_head
(byte*) heap_head#1 1.0
(byte*) heap_head#7 4.0
(void()) main()
(byte~) main::$4 22.0
(byte*) main::buf1
(void*) main::buf1#0 0.15384615384615385
(byte*) main::buf2
(void*) main::buf2#0 0.16666666666666666
(byte) main::i
(byte) main::i#1 16.5
(byte) main::i#2 16.5
(void*()) malloc((word) malloc::size)
(byte*) malloc::mem
(byte*) malloc::mem#0 0.8
(void*) malloc::return
(word) malloc::size

Initial phi equivalence classes
[ main::i#2 main::i#1 ]
[ heap_head#7 heap_head#1 ]
Added variable main::buf1#0 to live range equivalence class [ main::buf1#0 ]
Added variable main::buf2#0 to live range equivalence class [ main::buf2#0 ]
Added variable main::$4 to live range equivalence class [ main::$4 ]
Added variable malloc::mem#0 to live range equivalence class [ malloc::mem#0 ]
Complete equivalence classes
[ main::i#2 main::i#1 ]
[ heap_head#7 heap_head#1 ]
[ main::buf1#0 ]
[ main::buf2#0 ]
[ main::$4 ]
[ malloc::mem#0 ]
Allocated zp[1]:2 [ main::i#2 main::i#1 ]
Allocated zp[2]:3 [ heap_head#7 heap_head#1 ]
Allocated zp[2]:5 [ main::buf1#0 ]
Allocated zp[2]:7 [ main::buf2#0 ]
Allocated zp[1]:9 [ main::$4 ]
Allocated zp[2]:10 [ malloc::mem#0 ]

INITIAL ASM
Target platform is c64basic / MOS6502X
  // File Comments
// Experiments with malloc()
  // Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
  // Global Constants & labels
  // Top of the heap used by malloc()
  .label HEAP_TOP = $a000
  // Head of the heap. Moved backward each malloc()
  .label heap_head = 3
  // @begin
__bbegin:
  // [1] phi from @begin to @1 [phi:@begin->@1]
__b1_from___bbegin:
  jmp __b1
  // @1
__b1:
  // [2] call main 
  // [4] phi from @1 to main [phi:@1->main]
main_from___b1:
  jsr main
  // [3] phi from @1 to @end [phi:@1->@end]
__bend_from___b1:
  jmp __bend
  // @end
__bend:
  // main
main: {
    .label screen = $400
    .label __4 = 9
    .label buf1 = 5
    .label buf2 = 7
    .label i = 2
    // [5] call malloc 
    // [24] phi from main to malloc [phi:main->malloc]
  malloc_from_main:
    // [24] phi (byte*) heap_head#7 = (const byte*) HEAP_TOP [phi:main->malloc#0] -- pbuz1=pbuc1 
    lda #<HEAP_TOP
    sta.z heap_head
    lda #>HEAP_TOP
    sta.z heap_head+1
    jsr malloc
    jmp __b3
    // main::@3
  __b3:
    // [6] (void*) main::buf1#0 ← (void*)(byte*) malloc::mem#0 -- pvoz1=pvoz2 
    lda.z malloc.mem
    sta.z buf1
    lda.z malloc.mem+1
    sta.z buf1+1
    // [7] call malloc 
    // [24] phi from main::@3 to malloc [phi:main::@3->malloc]
  malloc_from___b3:
    // [24] phi (byte*) heap_head#7 = (byte*) heap_head#1 [phi:main::@3->malloc#0] -- register_copy 
    jsr malloc
    jmp __b4
    // main::@4
  __b4:
    // [8] (void*) main::buf2#0 ← (void*)(byte*) malloc::mem#0 -- pvoz1=pvoz2 
    lda.z malloc.mem
    sta.z buf2
    lda.z malloc.mem+1
    sta.z buf2+1
    // [9] phi from main::@4 to main::@1 [phi:main::@4->main::@1]
  __b1_from___b4:
    // [9] phi (byte) main::i#2 = (byte) 0 [phi:main::@4->main::@1#0] -- vbuz1=vbuc1 
    lda #0
    sta.z i
    jmp __b1
    // [9] phi from main::@1 to main::@1 [phi:main::@1->main::@1]
  __b1_from___b1:
    // [9] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@1->main::@1#0] -- register_copy 
    jmp __b1
    // main::@1
  __b1:
    // [10] *((byte*)(void*) main::buf1#0 + (byte) main::i#2) ← (byte) main::i#2 -- pbuz1_derefidx_vbuz2=vbuz2 
    lda.z i
    tay
    sta (buf1),y
    // [11] (byte~) main::$4 ← (byte) $ff - (byte) main::i#2 -- vbuz1=vbuc1_minus_vbuz2 
    lda #$ff
    sec
    sbc.z i
    sta.z __4
    // [12] *((byte*)(void*) main::buf2#0 + (byte) main::i#2) ← (byte~) main::$4 -- pbuz1_derefidx_vbuz2=vbuz3 
    lda.z __4
    ldy.z i
    sta (buf2),y
    // [13] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuz1=_inc_vbuz1 
    inc.z i
    // [14] if((byte) main::i#1!=(byte) $64) goto main::@1 -- vbuz1_neq_vbuc1_then_la1 
    lda #$64
    cmp.z i
    bne __b1_from___b1
    // [15] phi from main::@1 to main::@2 [phi:main::@1->main::@2]
  __b2_from___b1:
    jmp __b2
    // main::@2
  __b2:
    // [16] call free 
    // [22] phi from main::@2 to free [phi:main::@2->free]
  free_from___b2:
    jsr free
    // [17] phi from main::@2 to main::@5 [phi:main::@2->main::@5]
  __b5_from___b2:
    jmp __b5
    // main::@5
  __b5:
    // [18] call free 
    // [22] phi from main::@5 to free [phi:main::@5->free]
  free_from___b5:
    jsr free
    jmp __b6
    // main::@6
  __b6:
    // [19] *((const byte*) main::screen) ← *((byte*)(void*) main::buf1#0) -- _deref_pbuc1=_deref_pbuz1 
    ldy #0
    lda (buf1),y
    sta screen
    // [20] *((const byte*) main::screen+(byte) 1) ← *((byte*)(void*) main::buf2#0) -- _deref_pbuc1=_deref_pbuz1 
    ldy #0
    lda (buf2),y
    sta screen+1
    jmp __breturn
    // main::@return
  __breturn:
    // [21] return 
    rts
}
  // free
// A block of memory previously allocated by a call to malloc is deallocated, making it available again for further allocations.
// If ptr is a null pointer, the function does nothing.
free: {
    jmp __breturn
    // free::@return
  __breturn:
    // [23] return 
    rts
}
  // malloc
// Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.
// The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.
malloc: {
    .label mem = $a
    // [25] (byte*) malloc::mem#0 ← (byte*) heap_head#7 - (byte) $64 -- pbuz1=pbuz2_minus_vwuc1 
    lda.z heap_head
    sec
    sbc #<$64
    sta.z mem
    lda.z heap_head+1
    sbc #>$64
    sta.z mem+1
    // [26] (byte*) heap_head#1 ← (byte*) malloc::mem#0 -- pbuz1=pbuz2 
    lda.z mem
    sta.z heap_head
    lda.z mem+1
    sta.z heap_head+1
    jmp __breturn
    // malloc::@return
  __breturn:
    // [27] return 
    rts
}
  // File Data

REGISTER UPLIFT POTENTIAL REGISTERS
Statement [6] (void*) main::buf1#0 ← (void*)(byte*) malloc::mem#0 [ main::buf1#0 heap_head#1 ] ( main:2 [ main::buf1#0 heap_head#1 ] ) always clobbers reg byte a 
Statement [8] (void*) main::buf2#0 ← (void*)(byte*) malloc::mem#0 [ main::buf1#0 main::buf2#0 ] ( main:2 [ main::buf1#0 main::buf2#0 ] ) always clobbers reg byte a 
Statement [11] (byte~) main::$4 ← (byte) $ff - (byte) main::i#2 [ main::buf1#0 main::buf2#0 main::i#2 main::$4 ] ( main:2 [ main::buf1#0 main::buf2#0 main::i#2 main::$4 ] ) always clobbers reg byte a 
Removing always clobbered register reg byte a as potential for zp[1]:2 [ main::i#2 main::i#1 ]
Statement [19] *((const byte*) main::screen) ← *((byte*)(void*) main::buf1#0) [ main::buf2#0 ] ( main:2 [ main::buf2#0 ] ) always clobbers reg byte a reg byte y 
Statement [20] *((const byte*) main::screen+(byte) 1) ← *((byte*)(void*) main::buf2#0) [ ] ( main:2 [ ] ) always clobbers reg byte a reg byte y 
Statement [25] (byte*) malloc::mem#0 ← (byte*) heap_head#7 - (byte) $64 [ malloc::mem#0 ] ( main:2::malloc:5 [ malloc::mem#0 ] main:2::malloc:7 [ main::buf1#0 malloc::mem#0 ] ) always clobbers reg byte a 
Statement [26] (byte*) heap_head#1 ← (byte*) malloc::mem#0 [ malloc::mem#0 heap_head#1 ] ( main:2::malloc:5 [ malloc::mem#0 heap_head#1 ] main:2::malloc:7 [ main::buf1#0 malloc::mem#0 heap_head#1 ] ) always clobbers reg byte a 
Statement [6] (void*) main::buf1#0 ← (void*)(byte*) malloc::mem#0 [ main::buf1#0 heap_head#1 ] ( main:2 [ main::buf1#0 heap_head#1 ] ) always clobbers reg byte a 
Statement [8] (void*) main::buf2#0 ← (void*)(byte*) malloc::mem#0 [ main::buf1#0 main::buf2#0 ] ( main:2 [ main::buf1#0 main::buf2#0 ] ) always clobbers reg byte a 
Statement [10] *((byte*)(void*) main::buf1#0 + (byte) main::i#2) ← (byte) main::i#2 [ main::buf1#0 main::buf2#0 main::i#2 ] ( main:2 [ main::buf1#0 main::buf2#0 main::i#2 ] ) always clobbers reg byte a 
Statement [11] (byte~) main::$4 ← (byte) $ff - (byte) main::i#2 [ main::buf1#0 main::buf2#0 main::i#2 main::$4 ] ( main:2 [ main::buf1#0 main::buf2#0 main::i#2 main::$4 ] ) always clobbers reg byte a 
Statement [19] *((const byte*) main::screen) ← *((byte*)(void*) main::buf1#0) [ main::buf2#0 ] ( main:2 [ main::buf2#0 ] ) always clobbers reg byte a reg byte y 
Statement [20] *((const byte*) main::screen+(byte) 1) ← *((byte*)(void*) main::buf2#0) [ ] ( main:2 [ ] ) always clobbers reg byte a reg byte y 
Statement [25] (byte*) malloc::mem#0 ← (byte*) heap_head#7 - (byte) $64 [ malloc::mem#0 ] ( main:2::malloc:5 [ malloc::mem#0 ] main:2::malloc:7 [ main::buf1#0 malloc::mem#0 ] ) always clobbers reg byte a 
Statement [26] (byte*) heap_head#1 ← (byte*) malloc::mem#0 [ malloc::mem#0 heap_head#1 ] ( main:2::malloc:5 [ malloc::mem#0 heap_head#1 ] main:2::malloc:7 [ main::buf1#0 malloc::mem#0 heap_head#1 ] ) always clobbers reg byte a 
Potential registers zp[1]:2 [ main::i#2 main::i#1 ] : zp[1]:2 , reg byte x , reg byte y , 
Potential registers zp[2]:3 [ heap_head#7 heap_head#1 ] : zp[2]:3 , 
Potential registers zp[2]:5 [ main::buf1#0 ] : zp[2]:5 , 
Potential registers zp[2]:7 [ main::buf2#0 ] : zp[2]:7 , 
Potential registers zp[1]:9 [ main::$4 ] : zp[1]:9 , reg byte a , reg byte x , reg byte y , 
Potential registers zp[2]:10 [ malloc::mem#0 ] : zp[2]:10 , 

REGISTER UPLIFT SCOPES
Uplift Scope [main] 33: zp[1]:2 [ main::i#2 main::i#1 ] 22: zp[1]:9 [ main::$4 ] 0.17: zp[2]:7 [ main::buf2#0 ] 0.15: zp[2]:5 [ main::buf1#0 ] 
Uplift Scope [] 5: zp[2]:3 [ heap_head#7 heap_head#1 ] 
Uplift Scope [malloc] 0.8: zp[2]:10 [ malloc::mem#0 ] 
Uplift Scope [free] 
Uplift Scope [RADIX] 

Uplifting [main] best 556 combination reg byte y [ main::i#2 main::i#1 ] reg byte a [ main::$4 ] zp[2]:7 [ main::buf2#0 ] zp[2]:5 [ main::buf1#0 ] 
Uplifting [] best 556 combination zp[2]:3 [ heap_head#7 heap_head#1 ] 
Uplifting [malloc] best 556 combination zp[2]:10 [ malloc::mem#0 ] 
Uplifting [free] best 556 combination 
Uplifting [RADIX] best 556 combination 
Coalescing zero page register [ zp[2]:7 [ main::buf2#0 ] ] with [ zp[2]:10 [ malloc::mem#0 ] ] - score: 1
Allocated (was zp[2]:3) zp[2]:2 [ heap_head#7 heap_head#1 ]
Allocated (was zp[2]:5) zp[2]:4 [ main::buf1#0 ]
Allocated (was zp[2]:7) zp[2]:6 [ main::buf2#0 malloc::mem#0 ]

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Experiments with malloc()
  // Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
  // Global Constants & labels
  // Top of the heap used by malloc()
  .label HEAP_TOP = $a000
  // Head of the heap. Moved backward each malloc()
  .label heap_head = 2
  // @begin
__bbegin:
  // [1] phi from @begin to @1 [phi:@begin->@1]
__b1_from___bbegin:
  jmp __b1
  // @1
__b1:
  // [2] call main 
  // [4] phi from @1 to main [phi:@1->main]
main_from___b1:
  jsr main
  // [3] phi from @1 to @end [phi:@1->@end]
__bend_from___b1:
  jmp __bend
  // @end
__bend:
  // main
main: {
    .label screen = $400
    .label buf1 = 4
    .label buf2 = 6
    // [5] call malloc 
    // [24] phi from main to malloc [phi:main->malloc]
  malloc_from_main:
    // [24] phi (byte*) heap_head#7 = (const byte*) HEAP_TOP [phi:main->malloc#0] -- pbuz1=pbuc1 
    lda #<HEAP_TOP
    sta.z heap_head
    lda #>HEAP_TOP
    sta.z heap_head+1
    jsr malloc
    jmp __b3
    // main::@3
  __b3:
    // [6] (void*) main::buf1#0 ← (void*)(byte*) malloc::mem#0 -- pvoz1=pvoz2 
    lda.z malloc.mem
    sta.z buf1
    lda.z malloc.mem+1
    sta.z buf1+1
    // [7] call malloc 
    // [24] phi from main::@3 to malloc [phi:main::@3->malloc]
  malloc_from___b3:
    // [24] phi (byte*) heap_head#7 = (byte*) heap_head#1 [phi:main::@3->malloc#0] -- register_copy 
    jsr malloc
    jmp __b4
    // main::@4
  __b4:
    // [8] (void*) main::buf2#0 ← (void*)(byte*) malloc::mem#0
    // [9] phi from main::@4 to main::@1 [phi:main::@4->main::@1]
  __b1_from___b4:
    // [9] phi (byte) main::i#2 = (byte) 0 [phi:main::@4->main::@1#0] -- vbuyy=vbuc1 
    ldy #0
    jmp __b1
    // [9] phi from main::@1 to main::@1 [phi:main::@1->main::@1]
  __b1_from___b1:
    // [9] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@1->main::@1#0] -- register_copy 
    jmp __b1
    // main::@1
  __b1:
    // [10] *((byte*)(void*) main::buf1#0 + (byte) main::i#2) ← (byte) main::i#2 -- pbuz1_derefidx_vbuyy=vbuyy 
    tya
    sta (buf1),y
    // [11] (byte~) main::$4 ← (byte) $ff - (byte) main::i#2 -- vbuaa=vbuc1_minus_vbuyy 
    tya
    eor #$ff
    clc
    adc #$ff+1
    // [12] *((byte*)(void*) main::buf2#0 + (byte) main::i#2) ← (byte~) main::$4 -- pbuz1_derefidx_vbuyy=vbuaa 
    sta (buf2),y
    // [13] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuyy=_inc_vbuyy 
    iny
    // [14] if((byte) main::i#1!=(byte) $64) goto main::@1 -- vbuyy_neq_vbuc1_then_la1 
    cpy #$64
    bne __b1_from___b1
    // [15] phi from main::@1 to main::@2 [phi:main::@1->main::@2]
  __b2_from___b1:
    jmp __b2
    // main::@2
  __b2:
    // [16] call free 
    // [22] phi from main::@2 to free [phi:main::@2->free]
  free_from___b2:
    jsr free
    // [17] phi from main::@2 to main::@5 [phi:main::@2->main::@5]
  __b5_from___b2:
    jmp __b5
    // main::@5
  __b5:
    // [18] call free 
    // [22] phi from main::@5 to free [phi:main::@5->free]
  free_from___b5:
    jsr free
    jmp __b6
    // main::@6
  __b6:
    // [19] *((const byte*) main::screen) ← *((byte*)(void*) main::buf1#0) -- _deref_pbuc1=_deref_pbuz1 
    ldy #0
    lda (buf1),y
    sta screen
    // [20] *((const byte*) main::screen+(byte) 1) ← *((byte*)(void*) main::buf2#0) -- _deref_pbuc1=_deref_pbuz1 
    ldy #0
    lda (buf2),y
    sta screen+1
    jmp __breturn
    // main::@return
  __breturn:
    // [21] return 
    rts
}
  // free
// A block of memory previously allocated by a call to malloc is deallocated, making it available again for further allocations.
// If ptr is a null pointer, the function does nothing.
free: {
    jmp __breturn
    // free::@return
  __breturn:
    // [23] return 
    rts
}
  // malloc
// Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.
// The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.
malloc: {
    .label mem = 6
    // [25] (byte*) malloc::mem#0 ← (byte*) heap_head#7 - (byte) $64 -- pbuz1=pbuz2_minus_vwuc1 
    lda.z heap_head
    sec
    sbc #<$64
    sta.z mem
    lda.z heap_head+1
    sbc #>$64
    sta.z mem+1
    // [26] (byte*) heap_head#1 ← (byte*) malloc::mem#0 -- pbuz1=pbuz2 
    lda.z mem
    sta.z heap_head
    lda.z mem+1
    sta.z heap_head+1
    jmp __breturn
    // malloc::@return
  __breturn:
    // [27] return 
    rts
}
  // File Data

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __bend
Removing instruction jmp __b3
Removing instruction jmp __b4
Removing instruction jmp __b1
Removing instruction jmp __b2
Removing instruction jmp __b5
Removing instruction jmp __b6
Removing instruction jmp __breturn
Removing instruction jmp __breturn
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction ldy #0
Succesful ASM optimization Pass5UnnecesaryLoadElimination
Replacing label __bbegin with __b1
Replacing label __b1_from___b1 with __b1
Removing instruction __bbegin:
Removing instruction __b1_from___bbegin:
Removing instruction main_from___b1:
Removing instruction __bend_from___b1:
Removing instruction __b1_from___b4:
Removing instruction __b1_from___b1:
Removing instruction __b2_from___b1:
Removing instruction free_from___b2:
Removing instruction __b5_from___b2:
Removing instruction free_from___b5:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction __bend:
Removing instruction malloc_from_main:
Removing instruction __b3:
Removing instruction malloc_from___b3:
Removing instruction __b4:
Removing instruction __b2:
Removing instruction __b5:
Removing instruction __b6:
Removing instruction __breturn:
Removing instruction __breturn:
Removing instruction __breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
Updating BasicUpstart to call main directly
Removing instruction jsr main
Succesful ASM optimization Pass5SkipBegin
Removing instruction jmp __b1
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction __b1:
Succesful ASM optimization Pass5UnusedLabelElimination

FINAL SYMBOL TABLE
(label) @1
(label) @begin
(label) @end
(const byte*) HEAP_TOP = (byte*) 40960
(const byte) RADIX::BINARY = (number) 2
(const byte) RADIX::DECIMAL = (number) $a
(const byte) RADIX::HEXADECIMAL = (number) $10
(const byte) RADIX::OCTAL = (number) 8
(void()) free((void*) free::ptr)
(label) free::@return
(void*) free::ptr
(byte*) heap_head
(byte*) heap_head#1 heap_head zp[2]:2 1.0
(byte*) heap_head#7 heap_head zp[2]:2 4.0
(void()) main()
(byte~) main::$4 reg byte a 22.0
(label) main::@1
(label) main::@2
(label) main::@3
(label) main::@4
(label) main::@5
(label) main::@6
(label) main::@return
(byte*) main::buf1
(void*) main::buf1#0 buf1 zp[2]:4 0.15384615384615385
(byte*) main::buf2
(void*) main::buf2#0 buf2 zp[2]:6 0.16666666666666666
(byte) main::i
(byte) main::i#1 reg byte y 16.5
(byte) main::i#2 reg byte y 16.5
(const byte*) main::screen = (byte*) 1024
(void*()) malloc((word) malloc::size)
(label) malloc::@return
(byte*) malloc::mem
(byte*) malloc::mem#0 mem zp[2]:6 0.8
(void*) malloc::return
(word) malloc::size

reg byte y [ main::i#2 main::i#1 ]
zp[2]:2 [ heap_head#7 heap_head#1 ]
zp[2]:4 [ main::buf1#0 ]
zp[2]:6 [ main::buf2#0 malloc::mem#0 ]
reg byte a [ main::$4 ]


FINAL ASSEMBLER
Score: 419

  // File Comments
// Experiments with malloc()
  // Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
  // Global Constants & labels
  // Top of the heap used by malloc()
  .label HEAP_TOP = $a000
  // Head of the heap. Moved backward each malloc()
  .label heap_head = 2
  // @begin
  // [1] phi from @begin to @1 [phi:@begin->@1]
  // @1
  // [2] call main 
  // [4] phi from @1 to main [phi:@1->main]
  // [3] phi from @1 to @end [phi:@1->@end]
  // @end
  // main
main: {
    .label screen = $400
    .label buf1 = 4
    .label buf2 = 6
    // malloc(100)
    // [5] call malloc 
    // [24] phi from main to malloc [phi:main->malloc]
    // [24] phi (byte*) heap_head#7 = (const byte*) HEAP_TOP [phi:main->malloc#0] -- pbuz1=pbuc1 
    lda #<HEAP_TOP
    sta.z heap_head
    lda #>HEAP_TOP
    sta.z heap_head+1
    jsr malloc
    // main::@3
    // malloc(100)
    // [6] (void*) main::buf1#0 ← (void*)(byte*) malloc::mem#0 -- pvoz1=pvoz2 
    lda.z malloc.mem
    sta.z buf1
    lda.z malloc.mem+1
    sta.z buf1+1
    // [7] call malloc 
    // [24] phi from main::@3 to malloc [phi:main::@3->malloc]
    // [24] phi (byte*) heap_head#7 = (byte*) heap_head#1 [phi:main::@3->malloc#0] -- register_copy 
    jsr malloc
    // main::@4
    // malloc(100)
    // [8] (void*) main::buf2#0 ← (void*)(byte*) malloc::mem#0
    // [9] phi from main::@4 to main::@1 [phi:main::@4->main::@1]
    // [9] phi (byte) main::i#2 = (byte) 0 [phi:main::@4->main::@1#0] -- vbuyy=vbuc1 
    ldy #0
    // [9] phi from main::@1 to main::@1 [phi:main::@1->main::@1]
    // [9] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@1->main::@1#0] -- register_copy 
    // main::@1
  __b1:
    // buf1[i] = i
    // [10] *((byte*)(void*) main::buf1#0 + (byte) main::i#2) ← (byte) main::i#2 -- pbuz1_derefidx_vbuyy=vbuyy 
    tya
    sta (buf1),y
    // 255-i
    // [11] (byte~) main::$4 ← (byte) $ff - (byte) main::i#2 -- vbuaa=vbuc1_minus_vbuyy 
    tya
    eor #$ff
    clc
    adc #$ff+1
    // buf2[i] = 255-i
    // [12] *((byte*)(void*) main::buf2#0 + (byte) main::i#2) ← (byte~) main::$4 -- pbuz1_derefidx_vbuyy=vbuaa 
    sta (buf2),y
    // for(unsigned char i:0..99)
    // [13] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuyy=_inc_vbuyy 
    iny
    // [14] if((byte) main::i#1!=(byte) $64) goto main::@1 -- vbuyy_neq_vbuc1_then_la1 
    cpy #$64
    bne __b1
    // [15] phi from main::@1 to main::@2 [phi:main::@1->main::@2]
    // main::@2
    // free(buf1)
    // [16] call free 
    // [22] phi from main::@2 to free [phi:main::@2->free]
    jsr free
    // [17] phi from main::@2 to main::@5 [phi:main::@2->main::@5]
    // main::@5
    // free(buf2)
    // [18] call free 
    // [22] phi from main::@5 to free [phi:main::@5->free]
    jsr free
    // main::@6
    // screen[0] = *buf1
    // [19] *((const byte*) main::screen) ← *((byte*)(void*) main::buf1#0) -- _deref_pbuc1=_deref_pbuz1 
    ldy #0
    lda (buf1),y
    sta screen
    // screen[1] = *buf2
    // [20] *((const byte*) main::screen+(byte) 1) ← *((byte*)(void*) main::buf2#0) -- _deref_pbuc1=_deref_pbuz1 
    lda (buf2),y
    sta screen+1
    // main::@return
    // }
    // [21] return 
    rts
}
  // free
// A block of memory previously allocated by a call to malloc is deallocated, making it available again for further allocations.
// If ptr is a null pointer, the function does nothing.
free: {
    // free::@return
    // [23] return 
    rts
}
  // malloc
// Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.
// The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.
malloc: {
    .label mem = 6
    // mem = heap_head-size
    // [25] (byte*) malloc::mem#0 ← (byte*) heap_head#7 - (byte) $64 -- pbuz1=pbuz2_minus_vwuc1 
    lda.z heap_head
    sec
    sbc #<$64
    sta.z mem
    lda.z heap_head+1
    sbc #>$64
    sta.z mem+1
    // heap_head = mem
    // [26] (byte*) heap_head#1 ← (byte*) malloc::mem#0 -- pbuz1=pbuz2 
    lda.z mem
    sta.z heap_head
    lda.z mem+1
    sta.z heap_head+1
    // malloc::@return
    // }
    // [27] return 
    rts
}
  // File Data

