// File: /workspace/github_cpp_makefile_star2_O3/taurheim[P]AgNES/codeGenerator.cpp
// Function: CodeGenerator::generateBeginFunc(TAC)
void CodeGenerator::generateBeginFunc(TAC tac) {
    std::string funcName = tac.first;
    int paramSize = stoi(tac.second);

    code << "; beginfunc " << funcName << nl;
    
    // Allocate space for the locals
    code << "; Allocating " << paramSize << " for locals" << nl;
    allocateStackSpace(paramSize);

    // Create a map for the local param names
    currentLocalOffset = 1;
    currentParamMap.clear();
    int paramCount = 1;
    STEntry * functionScope = symbolTable->lookupScope(funcName);
    STEntry * currentEntry = functionScope->next;
    std::list<std::string> paramNames;
    while (currentEntry != nullptr) {
        std::string name = currentEntry->name;
        paramNames.push_front(name); 
        currentEntry = currentEntry->next;
    }
    for (auto & name : paramNames) {
        currentParamMap[name] = (paramCount++);
        code << "; arg " << name << " has stack offset " << currentParamMap[name] << nl; 
    } 
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r14
movq %rdi, -0x30(%rbp)
leaq -0x48(%rbp), %rax
movq %rax, -0x10(%rax)
movq 0x8(%rsi), %rsi
movq 0x10(%r14), %rdx
addq %rsi, %rdx
leaq -0x58(%rbp), %rdi
callq 0x6906
movq 0x28(%r14), %r12
callq 0x3050
movq %rax, %r14
movl (%rax), %ebx
movl $0x0, (%rax)
leaq -0x90(%rbp), %r13
movq %r12, %rdi
movq %r13, %rsi
movl $0xa, %edx
callq 0x3330
cmpq %r12, (%r13)
je 0xf9f4
movq %rax, %r15
movslq %r15d, %rax
cmpq %r15, %rax
jne 0xfa00
movl (%r14), %eax
cmpl $0x22, %eax
je 0xfa00
testl %eax, %eax
jne 0xf7b6
movl %ebx, (%r14)
movq -0x30(%rbp), %rax
leaq 0x80(%rax), %rdi
leaq 0x32b4(%rip), %rsi      # 0x12a7c
movl $0xc, %edx
movq %rdi, -0x38(%rbp)
callq 0x32d0
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x38(%rbp), %rdi
callq 0x32d0
movq -0x30(%rbp), %rcx
movq (%rcx), %rsi
movq 0x8(%rcx), %rdx
movq %rax, %rdi
callq 0x32d0
leaq 0x3288(%rip), %rsi      # 0x12a89
movl $0xd, %edx
movq -0x38(%rbp), %rdi
callq 0x32d0
movq -0x38(%rbp), %rdi
movl %r15d, %esi
callq 0x3410
movq %rax, %r12
leaq 0x3272(%rip), %rsi      # 0x12a97
movl $0xb, %edx
movq %rax, %rdi
callq 0x32d0
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r12, %rdi
callq 0x32d0
movq -0x30(%rbp), %rdi
movl %r15d, %esi
callq 0x110c6
movq -0x30(%rbp), %rbx
movl $0x1, 0x298(%rbx)
leaq 0x258(%rbx), %r15
movq %r15, %rdi
callq 0x11622
movq 0x288(%rbx), %r12
leaq -0xa0(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
addq %rsi, %rdx
leaq -0xb0(%rbp), %rdi
callq 0x6906
leaq -0xb0(%rbp), %rsi
movq %r12, %rdi
callq 0xc17a
movq %rax, %r12
movq -0xb0(%rbp), %rdi
cmpq %rbx, %rdi
je 0xf8ba
callq 0x3250
movq 0x10(%r12), %rbx
leaq -0x70(%rbp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
movq $0x0, 0x10(%rax)
testq %rbx, %rbx
je 0xf935
leaq -0x80(%rbp), %r14
leaq -0x90(%rbp), %r12
movq %r14, -0x90(%rbp)
movq 0x38(%rbx), %rsi
movq 0x40(%rbx), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x6906
movq -0x70(%rbp), %r13
leaq -0x70(%rbp), %rdi
movq %r12, %rsi
callq 0x1165a
movq %rax, %rdi
movq %r13, %rsi
callq 0x3070
incq -0x60(%rbp)
movq 0x10(%rbx), %rbx
movq -0x90(%rbp), %rdi
cmpq %r14, %rdi
je 0xf930
callq 0x3250
testq %rbx, %rbx
jne 0xf8e2
movl $0x1, %ebx
leaq -0x70(%rbp), %r14
movq (%r14), %r14
leaq -0x70(%rbp), %rax
cmpq %rax, %r14
je 0xf9c7
leaq 0x10(%r14), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x111ba
movl %ebx, (%rax)
movl $0x6, %edx
movq -0x38(%rbp), %rdi
leaq 0x3138(%rip), %rsi      # 0x12aa3
callq 0x32d0
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movq -0x38(%rbp), %rdi
callq 0x32d0
movq %rax, %r13
movl $0x12, %edx
movq %rax, %rdi
leaq 0x3117(%rip), %rsi      # 0x12aaa
callq 0x32d0
movq %r15, %rdi
movq %r12, %rsi
callq 0x111ba
movl (%rax), %esi
movq %r13, %rdi
callq 0x3410
movq -0x30(%rbp), %rcx
movq (%rcx), %rsi
movq 0x8(%rcx), %rdx
incl %ebx
movq %rax, %rdi
callq 0x32d0
jmp 0xf93e
leaq -0x70(%rbp), %rdi
callq 0x1123a
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0xf9e2
callq 0x3250
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x30db(%rip), %rdi      # 0x12ad6
callq 0x3190
leaq 0x30cf(%rip), %rdi      # 0x12ad6
callq 0x3350
movq %rax, %r15
movq -0xb0(%rbp), %rdi
cmpq %rbx, %rdi
je 0xfa5b
callq 0x3250
jmp 0xfa5b
jmp 0xfa32
movq %rax, %r15
cmpl $0x0, (%r14)
jne 0xfa5b
movl %ebx, (%r14)
jmp 0xfa5b
movq %rax, %r15
jmp 0xfa5b
jmp 0xfa4f
movq %rax, %r15
movq -0x90(%rbp), %rdi
cmpq %r14, %rdi
je 0xfa52
callq 0x3250
jmp 0xfa52
movq %rax, %r15
leaq -0x70(%rbp), %rdi
callq 0x1123a
leaq -0x48(%rbp), %rax
movq -0x58(%rbp), %rdi
cmpq %rax, %rdi
je 0xfa6d
callq 0x3250
movq %r15, %rdi
callq 0x3420
nop