Inlined call (byte~) vicSelectGfxBank::$0 ← call toDd00 (byte*) vicSelectGfxBank::gfx 
Culled Empty Block (label) @1
Culled Empty Block (label) @2
Culled Empty Block (label) @3
Culled Empty Block (label) @4
Culled Empty Block (label) @5
Culled Empty Block (label) @6
Culled Empty Block (label) @7
Culled Empty Block (label) mul16u::@5
Culled Empty Block (label) mul16u::@6
Culled Empty Block (label) mul16u::@8
Culled Empty Block (label) mul16u::@9
Culled Empty Block (label) @8
Culled Empty Block (label) @9

CONTROL FLOW GRAPH SSA
@begin: scope:[]  from
  to:@10

(dword()) mul16u((word) mul16u::a , (word) mul16u::b)
mul16u: scope:[mul16u]  from main
  (word) mul16u::a#5 ← phi( main/(word) mul16u::a#1 )
  (word) mul16u::b#1 ← phi( main/(word) mul16u::b#0 )
  (dword) mul16u::res#0 ← (dword) 0
  (dword) mul16u::mb#0 ← (dword)(word) mul16u::b#1
  to:mul16u::@1
mul16u::@1: scope:[mul16u]  from mul16u mul16u::@4
  (dword) mul16u::mb#5 ← phi( mul16u/(dword) mul16u::mb#0 mul16u::@4/(dword) mul16u::mb#1 )
  (dword) mul16u::res#4 ← phi( mul16u/(dword) mul16u::res#0 mul16u::@4/(dword) mul16u::res#6 )
  (word) mul16u::a#2 ← phi( mul16u/(word) mul16u::a#5 mul16u::@4/(word) mul16u::a#0 )
  (bool~) mul16u::$0 ← (word) mul16u::a#2 != (number) 0
  if((bool~) mul16u::$0) goto mul16u::@2
  to:mul16u::@3
mul16u::@2: scope:[mul16u]  from mul16u::@1
  (dword) mul16u::res#5 ← phi( mul16u::@1/(dword) mul16u::res#4 )
  (dword) mul16u::mb#4 ← phi( mul16u::@1/(dword) mul16u::mb#5 )
  (word) mul16u::a#3 ← phi( mul16u::@1/(word) mul16u::a#2 )
  (number~) mul16u::$1 ← (word) mul16u::a#3 & (number) 1
  (bool~) mul16u::$2 ← (number~) mul16u::$1 != (number) 0
  (bool~) mul16u::$3 ← ! (bool~) mul16u::$2
  if((bool~) mul16u::$3) goto mul16u::@4
  to:mul16u::@7
mul16u::@3: scope:[mul16u]  from mul16u::@1
  (dword) mul16u::res#2 ← phi( mul16u::@1/(dword) mul16u::res#4 )
  (dword) mul16u::return#0 ← (dword) mul16u::res#2
  to:mul16u::@return
mul16u::@4: scope:[mul16u]  from mul16u::@2 mul16u::@7
  (dword) mul16u::res#6 ← phi( mul16u::@2/(dword) mul16u::res#5 mul16u::@7/(dword) mul16u::res#1 )
  (dword) mul16u::mb#2 ← phi( mul16u::@2/(dword) mul16u::mb#4 mul16u::@7/(dword) mul16u::mb#3 )
  (word) mul16u::a#4 ← phi( mul16u::@2/(word) mul16u::a#3 mul16u::@7/(word) mul16u::a#6 )
  (word~) mul16u::$5 ← (word) mul16u::a#4 >> (number) 1
  (word) mul16u::a#0 ← (word~) mul16u::$5
  (dword~) mul16u::$6 ← (dword) mul16u::mb#2 << (number) 1
  (dword) mul16u::mb#1 ← (dword~) mul16u::$6
  to:mul16u::@1
mul16u::@7: scope:[mul16u]  from mul16u::@2
  (word) mul16u::a#6 ← phi( mul16u::@2/(word) mul16u::a#3 )
  (dword) mul16u::mb#3 ← phi( mul16u::@2/(dword) mul16u::mb#4 )
  (dword) mul16u::res#3 ← phi( mul16u::@2/(dword) mul16u::res#5 )
  (dword~) mul16u::$4 ← (dword) mul16u::res#3 + (dword) mul16u::mb#3
  (dword) mul16u::res#1 ← (dword~) mul16u::$4
  to:mul16u::@4
mul16u::@return: scope:[mul16u]  from mul16u::@3
  (dword) mul16u::return#3 ← phi( mul16u::@3/(dword) mul16u::return#0 )
  (dword) mul16u::return#1 ← (dword) mul16u::return#3
  return 
  to:@return

(void()) main()
main: scope:[main]  from @10
  (word) mul16u::a#1 ← (number) 4
  (word) mul16u::b#0 ← (number) $7b
  call mul16u 
  (dword) mul16u::return#2 ← (dword) mul16u::return#1
  to:main::@1
main::@1: scope:[main]  from main
  (dword) mul16u::return#4 ← phi( main/(dword) mul16u::return#2 )
  (dword~) main::$0 ← (dword) mul16u::return#4
  (dword) main::result#0 ← (dword~) main::$0
  (word~) main::$1 ← < (dword) main::result#0
  (word) main::kaputt#0 ← (word~) main::$1
  (byte~) main::$2 ← < (word) main::kaputt#0
  *((const byte*) BORDERCOL) ← (byte~) main::$2
  (byte~) main::$3 ← > (word) main::kaputt#0
  *((const byte*) BGCOL) ← (byte~) main::$3
  to:main::@return
main::@return: scope:[main]  from main::@1
  return 
  to:@return
