// File: /workspace/github_cpp_makefile_star2_O1/JameyWoo[P]csmile/assembly.cpp
// Function: genCall(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 genCall(TreeNode* call, map<string, string> var2stack) {
    // ! 结果在 %eax中
    if (call == NULL) return;
    // debug("call: " + call->child[0]->nodekind);
    // 解析 Call 的参数
    // call->child[0] 是这个函数的名字
    // call->child[1] 如果不为 NULL 的话, 是 Args 类型的
    // 解析args, arg可以是expression, 种类比较多. 考虑是不是要综合一个整体的
    TreeNode* args = call->child[1];
    if (args == NULL) {
        // TODO: call 没有参数的情况
        assout << "\tcall\t" << call->child[0]->name << endl;
        return;
    }
    // debug("args: " + args->child[0]->nodekind);
    // debug("args: " + args->child[0]->sibling->nodekind);

    // 传递参数
    stack<TreeNode*> call_stack;
    TreeNode* arg = args->child[0];
    int cnt       = 0;
    while (arg != NULL) {
        cnt += 1;
        call_stack.push(arg);
        arg = arg->sibling;
    }
    // 调整栈的位置, 防止参数过多
    cnt -= 1;
    assout << "\tsubl\t$" << 4 * cnt << ", %esp" << endl;
    while (not call_stack.empty()) {
        string stack_loc = to_string(4 * cnt) + "(%esp)";
        TreeNode* tmp    = call_stack.top();
        // debug("arg type: " + tmp->nodekind);
        string left = "";
        if (tmp->nodekind == "Const") {
            left += "$" + to_string(tmp->val);
        } else if (tmp->nodekind == "Id") {
            assout << "\tmovl\t" << var2stack[tmp->name] << ", %edx" << endl;
            left += "%edx";
        } else if (tmp->nodekind == "PMOp" || tmp->nodekind == "MDOp") {
            // 一个计算的表达式
            genCalc(tmp, var2stack);
            left += "%edx";
        }
        assout << "\tmovl\t" << left << ", " << stack_loc << endl;
        // TODO: 支持更多的参数类型

        call_stack.pop();
        cnt -= 1;
    }
    // 调用函数
    assout << "\tcall\t" << call->child[0]->name << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rsi, 0x48(%rsp)
