// File: /workspace/github_cpp_makefile_star2_O2/JameyWoo[P]csmile/assembly.cpp
// Function: genAssign(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>>>>>, bool)
void genAssign(TreeNode* assign, map<string, string> var2stack, bool need) {
    // debug("assign " + assign->child[0]->name);
    string left_loc = var2stack[assign->child[0]->name];
    if (assign->child[1]->nodekind == "Const") {
        assout << "\tmovl\t$"
               << to_string(assign->child[1]->val)
               << ", " << left_loc << endl;
    } else if (assign->child[1]->nodekind == "Id") {
        // debug("assign->child[1]->name: " + assign->child[1]->name);
        string right_loc = var2stack[assign->child[1]->name];
        // 先将该值赋值给某个寄存器, 然后再将寄存器赋值给左值
        assout << "\tmovl\t" << right_loc << ", %edx" << endl
               << "\tmovl\t%edx, " << left_loc << endl;
    } else if (assign->child[1]->nodekind == "Assign") {
        genAssign(assign->child[1], var2stack, true);
        assout << "\tmovl\t%edx, " << left_loc << endl;
    } else if (assign->child[1]->nodekind == "PMOp" || assign->child[1]->nodekind == "MDOp") {
        // debug("assign->child[1]->nodekind: " + assign->child[1]->nodekind);
        // 计算(加减乘除)
        // debug("assign->child[1]->nodekind: " + assign->child[1]->nodekind);
        genCalc(assign->child[1], var2stack);
        assout << "\tmovl\t%edx, " << left_loc << endl;
    } else if (assign->child[1]->nodekind == "Input") {
        genInput(assign->child[1], left_loc);  // 第二个参数是赋值的地址
    } else if (assign->child[1]->nodekind == "Call") {
        // call 有一种是在赋值中的, 有一种是普通的没有被赋值的调用
        genCall(assign->child[1], var2stack);
        // 返回值
        assout << "\tmovl\t%eax, " << left_loc << endl;
    }
    if (need) {
        assout << "\tmovl\t" << left_loc << ", %edx" << endl;
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
movl $0xc0, %esi
addq (%rdi), %rsi
movq %r14, %rdi
callq 0x7c16
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x2250
movq 0x8(%r15), %rdi
addq $0x30, %rdi
leaq 0x401e(%rip), %rsi      # 0xf265
callq 0x368e
testb %al, %al
je 0xb2b0
leaq 0xb989(%rip), %rdi      # 0x16be0
leaq 0x4541(%rip), %rsi      # 0xf79f
callq 0x21b0
movq %rax, %r14
movq 0x8(%r15), %rax
movl 0xb8(%rax), %esi
leaq 0x8(%rsp), %rdi
callq 0x7cbf
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x21a0
leaq 0x4589(%rip), %rsi      # 0xf817
movq %rax, %rdi
callq 0x21b0
leaq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x21a0
movq %rax, %rdi
callq 0x20f0
jmp 0xb345
movq 0x8(%r15), %rdi
addq $0x30, %rdi
leaq 0x3f69(%rip), %rsi      # 0xf228
callq 0x368e
testb %al, %al
je 0xb354
movl $0xc0, %esi
addq 0x8(%r15), %rsi
movq %r14, %rdi
callq 0x7c16
leaq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x2250
leaq 0xb8ef(%rip), %rdi      # 0x16be0
leaq 0x441e(%rip), %rsi      # 0xf716
callq 0x21b0
leaq 0x8(%rsp), %rsi
movq %rax, %rdi
callq 0x21a0
leaq 0x4547(%rip), %rsi      # 0xf858
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
callq 0x20f0
leaq 0x44d8(%rip), %rsi      # 0xf800
movq %rax, %rdi
callq 0x21b0
leaq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x21a0
movq %rax, %rdi
callq 0x20f0
leaq 0x8(%rsp), %rdi
callq 0x2358
jmp 0xb41b
movq 0x8(%r15), %rdi
addq $0x30, %rdi
leaq 0x3f47(%rip), %rsi      # 0xf2aa
callq 0x368e
movq 0x8(%r15), %r12
testb %al, %al
je 0xb393
leaq 0xc8(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0xd3ce
pushq $0x1
popq %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0xb205
jmp 0xb3e8
addq $0x30, %r12
leaq 0x3ed2(%rip), %rsi      # 0xf270
movq %r12, %rdi
callq 0x368e
testb %al, %al
jne 0xb3c6
movq 0x8(%r15), %rdi
addq $0x30, %rdi
leaq 0x3eb2(%rip), %rsi      # 0xf26b
callq 0x368e
testb %al, %al
je 0xb46f
movq 0x8(%r15), %r12
leaq 0x98(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0xd3ce
movq %r12, %rdi
movq %r15, %rsi
callq 0xc22d
leaq 0x4411(%rip), %r14      # 0xf800
movq %r15, %rdi
callq 0x7b9c
leaq 0xb7e2(%rip), %rdi      # 0x16be0
movq %r14, %rsi
callq 0x21b0
leaq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x21a0
movq %rax, %rdi
callq 0x20f0
testb %bl, %bl
je 0xb456
leaq 0xb7ba(%rip), %rdi      # 0x16be0
leaq 0x42e9(%rip), %rsi      # 0xf716
callq 0x21b0
leaq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x21a0
leaq 0x4412(%rip), %rsi      # 0xf858
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
callq 0x20f0
leaq 0x28(%rsp), %rdi
callq 0x2358
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x8(%r15), %rdi
addq $0x30, %rdi
leaq 0x3df7(%rip), %rsi      # 0xf275
callq 0x368e
testb %al, %al
je 0xb4aa
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x2250
leaq 0x48(%rsp), %rsi
callq 0xaf7c
leaq 0x48(%rsp), %rdi
jmp 0xb34a
movq 0x8(%r15), %rdi
addq $0x30, %rdi
leaq 0x3d9d(%rip), %rsi      # 0xf256
callq 0x368e
testb %al, %al
je 0xb41b
movq 0x8(%r15), %r12
leaq 0x68(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0xd3ce
movq %r12, %rdi
movq %r15, %rsi
callq 0xabcd
leaq 0x4321(%rip), %r14      # 0xf80d
jmp 0xb3ef
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
jmp 0xb51d
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
jmp 0xb532
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
jmp 0xb51d
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x7b9c
jmp 0xb53c
jmp 0xb539
jmp 0xb539
jmp 0xb52a
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x2358
jmp 0xb53c
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x2358
movq %rbx, %rdi
callq 0x2300