@10: scope:[]  from @begin
  call main 
  to:@11
@11: scope:[]  from @10
  to:@end
@end: scope:[]  from @11

SYMBOL TABLE SSA
(label) @10
(label) @11
(label) @begin
(label) @end
(const byte*) BGCOL = (byte*)(number) $d021
(const byte*) BORDERCOL = (byte*)(number) $d020
(void()) main()
(dword~) main::$0
(word~) main::$1
(byte~) main::$2
(byte~) main::$3
(label) main::@1
(label) main::@return
(word) main::kaputt
(word) main::kaputt#0
(dword) main::result
(dword) main::result#0
(dword()) mul16u((word) mul16u::a , (word) mul16u::b)
(bool~) mul16u::$0
(number~) mul16u::$1
(bool~) mul16u::$2
(bool~) mul16u::$3
(dword~) mul16u::$4
(word~) mul16u::$5
(dword~) mul16u::$6
(label) mul16u::@1
(label) mul16u::@2
(label) mul16u::@3
(label) mul16u::@4
(label) mul16u::@7
(label) mul16u::@return
(word) mul16u::a
(word) mul16u::a#0
(word) mul16u::a#1
(word) mul16u::a#2
(word) mul16u::a#3
(word) mul16u::a#4
(word) mul16u::a#5
(word) mul16u::a#6
(word) mul16u::b
(word) mul16u::b#0
(word) mul16u::b#1
(dword) mul16u::mb
(dword) mul16u::mb#0
(dword) mul16u::mb#1
(dword) mul16u::mb#2
(dword) mul16u::mb#3
(dword) mul16u::mb#4
(dword) mul16u::mb#5
(dword) mul16u::res
(dword) mul16u::res#0
(dword) mul16u::res#1
(dword) mul16u::res#2
(dword) mul16u::res#3
(dword) mul16u::res#4
(dword) mul16u::res#5
(dword) mul16u::res#6
(dword) mul16u::return
(dword) mul16u::return#0
(dword) mul16u::return#1
(dword) mul16u::return#2
(dword) mul16u::return#3
(dword) mul16u::return#4

