// File: /workspace/github_cpp_makefile_star2_O3/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 0x3391(%rip), %rsi      # 0x1526b
callq 0x2090
testl %eax, %eax
je 0x11fad
movq (%rbx), %rdi
addq $0x30, %rdi
leaq 0x337b(%rip), %rsi      # 0x15270
callq 0x2090
testl %eax, %eax
je 0x11fad
movq (%rbx), %rdi
addq $0x30, %rdi
leaq 0x3310(%rip), %rsi      # 0x15220
callq 0x2090
movq (%rbx), %rcx
testl %eax, %eax
je 0x124b3
addq $0x30, %rcx
leaq 0x333a(%rip), %rsi      # 0x15265
movq %rcx, %rdi
callq 0x2090
testl %eax, %eax
jne 0x12023
movq 0x8(%rbx), %rax
movl 0xb8(%rax), %r15d
movl %r15d, %ebp
negl %ebp
cmovsl %r15d, %ebp
movl $0x1, %r12d
cmpl $0xa, %ebp
jb 0x12612
movl $0x4, %r12d
movl $0xd1b71759, %eax       # imm = 0xD1B71759
movl %ebp, %ecx
cmpl $0x63, %ecx
jbe 0x12600
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x1260f
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x12612
movl %ecx, %edx
imulq %rax, %rdx
shrq $0x2d, %rdx
addl $0x4, %r12d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x11f6b
addl $-0x3, %r12d
jmp 0x12612
movq (%rbx), %r15
leaq 0xb8(%rsp), %rdi
movq %r14, %rsi
callq 0x13e16
leaq 0xb8(%rsp), %rsi
movq %r15, %rdi
callq 0x11e8f
leaq 0xb8(%rsp), %rdi
callq 0xab5a
leaq 0x9bfc(%rip), %r15      # 0x1bbe0
leaq 0x3846(%rip), %rsi      # 0x15831
movl $0x10, %edx
movq %r15, %rdi
callq 0x2220
movq 0x9be1(%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 0x3722(%rip), %rcx      # 0x15755
movl $0x4, %r8d
xorl %esi, %esi
callq 0x22b0
movq 0x8(%rbx), %rdi
addq $0x30, %rdi
leaq 0x321c(%rip), %rsi      # 0x1526b
callq 0x2090
testl %eax, %eax
je 0x12125
movq 0x8(%rbx), %rdi
addq $0x30, %rdi
leaq 0x3205(%rip), %rsi      # 0x15270
callq 0x2090
testl %eax, %eax
je 0x12125
movq 0x8(%rbx), %rdi
addq $0x30, %rdi
leaq 0x3199(%rip), %rsi      # 0x15220
callq 0x2090
movq 0x8(%rbx), %rcx
testl %eax, %eax
je 0x12530
addq $0x30, %rcx
leaq 0x31c2(%rip), %rsi      # 0x15265
movq %rcx, %rdi
callq 0x2090
testl %eax, %eax
jne 0x121bd
movq 0x8(%rbx), %rax
movl 0xb8(%rax), %r14d
movl %r14d, %ebp
negl %ebp
cmovsl %r14d, %ebp
movl $0x1, %r15d
cmpl $0xa, %ebp
jb 0x12756
movl $0x4, %r15d
movl $0xd1b71759, %eax       # imm = 0xD1B71759
movl %ebp, %ecx
cmpl $0x63, %ecx
jbe 0x12606
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x12753
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x12756
movl %ecx, %edx
imulq %rax, %rdx
shrq $0x2d, %rdx
addl $0x4, %r15d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x120e3
addl $-0x3, %r15d
jmp 0x12756
movq 0x8(%rbx), %r15
leaq 0x88(%rsp), %rdi
movq %r14, %rsi
callq 0x13e16
leaq 0x88(%rsp), %rsi
movq %r15, %rdi
callq 0x11e8f
leaq 0x88(%rsp), %rdi
callq 0xab5a
leaq 0x9a83(%rip), %r14      # 0x1bbe0
leaq 0x36de(%rip), %rsi      # 0x15842
movl $0x10, %edx
movq %r14, %rdi
callq 0x2220
movq 0x9a68(%rip), %rax      # 0x1bbe0
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
leaq 0x9a4d(%rip), %rdi      # 0x1bbe0
callq 0x2030
movq %rax, %rdi
callq 0x2120
leaq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x369e(%rip), %rcx      # 0x1584e
movl $0x4, %r8d
xorl %esi, %esi
callq 0x22b0
addq $0x98, %rbx
leaq 0x3688(%rip), %rsi      # 0x15853
movq %rbx, %rdi
callq 0x2090
testl %eax, %eax
je 0x122ea
leaq 0x99fe(%rip), %rdi      # 0x1bbe0
leaq 0x3544(%rip), %rsi      # 0x1572d
movl $0x6, %edx
callq 0x2220
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0x99dc(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %r14
leaq 0x365c(%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 0x3630(%rip), %rsi      # 0x1587e
movq %rbx, %rdi
callq 0x2090
testl %eax, %eax
je 0x12455
leaq 0x340d(%rip), %rsi      # 0x15672
movq %rbx, %rdi
callq 0x2090
testl %eax, %eax
je 0x12554
leaq 0x3612(%rip), %rsi      # 0x1588e
movq %rbx, %rdi
callq 0x2090
testl %eax, %eax
jne 0x125c0
leaq 0x994d(%rip), %rdi      # 0x1bbe0
leaq 0x35f6(%rip), %rsi      # 0x15890
movl $0x7, %edx
callq 0x2220
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x992b(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %rbx
leaq 0x35ab(%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 0x125ad
leaq 0x98ef(%rip), %rdi      # 0x1bbe0
leaq 0x3435(%rip), %rsi      # 0x1572d
movl $0x6, %edx
callq 0x2220
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0x98cd(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %rbx
leaq 0x3431(%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 0x34f5(%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 0x34c8(%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 0x349a(%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 0x3426(%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 0x125ad
leaq 0x9784(%rip), %rdi      # 0x1bbe0
leaq 0x341d(%rip), %rsi      # 0x15880
movl $0x6, %edx
callq 0x2220
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x9762(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %rbx
leaq 0x33e2(%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 0x125ad
addq $0xc0, %rcx
movq %r14, %rdi
movq %rcx, %rsi
callq 0xabec
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x2100
leaq 0x9707(%rip), %rdi      # 0x1bbe0
leaq 0x324d(%rip), %rsi      # 0x1572d
movl $0x6, %edx
callq 0x2220
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0x96e5(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %r15
leaq 0x3249(%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 0x12010
addq $0xc0, %rcx
movq %r14, %rdi
movq %rcx, %rsi
callq 0xabec
leaq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x2100
jmp 0x121bd
leaq 0x9685(%rip), %rdi      # 0x1bbe0
leaq 0x3325(%rip), %rsi      # 0x15887
movl $0x6, %edx
callq 0x2220
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x9663(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %rbx
leaq 0x32e3(%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 0x125d7
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x125ee
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 0x12612
addl $-0x2, %r15d
jmp 0x12756
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 0xafe5
leaq 0x319f(%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 0x12689
movq %rdx, 0x68(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x78(%rsp)
jmp 0x12691
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 0x126d4
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
leaq 0x18(%rsp), %r12
je 0x126f5
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21c0
leaq 0x94e4(%rip), %rdi      # 0x1bbe0
leaq 0x302a(%rip), %rsi      # 0x1572d
movl $0x6, %edx
callq 0x2220
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0x94c2(%rip), %rdi      # 0x1bbe0
callq 0x2220
movq %rax, %r15
leaq 0x3026(%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 0x12010
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 0xafe5
leaq 0x305b(%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 0x127cc
movq %rdx, 0x68(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x78(%rsp)
jmp 0x127d3
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 0x12816
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x48(%rsp), %rdi
cmpq %r13, %rdi
je 0x1282d
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21c0
leaq 0x38(%rsp), %r13
jmp 0x121bd
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r13, %rdi
jne 0x1285a
jmp 0x1288d
jmp 0x1288a
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x1288d
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21c0
jmp 0x1288d
jmp 0x1288a
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
jmp 0x12883
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0xab5a
jmp 0x1288d
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x128a9
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x128c5
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x2310