
CONTROL FLOW GRAPH SSA
@begin: scope:[]  from
  to:@1
main: scope:[main]  from @1
  (byte*) main::SCREEN#0 ← ((byte*)) (word/signed word/dword/signed dword) 1024
  (byte) main::min#0 ← (byte/signed byte/word/signed word/dword/signed dword) 10
  (byte) main::max#0 ← (byte/word/signed word/dword/signed dword) 200
  (byte~) main::$0 ← (byte) main::min#0 + (byte) main::max#0
  (word) main::sumw#0 ← ((word)) (byte~) main::$0
  (word~) main::$1 ← (word) main::sumw#0 >> (byte/signed byte/word/signed word/dword/signed dword) 1
  (byte~) main::$2 ← ((byte)) (word~) main::$1
  (byte/signed word/word/dword/signed dword~) main::$3 ← (byte~) main::$2 + (byte/signed byte/word/signed word/dword/signed dword) 1
  (byte) main::midw#0 ← (byte/signed word/word/dword/signed dword~) main::$3
  *((byte*) main::SCREEN#0 + (byte/signed byte/word/signed word/dword/signed dword) 0) ← (byte) main::midw#0
  (byte~) main::$4 ← (byte) main::min#0 + (byte) main::max#0
  (byte) main::sumb#0 ← (byte~) main::$4
  (byte~) main::$5 ← (byte) main::sumb#0 >> (byte/signed byte/word/signed word/dword/signed dword) 1
  (byte/signed word/word/dword/signed dword~) main::$6 ← (byte~) main::$5 + (byte/signed byte/word/signed word/dword/signed dword) 1
  (byte) main::midb#0 ← (byte/signed word/word/dword/signed dword~) main::$6
  *((byte*) main::SCREEN#0 + (byte/signed byte/word/signed word/dword/signed dword) 1) ← (byte) main::midb#0
  (byte*) main::BGCOL#0 ← ((byte*)) (word/dword/signed dword) 53281
  (bool~) main::$7 ← *((byte*) main::SCREEN#0 + (byte/signed byte/word/signed word/dword/signed dword) 0) == *((byte*) main::SCREEN#0 + (byte/signed byte/word/signed word/dword/signed dword) 1)
  if((bool~) main::$7) goto main::@1
  to:main::@3
main::@1: scope:[main]  from main
  (byte*) main::BGCOL#1 ← phi( main/(byte*) main::BGCOL#0 )
  *((byte*) main::BGCOL#1) ← (byte/signed byte/word/signed word/dword/signed dword) 5
  to:main::@return
main::@3: scope:[main]  from main
  (byte*) main::BGCOL#2 ← phi( main/(byte*) main::BGCOL#0 )
  *((byte*) main::BGCOL#2) ← (byte/signed byte/word/signed word/dword/signed dword) 2
  to:main::@return
main::@return: scope:[main]  from main::@1 main::@3
  return 
  to:@return
@1: scope:[]  from @begin
  call main 
  to:@2
@2: scope:[]  from @1
  to:@end
@end: scope:[]  from @2

SYMBOL TABLE SSA
(label) @1
(label) @2
(label) @begin
(label) @end
(void()) main()
(byte~) main::$0
(word~) main::$1
(byte~) main::$2
(byte/signed word/word/dword/signed dword~) main::$3
(byte~) main::$4
(byte~) main::$5
(byte/signed word/word/dword/signed dword~) main::$6
(bool~) main::$7
(label) main::@1
(label) main::@3
(label) main::@return
(byte*) main::BGCOL
(byte*) main::BGCOL#0
(byte*) main::BGCOL#1
(byte*) main::BGCOL#2
(byte*) main::SCREEN
(byte*) main::SCREEN#0
(byte) main::max
(byte) main::max#0
(byte) main::midb
(byte) main::midb#0
(byte) main::midw
(byte) main::midw#0
(byte) main::min
(byte) main::min#0
(byte) main::sumb
(byte) main::sumb#0
(word) main::sumw
(word) main::sumw#0

Culled Empty Block (label) @2
Successful SSA optimization Pass2CullEmptyBlocks
Alias (byte) main::midw#0 = (byte/signed word/word/dword/signed dword~) main::$3 
Alias (byte) main::sumb#0 = (byte~) main::$4 
Alias (byte) main::midb#0 = (byte/signed word/word/dword/signed dword~) main::$6 
Alias (byte*) main::BGCOL#0 = (byte*) main::BGCOL#1 (byte*) main::BGCOL#2 
Successful SSA optimization Pass2AliasElimination
Simple Condition (bool~) main::$7 [18] if(*((byte*) main::SCREEN#0 + (byte/signed byte/word/signed word/dword/signed dword) 0)==*((byte*) main::SCREEN#0 + (byte/signed byte/word/signed word/dword/signed dword) 1)) goto main::@1
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant (const byte*) main::SCREEN#0 = ((byte*))1024
Constant (const byte) main::min#0 = 10
Constant (const byte) main::max#0 = 200
Constant (const byte*) main::BGCOL#0 = ((byte*))53281
Successful SSA optimization Pass2ConstantIdentification
Constant (const byte) main::$0 = main::min#0+main::max#0
Constant (const byte) main::sumb#0 = main::min#0+main::max#0
Successful SSA optimization Pass2ConstantIdentification
Constant (const word) main::sumw#0 = ((word))main::$0
Constant (const byte) main::$5 = main::sumb#0>>1
Successful SSA optimization Pass2ConstantIdentification
Constant (const word) main::$1 = main::sumw#0>>1
Constant (const byte) main::midb#0 = main::$5+1
Successful SSA optimization Pass2ConstantIdentification
Constant (const byte) main::$2 = ((byte))main::$1
Successful SSA optimization Pass2ConstantIdentification
Constant (const byte) main::midw#0 = main::$2+1
Successful SSA optimization Pass2ConstantIdentification
Consolidated array index constant in *(main::SCREEN#0+0)
Consolidated array index constant in *(main::SCREEN#0+1)
Consolidated array index constant in *(main::SCREEN#0+0)
Consolidated array index constant in *(main::SCREEN#0+1)
Successful SSA optimization Pass2ConstantAdditionElimination
Constant inlined main::$5 = (const byte) main::sumb#0>>(byte/signed byte/word/signed word/dword/signed dword) 1
Constant inlined main::$1 = (const word) main::sumw#0>>(byte/signed byte/word/signed word/dword/signed dword) 1
Constant inlined main::$2 = ((byte))(const word) main::sumw#0>>(byte/signed byte/word/signed word/dword/signed dword) 1
Constant inlined main::$0 = (const byte) main::min#0+(const byte) main::max#0
Successful SSA optimization Pass2ConstantInlining
Simplifying constant plus zero main::SCREEN#0+0
Simplifying constant plus zero main::SCREEN#0+0
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @1
Adding NOP phi() at start of @end
CALL GRAPH
Calls in [] to main:2 

Created 0 initial phi equivalence classes
Coalesced down to 0 phi equivalence classes
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @1
Adding NOP phi() at start of @end

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()
main: scope:[main]  from @1
  [4] *((const byte*) main::SCREEN#0) ← (const byte) main::midw#0
  [5] *((const byte*) main::SCREEN#0+(byte/signed byte/word/signed word/dword/signed dword) 1) ← (const byte) main::midb#0
  [6] if(*((const byte*) main::SCREEN#0)==*((const byte*) main::SCREEN#0+(byte/signed byte/word/signed word/dword/signed dword) 1)) goto main::@1
  to:main::@3
main::@3: scope:[main]  from main
  [7] *((const byte*) main::BGCOL#0) ← (byte/signed byte/word/signed word/dword/signed dword) 2
  to:main::@return
main::@return: scope:[main]  from main::@1 main::@3
  [8] return 
  to:@return
main::@1: scope:[main]  from main
  [9] *((const byte*) main::BGCOL#0) ← (byte/signed byte/word/signed word/dword/signed dword) 5
  to:main::@return


VARIABLE REGISTER WEIGHTS
(void()) main()
(byte*) main::BGCOL
(byte*) main::SCREEN
(byte) main::max
(byte) main::midb
(byte) main::midw
(byte) main::min
(byte) main::sumb
(word) main::sumw

Initial phi equivalence classes
Complete equivalence classes

INITIAL ASM
//SEG0 File Comments
// Tests that casting inside constants in the output handles precedence between cast and + correctly - should generate the following KA-expression ($ff & sumw>>1)+1
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
//SEG2 Global Constants & labels
//SEG3 @begin
bbegin:
//SEG4 [1] phi from @begin to @1 [phi:@begin->@1]
b1_from_bbegin:
  jmp b1
//SEG5 @1
b1:
//SEG6 [2] call main 
  jsr main
//SEG7 [3] phi from @1 to @end [phi:@1->@end]
bend_from_b1:
  jmp bend
//SEG8 @end
bend:
//SEG9 main
main: {
    .label SCREEN = $400
    .const min = $a
    .const max = $c8
    .label BGCOL = $d021
    .const sumb = min+max
    .const sumw = min+max
    .const midb = (sumb>>1)+1
    .const midw = (sumw>>1)+1
  //SEG10 [4] *((const byte*) main::SCREEN#0) ← (const byte) main::midw#0 -- _deref_pbuc1=vbuc2 
    lda #midw
    sta SCREEN
  //SEG11 [5] *((const byte*) main::SCREEN#0+(byte/signed byte/word/signed word/dword/signed dword) 1) ← (const byte) main::midb#0 -- _deref_pbuc1=vbuc2 
    lda #midb
    sta SCREEN+1
  //SEG12 [6] if(*((const byte*) main::SCREEN#0)==*((const byte*) main::SCREEN#0+(byte/signed byte/word/signed word/dword/signed dword) 1)) goto main::@1 -- _deref_pbuc1_eq__deref_pbuc2_then_la1 
    lda SCREEN
    cmp SCREEN+1
    beq b1
    jmp b3
  //SEG13 main::@3
  b3:
  //SEG14 [7] *((const byte*) main::BGCOL#0) ← (byte/signed byte/word/signed word/dword/signed dword) 2 -- _deref_pbuc1=vbuc2 
    lda #2
    sta BGCOL
    jmp breturn
  //SEG15 main::@return
  breturn:
  //SEG16 [8] return 
    rts
  //SEG17 main::@1
  b1:
  //SEG18 [9] *((const byte*) main::BGCOL#0) ← (byte/signed byte/word/signed word/dword/signed dword) 5 -- _deref_pbuc1=vbuc2 
    lda #5
    sta BGCOL
    jmp breturn
}

REGISTER UPLIFT POTENTIAL REGISTERS
Statement [4] *((const byte*) main::SCREEN#0) ← (const byte) main::midw#0 [ ] ( main:2 [ ] ) always clobbers reg byte a 
Statement [5] *((const byte*) main::SCREEN#0+(byte/signed byte/word/signed word/dword/signed dword) 1) ← (const byte) main::midb#0 [ ] ( main:2 [ ] ) always clobbers reg byte a 
Statement [6] if(*((const byte*) main::SCREEN#0)==*((const byte*) main::SCREEN#0+(byte/signed byte/word/signed word/dword/signed dword) 1)) goto main::@1 [ ] ( main:2 [ ] ) always clobbers reg byte a 
Statement [7] *((const byte*) main::BGCOL#0) ← (byte/signed byte/word/signed word/dword/signed dword) 2 [ ] ( main:2 [ ] ) always clobbers reg byte a 
Statement [9] *((const byte*) main::BGCOL#0) ← (byte/signed byte/word/signed word/dword/signed dword) 5 [ ] ( main:2 [ ] ) always clobbers reg byte a 

REGISTER UPLIFT SCOPES
Uplift Scope [main] 
Uplift Scope [] 

Uplifting [main] best 61 combination 
Uplifting [] best 61 combination 

ASSEMBLER BEFORE OPTIMIZATION
//SEG0 File Comments
// Tests that casting inside constants in the output handles precedence between cast and + correctly - should generate the following KA-expression ($ff & sumw>>1)+1
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(bbegin)
.pc = $80d "Program"
//SEG2 Global Constants & labels
//SEG3 @begin
bbegin:
//SEG4 [1] phi from @begin to @1 [phi:@begin->@1]
b1_from_bbegin:
  jmp b1
//SEG5 @1
b1:
//SEG6 [2] call main 
  jsr main
//SEG7 [3] phi from @1 to @end [phi:@1->@end]
bend_from_b1:
  jmp bend
//SEG8 @end
bend:
//SEG9 main
main: {
    .label SCREEN = $400
    .const min = $a
    .const max = $c8
    .label BGCOL = $d021
    .const sumb = min+max
    .const sumw = min+max
    .const midb = (sumb>>1)+1
    .const midw = (sumw>>1)+1
  //SEG10 [4] *((const byte*) main::SCREEN#0) ← (const byte) main::midw#0 -- _deref_pbuc1=vbuc2 
    lda #midw
    sta SCREEN
  //SEG11 [5] *((const byte*) main::SCREEN#0+(byte/signed byte/word/signed word/dword/signed dword) 1) ← (const byte) main::midb#0 -- _deref_pbuc1=vbuc2 
    lda #midb
    sta SCREEN+1
  //SEG12 [6] if(*((const byte*) main::SCREEN#0)==*((const byte*) main::SCREEN#0+(byte/signed byte/word/signed word/dword/signed dword) 1)) goto main::@1 -- _deref_pbuc1_eq__deref_pbuc2_then_la1 
    lda SCREEN
    cmp SCREEN+1
    beq b1
    jmp b3
  //SEG13 main::@3
  b3:
  //SEG14 [7] *((const byte*) main::BGCOL#0) ← (byte/signed byte/word/signed word/dword/signed dword) 2 -- _deref_pbuc1=vbuc2 
    lda #2
    sta BGCOL
    jmp breturn
  //SEG15 main::@return
  breturn:
  //SEG16 [8] return 
    rts
  //SEG17 main::@1
  b1:
  //SEG18 [9] *((const byte*) main::BGCOL#0) ← (byte/signed byte/word/signed word/dword/signed dword) 5 -- _deref_pbuc1=vbuc2 
    lda #5
    sta BGCOL
    jmp breturn
}

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp b1
Removing instruction jmp bend
Removing instruction jmp b3
Removing instruction jmp breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction b1_from_bbegin:
Removing instruction b1:
Removing instruction bend_from_b1:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction bend:
Removing instruction b3:
Succesful ASM optimization Pass5UnusedLabelElimination
Updating BasicUpstart to call main directly
Removing instruction jsr main
Succesful ASM optimization Pass5SkipBegin
Removing instruction bbegin:
Succesful ASM optimization Pass5UnusedLabelElimination

FINAL SYMBOL TABLE
(label) @1
(label) @begin
(label) @end
(void()) main()
(label) main::@1
(label) main::@3
(label) main::@return
(byte*) main::BGCOL
(const byte*) main::BGCOL#0 BGCOL = ((byte*))(word/dword/signed dword) 53281
(byte*) main::SCREEN
(const byte*) main::SCREEN#0 SCREEN = ((byte*))(word/signed word/dword/signed dword) 1024
(byte) main::max
(const byte) main::max#0 max = (byte/word/signed word/dword/signed dword) 200
(byte) main::midb
(const byte) main::midb#0 midb = (const byte) main::sumb#0>>(byte/signed byte/word/signed word/dword/signed dword) 1+(byte/signed byte/word/signed word/dword/signed dword) 1
(byte) main::midw
(const byte) main::midw#0 midw = ((byte))(const word) main::sumw#0>>(byte/signed byte/word/signed word/dword/signed dword) 1+(byte/signed byte/word/signed word/dword/signed dword) 1
(byte) main::min
(const byte) main::min#0 min = (byte/signed byte/word/signed word/dword/signed dword) 10
(byte) main::sumb
(const byte) main::sumb#0 sumb = (const byte) main::min#0+(const byte) main::max#0
(word) main::sumw
(const word) main::sumw#0 sumw = ((word))(const byte) main::min#0+(const byte) main::max#0



FINAL ASSEMBLER
Score: 43

//SEG0 File Comments
// Tests that casting inside constants in the output handles precedence between cast and + correctly - should generate the following KA-expression ($ff & sumw>>1)+1
//SEG1 Basic Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
//SEG2 Global Constants & labels
//SEG3 @begin
//SEG4 [1] phi from @begin to @1 [phi:@begin->@1]
//SEG5 @1
//SEG6 [2] call main 
//SEG7 [3] phi from @1 to @end [phi:@1->@end]
//SEG8 @end
//SEG9 main
main: {
    .label SCREEN = $400
    .const min = $a
    .const max = $c8
    .label BGCOL = $d021
    .const sumb = min+max
    .const sumw = min+max
    .const midb = (sumb>>1)+1
    .const midw = (sumw>>1)+1
  //SEG10 [4] *((const byte*) main::SCREEN#0) ← (const byte) main::midw#0 -- _deref_pbuc1=vbuc2 
    lda #midw
    sta SCREEN
  //SEG11 [5] *((const byte*) main::SCREEN#0+(byte/signed byte/word/signed word/dword/signed dword) 1) ← (const byte) main::midb#0 -- _deref_pbuc1=vbuc2 
    lda #midb
    sta SCREEN+1
  //SEG12 [6] if(*((const byte*) main::SCREEN#0)==*((const byte*) main::SCREEN#0+(byte/signed byte/word/signed word/dword/signed dword) 1)) goto main::@1 -- _deref_pbuc1_eq__deref_pbuc2_then_la1 
    lda SCREEN
    cmp SCREEN+1
    beq b1
  //SEG13 main::@3
  //SEG14 [7] *((const byte*) main::BGCOL#0) ← (byte/signed byte/word/signed word/dword/signed dword) 2 -- _deref_pbuc1=vbuc2 
    lda #2
    sta BGCOL
  //SEG15 main::@return
  breturn:
  //SEG16 [8] return 
    rts
  //SEG17 main::@1
  b1:
  //SEG18 [9] *((const byte*) main::BGCOL#0) ← (byte/signed byte/word/signed word/dword/signed dword) 5 -- _deref_pbuc1=vbuc2 
    lda #5
    sta BGCOL
    jmp breturn
}

