// File: /workspace/github_cpp_makefile_star2_O1/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 0x6992
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 0xfb58
movq %rax, %r15
movslq %r15d, %rax
cmpq %r15, %rax
jne 0xfb64
movl (%r14), %eax
cmpl $0x22, %eax
je 0xfb64
testl %eax, %eax
jne 0xf91a
movl %ebx, (%r14)
movq -0x30(%rbp), %rax
leaq 0x80(%rax), %rdi
leaq 0x3150(%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 0x3124(%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 0x310e(%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 0x11232
movq -0x30(%rbp), %rbx
movl $0x1, 0x298(%rbx)
leaq 0x258(%rbx), %r15
movq %r15, %rdi
callq 0x11798
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 0x6992
leaq -0xb0(%rbp), %rsi
movq %r12, %rdi
callq 0xc2f8
movq %rax, %r12
movq -0xb0(%rbp), %rdi
cmpq %rbx, %rdi
je 0xfa1e
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 0xfa99
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 0x6992
movq -0x70(%rbp), %r13
leaq -0x70(%rbp), %rdi
movq %r12, %rsi
callq 0x117d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x3070
incq -0x60(%rbp)
movq 0x10(%rbx), %rbx
movq -0x90(%rbp), %rdi
cmpq %r14, %rdi
je 0xfa94
callq 0x3250
testq %rbx, %rbx
jne 0xfa46
movl $0x1, %ebx
leaq -0x70(%rbp), %r14
movq (%r14), %r14
leaq -0x70(%rbp), %rax
cmpq %rax, %r14
je 0xfb2b
leaq 0x10(%r14), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x11326
movl %ebx, (%rax)
movl $0x6, %edx
movq -0x38(%rbp), %rdi
leaq 0x2fd4(%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 0x2fb3(%rip), %rsi      # 0x12aaa
callq 0x32d0
movq %r15, %rdi
movq %r12, %rsi
callq 0x11326
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 0xfaa2
leaq -0x70(%rbp), %rdi
callq 0x113a6
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0xfb46
callq 0x3250
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2f77(%rip), %rdi      # 0x12ad6
callq 0x3190
leaq 0x2f6b(%rip), %rdi      # 0x12ad6
callq 0x3350
movq %rax, %r15
movq -0xb0(%rbp), %rdi
cmpq %rbx, %rdi
je 0xfbbf
callq 0x3250
jmp 0xfbbf
jmp 0xfb96
movq %rax, %r15
cmpl $0x0, (%r14)
jne 0xfbbf
movl %ebx, (%r14)
jmp 0xfbbf
movq %rax, %r15
jmp 0xfbbf
jmp 0xfbb3
movq %rax, %r15
movq -0x90(%rbp), %rdi
cmpq %r14, %rdi
je 0xfbb6
callq 0x3250
jmp 0xfbb6
movq %rax, %r15
leaq -0x70(%rbp), %rdi
callq 0x113a6
leaq -0x48(%rbp), %rax
movq -0x58(%rbp), %rdi
cmpq %rax, %rdi
je 0xfbd1
callq 0x3250
movq %r15, %rdi
callq 0x3420
nop