// File: /workspace/github_cpp_makefile_star2_O1/taurheim[P]AgNES/irGenerator.cpp
// Function: IRGenerator::genTACFunction(ASTNode*)
void IRGenerator::genTACFunction(ASTNode * func) {
    IdentifierNode * functionName = (IdentifierNode*) func->children[1];
    intermediateCode.push_back({ IR_NEWLABEL, functionName->name});

    // TODO this doesn't support variables with different sizes
    intermediateCode.push_back({ IR_BEGINFUNC, functionName->name });
    auto beginFuncRef = std::prev(intermediateCode.end());
    int startingRegisterCount = registerCount;

    ASTNode * functionBody = func->children[3];
    for(auto & statement : functionBody->children) {
        genTACStatement(statement);
    }

    // How many locals are there
    std::string localsCount = std::to_string((registerCount - startingRegisterCount));
    intermediateCode.push_back({ IR_ENDFUNC, localsCount });
    // Now that we know how many labels are used
    (*beginFuncRef).second = localsCount;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r12
movq %rdi, %r15
movq 0x8(%rsi), %rax
movq 0x8(%rax), %r13
leaq 0x10(%rdi), %rbx
leaq -0x88(%rbp), %rdi
movl $0x3, -0x8(%rdi)
leaq -0x78(%rbp), %rax
movq %rax, -0x10(%rax)
movq 0x20(%r13), %rsi
movq 0x28(%r13), %rdx
addq %rsi, %rdx
callq 0x6992
leaq -0x58(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x38(%rbp), %r14
movq %r14, -0x10(%r14)
movq %rax, -0x8(%r14)
movb %al, (%r14)
leaq -0x90(%rbp), %rdx
movq %rbx, %rdi
movq %rbx, %rsi
callq 0xe44e
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0xdcdd
callq 0x3250
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0xdcef
callq 0x3250
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0xdd04
callq 0x3250
leaq -0x88(%rbp), %r14
movl $0x7, -0x8(%r14)
leaq -0x78(%rbp), %rax
movq %rax, (%r14)
movq 0x20(%r13), %rsi
movq 0x28(%r13), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x6992
leaq -0x58(%rbp), %rax
movq %rax, 0x20(%r14)
xorl %eax, %eax
movq %rax, 0x28(%r14)
movb %al, 0x30(%r14)
leaq -0x38(%rbp), %r13
movq %r13, 0x40(%r14)
movq %rax, 0x48(%r14)
movb %al, 0x50(%r14)
leaq -0x90(%rbp), %rdx
movq %rbx, %rdi
movq %rbx, %rsi
callq 0xe44e
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0xdd6f
callq 0x3250
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0xdd81
callq 0x3250
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0xdd96
callq 0x3250
movq 0x18(%r15), %rax
movq %rax, -0x98(%rbp)
movl 0x8(%r15), %r14d
movq 0x8(%r12), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %r12
movq 0x10(%rax), %r13
cmpq %r13, %r12
je 0xddcd
movq (%r12), %rsi
movq %r15, %rdi
callq 0xce96
addq $0x8, %r12
jmp 0xddb6
movl 0x8(%r15), %r15d
subl %r14d, %r15d
movl %r15d, %r12d
negl %r12d
cmovsl %r15d, %r12d
movl $0x1, %r13d
cmpl $0xa, %r12d
jb 0xde34
movl $0x4, %r13d
movl $0xd1b71759, %eax       # imm = 0xD1B71759
movl %r12d, %ecx
cmpl $0x63, %ecx
jbe 0xde2b
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0xde31
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0xde34
movl %ecx, %edx
imulq %rax, %rdx
shrq $0x2d, %rdx
addl $0x4, %r13d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0xddf8
addl $-0x3, %r13d
jmp 0xde34
addl $-0x2, %r13d
jmp 0xde34
decl %r13d
shrl $0x1f, %r15d
leal (%r15,%r13), %esi
leaq -0xa8(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0xb8(%rbp), %r14
movq %r14, %rdi
movl $0x2d, %edx
callq 0x32e0
addq (%r14), %r15
movq %r15, %rdi
movl %r13d, %esi
movl %r12d, %edx
callq 0xa9d4
leaq -0x88(%rbp), %rdi
movl $0x8, -0x8(%rdi)
leaq -0x78(%rbp), %rax
movq %rax, (%rdi)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
callq 0x6992
leaq -0x90(%rbp), %rdx
leaq -0x58(%rbp), %rax
movq %rax, 0x28(%rdx)
xorl %eax, %eax
movq %rax, 0x30(%rdx)
movb %al, 0x38(%rdx)
leaq -0x38(%rbp), %r14
movq %r14, 0x48(%rdx)
movq %rax, 0x50(%rdx)
movb %al, 0x58(%rdx)
movq %rbx, %rdi
movq %rbx, %rsi
callq 0xe44e
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0xded0
callq 0x3250
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0xdee2
callq 0x3250
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0xdef7
callq 0x3250
movq -0x98(%rbp), %rdi
addq $0x38, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x3180
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0xdf26
callq 0x3250
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xdf4b
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0xe35c
jmp 0xdf4e
movq %rax, %rbx
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0xdf79
callq 0x3250
jmp 0xdf79
jmp 0xdf6a
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0xe35c
movq %rbx, %rdi
callq 0x3420
nop