
CONTROL FLOW GRAPH SSA
@begin: scope:[]  from
  to:@2
main: scope:[main]  from @3
  (byte) main::i#0 ← (byte/signed byte/word/signed word/dword/signed dword) 0
  to:main::@1
main::@1: scope:[main]  from main main::@3
  (byte) main::i#2 ← phi( main/(byte) main::i#0 main::@3/(byte) main::i#1 )
  (byte) b::i#0 ← (byte) main::i#2
  call b 
  to:main::@3
main::@3: scope:[main]  from main::@1
  (byte) main::i#3 ← phi( main::@1/(byte) main::i#2 )
  (byte) main::i#1 ← (byte) main::i#3 + rangenext(0,100)
  (bool~) main::$1 ← (byte) main::i#1 != rangelast(0,100)
  if((bool~) main::$1) goto main::@1
  to:main::@return
main::@return: scope:[main]  from main::@3
  return 
  to:@return
b: scope:[b]  from main::@1
  (byte) b::i#1 ← phi( main::@1/(byte) b::i#0 )
  (byte) c::i#0 ← (byte) b::i#1
  call c 
  to:b::@1
b::@1: scope:[b]  from b
  to:b::@return
b::@return: scope:[b]  from b::@1
  return 
  to:@return
@2: scope:[]  from @begin
  (byte*) SCREEN#0 ← ((byte*)) (word/signed word/dword/signed dword) 1024
  to:@3
c: scope:[c]  from b
  (byte) c::i#2 ← phi( b/(byte) c::i#0 )
  (byte) c::j#0 ← (byte/signed byte/word/signed word/dword/signed dword) 0
  to:c::@1
