// File: /workspace/github_cpp_makefile_star2_O3/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 0xfb2c
movq %rdi, %r15
movq 0x8(%rdi), %rbx
testq %rbx, %rbx
je 0xfb3e
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 0x14854
movq (%rbx), %rax
movq %rax, 0x20(%rsp)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testq %rax, %rax
je 0xf52f
leaq 0x90(%rsp), %rbx
leaq 0x20(%rsp), %r14
movq 0xc0(%rsp), %rcx
movq 0xd0(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0xf50e
movq %rax, (%rcx)
addq $0x8, 0xc0(%rsp)
jmp 0xf519
movq %rbx, %rdi
movq %r14, %rsi
callq 0x149ca
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x20(%rsp)
incl %r13d
testq %rax, %rax
jne 0xf4e7
movq %r15, 0xe0(%rsp)
leaq 0xc6a2(%rip), %rdi      # 0x1bbe0
leaq 0x62a3(%rip), %rsi      # 0x157e8
movl $0x7, %edx
callq 0x2220
leal (,%r13,4), %esi
leaq 0xc682(%rip), %rdi      # 0x1bbe0
callq 0x2300
movq %rax, %rbx
leaq 0x6283(%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 0xfab8
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 0xf621
movl $0x4, %r12d
movl %ebp, %eax
cmpl $0x63, %eax
jbe 0xf618
cmpl $0x3e7, %eax            # imm = 0x3E7
jbe 0xf61e
cmpl $0x2710, %eax           # imm = 0x2710
jb 0xf621
movl %eax, %ecx
imulq %rdx, %rcx
shrq $0x2d, %rcx
addl $0x4, %r12d
cmpl $0x1869f, %eax          # imm = 0x1869F
movl %ecx, %eax
ja 0xf5e8
addl $-0x3, %r12d
jmp 0xf621
addl $-0x2, %r12d
jmp 0xf621
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 0xafe5
movq %rbx, %rdi
leaq 0x610c(%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 0xf68c
movq %rdx, 0x28(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x38(%rsp)
jmp 0xf693
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 0xf6c6
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0xc0(%rsp), %rax
cmpq 0xc8(%rsp), %rax
jne 0xf6ec
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 0x6029(%rip), %rdx      # 0x1572c
movq %rdx, %rsi
callq 0x312c
leaq 0x30(%r12), %r14
movq %r14, %rdi
leaq 0x5b4b(%rip), %rsi      # 0x15265
callq 0x2090
testl %eax, %eax
je 0xf79d
movq %r14, %rdi
leaq 0x5af3(%rip), %rsi      # 0x15220
callq 0x2090
testl %eax, %eax
je 0xf80c
movq %r14, %rdi
leaq 0x5b2c(%rip), %rsi      # 0x15270
callq 0x2090
testl %eax, %eax
leaq 0xc48e(%rip), %rbx      # 0x1bbe0
je 0xf76b
movq %r14, %rdi
leaq 0x5b0d(%rip), %rsi      # 0x1526b
callq 0x2090
testl %eax, %eax
jne 0xf98b
leaq 0xe8(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x13e16
movq %r12, %rdi
leaq 0xe8(%rsp), %r14
movq %r14, %rsi
callq 0x11e8f
movq %r14, %rdi
callq 0xab5a
jmp 0xf888
movl 0xb8(%r12), %ebp
movl %ebp, %r12d
negl %r12d
cmovsl %ebp, %r12d
movl $0x1, %r14d
cmpl $0xa, %r12d
jb 0xf8a5
movl $0x4, %r14d
movl %r12d, %eax
movl $0xd1b71759, %edx       # imm = 0xD1B71759
cmpl $0x63, %eax
jbe 0xf89c
cmpl $0x3e7, %eax            # imm = 0x3E7
jbe 0xf8a2
cmpl $0x2710, %eax           # imm = 0x2710
jb 0xf8a5
movl %eax, %ecx
imulq %rdx, %rcx
shrq $0x2d, %rcx
addl $0x4, %r14d
cmpl $0x1869f, %eax          # imm = 0x1869F
movl %ecx, %eax
ja 0xf7cd
addl $-0x3, %r14d
jmp 0xf8a5
movl $0x6, %edx
leaq 0xc3c8(%rip), %rbx      # 0x1bbe0
movq %rbx, %rdi
leaq 0x5f0b(%rip), %rsi      # 0x1572d
callq 0x2220
addq $0xc0, %r12
movq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0xabec
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %rbx, %rdi
callq 0x2220
movq %rax, %r14
movl $0x6, %edx
movq %rax, %rdi
leaq 0x6013(%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 0x5fdf(%rip), %rsi      # 0x15871
callq 0x2340
jmp 0xf98b
addl $-0x2, %r14d
jmp 0xf8a5
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 0xafe5
movl $0x1, %r8d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x5f01(%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 0xc2cd(%rip), %rbx      # 0x1bbe0
je 0xf924
movq %rdx, 0x50(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x60(%rsp)
jmp 0xf92b
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 0xf969
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0xf98b
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21c0
movl $0x6, %edx
movq %rbx, %rdi
leaq 0x5d93(%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 0x5e6c(%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 0xfa1e
addq $-0x8, %rax
jmp 0xfa5d
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 0xfa80
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0xfa97
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 0xf5c4
leaq 0xc121(%rip), %rdi      # 0x1bbe0
leaq 0x5d1b(%rip), %rsi      # 0x157e1
movl $0x6, %edx
callq 0x2220
movq 0xe0(%rsp), %rax
movq (%rax), %rax
movq 0xc0(%rax), %rsi
movq 0xc8(%rax), %rdx
leaq 0xc0f0(%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 0x13fd6
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc09b(%rip), %rbx      # 0x1bbe0
leaq 0x5c95(%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 0xab5a
jmp 0xfc2d
jmp 0xfc2a
movq %rax, %r14
movq 0x50(%rsp), %rdi
cmpq %r15, %rdi
je 0xfbe0
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21c0
jmp 0xfbe0
movq %rax, %r14
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0xfc2d
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21c0
jmp 0xfc2d
jmp 0xfc08
jmp 0xfc08
movq %rax, %r14
jmp 0xfc64
movq %rax, %r14
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xfc64
movq 0x10(%rsp), %rsi
jmp 0xfc5c
movq %rax, %r14
jmp 0xfc48
movq %rax, %r14
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xfc48
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0xfc64
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
leaq 0x90(%rsp), %rdi
callq 0x13fd6
movq %r14, %rdi
callq 0x2310