// File: /workspace/github_cpp_makefile_star2_O1/SunriseFox[P]very-simple-c-compiler/./src/asm.cpp
// Function: generateCode(TreeNode*)
void generateCode(TreeNode* node) {
    if (node == nullptr) return;

    Symbol* ownSymbol = nullptr;
    string reg;

    if (node->nodeType == TreeNode::NODE_STATMENT) {
        StatementNode* stnode = static_cast<StatementNode*>(node);
        if (stnode->type == StatementNode::ST_IF_ELSE
                || stnode->type == StatementNode::ST_IF) {
            handleIf(stnode);
        } else if (stnode->type == StatementNode::ST_WHILE) {
            handleWhile(stnode);
        } else if (stnode->type == StatementNode::ST_FOR) {
            handleFor(stnode);
        }  else if (stnode->type == StatementNode::ST_FUNCTION) {
            handleFunctionNode(node);
        } else if (stnode->type == StatementNode::ST_RET) {
            reg = handleExprNode(stnode->child);
            of << "\tmovl\t" << reg << ", %eax\n";
            of << "\tleave\n";
            of << "\tret\n";
        } else if (static_cast<StatementNode*>(node)->type == StatementNode::ST_SCOPE) {
            handleCodeBlock(node);
        }
    }

    else if (node->nodeType == TreeNode::NODE_DECLARATION) {
        ownSymbol = static_cast<DeclarationNode*>(node)->symbol;
        if (!node->child) return;
        switch (ownSymbol->type) {
        case Symbol::VALUE_INT:
        case Symbol::VALUE_CHAR:
            reg = handleExprNode(node->child);
            of << "\tmovl\t" << reg << ", " << getSymbolMemory(ownSymbol) << "\n";
            return;
        case Symbol::VALUE_STRING:
            varDeclare[static_cast<DeclarationNode*>(node)->symbol] = varDeclare[node->child->symbol];
            return;
        default:
            cerr << "could not handle type" << endl;
            return;
        }
    }

    else if (node->nodeType == TreeNode::NODE_FUNCCALL) {
        handleFuncCall(node);
    }

    else if (node->nodeType == TreeNode::NODE_OPERATOR
             || node->nodeType == TreeNode::NODE_EXPRESSION) {
        handleExprNode(node);
    }

    else {
        cerr << "+++++ ignoring node !" << endl;
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x68, %rsp
testq %rdi, %rdi
je 0x4a10
movq %rdi, %rbx
leaq 0x38(%rsp), %r15
movq %r15, -0x10(%r15)
movq $0x0, -0x8(%r15)
movb $0x0, (%r15)
movl 0x10(%rdi), %eax
decl %eax
cmpl $0x4, %eax
ja 0x49b3
leaq 0x8865(%rip), %rcx      # 0xd0b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x5226
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x49f9
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x49f9
movq %rbx, %rdi
callq 0x4ba0
jmp 0x49f9
movl 0x30(%rbx), %eax
addl $-0x2, %eax
cmpl $0xa, %eax
ja 0x49f9
leaq 0x881c(%rip), %rcx      # 0xd0cc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
callq 0x6eed
jmp 0x49f9
movq 0x18(%rbx), %rsi
testq %rsi, %rsi
je 0x49f9
movq 0x28(%rbx), %r14
movl (%r14), %eax
cmpl $0x5, %eax
je 0x48f1
cmpl $0x4, %eax
je 0x4a1c
cmpl $0x2, %eax
jne 0x4a4f
leaq 0x8(%rsp), %rdi
callq 0x5226
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x32b0
movq (%rbx), %rdi
leaq 0x18(%rsp), %r12
cmpq %r12, %rdi
je 0x4927
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3200
leaq 0xe942(%rip), %rdi      # 0x13270
leaq 0x89e0(%rip), %rsi      # 0xd315
movl $0x6, %edx
callq 0x3260
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0xe920(%rip), %rdi      # 0x13270
callq 0x3260
movq %rax, %rbx
leaq 0x8a74(%rip), %rsi      # 0xd3d3
movl $0x2, %edx
movq %rax, %rdi
callq 0x3260
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x3efe
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x3260
leaq 0x8b8e(%rip), %rsi      # 0xd520
movl $0x1, %edx
movq %rax, %rdi
callq 0x3260
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x49f9
movq 0x18(%rsp), %rsi
jmp 0x4880
movq 0xe636(%rip), %rbx      # 0x12ff0
leaq 0x8add(%rip), %rsi      # 0xd49e
movl $0x15, %edx
movq %rbx, %rdi
callq 0x3260
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x31d0
movsbl %al, %esi
movq 0xe604(%rip), %rdi      # 0x12ff0
callq 0x3030
movq %rax, %rdi
callq 0x3150
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x4a10
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3200
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
addq $0x28, %rsi
leaq 0xe819(%rip), %rdi      # 0x13240
callq 0x7706
movq %rax, %r14
addq $0x28, %rbx
leaq 0xe806(%rip), %rdi      # 0x13240
movq %rbx, %rsi
callq 0x7706
movq %rax, %rdi
movq %r14, %rsi
callq 0x3130
jmp 0x49f9
movq 0xe59a(%rip), %rdi      # 0x12ff0
leaq 0x8a2b(%rip), %rsi      # 0xd488
movl $0x15, %edx
callq 0x3260
movq 0xe582(%rip), %rdi      # 0x12ff0
callq 0x3100
jmp 0x49f9
movq 0x18(%rbx), %rsi
leaq 0x8(%rsp), %rdi
callq 0x5226
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x32b0
movq (%rbx), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x4aaf
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3200
leaq 0xe7ba(%rip), %rdi      # 0x13270
leaq 0x8858(%rip), %rsi      # 0xd315
movl $0x6, %edx
callq 0x3260
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0xe798(%rip), %rdi      # 0x13270
callq 0x3260
leaq 0x8981(%rip), %rsi      # 0xd465
movl $0x7, %edx
movq %rax, %rdi
callq 0x3260
leaq 0xe778(%rip), %rdi      # 0x13270
leaq 0x896e(%rip), %rsi      # 0xd46d
movl $0x7, %edx
callq 0x3260
leaq 0xe760(%rip), %rdi      # 0x13270
leaq 0x895e(%rip), %rsi      # 0xd475
movl $0x5, %edx
callq 0x3260
jmp 0x49f9
movq %rbx, %rdi
callq 0x47e9
jmp 0x49f9
movq %rbx, %rdi
callq 0x683b
jmp 0x49f9
movq %rbx, %rdi
callq 0x6a0b
jmp 0x49f9
movq %rbx, %rdi
callq 0x6c42
jmp 0x49f9
jmp 0x4b7e
jmp 0x4b7e
jmp 0x4b7e
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x4b81
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x4b81
jmp 0x4b7e
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x4b98
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3200
movq %rbx, %rdi
callq 0x33d0