// File: taurheim[P]AgNES/codeGenerator.cpp
// Function: CodeGenerator::generateSubtraction(TAC)
void CodeGenerator::generateSubtraction(TAC tac) {
    code << "; subtract " << tac.first << " := " << tac.second << " - " << tac.third << nl;
    CodeVar assignTo = lookup(tac.first);
    // TODO why are these flipped?
    CodeVar arg1 = lookup(tac.third);
    CodeVar arg2 = lookup(tac.second);

    // Reset A to 0
    loadConstIntoA(0);
    code << "STA $0" << nl;

    // Add arg1
    loadLocalIntoA(arg1.value);
    code << "CLC" << nl;
    code << "ADC $0" << nl;
    code << "STA $0" << nl;

    // Subtract arg2
    loadLocalIntoA(arg2.value);
    code << "CLC" << nl;
    code << "SBC $0" << nl;

    // Store the output
    storeAIntoLocal(assignTo.value);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rsi, -0xc0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
subq $-0x80, %rdi
leaq 0x4247(%rip), %rsi      # 0x24ac7
callq 0x32e0
movq -0xc0(%rbp), %rsi
movq %rax, %rdi
addq $0x8, %rsi
movq %rsi, -0xb8(%rbp)
callq 0x32a0
movq %rax, %rdi
leaq 0x3f8d(%rip), %rsi      # 0x24836
callq 0x32e0
movq -0xc0(%rbp), %rsi
movq %rax, %rdi
addq $0x28, %rsi
callq 0x32a0
movq %rax, %rdi
leaq 0x3f74(%rip), %rsi      # 0x2483f
callq 0x32e0
movq -0xc0(%rbp), %rsi
movq %rax, %rdi
addq $0x48, %rsi
callq 0x32a0
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x32a0
movq -0xb8(%rbp), %rsi
leaq -0x38(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x3140
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
callq 0x21250
movq %rax, -0xa0(%rbp)
jmp 0x20925
movq -0xa0(%rbp), %rax
movq %rax, -0x18(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x31d0
movq -0xc0(%rbp), %rsi
addq $0x48, %rsi
leaq -0x70(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x3140
movq -0xb0(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x21250
movq %rax, -0xc8(%rbp)
jmp 0x20970
movq -0xc8(%rbp), %rax
movq %rax, -0x4c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x31d0
movq -0xc0(%rbp), %rsi
addq $0x28, %rsi
leaq -0x98(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x3140
movq -0xb0(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x21250
movq %rax, -0xd8(%rbp)
jmp 0x209be
movq -0xd8(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x31d0
movq -0xb0(%rbp), %rdi
xorl %esi, %esi
callq 0x21600
movq -0xb0(%rbp), %rdi
addq $0x80, %rdi
leaq 0x40bd(%rip), %rsi      # 0x24ab5
callq 0x32e0
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x32a0
movq -0xb0(%rbp), %rdi
movl -0x48(%rbp), %esi
callq 0x214d0
movq -0xb0(%rbp), %rdi
addq $0x80, %rdi
leaq 0x408c(%rip), %rsi      # 0x24abc
callq 0x32e0
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x32a0
movq -0xb0(%rbp), %rdi
addq $0x80, %rdi
leaq 0x4067(%rip), %rsi      # 0x24ac0
callq 0x32e0
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x32a0
movq -0xb0(%rbp), %rdi
addq $0x80, %rdi
leaq 0x4033(%rip), %rsi      # 0x24ab5
callq 0x32e0
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x32a0
movq -0xb0(%rbp), %rdi
movl -0x74(%rbp), %esi
callq 0x214d0
movq -0xb0(%rbp), %rdi
addq $0x80, %rdi
leaq 0x4002(%rip), %rsi      # 0x24abc
callq 0x32e0
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x32a0
movq -0xb0(%rbp), %rdi
addq $0x80, %rdi
leaq 0x3ff0(%rip), %rsi      # 0x24ad3
callq 0x32e0
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x32a0
movq -0xb0(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x21650
addq $0xe0, %rsp
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x31d0
jmp 0x20b55
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x31d0
jmp 0x20b55
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x31d0
movq -0x40(%rbp), %rdi
callq 0x3520
nop