testq %rdi, %rdi
je 0xfbbc
movq %rdi, %r15
movq 0x8(%rdi), %rbx
testq %rbx, %rbx
je 0xfbce
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %rdi
movaps %xmm0, 0x40(%rdi)
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
xorl %esi, %esi
callq 0x148d6
movq (%rbx), %rax
movq %rax, 0x20(%rsp)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testq %rax, %rax
je 0xf5bf
leaq 0x90(%rsp), %rbx
leaq 0x20(%rsp), %r14
movq 0xc0(%rsp), %rcx
movq 0xd0(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0xf59e
movq %rax, (%rcx)
addq $0x8, 0xc0(%rsp)
jmp 0xf5a9
movq %rbx, %rdi
movq %r14, %rsi
callq 0x14a4c
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x20(%rsp)
incl %r13d
testq %rax, %rax
jne 0xf577
movq %r15, 0xe0(%rsp)
leaq 0xc612(%rip), %rdi      # 0x1bbe0
leaq 0x6213(%rip), %rsi      # 0x157e8
movl $0x7, %edx
callq 0x2220
leal (,%r13,4), %esi
leaq 0xc5f2(%rip), %rdi      # 0x1bbe0
callq 0x2300
movq %rax, %rbx
leaq 0x61f3(%rip), %rsi      # 0x157f0
movl $0x6, %edx
movq %rax, %rdi
callq 0x2220
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2120
movq 0xc0(%rsp), %rax
cmpq 0xa0(%rsp), %rax
je 0xfb48
leaq 0x60(%rsp), %r15
movq %rsp, %rbx
movl $0xd1b71759, %edx       # imm = 0xD1B71759
leal (,%r13,4), %r14d
movl %r14d, %ebp
negl %ebp
cmovsl %r14d, %ebp
movl $0x1, %r12d
cmpl $0xa, %ebp
jb 0xf6b1
movl $0x4, %r12d
movl %ebp, %eax
cmpl $0x63, %eax
jbe 0xf6a8
cmpl $0x3e7, %eax            # imm = 0x3E7
jbe 0xf6ae
cmpl $0x2710, %eax           # imm = 0x2710
jb 0xf6b1
movl %eax, %ecx
imulq %rdx, %rcx
shrq $0x2d, %rcx
addl $0x4, %r12d
cmpl $0x1869f, %eax          # imm = 0x1869F
movl %ecx, %eax
ja 0xf678
addl $-0x3, %r12d
jmp 0xf6b1
addl $-0x2, %r12d
jmp 0xf6b1
decl %r12d
shrl $0x1f, %r14d
leal (%r12,%r14), %esi
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movl $0x2d, %edx
callq 0x2230
movl %r14d, %edi
addq (%rsp), %rdi
movl %r12d, %esi
movl %ebp, %edx
callq 0xb06f
movq %rbx, %rdi
leaq 0x607c(%rip), %rsi      # 0x15766
callq 0x2340
leaq 0x38(%rsp), %rbp
movq %rbp, 0x28(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
leaq 0x70(%rsp), %rbx
je 0xf71c
movq %rdx, 0x28(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x38(%rsp)
jmp 0xf723
movups (%rcx), %xmm0
movups %xmm0, (%rbp)
movq 0x8(%rax), %rdx
movq %rdx, 0x30(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xf756
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0xc0(%rsp), %rax
cmpq 0xc8(%rsp), %rax
jne 0xf77c
movq 0xd8(%rsp), %rax
movq -0x8(%rax), %rax
movl $0x200, %ecx            # imm = 0x200
addq %rcx, %rax
movq -0x8(%rax), %r12
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %rdi
leaq 0x5f99(%rip), %rdx      # 0x1572c
movq %rdx, %rsi
callq 0x312c
leaq 0x30(%r12), %r14
movq %r14, %rdi
leaq 0x5abb(%rip), %rsi      # 0x15265
callq 0x2090
testl %eax, %eax
je 0xf82d
movq %r14, %rdi
leaq 0x5a63(%rip), %rsi      # 0x15220
callq 0x2090
testl %eax, %eax
je 0xf89c
movq %r14, %rdi
leaq 0x5a9c(%rip), %rsi      # 0x15270
callq 0x2090
testl %eax, %eax
leaq 0xc3fe(%rip), %rbx      # 0x1bbe0
je 0xf7fb
movq %r14, %rdi
leaq 0x5a7d(%rip), %rsi      # 0x1526b
callq 0x2090
testl %eax, %eax
jne 0xfa1b
leaq 0xe8(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x13e9a
movq %r12, %rdi
leaq 0xe8(%rsp), %r14
movq %r14, %rsi
callq 0x11f1f
movq %r14, %rdi
callq 0xabe4
jmp 0xf918
movl 0xb8(%r12), %ebp
movl %ebp, %r12d
negl %r12d
cmovsl %ebp, %r12d
movl $0x1, %r14d
cmpl $0xa, %r12d
jb 0xf935
movl $0x4, %r14d
movl %r12d, %eax
movl $0xd1b71759, %edx       # imm = 0xD1B71759
cmpl $0x63, %eax
jbe 0xf92c
cmpl $0x3e7, %eax            # imm = 0x3E7
jbe 0xf932
cmpl $0x2710, %eax           # imm = 0x2710
jb 0xf935
movl %eax, %ecx
imulq %rdx, %rcx
shrq $0x2d, %rcx
addl $0x4, %r14d
cmpl $0x1869f, %eax          # imm = 0x1869F
movl %ecx, %eax
ja 0xf85d
addl $-0x3, %r14d
jmp 0xf935
movl $0x6, %edx
leaq 0xc338(%rip), %rbx      # 0x1bbe0
movq %rbx, %rdi
leaq 0x5e7b(%rip), %rsi      # 0x1572d
callq 0x2220
addq $0xc0, %r12
movq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0xac76
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %rbx, %rdi
callq 0x2220
movq %rax, %r14
movl $0x6, %edx
movq %rax, %rdi
leaq 0x5f83(%rip), %rsi      # 0x1586f
callq 0x2220
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2120
movq %rsp, %rdi
leaq 0x5f4f(%rip), %rsi      # 0x15871
callq 0x2340
jmp 0xfa1b
addl $-0x2, %r14d
jmp 0xf935
decl %r14d
shrl $0x1f, %ebp
leal (%r14,%rbp), %esi
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
movq %rbx, %rdi
movl $0x2d, %edx
callq 0x2230
addq 0x70(%rsp), %rbp
movq %rbp, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0xb06f
movl $0x1, %r8d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x5e71(%rip), %rcx      # 0x157ee
callq 0x2200
movq %r15, 0x50(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
leaq 0x38(%rsp), %rbp
movq %rsp, %rdi
leaq 0xc23d(%rip), %rbx      # 0x1bbe0
je 0xf9b4
movq %rdx, 0x50(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x60(%rsp)
jmp 0xf9bb
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
movq %rdx, 0x58(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
callq 0x20a0
movq 0x50(%rsp), %rdi
cmpq %r15, %rdi
je 0xf9f9
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0xfa1b
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21c0
movl $0x6, %edx
movq %rbx, %rdi
leaq 0x5d03(%rip), %rsi      # 0x1572d
callq 0x2220
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x2220
movq %rax, %r14
movl $0x2, %edx
movq %rax, %rdi
leaq 0x5ddc(%rip), %rsi      # 0x1582e
callq 0x2220
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq %r14, %rdi
callq 0x2220
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2120
movq 0xc0(%rsp), %rax
movq 0xc8(%rsp), %rdi
cmpq %rdi, %rax
je 0xfaae
addq $-0x8, %rax
jmp 0xfaed
movl $0x200, %esi            # imm = 0x200
callq 0x21c0
movq 0xd8(%rsp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0xd8(%rsp)
movq -0x8(%rax), %rax
movq %rax, 0xc8(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0xd0(%rsp)
addq $0x1f8, %rax            # imm = 0x1F8
movq %rax, 0xc0(%rsp)
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xfb10
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0xfb27
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
decl %r13d
movq 0xc0(%rsp), %rax
cmpq 0xa0(%rsp), %rax
movq %rsp, %rbx
movl $0xd1b71759, %edx       # imm = 0xD1B71759
jne 0xf654
leaq 0xc091(%rip), %rdi      # 0x1bbe0
leaq 0x5c8b(%rip), %rsi      # 0x157e1
movl $0x6, %edx
callq 0x2220
movq 0xe0(%rsp), %rax
movq (%rax), %rax
movq 0xc0(%rax), %rsi
movq 0xc8(%rax), %rdx
leaq 0xc060(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2120
leaq 0x90(%rsp), %rdi
callq 0x1405a
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc00b(%rip), %rbx      # 0x1bbe0
leaq 0x5c05(%rip), %rsi      # 0x157e1
movl $0x6, %edx
movq %rbx, %rdi
callq 0x2220
movq (%r15), %rax
movq 0xc0(%rax), %rsi
movq 0xc8(%rax), %rdx
movq %rbx, %rdi
callq 0x2220
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2120
movq %rax, %r14
leaq 0xe8(%rsp), %rdi
callq 0xabe4
jmp 0xfcbd
jmp 0xfcba
movq %rax, %r14
movq 0x50(%rsp), %rdi
cmpq %r15, %rdi
je 0xfc70
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21c0
jmp 0xfc70
movq %rax, %r14
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0xfcbd
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21c0
jmp 0xfcbd
jmp 0xfc98
jmp 0xfc98
movq %rax, %r14
jmp 0xfcf4
movq %rax, %r14
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xfcf4
movq 0x10(%rsp), %rsi
jmp 0xfcec
movq %rax, %r14
jmp 0xfcd8
movq %rax, %r14
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xfcd8
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0xfcf4
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
leaq 0x90(%rsp), %rdi
callq 0x1405a
movq %r14, %rdi
callq 0x2310