c::@1: scope:[c]  from c c::@1
  (byte) c::j#2 ← phi( c/(byte) c::j#0 c::@1/(byte) c::j#1 )
  (byte) c::i#1 ← phi( c/(byte) c::i#2 c::@1/(byte) c::i#1 )
  *((byte*) SCREEN#0 + (byte) c::j#2) ← (byte) c::i#1
  (byte) c::j#1 ← (byte) c::j#2 + rangenext(0,100)
  (bool~) c::$0 ← (byte) c::j#1 != rangelast(0,100)
  if((bool~) c::$0) goto c::@1
  to:c::@return
c::@return: scope:[c]  from c::@1
  return 
  to:@return
@3: scope:[]  from @2
  call main 
  to:@4
@4: scope:[]  from @3
  to:@end
@end: scope:[]  from @4

SYMBOL TABLE SSA
(label) @2
(label) @3
(label) @4
(label) @begin
(label) @end
(byte*) SCREEN
(byte*) SCREEN#0
(void()) b((byte) b::i)
(label) b::@1
(label) b::@return
(byte) b::i
(byte) b::i#0
(byte) b::i#1
(void()) c((byte) c::i)
(bool~) c::$0
(label) c::@1
(label) c::@return
(byte) c::i
(byte) c::i#0
(byte) c::i#1
(byte) c::i#2
(byte) c::j
(byte) c::j#0
(byte) c::j#1
(byte) c::j#2
(void()) main()
(bool~) main::$1
(label) main::@1
(label) main::@3
(label) main::@return
(byte) main::i
(byte) main::i#0
(byte) main::i#1
(byte) main::i#2
(byte) main::i#3

Culled Empty Block (label) b::@1
Culled Empty Block (label) @4
Successful SSA optimization Pass2CullEmptyBlocks
Alias (byte) main::i#2 = (byte) main::i#3 
Successful SSA optimization Pass2AliasElimination
Self Phi Eliminated (byte) c::i#1
Successful SSA optimization Pass2SelfPhiElimination
Redundant Phi (byte) b::i#1 (byte) b::i#0
Redundant Phi (byte) c::i#2 (byte) c::i#0
Redundant Phi (byte) c::i#1 (byte) c::i#2
Successful SSA optimization Pass2RedundantPhiElimination
Simple Condition (bool~) main::$1 [7] if((byte) main::i#1!=rangelast(0,100)) goto main::@1
Simple Condition (bool~) c::$0 [20] if((byte) c::j#1!=rangelast(0,100)) goto c::@1
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant (const byte) main::i#0 = 0
Constant (const byte*) SCREEN#0 = ((byte*))1024
Constant (const byte) c::j#0 = 0
Successful SSA optimization Pass2ConstantIdentification
Resolved ranged next value main::i#1 ← ++ main::i#2 to ++
Resolved ranged comparison value if(main::i#1!=rangelast(0,100)) goto main::@1 to (byte/signed byte/word/signed word/dword/signed dword) 101
Resolved ranged next value c::j#1 ← ++ c::j#2 to ++
Resolved ranged comparison value if(c::j#1!=rangelast(0,100)) goto c::@1 to (byte/signed byte/word/signed word/dword/signed dword) 101
Culled Empty Block (label) @2
Successful SSA optimization Pass2CullEmptyBlocks
Inlining constant with var siblings (const byte) main::i#0
Inlining constant with var siblings (const byte) c::j#0
Constant inlined main::i#0 = (byte/signed byte/word/signed word/dword/signed dword) 0
Constant inlined c::j#0 = (byte/signed byte/word/signed word/dword/signed dword) 0
Successful SSA optimization Pass2ConstantInlining
Added new block during phi lifting main::@4(between main::@3 and main::@1)
Added new block during phi lifting c::@3(between c::@1 and c::@1)
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @3
Adding NOP phi() at start of @end
Adding NOP phi() at start of main
Adding NOP phi() at start of c
CALL GRAPH
Calls in [] to main:2 
Calls in [main] to b:7 
Calls in [b] to c:13 

Created 2 initial phi equivalence classes
Coalesced [11] main::i#4 ← main::i#1
Coalesced [21] c::j#3 ← c::j#1
Coalesced down to 2 phi equivalence classes
Culled Empty Block (label) main::@4
Culled Empty Block (label) c::@3
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @3
Adding NOP phi() at start of @end
Adding NOP phi() at start of main
Adding NOP phi() at start of c

FINAL CONTROL FLOW GRAPH
@begin: scope:[]  from
  [0] phi()
  to:@3
@3: scope:[]  from @begin
  [1] phi()
  [2] call main 
  to:@end
@end: scope:[]  from @3
  [3] phi()
main: scope:[main]  from @3
  [4] phi()
  to:main::@1
main::@1: scope:[main]  from main main::@3
  [5] (byte) main::i#2 ← phi( main/(byte/signed byte/word/signed word/dword/signed dword) 0 main::@3/(byte) main::i#1 )
  [6] (byte) b::i#0 ← (byte) main::i#2
  [7] call b 
  to:main::@3
main::@3: scope:[main]  from main::@1
  [8] (byte) main::i#1 ← ++ (byte) main::i#2
  [9] if((byte) main::i#1!=(byte/signed byte/word/signed word/dword/signed dword) 101) goto main::@1
  to:main::@return
main::@return: scope:[main]  from main::@3
  [10] return 
  to:@return
b: scope:[b]  from main::@1
  [11] (byte) c::i#0 ← (byte) b::i#0
  [12] call c 
  to:b::@return
b::@return: scope:[b]  from b
  [13] return 
  to:@return
c: scope:[c]  from b
  [14] phi()
  to:c::@1
c::@1: scope:[c]  from c c::@1
  [15] (byte) c::j#2 ← phi( c/(byte/signed byte/word/signed word/dword/signed dword) 0 c::@1/(byte) c::j#1 )
  [16] *((const byte*) SCREEN#0 + (byte) c::j#2) ← (byte) c::i#0
  [17] (byte) c::j#1 ← ++ (byte) c::j#2
  [18] if((byte) c::j#1!=(byte/signed byte/word/signed word/dword/signed dword) 101) goto c::@1
  to:c::@return
c::@return: scope:[c]  from c::@1
  [19] return 
  to:@return


VARIABLE REGISTER WEIGHTS
(byte*) SCREEN
(void()) b((byte) b::i)
(byte) b::i
(byte) b::i#0 13.0
(void()) c((byte) c::i)
(byte) c::i
(byte) c::i#0 17.166666666666664
(byte) c::j
(byte) c::j#1 151.5
(byte) c::j#2 151.5
(void()) main()
(byte) main::i
(byte) main::i#1 16.5
(byte) main::i#2 11.0

Initial phi equivalence classes
[ main::i#2 main::i#1 ]
[ c::j#2 c::j#1 ]
Added variable b::i#0 to zero page equivalence class [ b::i#0 ]
Added variable c::i#0 to zero page equivalence class [ c::i#0 ]
Complete equivalence classes
[ main::i#2 main::i#1 ]
[ c::j#2 c::j#1 ]
[ b::i#0 ]
[ c::i#0 ]
Allocated zp ZP_BYTE:2 [ main::i#2 main::i#1 ]
Allocated zp ZP_BYTE:3 [ c::j#2 c::j#1 ]
Allocated zp ZP_BYTE:4 [ b::i#0 ]
Allocated zp ZP_BYTE:5 [ c::i#0 ]

INITIAL ASM
//SEG0 File Comments
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
//SEG2 Global Constants & labels
  .label SCREEN = $400
//SEG3 @begin
bbegin:
//SEG4 [1] phi from @begin to @3 [phi:@begin->@3]
b3_from_bbegin:
  jmp b3
//SEG5 @3
b3:
//SEG6 [2] call main 
//SEG7 [4] phi from @3 to main [phi:@3->main]
main_from_b3:
  jsr main
//SEG8 [3] phi from @3 to @end [phi:@3->@end]
bend_from_b3:
  jmp bend
//SEG9 @end
bend:
//SEG10 main
main: {
    .label i = 2
  //SEG11 [5] phi from main to main::@1 [phi:main->main::@1]
  b1_from_main:
  //SEG12 [5] phi (byte) main::i#2 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:main->main::@1#0] -- vbuz1=vbuc1 
    lda #0
    sta i
    jmp b1
  //SEG13 [5] phi from main::@3 to main::@1 [phi:main::@3->main::@1]
  b1_from_b3:
  //SEG14 [5] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@3->main::@1#0] -- register_copy 
    jmp b1
  //SEG15 main::@1
  b1:
  //SEG16 [6] (byte) b::i#0 ← (byte) main::i#2 -- vbuz1=vbuz2 
    lda i
    sta b.i
  //SEG17 [7] call b 
    jsr b
    jmp b3
  //SEG18 main::@3
  b3:
  //SEG19 [8] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuz1=_inc_vbuz1 
    inc i
  //SEG20 [9] if((byte) main::i#1!=(byte/signed byte/word/signed word/dword/signed dword) 101) goto main::@1 -- vbuz1_neq_vbuc1_then_la1 
    lda i
    cmp #$65
    bne b1_from_b3
    jmp breturn
  //SEG21 main::@return
  breturn:
  //SEG22 [10] return 
    rts
}
//SEG23 b
// b(byte zeropage(4) i)
b: {
    .label i = 4
  //SEG24 [11] (byte) c::i#0 ← (byte) b::i#0 -- vbuz1=vbuz2 
    lda i
    sta c.i
  //SEG25 [12] call c 
  //SEG26 [14] phi from b to c [phi:b->c]
  c_from_b:
    jsr c
    jmp breturn
  //SEG27 b::@return
  breturn:
  //SEG28 [13] return 
    rts
}
//SEG29 c
// c(byte zeropage(5) i)
c: {
    .label i = 5
    .label j = 3
  //SEG30 [15] phi from c to c::@1 [phi:c->c::@1]
  b1_from_c:
  //SEG31 [15] phi (byte) c::j#2 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:c->c::@1#0] -- vbuz1=vbuc1 
    lda #0
    sta j
    jmp b1
  //SEG32 [15] phi from c::@1 to c::@1 [phi:c::@1->c::@1]
  b1_from_b1:
  //SEG33 [15] phi (byte) c::j#2 = (byte) c::j#1 [phi:c::@1->c::@1#0] -- register_copy 
    jmp b1
  //SEG34 c::@1
  b1:
  //SEG35 [16] *((const byte*) SCREEN#0 + (byte) c::j#2) ← (byte) c::i#0 -- pbuc1_derefidx_vbuz1=vbuz2 
    lda i
    ldy j
    sta SCREEN,y
  //SEG36 [17] (byte) c::j#1 ← ++ (byte) c::j#2 -- vbuz1=_inc_vbuz1 
    inc j
  //SEG37 [18] if((byte) c::j#1!=(byte/signed byte/word/signed word/dword/signed dword) 101) goto c::@1 -- vbuz1_neq_vbuc1_then_la1 
    lda j
    cmp #$65
    bne b1_from_b1
    jmp breturn
  //SEG38 c::@return
  breturn:
  //SEG39 [19] return 
    rts
}

REGISTER UPLIFT POTENTIAL REGISTERS
Potential registers zp ZP_BYTE:2 [ main::i#2 main::i#1 ] : zp ZP_BYTE:2 , reg byte a , reg byte x , reg byte y , 
Potential registers zp ZP_BYTE:3 [ c::j#2 c::j#1 ] : zp ZP_BYTE:3 , reg byte a , reg byte x , reg byte y , 
Potential registers zp ZP_BYTE:4 [ b::i#0 ] : zp ZP_BYTE:4 , reg byte a , reg byte x , reg byte y , 
Potential registers zp ZP_BYTE:5 [ c::i#0 ] : zp ZP_BYTE:5 , reg byte a , reg byte x , reg byte y , 

REGISTER UPLIFT SCOPES
Uplift Scope [c] 303: zp ZP_BYTE:3 [ c::j#2 c::j#1 ] 17.17: zp ZP_BYTE:5 [ c::i#0 ] 
Uplift Scope [main] 27.5: zp ZP_BYTE:2 [ main::i#2 main::i#1 ] 
Uplift Scope [b] 13: zp ZP_BYTE:4 [ b::i#0 ] 
Uplift Scope [] 

Uplifting [c] best 2707 combination reg byte x [ c::j#2 c::j#1 ] reg byte a [ c::i#0 ] 
Uplifting [main] best 2587 combination reg byte y [ main::i#2 main::i#1 ] 
Uplifting [b] best 2556 combination reg byte y [ b::i#0 ] 
Uplifting [] best 2556 combination 

ASSEMBLER BEFORE OPTIMIZATION
//SEG0 File Comments
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
//SEG2 Global Constants & labels
  .label SCREEN = $400
//SEG3 @begin
bbegin:
//SEG4 [1] phi from @begin to @3 [phi:@begin->@3]
b3_from_bbegin:
  jmp b3
//SEG5 @3
b3:
//SEG6 [2] call main 
//SEG7 [4] phi from @3 to main [phi:@3->main]
main_from_b3:
  jsr main
//SEG8 [3] phi from @3 to @end [phi:@3->@end]
bend_from_b3:
  jmp bend
//SEG9 @end
bend:
//SEG10 main
main: {
  //SEG11 [5] phi from main to main::@1 [phi:main->main::@1]
  b1_from_main:
  //SEG12 [5] phi (byte) main::i#2 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:main->main::@1#0] -- vbuyy=vbuc1 
    ldy #0
    jmp b1
  //SEG13 [5] phi from main::@3 to main::@1 [phi:main::@3->main::@1]
  b1_from_b3:
  //SEG14 [5] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@3->main::@1#0] -- register_copy 
    jmp b1
  //SEG15 main::@1
  b1:
  //SEG16 [6] (byte) b::i#0 ← (byte) main::i#2
  //SEG17 [7] call b 
    jsr b
    jmp b3
  //SEG18 main::@3
  b3:
  //SEG19 [8] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuyy=_inc_vbuyy 
    iny
  //SEG20 [9] if((byte) main::i#1!=(byte/signed byte/word/signed word/dword/signed dword) 101) goto main::@1 -- vbuyy_neq_vbuc1_then_la1 
    cpy #$65
    bne b1_from_b3
    jmp breturn
  //SEG21 main::@return
  breturn:
  //SEG22 [10] return 
    rts
}
//SEG23 b
// b(byte register(Y) i)
b: {
  //SEG24 [11] (byte) c::i#0 ← (byte) b::i#0 -- vbuaa=vbuyy 
    tya
  //SEG25 [12] call c 
  //SEG26 [14] phi from b to c [phi:b->c]
  c_from_b:
    jsr c
    jmp breturn
  //SEG27 b::@return
  breturn:
  //SEG28 [13] return 
    rts
}
//SEG29 c
// c(byte register(A) i)
c: {
  //SEG30 [15] phi from c to c::@1 [phi:c->c::@1]
  b1_from_c:
  //SEG31 [15] phi (byte) c::j#2 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:c->c::@1#0] -- vbuxx=vbuc1 
    ldx #0
    jmp b1
  //SEG32 [15] phi from c::@1 to c::@1 [phi:c::@1->c::@1]
  b1_from_b1:
  //SEG33 [15] phi (byte) c::j#2 = (byte) c::j#1 [phi:c::@1->c::@1#0] -- register_copy 
    jmp b1
  //SEG34 c::@1
  b1:
  //SEG35 [16] *((const byte*) SCREEN#0 + (byte) c::j#2) ← (byte) c::i#0 -- pbuc1_derefidx_vbuxx=vbuaa 
    sta SCREEN,x
  //SEG36 [17] (byte) c::j#1 ← ++ (byte) c::j#2 -- vbuxx=_inc_vbuxx 
    inx
  //SEG37 [18] if((byte) c::j#1!=(byte/signed byte/word/signed word/dword/signed dword) 101) goto c::@1 -- vbuxx_neq_vbuc1_then_la1 
    cpx #$65
    bne b1_from_b1
    jmp breturn
  //SEG38 c::@return
  breturn:
  //SEG39 [19] return 
    rts
}

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp b3
Removing instruction jmp bend
Removing instruction jmp b1
Removing instruction jmp b3
Removing instruction jmp breturn
Removing instruction jmp breturn
Removing instruction jmp b1
Removing instruction jmp breturn
Succesful ASM optimization Pass5NextJumpElimination
Replacing label b1_from_b3 with b1
Replacing label b1_from_b1 with b1
Removing instruction b3_from_bbegin:
Removing instruction b3:
Removing instruction main_from_b3:
Removing instruction bend_from_b3:
Removing instruction b1_from_b3:
Removing instruction b1_from_b1:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction bend:
Removing instruction b1_from_main:
Removing instruction b3:
Removing instruction breturn:
Removing instruction c_from_b:
Removing instruction breturn:
Removing instruction b1_from_c:
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
Removing instruction jmp b1
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction bbegin:
Succesful ASM optimization Pass5UnusedLabelElimination

FINAL SYMBOL TABLE
(label) @3
(label) @begin
(label) @end
(byte*) SCREEN
(const byte*) SCREEN#0 SCREEN = ((byte*))(word/signed word/dword/signed dword) 1024
(void()) b((byte) b::i)
(label) b::@return
(byte) b::i
(byte) b::i#0 reg byte y 13.0
(void()) c((byte) c::i)
(label) c::@1
(label) c::@return
(byte) c::i
(byte) c::i#0 reg byte a 17.166666666666664
(byte) c::j
(byte) c::j#1 reg byte x 151.5
(byte) c::j#2 reg byte x 151.5
(void()) main()
(label) main::@1
(label) main::@3
(label) main::@return
(byte) main::i
(byte) main::i#1 reg byte y 16.5
(byte) main::i#2 reg byte y 11.0

reg byte y [ main::i#2 main::i#1 ]
reg byte x [ c::j#2 c::j#1 ]
reg byte y [ b::i#0 ]
reg byte a [ c::i#0 ]


FINAL ASSEMBLER
Score: 1521

//SEG0 File Comments
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
//SEG2 Global Constants & labels
  .label SCREEN = $400
//SEG3 @begin
//SEG4 [1] phi from @begin to @3 [phi:@begin->@3]
//SEG5 @3
//SEG6 [2] call main 
//SEG7 [4] phi from @3 to main [phi:@3->main]
//SEG8 [3] phi from @3 to @end [phi:@3->@end]
//SEG9 @end
//SEG10 main
main: {
  //SEG11 [5] phi from main to main::@1 [phi:main->main::@1]
  //SEG12 [5] phi (byte) main::i#2 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:main->main::@1#0] -- vbuyy=vbuc1 
    ldy #0
  //SEG13 [5] phi from main::@3 to main::@1 [phi:main::@3->main::@1]
  //SEG14 [5] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@3->main::@1#0] -- register_copy 
  //SEG15 main::@1
  b1:
  //SEG16 [6] (byte) b::i#0 ← (byte) main::i#2
  //SEG17 [7] call b 
    jsr b
  //SEG18 main::@3
  //SEG19 [8] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuyy=_inc_vbuyy 
    iny
  //SEG20 [9] if((byte) main::i#1!=(byte/signed byte/word/signed word/dword/signed dword) 101) goto main::@1 -- vbuyy_neq_vbuc1_then_la1 
    cpy #$65
    bne b1
  //SEG21 main::@return
  //SEG22 [10] return 
    rts
}
//SEG23 b
// b(byte register(Y) i)
b: {
  //SEG24 [11] (byte) c::i#0 ← (byte) b::i#0 -- vbuaa=vbuyy 
    tya
  //SEG25 [12] call c 
  //SEG26 [14] phi from b to c [phi:b->c]
    jsr c
  //SEG27 b::@return
  //SEG28 [13] return 
    rts
}
//SEG29 c
// c(byte register(A) i)
c: {
  //SEG30 [15] phi from c to c::@1 [phi:c->c::@1]
  //SEG31 [15] phi (byte) c::j#2 = (byte/signed byte/word/signed word/dword/signed dword) 0 [phi:c->c::@1#0] -- vbuxx=vbuc1 
    ldx #0
  //SEG32 [15] phi from c::@1 to c::@1 [phi:c::@1->c::@1]
  //SEG33 [15] phi (byte) c::j#2 = (byte) c::j#1 [phi:c::@1->c::@1#0] -- register_copy 
  //SEG34 c::@1
  b1:
  //SEG35 [16] *((const byte*) SCREEN#0 + (byte) c::j#2) ← (byte) c::i#0 -- pbuc1_derefidx_vbuxx=vbuaa 
    sta SCREEN,x
  //SEG36 [17] (byte) c::j#1 ← ++ (byte) c::j#2 -- vbuxx=_inc_vbuxx 
    inx
  //SEG37 [18] if((byte) c::j#1!=(byte/signed byte/word/signed word/dword/signed dword) 101) goto c::@1 -- vbuxx_neq_vbuc1_then_la1 
    cpx #$65
    bne b1
  //SEG38 c::@return
  //SEG39 [19] return 
    rts
}