Adding number conversion cast (unumber) 0 in (bool~) mul16u::$0 ← (word) mul16u::a#2 != (number) 0
Adding number conversion cast (unumber) 1 in (number~) mul16u::$1 ← (word) mul16u::a#3 & (number) 1
Adding number conversion cast (unumber) mul16u::$1 in (number~) mul16u::$1 ← (word) mul16u::a#3 & (unumber)(number) 1
Adding number conversion cast (unumber) 0 in (bool~) mul16u::$2 ← (unumber~) mul16u::$1 != (number) 0
Adding number conversion cast (unumber) 1 in (word~) mul16u::$5 ← (word) mul16u::a#4 >> (number) 1
Adding number conversion cast (unumber) 1 in (dword~) mul16u::$6 ← (dword) mul16u::mb#2 << (number) 1
Adding number conversion cast (unumber) 4 in (word) mul16u::a#1 ← (number) 4
Adding number conversion cast (unumber) $7b in (word) mul16u::b#0 ← (number) $7b
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast (word) mul16u::a#1 ← (unumber)(number) 4
Inlining cast (word) mul16u::b#0 ← (unumber)(number) $7b
Successful SSA optimization Pass2InlineCast
Simplifying constant pointer cast (byte*) 53280
Simplifying constant pointer cast (byte*) 53281
Simplifying constant integer cast 0
Simplifying constant integer cast 1
Simplifying constant integer cast 0
Simplifying constant integer cast 1
Simplifying constant integer cast 1
Simplifying constant integer cast 4
Simplifying constant integer cast $7b
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (byte) 0
Finalized unsigned number type (byte) 1
Finalized unsigned number type (byte) 0
Finalized unsigned number type (byte) 1
Finalized unsigned number type (byte) 1
Finalized unsigned number type (byte) 4
Finalized unsigned number type (byte) $7b
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inferred type updated to byte in (unumber~) mul16u::$1 ← (word) mul16u::a#3 & (byte) 1
Inversing boolean not [9] (bool~) mul16u::$3 ← (byte~) mul16u::$1 == (byte) 0 from [8] (bool~) mul16u::$2 ← (byte~) mul16u::$1 != (byte) 0
Successful SSA optimization Pass2UnaryNotSimplification
Alias (word) mul16u::a#2 = (word) mul16u::a#3 (word) mul16u::a#6 
Alias (dword) mul16u::mb#3 = (dword) mul16u::mb#4 (dword) mul16u::mb#5 
Alias (dword) mul16u::res#2 = (dword) mul16u::res#5 (dword) mul16u::res#4 (dword) mul16u::return#0 (dword) mul16u::res#3 (dword) mul16u::return#3 (dword) mul16u::return#1 
Alias (word) mul16u::a#0 = (word~) mul16u::$5 
Alias (dword) mul16u::mb#1 = (dword~) mul16u::$6 
Alias (dword) mul16u::res#1 = (dword~) mul16u::$4 
Alias (dword) mul16u::return#2 = (dword) mul16u::return#4 
Alias (dword) main::result#0 = (dword~) main::$0 
Alias (word) main::kaputt#0 = (word~) main::$1 
Successful SSA optimization Pass2AliasElimination
Alias (word) mul16u::a#2 = (word) mul16u::a#4 
Alias (dword) mul16u::mb#2 = (dword) mul16u::mb#3 
Successful SSA optimization Pass2AliasElimination
Identical Phi Values (word) mul16u::b#1 (word) mul16u::b#0
Identical Phi Values (word) mul16u::a#5 (word) mul16u::a#1
Successful SSA optimization Pass2IdenticalPhiElimination
Simple Condition (bool~) mul16u::$0 [5] if((word) mul16u::a#2!=(byte) 0) goto mul16u::@2
Simple Condition (bool~) mul16u::$3 [8] if((byte~) mul16u::$1==(byte) 0) goto mul16u::@4
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant (const dword) mul16u::res#0 = 0
Constant (const word) mul16u::a#1 = 4
Constant (const word) mul16u::b#0 = $7b
Successful SSA optimization Pass2ConstantIdentification
Constant (const dword) mul16u::mb#0 = (dword)mul16u::b#0
Successful SSA optimization Pass2ConstantIdentification
Inlining constant with var siblings (const dword) mul16u::res#0
Inlining constant with var siblings (const word) mul16u::a#1
Inlining constant with var siblings (const dword) mul16u::mb#0
Constant inlined mul16u::mb#0 = (dword)(const word) mul16u::b#0
Constant inlined mul16u::res#0 = (dword) 0
Constant inlined mul16u::a#1 = (byte) 4
Successful SSA optimization Pass2ConstantInlining
Added new block during phi lifting mul16u::@10(between mul16u::@2 and mul16u::@4)
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @10
Adding NOP phi() at start of @11
Adding NOP phi() at start of @end
Adding NOP phi() at start of main
Adding NOP phi() at start of mul16u
Adding NOP phi() at start of mul16u::@3
CALL GRAPH
Calls in [] to main:2 
Calls in [main] to mul16u:6 

Created 4 initial phi equivalence classes
Coalesced [23] mul16u::res#9 ← mul16u::res#1
Coalesced [27] mul16u::a#7 ← mul16u::a#0
Coalesced [28] mul16u::res#7 ← mul16u::res#6
Coalesced [29] mul16u::mb#6 ← mul16u::mb#1
Coalesced (already) [30] mul16u::res#8 ← mul16u::res#2
Coalesced down to 3 phi equivalence classes
Culled Empty Block (label) @11
Culled Empty Block (label) mul16u::@3
Culled Empty Block (label) mul16u::@10
Renumbering block @10 to @1
Renumbering block mul16u::@4 to mul16u::@3
Renumbering block mul16u::@7 to mul16u::@4
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 mul16u

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 mul16u 
  [6] (dword) mul16u::return#2 ← (dword) mul16u::res#2
  to:main::@1
main::@1: scope:[main]  from main
  [7] (dword) main::result#0 ← (dword) mul16u::return#2
  [8] (word) main::kaputt#0 ← < (dword) main::result#0
  [9] (byte~) main::$2 ← < (word) main::kaputt#0
  [10] *((const byte*) BORDERCOL) ← (byte~) main::$2
  [11] (byte~) main::$3 ← > (word) main::kaputt#0
  [12] *((const byte*) BGCOL) ← (byte~) main::$3
  to:main::@return
main::@return: scope:[main]  from main::@1
  [13] return 
  to:@return

(dword()) mul16u((word) mul16u::a , (word) mul16u::b)
mul16u: scope:[mul16u]  from main
  [14] phi()
  to:mul16u::@1
mul16u::@1: scope:[mul16u]  from mul16u mul16u::@3
  [15] (dword) mul16u::mb#2 ← phi( mul16u/(dword)(const word) mul16u::b#0 mul16u::@3/(dword) mul16u::mb#1 )
  [15] (dword) mul16u::res#2 ← phi( mul16u/(dword) 0 mul16u::@3/(dword) mul16u::res#6 )
  [15] (word) mul16u::a#2 ← phi( mul16u/(byte) 4 mul16u::@3/(word) mul16u::a#0 )
  [16] if((word) mul16u::a#2!=(byte) 0) goto mul16u::@2
  to:mul16u::@return
mul16u::@return: scope:[mul16u]  from mul16u::@1
  [17] return 
  to:@return
mul16u::@2: scope:[mul16u]  from mul16u::@1
  [18] (byte~) mul16u::$1 ← (word) mul16u::a#2 & (byte) 1
  [19] if((byte~) mul16u::$1==(byte) 0) goto mul16u::@3
  to:mul16u::@4
mul16u::@4: scope:[mul16u]  from mul16u::@2
  [20] (dword) mul16u::res#1 ← (dword) mul16u::res#2 + (dword) mul16u::mb#2
  to:mul16u::@3
mul16u::@3: scope:[mul16u]  from mul16u::@2 mul16u::@4
  [21] (dword) mul16u::res#6 ← phi( mul16u::@2/(dword) mul16u::res#2 mul16u::@4/(dword) mul16u::res#1 )
  [22] (word) mul16u::a#0 ← (word) mul16u::a#2 >> (byte) 1
  [23] (dword) mul16u::mb#1 ← (dword) mul16u::mb#2 << (byte) 1
  to:mul16u::@1


VARIABLE REGISTER WEIGHTS
(void()) main()
(byte~) main::$2 4.0
(byte~) main::$3 4.0
(word) main::kaputt
(word) main::kaputt#0 2.0
(dword) main::result
(dword) main::result#0 4.0
(dword()) mul16u((word) mul16u::a , (word) mul16u::b)
(byte~) mul16u::$1 22.0
(word) mul16u::a
(word) mul16u::a#0 11.0
(word) mul16u::a#2 7.333333333333333
(word) mul16u::b
(dword) mul16u::mb
(dword) mul16u::mb#1 22.0
(dword) mul16u::mb#2 4.714285714285714
(dword) mul16u::res
(dword) mul16u::res#1 22.0
(dword) mul16u::res#2 5.833333333333333
(dword) mul16u::res#6 11.0
(dword) mul16u::return
(dword) mul16u::return#2 4.0

Initial phi equivalence classes
[ mul16u::a#2 mul16u::a#0 ]
[ mul16u::res#2 mul16u::res#6 mul16u::res#1 ]
[ mul16u::mb#2 mul16u::mb#1 ]
Added variable mul16u::return#2 to live range equivalence class [ mul16u::return#2 ]
Added variable main::result#0 to live range equivalence class [ main::result#0 ]
Added variable main::kaputt#0 to live range equivalence class [ main::kaputt#0 ]
Added variable main::$2 to live range equivalence class [ main::$2 ]
Added variable main::$3 to live range equivalence class [ main::$3 ]
Added variable mul16u::$1 to live range equivalence class [ mul16u::$1 ]
Complete equivalence classes
[ mul16u::a#2 mul16u::a#0 ]
[ mul16u::res#2 mul16u::res#6 mul16u::res#1 ]
[ mul16u::mb#2 mul16u::mb#1 ]
[ mul16u::return#2 ]
[ main::result#0 ]
[ main::kaputt#0 ]
[ main::$2 ]
[ main::$3 ]
[ mul16u::$1 ]
Allocated zp[2]:2 [ mul16u::a#2 mul16u::a#0 ]
Allocated zp[4]:4 [ mul16u::res#2 mul16u::res#6 mul16u::res#1 ]
Allocated zp[4]:8 [ mul16u::mb#2 mul16u::mb#1 ]
Allocated zp[4]:12 [ mul16u::return#2 ]
Allocated zp[4]:16 [ main::result#0 ]
Allocated zp[2]:20 [ main::kaputt#0 ]
Allocated zp[1]:22 [ main::$2 ]
Allocated zp[1]:23 [ main::$3 ]
Allocated zp[1]:24 [ mul16u::$1 ]

INITIAL ASM
Target platform is c64basic / MOS6502X
  // File Comments
// Program where loop-head optimization produces wrong return value
// Reported by Richard-William Loerakker
  // Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
  // Global Constants & labels
  .label BORDERCOL = $d020
  .label BGCOL = $d021
  // @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 __2 = $16
    .label __3 = $17
    .label result = $10
    .label kaputt = $14
    // [5] call mul16u 
    // [14] phi from main to mul16u [phi:main->mul16u]
  mul16u_from_main:
    jsr mul16u
    // [6] (dword) mul16u::return#2 ← (dword) mul16u::res#2 -- vduz1=vduz2 
    lda.z mul16u.res
    sta.z mul16u.return
    lda.z mul16u.res+1
    sta.z mul16u.return+1
    lda.z mul16u.res+2
    sta.z mul16u.return+2
    lda.z mul16u.res+3
    sta.z mul16u.return+3
    jmp __b1
    // main::@1
  __b1:
    // [7] (dword) main::result#0 ← (dword) mul16u::return#2 -- vduz1=vduz2 
    lda.z mul16u.return
    sta.z result
    lda.z mul16u.return+1
    sta.z result+1
    lda.z mul16u.return+2
    sta.z result+2
    lda.z mul16u.return+3
    sta.z result+3
    // [8] (word) main::kaputt#0 ← < (dword) main::result#0 -- vwuz1=_lo_vduz2 
    lda.z result
    sta.z kaputt
    lda.z result+1
    sta.z kaputt+1
    // [9] (byte~) main::$2 ← < (word) main::kaputt#0 -- vbuz1=_lo_vwuz2 
    lda.z kaputt
    sta.z __2
    // [10] *((const byte*) BORDERCOL) ← (byte~) main::$2 -- _deref_pbuc1=vbuz1 
    lda.z __2
    sta BORDERCOL
    // [11] (byte~) main::$3 ← > (word) main::kaputt#0 -- vbuz1=_hi_vwuz2 
    lda.z kaputt+1
    sta.z __3
    // [12] *((const byte*) BGCOL) ← (byte~) main::$3 -- _deref_pbuc1=vbuz1 
    lda.z __3
    sta BGCOL
    jmp __breturn
    // main::@return
  __breturn:
    // [13] return 
    rts
}
  // mul16u
// Perform binary multiplication of two unsigned 16-bit words into a 32-bit unsigned double word
// mul16u(word zp(2) a)
mul16u: {
    .const b = $7b
    .label __1 = $18
    .label a = 2
    .label mb = 8
    .label res = 4
    .label return = $c
    // [15] phi from mul16u to mul16u::@1 [phi:mul16u->mul16u::@1]
  __b1_from_mul16u:
    // [15] phi (dword) mul16u::mb#2 = (dword)(const word) mul16u::b#0 [phi:mul16u->mul16u::@1#0] -- vduz1=vduc1 
    lda #<b
    sta.z mb
    lda #>b
    sta.z mb+1
    lda #<b>>$10
    sta.z mb+2
    lda #>b>>$10
    sta.z mb+3
    // [15] phi (dword) mul16u::res#2 = (dword) 0 [phi:mul16u->mul16u::@1#1] -- vduz1=vduc1 
    lda #<0
    sta.z res
    lda #>0
    sta.z res+1
    lda #<0>>$10
    sta.z res+2
    lda #>0>>$10
    sta.z res+3
    // [15] phi (word) mul16u::a#2 = (byte) 4 [phi:mul16u->mul16u::@1#2] -- vwuz1=vbuc1 
    lda #<4
    sta.z a
    lda #>4
    sta.z a+1
    jmp __b1
    // mul16u::@1
  __b1:
    // [16] if((word) mul16u::a#2!=(byte) 0) goto mul16u::@2 -- vwuz1_neq_0_then_la1 
    lda.z a
    bne __b2
    lda.z a+1
    bne __b2
    jmp __breturn
    // mul16u::@return
  __breturn:
    // [17] return 
    rts
    // mul16u::@2
  __b2:
    // [18] (byte~) mul16u::$1 ← (word) mul16u::a#2 & (byte) 1 -- vbuz1=vwuz2_band_vbuc1 
    lda #1
    and.z a
    sta.z __1
    // [19] if((byte~) mul16u::$1==(byte) 0) goto mul16u::@3 -- vbuz1_eq_0_then_la1 
    lda.z __1
    cmp #0
    beq __b3_from___b2
    jmp __b4
    // mul16u::@4
  __b4:
    // [20] (dword) mul16u::res#1 ← (dword) mul16u::res#2 + (dword) mul16u::mb#2 -- vduz1=vduz1_plus_vduz2 
    lda.z res
    clc
    adc.z mb
    sta.z res
    lda.z res+1
    adc.z mb+1
    sta.z res+1
    lda.z res+2
    adc.z mb+2
    sta.z res+2
    lda.z res+3
    adc.z mb+3
    sta.z res+3
    // [21] phi from mul16u::@2 mul16u::@4 to mul16u::@3 [phi:mul16u::@2/mul16u::@4->mul16u::@3]
  __b3_from___b2:
  __b3_from___b4:
    // [21] phi (dword) mul16u::res#6 = (dword) mul16u::res#2 [phi:mul16u::@2/mul16u::@4->mul16u::@3#0] -- register_copy 
    jmp __b3
    // mul16u::@3
  __b3:
    // [22] (word) mul16u::a#0 ← (word) mul16u::a#2 >> (byte) 1 -- vwuz1=vwuz1_ror_1 
    lsr.z a+1
    ror.z a
    // [23] (dword) mul16u::mb#1 ← (dword) mul16u::mb#2 << (byte) 1 -- vduz1=vduz1_rol_1 
    asl.z mb
    rol.z mb+1
    rol.z mb+2
    rol.z mb+3
    // [15] phi from mul16u::@3 to mul16u::@1 [phi:mul16u::@3->mul16u::@1]
  __b1_from___b3:
    // [15] phi (dword) mul16u::mb#2 = (dword) mul16u::mb#1 [phi:mul16u::@3->mul16u::@1#0] -- register_copy 
    // [15] phi (dword) mul16u::res#2 = (dword) mul16u::res#6 [phi:mul16u::@3->mul16u::@1#1] -- register_copy 
    // [15] phi (word) mul16u::a#2 = (word) mul16u::a#0 [phi:mul16u::@3->mul16u::@1#2] -- register_copy 
    jmp __b1
}
  // File Data

REGISTER UPLIFT POTENTIAL REGISTERS
Statement [6] (dword) mul16u::return#2 ← (dword) mul16u::res#2 [ mul16u::return#2 ] ( main:2 [ mul16u::return#2 ] ) always clobbers reg byte a 
Statement [7] (dword) main::result#0 ← (dword) mul16u::return#2 [ main::result#0 ] ( main:2 [ main::result#0 ] ) always clobbers reg byte a 
Statement [8] (word) main::kaputt#0 ← < (dword) main::result#0 [ main::kaputt#0 ] ( main:2 [ main::kaputt#0 ] ) always clobbers reg byte a 
Statement [9] (byte~) main::$2 ← < (word) main::kaputt#0 [ main::kaputt#0 main::$2 ] ( main:2 [ main::kaputt#0 main::$2 ] ) always clobbers reg byte a 
Statement [11] (byte~) main::$3 ← > (word) main::kaputt#0 [ main::$3 ] ( main:2 [ main::$3 ] ) always clobbers reg byte a 
Statement [16] if((word) mul16u::a#2!=(byte) 0) goto mul16u::@2 [ mul16u::res#2 mul16u::a#2 mul16u::mb#2 ] ( main:2::mul16u:5 [ mul16u::res#2 mul16u::a#2 mul16u::mb#2 ] ) always clobbers reg byte a 
Statement [18] (byte~) mul16u::$1 ← (word) mul16u::a#2 & (byte) 1 [ mul16u::res#2 mul16u::a#2 mul16u::mb#2 mul16u::$1 ] ( main:2::mul16u:5 [ mul16u::res#2 mul16u::a#2 mul16u::mb#2 mul16u::$1 ] ) always clobbers reg byte a 
Statement [20] (dword) mul16u::res#1 ← (dword) mul16u::res#2 + (dword) mul16u::mb#2 [ mul16u::a#2 mul16u::mb#2 mul16u::res#1 ] ( main:2::mul16u:5 [ mul16u::a#2 mul16u::mb#2 mul16u::res#1 ] ) always clobbers reg byte a 
Potential registers zp[2]:2 [ mul16u::a#2 mul16u::a#0 ] : zp[2]:2 , 
Potential registers zp[4]:4 [ mul16u::res#2 mul16u::res#6 mul16u::res#1 ] : zp[4]:4 , 
Potential registers zp[4]:8 [ mul16u::mb#2 mul16u::mb#1 ] : zp[4]:8 , 
Potential registers zp[4]:12 [ mul16u::return#2 ] : zp[4]:12 , 
Potential registers zp[4]:16 [ main::result#0 ] : zp[4]:16 , 
Potential registers zp[2]:20 [ main::kaputt#0 ] : zp[2]:20 , 
Potential registers zp[1]:22 [ main::$2 ] : zp[1]:22 , reg byte a , reg byte x , reg byte y , 
Potential registers zp[1]:23 [ main::$3 ] : zp[1]:23 , reg byte a , reg byte x , reg byte y , 
Potential registers zp[1]:24 [ mul16u::$1 ] : zp[1]:24 , reg byte a , reg byte x , reg byte y , 

REGISTER UPLIFT SCOPES
Uplift Scope [mul16u] 38.83: zp[4]:4 [ mul16u::res#2 mul16u::res#6 mul16u::res#1 ] 26.71: zp[4]:8 [ mul16u::mb#2 mul16u::mb#1 ] 22: zp[1]:24 [ mul16u::$1 ] 18.33: zp[2]:2 [ mul16u::a#2 mul16u::a#0 ] 4: zp[4]:12 [ mul16u::return#2 ] 
Uplift Scope [main] 4: zp[4]:16 [ main::result#0 ] 4: zp[1]:22 [ main::$2 ] 4: zp[1]:23 [ main::$3 ] 2: zp[2]:20 [ main::kaputt#0 ] 
Uplift Scope [] 

Uplifting [mul16u] best 1657 combination zp[4]:4 [ mul16u::res#2 mul16u::res#6 mul16u::res#1 ] zp[4]:8 [ mul16u::mb#2 mul16u::mb#1 ] reg byte a [ mul16u::$1 ] zp[2]:2 [ mul16u::a#2 mul16u::a#0 ] zp[4]:12 [ mul16u::return#2 ] 
Uplifting [main] best 1645 combination zp[4]:16 [ main::result#0 ] reg byte a [ main::$2 ] reg byte a [ main::$3 ] zp[2]:20 [ main::kaputt#0 ] 
Uplifting [] best 1645 combination 
Coalescing zero page register [ zp[4]:4 [ mul16u::res#2 mul16u::res#6 mul16u::res#1 ] ] with [ zp[4]:12 [ mul16u::return#2 ] ] - score: 1
Coalescing zero page register [ zp[4]:4 [ mul16u::res#2 mul16u::res#6 mul16u::res#1 mul16u::return#2 ] ] with [ zp[4]:16 [ main::result#0 ] ] - score: 1
Coalescing zero page register [ zp[2]:20 [ main::kaputt#0 ] ] with [ zp[2]:2 [ mul16u::a#2 mul16u::a#0 ] ]
Allocated (was zp[4]:4) zp[4]:2 [ mul16u::res#2 mul16u::res#6 mul16u::res#1 mul16u::return#2 main::result#0 ]
Allocated (was zp[4]:8) zp[4]:6 [ mul16u::mb#2 mul16u::mb#1 ]
Allocated (was zp[2]:20) zp[2]:10 [ main::kaputt#0 mul16u::a#2 mul16u::a#0 ]

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Program where loop-head optimization produces wrong return value
// Reported by Richard-William Loerakker
  // Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
  // Global Constants & labels
  .label BORDERCOL = $d020
  .label BGCOL = $d021
  // @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 result = 2
    .label kaputt = $a
    // [5] call mul16u 
    // [14] phi from main to mul16u [phi:main->mul16u]
  mul16u_from_main:
    jsr mul16u
    // [6] (dword) mul16u::return#2 ← (dword) mul16u::res#2
    jmp __b1
    // main::@1
  __b1:
    // [7] (dword) main::result#0 ← (dword) mul16u::return#2
    // [8] (word) main::kaputt#0 ← < (dword) main::result#0 -- vwuz1=_lo_vduz2 
    lda.z result
    sta.z kaputt
    lda.z result+1
    sta.z kaputt+1
    // [9] (byte~) main::$2 ← < (word) main::kaputt#0 -- vbuaa=_lo_vwuz1 
    lda.z kaputt
    // [10] *((const byte*) BORDERCOL) ← (byte~) main::$2 -- _deref_pbuc1=vbuaa 
    sta BORDERCOL
    // [11] (byte~) main::$3 ← > (word) main::kaputt#0 -- vbuaa=_hi_vwuz1 
    lda.z kaputt+1
    // [12] *((const byte*) BGCOL) ← (byte~) main::$3 -- _deref_pbuc1=vbuaa 
    sta BGCOL
    jmp __breturn
    // main::@return
  __breturn:
    // [13] return 
    rts
}
  // mul16u
// Perform binary multiplication of two unsigned 16-bit words into a 32-bit unsigned double word
// mul16u(word zp($a) a)
mul16u: {
    .const b = $7b
    .label a = $a
    .label mb = 6
    .label res = 2
    .label return = 2
    // [15] phi from mul16u to mul16u::@1 [phi:mul16u->mul16u::@1]
  __b1_from_mul16u:
    // [15] phi (dword) mul16u::mb#2 = (dword)(const word) mul16u::b#0 [phi:mul16u->mul16u::@1#0] -- vduz1=vduc1 
    lda #<b
    sta.z mb
    lda #>b
    sta.z mb+1
    lda #<b>>$10
    sta.z mb+2
    lda #>b>>$10
    sta.z mb+3
    // [15] phi (dword) mul16u::res#2 = (dword) 0 [phi:mul16u->mul16u::@1#1] -- vduz1=vduc1 
    lda #<0
    sta.z res
    lda #>0
    sta.z res+1
    lda #<0>>$10
    sta.z res+2
    lda #>0>>$10
    sta.z res+3
    // [15] phi (word) mul16u::a#2 = (byte) 4 [phi:mul16u->mul16u::@1#2] -- vwuz1=vbuc1 
    lda #<4
    sta.z a
    lda #>4
    sta.z a+1
    jmp __b1
    // mul16u::@1
  __b1:
    // [16] if((word) mul16u::a#2!=(byte) 0) goto mul16u::@2 -- vwuz1_neq_0_then_la1 
    lda.z a
    bne __b2
    lda.z a+1
    bne __b2
    jmp __breturn
    // mul16u::@return
  __breturn:
    // [17] return 
    rts
    // mul16u::@2
  __b2:
    // [18] (byte~) mul16u::$1 ← (word) mul16u::a#2 & (byte) 1 -- vbuaa=vwuz1_band_vbuc1 
    lda #1
    and.z a
    // [19] if((byte~) mul16u::$1==(byte) 0) goto mul16u::@3 -- vbuaa_eq_0_then_la1 
    cmp #0
    beq __b3_from___b2
    jmp __b4
    // mul16u::@4
  __b4:
    // [20] (dword) mul16u::res#1 ← (dword) mul16u::res#2 + (dword) mul16u::mb#2 -- vduz1=vduz1_plus_vduz2 
    lda.z res
    clc
    adc.z mb
    sta.z res
    lda.z res+1
    adc.z mb+1
    sta.z res+1
    lda.z res+2
    adc.z mb+2
    sta.z res+2
    lda.z res+3
    adc.z mb+3
    sta.z res+3
    // [21] phi from mul16u::@2 mul16u::@4 to mul16u::@3 [phi:mul16u::@2/mul16u::@4->mul16u::@3]
  __b3_from___b2:
  __b3_from___b4:
    // [21] phi (dword) mul16u::res#6 = (dword) mul16u::res#2 [phi:mul16u::@2/mul16u::@4->mul16u::@3#0] -- register_copy 
    jmp __b3
    // mul16u::@3
  __b3:
    // [22] (word) mul16u::a#0 ← (word) mul16u::a#2 >> (byte) 1 -- vwuz1=vwuz1_ror_1 
    lsr.z a+1
    ror.z a
    // [23] (dword) mul16u::mb#1 ← (dword) mul16u::mb#2 << (byte) 1 -- vduz1=vduz1_rol_1 
    asl.z mb
    rol.z mb+1
    rol.z mb+2
    rol.z mb+3
    // [15] phi from mul16u::@3 to mul16u::@1 [phi:mul16u::@3->mul16u::@1]
  __b1_from___b3:
    // [15] phi (dword) mul16u::mb#2 = (dword) mul16u::mb#1 [phi:mul16u::@3->mul16u::@1#0] -- register_copy 
    // [15] phi (dword) mul16u::res#2 = (dword) mul16u::res#6 [phi:mul16u::@3->mul16u::@1#1] -- register_copy 
    // [15] phi (word) mul16u::a#2 = (word) mul16u::a#0 [phi:mul16u::@3->mul16u::@1#2] -- register_copy 
    jmp __b1
}
  // File Data

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __bend
Removing instruction jmp __b1
Removing instruction jmp __breturn
Removing instruction jmp __b1
Removing instruction jmp __breturn
Removing instruction jmp __b4
Removing instruction jmp __b3
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction lda #>0
Succesful ASM optimization Pass5UnnecesaryLoadElimination
Replacing label __bbegin with __b1
Replacing label __b3_from___b2 with __b3
Removing instruction __bbegin:
Removing instruction __b1_from___bbegin:
Removing instruction main_from___b1:
Removing instruction __bend_from___b1:
Removing instruction __b3_from___b2:
Removing instruction __b3_from___b4:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction __bend:
Removing instruction mul16u_from_main:
Removing instruction __b1:
Removing instruction __breturn:
Removing instruction __b1_from_mul16u:
Removing instruction __breturn:
Removing instruction __b4:
Removing instruction __b1_from___b3:
Succesful ASM optimization Pass5UnusedLabelElimination
Updating BasicUpstart to call main directly
Removing instruction jsr main
Succesful ASM optimization Pass5SkipBegin
Removing instruction __b1:
Succesful ASM optimization Pass5UnusedLabelElimination

FINAL SYMBOL TABLE
(label) @1
(label) @begin
(label) @end
(const byte*) BGCOL = (byte*) 53281
(const byte*) BORDERCOL = (byte*) 53280
(void()) main()
(byte~) main::$2 reg byte a 4.0
(byte~) main::$3 reg byte a 4.0
(label) main::@1
(label) main::@return
(word) main::kaputt
(word) main::kaputt#0 kaputt zp[2]:10 2.0
(dword) main::result
(dword) main::result#0 result zp[4]:2 4.0
(dword()) mul16u((word) mul16u::a , (word) mul16u::b)
(byte~) mul16u::$1 reg byte a 22.0
(label) mul16u::@1
(label) mul16u::@2
(label) mul16u::@3
(label) mul16u::@4
(label) mul16u::@return
(word) mul16u::a
(word) mul16u::a#0 a zp[2]:10 11.0
(word) mul16u::a#2 a zp[2]:10 7.333333333333333
(word) mul16u::b
(const word) mul16u::b#0 b = (byte) $7b
(dword) mul16u::mb
(dword) mul16u::mb#1 mb zp[4]:6 22.0
(dword) mul16u::mb#2 mb zp[4]:6 4.714285714285714
(dword) mul16u::res
(dword) mul16u::res#1 res zp[4]:2 22.0
(dword) mul16u::res#2 res zp[4]:2 5.833333333333333
(dword) mul16u::res#6 res zp[4]:2 11.0
(dword) mul16u::return
(dword) mul16u::return#2 return zp[4]:2 4.0

zp[4]:2 [ mul16u::res#2 mul16u::res#6 mul16u::res#1 mul16u::return#2 main::result#0 ]
zp[4]:6 [ mul16u::mb#2 mul16u::mb#1 ]
zp[2]:10 [ main::kaputt#0 mul16u::a#2 mul16u::a#0 ]
reg byte a [ main::$2 ]
reg byte a [ main::$3 ]
reg byte a [ mul16u::$1 ]


FINAL ASSEMBLER
Score: 1439

  // File Comments
// Program where loop-head optimization produces wrong return value
// Reported by Richard-William Loerakker
  // Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
  // Global Constants & labels
  .label BORDERCOL = $d020
  .label BGCOL = $d021
  // @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 result = 2
    .label kaputt = $a
    // mul16u(4,123)
    // [5] call mul16u 
    // [14] phi from main to mul16u [phi:main->mul16u]
    jsr mul16u
    // mul16u(4,123)
    // [6] (dword) mul16u::return#2 ← (dword) mul16u::res#2
    // main::@1
    // result = mul16u(4,123)
    // [7] (dword) main::result#0 ← (dword) mul16u::return#2
    // kaputt = <result
    // [8] (word) main::kaputt#0 ← < (dword) main::result#0 -- vwuz1=_lo_vduz2 
    lda.z result
    sta.z kaputt
    lda.z result+1
    sta.z kaputt+1
    // <kaputt
    // [9] (byte~) main::$2 ← < (word) main::kaputt#0 -- vbuaa=_lo_vwuz1 
    lda.z kaputt
    // *BORDERCOL = <kaputt
    // [10] *((const byte*) BORDERCOL) ← (byte~) main::$2 -- _deref_pbuc1=vbuaa 
    sta BORDERCOL
    // >kaputt
    // [11] (byte~) main::$3 ← > (word) main::kaputt#0 -- vbuaa=_hi_vwuz1 
    lda.z kaputt+1
    // *BGCOL = >kaputt
    // [12] *((const byte*) BGCOL) ← (byte~) main::$3 -- _deref_pbuc1=vbuaa 
    sta BGCOL
    // main::@return
    // }
    // [13] return 
    rts
}
  // mul16u
// Perform binary multiplication of two unsigned 16-bit words into a 32-bit unsigned double word
// mul16u(word zp($a) a)
mul16u: {
    .const b = $7b
    .label a = $a
    .label mb = 6
    .label res = 2
    .label return = 2
    // [15] phi from mul16u to mul16u::@1 [phi:mul16u->mul16u::@1]
    // [15] phi (dword) mul16u::mb#2 = (dword)(const word) mul16u::b#0 [phi:mul16u->mul16u::@1#0] -- vduz1=vduc1 
    lda #<b
    sta.z mb
    lda #>b
    sta.z mb+1
    lda #<b>>$10
    sta.z mb+2
    lda #>b>>$10
    sta.z mb+3
    // [15] phi (dword) mul16u::res#2 = (dword) 0 [phi:mul16u->mul16u::@1#1] -- vduz1=vduc1 
    lda #<0
    sta.z res
    sta.z res+1
    lda #<0>>$10
    sta.z res+2
    lda #>0>>$10
    sta.z res+3
    // [15] phi (word) mul16u::a#2 = (byte) 4 [phi:mul16u->mul16u::@1#2] -- vwuz1=vbuc1 
    lda #<4
    sta.z a
    lda #>4
    sta.z a+1
    // mul16u::@1
  __b1:
    // while(a!=0)
    // [16] if((word) mul16u::a#2!=(byte) 0) goto mul16u::@2 -- vwuz1_neq_0_then_la1 
    lda.z a
    bne __b2
    lda.z a+1
    bne __b2
    // mul16u::@return
    // }
    // [17] return 
    rts
    // mul16u::@2
  __b2:
    // a&1
    // [18] (byte~) mul16u::$1 ← (word) mul16u::a#2 & (byte) 1 -- vbuaa=vwuz1_band_vbuc1 
    lda #1
    and.z a
    // if( (a&1) != 0)
    // [19] if((byte~) mul16u::$1==(byte) 0) goto mul16u::@3 -- vbuaa_eq_0_then_la1 
    cmp #0
    beq __b3
    // mul16u::@4
    // res = res + mb
    // [20] (dword) mul16u::res#1 ← (dword) mul16u::res#2 + (dword) mul16u::mb#2 -- vduz1=vduz1_plus_vduz2 
    lda.z res
    clc
    adc.z mb
    sta.z res
    lda.z res+1
    adc.z mb+1
    sta.z res+1
    lda.z res+2
    adc.z mb+2
    sta.z res+2
    lda.z res+3
    adc.z mb+3
    sta.z res+3
    // [21] phi from mul16u::@2 mul16u::@4 to mul16u::@3 [phi:mul16u::@2/mul16u::@4->mul16u::@3]
    // [21] phi (dword) mul16u::res#6 = (dword) mul16u::res#2 [phi:mul16u::@2/mul16u::@4->mul16u::@3#0] -- register_copy 
    // mul16u::@3
  __b3:
    // a = a>>1
    // [22] (word) mul16u::a#0 ← (word) mul16u::a#2 >> (byte) 1 -- vwuz1=vwuz1_ror_1 
    lsr.z a+1
    ror.z a
    // mb = mb<<1
    // [23] (dword) mul16u::mb#1 ← (dword) mul16u::mb#2 << (byte) 1 -- vduz1=vduz1_rol_1 
    asl.z mb
    rol.z mb+1
    rol.z mb+2
    rol.z mb+3
    // [15] phi from mul16u::@3 to mul16u::@1 [phi:mul16u::@3->mul16u::@1]
    // [15] phi (dword) mul16u::mb#2 = (dword) mul16u::mb#1 [phi:mul16u::@3->mul16u::@1#0] -- register_copy 
    // [15] phi (dword) mul16u::res#2 = (dword) mul16u::res#6 [phi:mul16u::@3->mul16u::@1#1] -- register_copy 
    // [15] phi (word) mul16u::a#2 = (word) mul16u::a#0 [phi:mul16u::@3->mul16u::@1#2] -- register_copy 
    jmp __b1
}
  // File Data

