// File: /workspace/github_cpp_makefile_star2_O2/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 %r14
pushq %rbx
subq $0x68, %rsp
testq %rdi, %rdi
je 0x42e0
movq %rdi, %rbx
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl 0x10(%rdi), %eax
decl %eax
cmpl $0x4, %eax
ja 0x42bb
leaq 0x6f0f(%rip), %rcx      # 0xb0b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x488e
leaq 0x48(%rsp), %rdi
callq 0x30f0
jmp 0x42d6
movq %rbx, %rdi
callq 0x4406
jmp 0x42d6
movl 0x30(%rbx), %eax
addl $-0x2, %eax
cmpl $0xa, %eax
ja 0x42d6
leaq 0x6edb(%rip), %rcx      # 0xb0cc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
callq 0x5bc5
jmp 0x42d6
movq 0x18(%rbx), %rsi
testq %rsi, %rsi
je 0x42d6
movq 0x28(%rbx), %r14
movl (%r14), %eax
cmpl $0x5, %eax
je 0x4232
cmpl $0x4, %eax
je 0x42e8
cmpl $0x2, %eax
jne 0x431b
leaq 0x8(%rsp), %rdi
callq 0x488e
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x3280
movq %rbx, %rdi
callq 0x30f0
leaq 0xe00b(%rip), %rdi      # 0x12268
leaq 0x70a1(%rip), %rsi      # 0xb305
callq 0x31d0
leaq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x31c0
leaq 0x7146(%rip), %rsi      # 0xb3c3
movq %rax, %rdi
callq 0x31d0
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x3d89
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x31c0
leaq 0x7267(%rip), %rsi      # 0xb510
movq %rax, %rdi
callq 0x31d0
leaq 0x8(%rsp), %rdi
jmp 0x41c4
leaq 0x71cc(%rip), %rsi      # 0xb48e
movq 0xdd27(%rip), %rdi      # 0x11ff0
callq 0x31d0
movq %rax, %rdi
callq 0x3100
leaq 0x28(%rsp), %rdi
callq 0x30f0
addq $0x68, %rsp
popq %rbx
popq %r14
retq
addq $0x28, %rsi
leaq 0xdf45(%rip), %rdi      # 0x12238
callq 0x6198
movq %rax, %r14
addq $0x28, %rbx
leaq 0xdf32(%rip), %rdi      # 0x12238
movq %rbx, %rsi
callq 0x6198
movq %rax, %rdi
movq %r14, %rsi
callq 0x3130
jmp 0x42d6
leaq 0x7156(%rip), %rsi      # 0xb478
jmp 0x42c2
movq 0x18(%rbx), %rsi
leaq 0x8(%rsp), %rdi
callq 0x488e
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x3280
movq %rbx, %rdi
callq 0x30f0
leaq 0xdf15(%rip), %rdi      # 0x12268
leaq 0x6fab(%rip), %rsi      # 0xb305
callq 0x31d0
leaq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x31c0
leaq 0x70e2(%rip), %rsi      # 0xb455
movq %rax, %rdi
callq 0x31d0
leaq 0xdee6(%rip), %rdi      # 0x12268
leaq 0x70d4(%rip), %rsi      # 0xb45d
callq 0x31d0
leaq 0xded3(%rip), %rdi      # 0x12268
leaq 0x70c9(%rip), %rsi      # 0xb465
callq 0x31d0
jmp 0x42d6
movq %rbx, %rdi
callq 0x4144
jmp 0x42d6
movq %rbx, %rdi
callq 0x5737
jmp 0x42d6
movq %rbx, %rdi
callq 0x5893
jmp 0x42d6
movq %rbx, %rdi
callq 0x5a04
jmp 0x42d6
jmp 0x43f1
jmp 0x43f1
jmp 0x43f1
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x30f0
jmp 0x43f4
jmp 0x43f1
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x30f0
movq %rbx, %rdi
callq 0x3390