// File: JameyWoo[P]csmile/assembly.cpp
// Function: genReturn(TreeNode*, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>>)
void genReturn(TreeNode* returnStmt, map<string, string> var2stack) {
    // debug("return: " + returnStmt->child[0]->nodekind);
    if (returnStmt->child[0]->nodekind == "Id") {
        assout << "\tmovl\t" << var2stack[returnStmt->child[0]->name] << ", %eax" << endl;
    } else if (returnStmt->child[0]->nodekind == "Const") {
        assout << "\tmovl\t$" << to_string(returnStmt->child[0]->val) << ", %eax" << endl;
    } else if (returnStmt->child[0]->nodekind == "Call") {
        // debug("return call: " + returnStmt->child[0]->nodekind);
        genCall(returnStmt->child[0], var2stack);
    }
    // TODO: 除了直接返回Id, 还有其他返回形式. 如 a + b, input() 这种
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rsi, -0x78(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
addq $0x30, %rdi
leaq 0x7903(%rip), %rsi      # 0x211ac
callq 0x5e70
testb $0x1, %al
jne 0x198b4
jmp 0x19911
leaq 0x153ed(%rip), %rdi     # 0x2eca8
leaq 0x7dd4(%rip), %rsi      # 0x21696
callq 0x3270
movq -0x78(%rbp), %rdi
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rsi
addq $0xc0, %rsi
callq 0xf2e0
movq -0x80(%rbp), %rdi
movq %rax, %rsi
callq 0x3240
movq %rax, %rdi
leaq 0x7dc4(%rip), %rsi      # 0x216bc
callq 0x3270
movq %rax, %rdi
movq 0x146a9(%rip), %rsi     # 0x2dfb0
callq 0x32b0
jmp 0x19a57
movq -0x8(%rbp), %rax
movq (%rax), %rdi
addq $0x30, %rdi
leaq 0x78c6(%rip), %rsi      # 0x211e9
callq 0x5e70
testb $0x1, %al
jne 0x19931
jmp 0x199dd
leaq 0x15370(%rip), %rdi     # 0x2eca8
leaq 0x7de0(%rip), %rsi      # 0x2171f
callq 0x3270
movq %rax, -0x98(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0xb8(%rax), %esi
leaq -0x30(%rbp), %rdi
movq %rdi, -0x90(%rbp)
callq 0xf460
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x3240
movq %rax, -0x88(%rbp)
jmp 0x19984
movq -0x88(%rbp), %rdi
leaq 0x7d2a(%rip), %rsi      # 0x216bc
callq 0x3270
movq %rax, -0xa0(%rbp)
jmp 0x199a0
movq -0xa0(%rbp), %rdi
movq 0x14602(%rip), %rsi     # 0x2dfb0
callq 0x32b0
jmp 0x199b5
leaq -0x30(%rbp), %rdi
callq 0x34e8
jmp 0x19a55
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x34e8
jmp 0x19a60
movq -0x8(%rbp), %rax
movq (%rax), %rdi
addq $0x30, %rdi
leaq 0x77eb(%rip), %rsi      # 0x211da
callq 0x5e70
testb $0x1, %al
jne 0x199fa
jmp 0x19a53
movq -0x78(%rbp), %rsi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
leaq -0x70(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x1c040
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
callq 0x18230
jmp 0x19a31
leaq -0x70(%rbp), %rdi
callq 0xee40
jmp 0x19a53
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0xee40
jmp 0x19a60
jmp 0x19a55
jmp 0x19a57
addq $0xb0, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x3470
nopl (%rax)