// File: /workspace/github_cpp_makefile_star2_O3/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 0x49d0
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 0x4973
leaq 0x88a5(%rip), %rcx      # 0xd0b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x5235
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x49b9
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x49b9
movq %rbx, %rdi
callq 0x4b60
jmp 0x49b9
movl 0x30(%rbx), %eax
addl $-0x2, %eax
cmpl $0xa, %eax
ja 0x49b9
leaq 0x885c(%rip), %rcx      # 0xd0cc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
callq 0x6edc
jmp 0x49b9
movq 0x18(%rbx), %rsi
testq %rsi, %rsi
je 0x49b9
movq 0x28(%rbx), %r14
movl (%r14), %eax
cmpl $0x5, %eax
je 0x48b1
cmpl $0x4, %eax
je 0x49dc
cmpl $0x2, %eax
jne 0x4a0f
leaq 0x8(%rsp), %rdi
callq 0x5235
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x32b0
movq (%rbx), %rdi
leaq 0x18(%rsp), %r12
cmpq %r12, %rdi
je 0x48e7
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3200
leaq 0xe982(%rip), %rdi      # 0x13270
leaq 0x8a20(%rip), %rsi      # 0xd315
movl $0x6, %edx
callq 0x3260
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0xe960(%rip), %rdi      # 0x13270
callq 0x3260
movq %rax, %rbx
leaq 0x8ab4(%rip), %rsi      # 0xd3d3
movl $0x2, %edx
movq %rax, %rdi
callq 0x3260
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x3ecd
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x3260
leaq 0x8bce(%rip), %rsi      # 0xd520
movl $0x1, %edx
movq %rax, %rdi
callq 0x3260
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x49b9
movq 0x18(%rsp), %rsi
jmp 0x4840
movq 0xe676(%rip), %rbx      # 0x12ff0
leaq 0x8b1d(%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 0xe644(%rip), %rdi      # 0x12ff0
callq 0x3030
movq %rax, %rdi
callq 0x3150
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x49d0
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3200
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
addq $0x28, %rsi
leaq 0xe859(%rip), %rdi      # 0x13240
callq 0x76f8
movq %rax, %r14
addq $0x28, %rbx
leaq 0xe846(%rip), %rdi      # 0x13240
movq %rbx, %rsi
callq 0x76f8
movq %rax, %rdi
movq %r14, %rsi
callq 0x3130
jmp 0x49b9
movq 0xe5da(%rip), %rdi      # 0x12ff0
leaq 0x8a6b(%rip), %rsi      # 0xd488
movl $0x15, %edx
callq 0x3260
movq 0xe5c2(%rip), %rdi      # 0x12ff0
callq 0x3100
jmp 0x49b9
movq 0x18(%rbx), %rsi
leaq 0x8(%rsp), %rdi
callq 0x5235
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x32b0
movq (%rbx), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x4a6f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3200
leaq 0xe7fa(%rip), %rdi      # 0x13270
leaq 0x8898(%rip), %rsi      # 0xd315
movl $0x6, %edx
callq 0x3260
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0xe7d8(%rip), %rdi      # 0x13270
callq 0x3260
leaq 0x89c1(%rip), %rsi      # 0xd465
movl $0x7, %edx
movq %rax, %rdi
callq 0x3260
leaq 0xe7b8(%rip), %rdi      # 0x13270
leaq 0x89ae(%rip), %rsi      # 0xd46d
movl $0x7, %edx
callq 0x3260
leaq 0xe7a0(%rip), %rdi      # 0x13270
leaq 0x899e(%rip), %rsi      # 0xd475
movl $0x5, %edx
callq 0x3260
jmp 0x49b9
movq %rbx, %rdi
callq 0x47a9
jmp 0x49b9
movq %rbx, %rdi
callq 0x682a
jmp 0x49b9
movq %rbx, %rdi
callq 0x69fa
jmp 0x49b9
movq %rbx, %rdi
callq 0x6c31
jmp 0x49b9
jmp 0x4b3e
jmp 0x4b3e
jmp 0x4b3e
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x4b41
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x4b41
jmp 0x4b3e
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x4b58
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3200
movq %rbx, %rdi
callq 0x33d0