// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
movq %rdi, -0x30(%rbp)
addq $0x8, %rsi
leaq -0x70(%rbp), %rdi
callq 0x32e0
addq $0x28, %rbx
pushq $0xa
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xec35
movl %eax, %r14d
movq -0x30(%rbp), %rax
leaq 0x80(%rax), %rdi
leaq 0x319c(%rip), %rsi      # 0x10a84
movq %rdi, -0x38(%rbp)
callq 0x3230
leaq -0x70(%rbp), %rsi
movq %rax, %rdi
callq 0x3210
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x3210
leaq 0x3181(%rip), %rsi      # 0x10a91
movq -0x38(%rbp), %rdi
callq 0x3230
movq %rax, %rdi
movl %r14d, %esi
callq 0x33d0
leaq 0x3174(%rip), %rsi      # 0x10a9f
movq %rax, %rdi
callq 0x3230
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x3210
movq -0x30(%rbp), %rdi
movl %r14d, %esi
callq 0xeaee
movq -0x30(%rbp), %rbx
movl $0x1, 0x298(%rbx)
leaq 0x258(%rbx), %r15
movq %r15, %rdi
callq 0xf162
movq 0x288(%rbx), %rbx
leaq -0x90(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x32e0
leaq -0x90(%rbp), %rsi
movq %rbx, %rdi
callq 0xb800
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x3140
movq 0x10(%rbx), %r14
leaq -0x50(%rbp), %r12
movq %r12, 0x8(%r12)
movq %r12, (%r12)
andq $0x0, 0x10(%r12)
leaq -0xb0(%rbp), %rbx
testq %r14, %r14
je 0xd9e5
leaq 0x38(%r14), %rsi
movq %rbx, %rdi
callq 0x32e0
movq %r12, %rdi
movq %rbx, %rsi
callq 0xec56
movq 0x10(%r14), %r14
movq %rbx, %rdi
callq 0x3140
jmp 0xd9bb
pushq $0x1
popq %r14
movq (%r12), %r12
leaq -0x50(%rbp), %rax
cmpq %rax, %r12
je 0xda5c
leaq 0x10(%r12), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0xebcc
movl %r14d, (%rax)
movq -0x38(%rbp), %rdi
leaq 0x3097(%rip), %rsi      # 0x10aab
callq 0x3230
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3210
movq %rax, %rdi
leaq 0x3084(%rip), %rsi      # 0x10ab2
callq 0x3230
movq %rax, %r13
movq %r15, %rdi
movq %rbx, %rsi
callq 0xebcc
movl (%rax), %esi
movq %r13, %rdi
callq 0x33d0
incl %r14d
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x3210
jmp 0xd9e9
leaq -0x50(%rbp), %rdi
callq 0xed3c
leaq -0x70(%rbp), %rdi
callq 0x3140
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x3140
jmp 0xdab7
jmp 0xda93
movq %rax, %rbx
jmp 0xdab7
jmp 0xdaab
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
callq 0x3140
jmp 0xdaae
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0xed3c
leaq -0x70(%rbp), %rdi
callq 0x3140
movq %rbx, %rdi
callq 0x33e0