// File: /workspace/github_cpp_makefile_star2_O1/JameyWoo[P]csmile/assembly.cpp
// Function: genCalc(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 genCalc(TreeNode* calc, map<string, string> var2stack) {
    // TODO: 出了一个bug, 要注意, 相同优先级是要先左后右来计算的
    // TODO: 有 bug, 多重计算的时候, 寄存器会混淆
    // * 将计算结果保存到 %edx 中
    // * 递归地先将左值计算, 存储到%edx中, 将右值计算, 存储到%ecx中. %eax做暂时的中介.

    // debug("calc->nodekind: " + calc->nodekind);
    // debug("calc->op: " + calc->op);
    // 计算
    // debug("calc->child[1]->nodekind: " + calc->child[1]->nodekind);

    string left_loc;
    string right_loc;
    // 是否有左值也是表达式的情况?
    // 将左值放入 %eax
    if (calc->child[0]->nodekind == "MDOp" || calc->child[0]->nodekind == "PMOp") {
        genCalc(calc->child[0], var2stack);
        assout << "\tmovl\t%edx, %eax" << endl;
    } else if (calc->child[0]->nodekind == "Id") {
        left_loc = var2stack[calc->child[0]->name];
        assout << "\tmovl\t" << left_loc << ", %eax" << endl;
    } else if (calc->child[0]->nodekind == "Const") {
        left_loc = "$" + to_string(calc->child[1]->val);
        assout << "\tmovl\t" << left_loc << ", %eax" << endl;
    }
    left_loc = "%eax";

    // 如果是计算结点, 那么递归地调用, 得到的数放入寄存器 %ecx
    // 如果是 Id, 那么得到栈地址
    // 如果是 Const, 那么生成 $num
    if (calc->child[1]->nodekind == "MDOp" || calc->child[1]->nodekind == "PMOp") {
        genCalc(calc->child[1], var2stack);
        assout << "\tmovl\t%edx, %ecx" << endl;
        right_loc = "%ecx";
    } else if (calc->child[1]->nodekind == "Id") {
        right_loc = var2stack[calc->child[1]->name];
    } else if (calc->child[1]->nodekind == "Const") {
        right_loc = "$" + to_string(calc->child[1]->val);
    }
    if (calc->op == "/") {
        assout << "\tmovl\t" << left_loc << ", %eax" << endl
               << "\tmovl\t%eax, %edx" << endl
               << "\tsarl\t$31, %edx" << endl
               << "\tidivl\t" << right_loc << endl
               << "\tmovl\t%eax, %edx" << endl;
    } else {
        assout << "\tmovl\t" << left_loc << ", %edx" << endl;

        if (calc->op == "+") {
            assout << "\taddl\t" << right_loc << ", %edx" << endl;
        } else if (calc->op == "-") {
            assout << "\tsubl\t" << right_loc << ", %edx" << endl;
        } else if (calc->op == "*") {
            assout << "\timull\t" << right_loc << ", %edx" << endl;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x38(%rsp), %r13
movq %r13, -0x10(%r13)
xorl %eax, %eax
movq %rax, -0x8(%r13)
movb %al, (%r13)
leaq 0x18(%rsp), %r12
movq %r12, -0x10(%r12)
movq %rax, -0x8(%r12)
movb %al, (%r12)
movq (%rdi), %rdi
addq $0x30, %rdi
leaq 0x3301(%rip), %rsi      # 0x1526b
callq 0x2090
testl %eax, %eax
je 0x1203d
movq (%rbx), %rdi
addq $0x30, %rdi
leaq 0x32eb(%rip), %rsi      # 0x15270
callq 0x2090
testl %eax, %eax
je 0x1203d
movq (%rbx), %rdi
addq $0x30, %rdi
leaq 0x3280(%rip), %rsi      # 0x15220
callq 0x2090
movq (%rbx), %rcx
testl %eax, %eax
je 0x12543
addq $0x30, %rcx
leaq 0x32aa(%rip), %rsi      # 0x15265
movq %rcx, %rdi
callq 0x2090
testl %eax, %eax
jne 0x120b3
movq 0x8(%rbx), %rax
movl 0xb8(%rax), %r15d
movl %r15d, %ebp
negl %ebp
cmovsl %r15d, %ebp
movl $0x1, %r12d
cmpl $0xa, %ebp
jb 0x126a2
movl $0x4, %r12d
movl $0xd1b71759, %eax       # imm = 0xD1B71759
movl %ebp, %ecx
cmpl $0x63, %ecx
jbe 0x12690
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x1269f
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x126a2
movl %ecx, %edx
imulq %rax, %rdx
shrq $0x2d, %rdx
addl $0x4, %r12d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x11ffb
addl $-0x3, %r12d
jmp 0x126a2
movq (%rbx), %r15
leaq 0xb8(%rsp), %rdi
movq %r14, %rsi
callq 0x13e9a
leaq 0xb8(%rsp), %rsi
movq %r15, %rdi
callq 0x11f1f
leaq 0xb8(%rsp), %rdi
callq 0xabe4
leaq 0x9b6c(%rip), %r15      # 0x1bbe0
leaq 0x37b6(%rip), %rsi      # 0x15831
movl $0x10, %edx
movq %r15, %rdi
callq 0x2220
movq 0x9b51(%rip), %rax      # 0x1bbe0
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2120
leaq 0x28(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x3692(%rip), %rcx      # 0x15755
movl $0x4, %r8d
xorl %esi, %esi
callq 0x22b0
movq 0x8(%rbx), %rdi
addq $0x30, %rdi
leaq 0x318c(%rip), %rsi      # 0x1526b
callq 0x2090
testl %eax, %eax
je 0x121b5
movq 0x8(%rbx), %rdi
addq $0x30, %rdi
leaq 0x3175(%rip), %rsi      # 0x15270
callq 0x2090
testl %eax, %eax
je 0x121b5
movq 0x8(%rbx), %rdi
addq $0x30, %rdi
leaq 0x3109(%rip), %rsi      # 0x15220
callq 0x2090
movq 0x8(%rbx), %rcx
testl %eax, %eax
je 0x125c0
addq $0x30, %rcx
leaq 0x3132(%rip), %rsi      # 0x15265
movq %rcx, %rdi
callq 0x2090
testl %eax, %eax
jne 0x1224d
movq 0x8(%rbx), %rax
movl 0xb8(%rax), %r14d
movl %r14d, %ebp
negl %ebp
cmovsl %r14d, %ebp
movl $0x1, %r15d
cmpl $0xa, %ebp
jb 0x127e6
movl $0x4, %r15d
movl $0xd1b71759, %eax       # imm = 0xD1B71759
movl %ebp, %ecx
cmpl $0x63, %ecx
jbe 0x12696
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x127e3
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x127e6
movl %ecx, %edx
imulq %rax, %rdx
shrq $0x2d, %rdx
addl $0x4, %r15d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x12173
addl $-0x3, %r15d
jmp 0x127e6
movq 0x8(%rbx), %r15
leaq 0x88(%rsp), %rdi
movq %r14, %rsi
callq 0x13e9a
leaq 0x88(%rsp), %rsi
movq %r15, %rdi
callq 0x11f1f
leaq 0x88(%rsp), %rdi
callq 0xabe4
leaq 0x99f3(%rip), %r14      # 0x1bbe0
leaq 0x364e(%rip), %rsi      # 0x15842
movl $0x10, %edx
movq %r14, %rdi
callq 0x2220
movq 0x99d8(%rip), %rax      # 0x1bbe0
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
leaq 0x99bd(%rip), %rdi      # 0x1bbe0
callq 0x2030
movq %rax, %rdi
callq 0x2120
leaq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x360e(%rip), %rcx      # 0x1584e
movl $0x4, %r8d
xorl %esi, %esi
callq 0x22b0
addq $0x98, %rbx
leaq 0x35f8(%rip), %rsi      # 0x15853
movq %rbx, %rdi
callq 0x2090
testl %eax, %eax
je 0x1237a
leaq 0x996e(%rip), %rdi      # 0x1bbe0
leaq 0x34b4(%rip), %rsi      # 0x1572d
movl $0x6, %edx
callq 0x2220
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0x994c(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %r14
leaq 0x35cc(%rip), %rsi      # 0x1586f
movl $0x6, %edx
movq %rax, %rdi
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
leaq 0x35a0(%rip), %rsi      # 0x1587e
movq %rbx, %rdi
callq 0x2090
testl %eax, %eax
je 0x124e5
leaq 0x337d(%rip), %rsi      # 0x15672
movq %rbx, %rdi
callq 0x2090
testl %eax, %eax
je 0x125e4
leaq 0x3582(%rip), %rsi      # 0x1588e
movq %rbx, %rdi
callq 0x2090
testl %eax, %eax
jne 0x12650
leaq 0x98bd(%rip), %rdi      # 0x1bbe0
leaq 0x3566(%rip), %rsi      # 0x15890
movl $0x7, %edx
callq 0x2220
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x989b(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %rbx
leaq 0x351b(%rip), %rsi      # 0x1586f
movl $0x6, %edx
movq %rax, %rdi
callq 0x2220
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
jmp 0x1263d
leaq 0x985f(%rip), %rdi      # 0x1bbe0
leaq 0x33a5(%rip), %rsi      # 0x1572d
movl $0x6, %edx
callq 0x2220
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0x983d(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %rbx
leaq 0x33a1(%rip), %rsi      # 0x15753
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 %rax, %rbx
leaq 0x3465(%rip), %rsi      # 0x15855
movl $0x10, %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 %rax, %rbx
leaq 0x3438(%rip), %rsi      # 0x15866
movl $0xf, %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 %rax, %rbx
leaq 0x340a(%rip), %rsi      # 0x15876
movl $0x7, %edx
movq %rax, %rdi
callq 0x2220
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %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
callq 0x2120
movq %rax, %rbx
leaq 0x3396(%rip), %rsi      # 0x15855
movl $0x10, %edx
movq %rax, %rdi
callq 0x2220
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
jmp 0x1263d
leaq 0x96f4(%rip), %rdi      # 0x1bbe0
leaq 0x338d(%rip), %rsi      # 0x15880
movl $0x6, %edx
callq 0x2220
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x96d2(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %rbx
leaq 0x3352(%rip), %rsi      # 0x1586f
movl $0x6, %edx
movq %rax, %rdi
callq 0x2220
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
jmp 0x1263d
addq $0xc0, %rcx
movq %r14, %rdi
movq %rcx, %rsi
callq 0xac76
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x2100
leaq 0x9677(%rip), %rdi      # 0x1bbe0
leaq 0x31bd(%rip), %rsi      # 0x1572d
movl $0x6, %edx
callq 0x2220
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0x9655(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %r15
leaq 0x31b9(%rip), %rsi      # 0x15753
movl $0x6, %edx
movq %rax, %rdi
callq 0x2220
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21a0
jmp 0x120a0
addq $0xc0, %rcx
movq %r14, %rdi
movq %rcx, %rsi
callq 0xac76
leaq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x2100
jmp 0x1224d
leaq 0x95f5(%rip), %rdi      # 0x1bbe0
leaq 0x3295(%rip), %rsi      # 0x15887
movl $0x6, %edx
callq 0x2220
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x95d3(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %rbx
leaq 0x3253(%rip), %rsi      # 0x1586f
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 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x12667
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x1267e
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addl $-0x2, %r12d
jmp 0x126a2
addl $-0x2, %r15d
jmp 0x127e6
decl %r12d
shrl $0x1f, %r15d
leal (%r12,%r15), %esi
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x48(%rsp), %rdi
movl $0x2d, %edx
callq 0x2230
leaq 0x48(%rsp), %r13
addq (%r13), %r15
movq %r15, %rdi
movl %r12d, %esi
movl %ebp, %edx
callq 0xb06f
leaq 0x310f(%rip), %rcx      # 0x157ee
movl $0x1, %r8d
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2200
leaq 0x78(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x12719
movq %rdx, 0x68(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x78(%rsp)
jmp 0x12721
movups (%rcx), %xmm0
movups %xmm0, (%r12)
leaq 0x38(%rsp), %r13
movq 0x8(%rax), %rdx
leaq 0x68(%rsp), %r15
movq %rdx, 0x8(%r15)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x2240
movq (%r15), %rdi
cmpq %r12, %rdi
je 0x12764
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
leaq 0x18(%rsp), %r12
je 0x12785
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21c0
leaq 0x9454(%rip), %rdi      # 0x1bbe0
leaq 0x2f9a(%rip), %rsi      # 0x1572d
movl $0x6, %edx
callq 0x2220
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0x9432(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %r15
leaq 0x2f96(%rip), %rsi      # 0x15753
movl $0x6, %edx
movq %rax, %rdi
callq 0x2220
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21a0
jmp 0x120a0
decl %r15d
shrl $0x1f, %r14d
leal (%r15,%r14), %esi
leaq 0x58(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x48(%rsp), %rdi
movl $0x2d, %edx
callq 0x2230
leaq 0x48(%rsp), %r12
addq (%r12), %r14
movq %r14, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0xb06f
leaq 0x2fcb(%rip), %rcx      # 0x157ee
movl $0x1, %r8d
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2200
leaq 0x78(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x1285c
movq %rdx, 0x68(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x78(%rsp)
jmp 0x12863
movups (%rcx), %xmm0
movups %xmm0, (%r15)
leaq 0x18(%rsp), %r12
movq 0x8(%rax), %rdx
leaq 0x68(%rsp), %r14
movq %rdx, 0x8(%r14)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x2240
movq (%r14), %rdi
cmpq %r15, %rdi
je 0x128a6
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x48(%rsp), %rdi
cmpq %r13, %rdi
je 0x128bd
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21c0
leaq 0x38(%rsp), %r13
jmp 0x1224d
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r13, %rdi
jne 0x128ea
jmp 0x1291d
jmp 0x1291a
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x1291d
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21c0
jmp 0x1291d
jmp 0x1291a
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
jmp 0x12913
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0xabe4
jmp 0x1291d
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x12939
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x12955
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x2310