// File: JameyWoo[P]csmile/assembly.cpp
// Function: genIfElse(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 genIfElse(TreeNode* ifelse, map<string, string> var2stack) {
    // debug("ifelse->child[0]: " + ifelse->child[0]->nodekind);
    // debug("ifelse->child[0]: " + ifelse->child[1]->nodekind);
    // debug("ifelse->child[0]: " + ifelse->child[2]->nodekind);
    // ifelse->child[0] 是条件判断
    // ifelse->child[1] 是 if 的条件成立之后的的stmt
    // ifelse->child[2] 是 else 之后的stmt
    if (ifelse->child[0]->op == "==") {
        // debug(" == ");
        TreeNode* left = ifelse->child[0]->child[0];
        solveCompare(left, "%eax", var2stack);
        // 左边的结果放到 %eax, 右边的结果放到 %edx
        TreeNode* right = ifelse->child[0]->child[1];
        solveCompare(right, "%edx", var2stack);
        assout << "\tcmpl\t%edx, %eax" << endl;

        string L1 = ".L" + to_string(getId());
        string L2 = ".L" + to_string(getId());
        assout << "\tjne\t" << L1 << endl;
        genStmt(ifelse->child[1], var2stack);
        assout << "\tjmp\t" << L2 << endl;
        assout << L1 << ":" << endl;
        // debug("else: " + ifelse->child[2]->nodekind);
        genStmt(ifelse->child[2], var2stack);
        assout << L2 << ":" << endl;
    }
    // TODO: 其他的比较
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movq %rsi, -0x1c8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
addq $0x98, %rdi
leaq 0x83a8(%rip), %rsi      # 0x21727
callq 0x5e70
testb $0x1, %al
jne 0x1938d
jmp 0x1986a
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x1d8(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x1d0(%rbp)
callq 0x3480
movq -0x1d0(%rbp), %rdx
leaq 0x82fa(%rip), %rsi      # 0x216be
leaq -0x38(%rbp), %rdi
callq 0x4ef0
jmp 0x193cf
movq -0x1c8(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0x1c040
jmp 0x193e1
movq -0x1d8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x80(%rbp), %rdx
callq 0x1a720
jmp 0x193f7
leaq -0x80(%rbp), %rdi
callq 0xee40
leaq -0x38(%rbp), %rdi
callq 0x34e8
leaq -0x39(%rbp), %rdi
callq 0x32d0
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x1e8(%rbp)
leaq -0xa9(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x3480
movq -0x1e0(%rbp), %rdx
leaq 0x8387(%rip), %rsi      # 0x217da
leaq -0xa8(%rbp), %rdi
callq 0x4ef0
jmp 0x19461
movq -0x1c8(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
callq 0x1c040
jmp 0x19476
movq -0x1e8(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
callq 0x1a720
jmp 0x19492
leaq -0xe0(%rbp), %rdi
callq 0xee40
leaq -0xa8(%rbp), %rdi
callq 0x34e8
leaq -0xa9(%rbp), %rdi
callq 0x32d0
leaq 0x157eb(%rip), %rdi     # 0x2eca8
leaq 0x8266(%rip), %rsi      # 0x2172a
callq 0x3270
movq %rax, %rdi
movq 0x14add(%rip), %rsi     # 0x2dfb0
callq 0x32b0
callq 0x177b0
movl %eax, %esi
leaq -0x120(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0xf460
movq -0x1f0(%rbp), %rdx
leaq 0x823b(%rip), %rsi      # 0x2173b
leaq -0x100(%rbp), %rdi
callq 0xf410
jmp 0x1950e
leaq -0x120(%rbp), %rdi
callq 0x34e8
callq 0x177b0
movl %eax, %esi
leaq -0x160(%rbp), %rdi
callq 0xf460
jmp 0x1952f
leaq 0x8205(%rip), %rsi      # 0x2173b
leaq -0x140(%rbp), %rdi
leaq -0x160(%rbp), %rdx
callq 0xf410
jmp 0x1954b
leaq -0x160(%rbp), %rdi
callq 0x34e8
leaq 0x1574a(%rip), %rdi     # 0x2eca8
leaq 0x81d9(%rip), %rsi      # 0x2173e
callq 0x3270
movq %rax, -0x1f8(%rbp)
jmp 0x19573
movq -0x1f8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x3240
movq %rax, -0x200(%rbp)
jmp 0x1958f
movq -0x200(%rbp), %rdi
movq 0x14a13(%rip), %rsi     # 0x2dfb0
callq 0x32b0
jmp 0x195a4
movq -0x1c8(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x208(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x1c040
jmp 0x195c8
movq -0x208(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x18940
jmp 0x195dd
leaq -0x190(%rbp), %rdi
callq 0xee40
leaq 0x156b8(%rip), %rdi     # 0x2eca8
leaq 0x814d(%rip), %rsi      # 0x21744
callq 0x3270
movq %rax, -0x210(%rbp)
jmp 0x19605
movq -0x210(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x3240
movq %rax, -0x218(%rbp)
jmp 0x19621
movq -0x218(%rbp), %rdi
movq 0x14981(%rip), %rsi     # 0x2dfb0
callq 0x32b0
jmp 0x19636
leaq 0x1566b(%rip), %rdi     # 0x2eca8
leaq -0x100(%rbp), %rsi
callq 0x3240
movq %rax, -0x220(%rbp)
jmp 0x19652
movq -0x220(%rbp), %rdi
leaq 0x8238(%rip), %rsi      # 0x21898
callq 0x3270
movq %rax, -0x228(%rbp)
jmp 0x1966e
movq -0x228(%rbp), %rdi
movq 0x14934(%rip), %rsi     # 0x2dfb0
callq 0x32b0
jmp 0x19683
movq -0x1c8(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x230(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x1c040
jmp 0x196a7
movq -0x230(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x18940
jmp 0x196bc
leaq -0x1c0(%rbp), %rdi
callq 0xee40
leaq 0x155d9(%rip), %rdi     # 0x2eca8
leaq -0x140(%rbp), %rsi
callq 0x3240
movq %rax, -0x238(%rbp)
jmp 0x196e4
movq -0x238(%rbp), %rdi
leaq 0x81a6(%rip), %rsi      # 0x21898
callq 0x3270
movq %rax, -0x240(%rbp)
jmp 0x19700
movq -0x240(%rbp), %rdi
movq 0x148a2(%rip), %rsi     # 0x2dfb0
callq 0x32b0
jmp 0x19715
leaq -0x140(%rbp), %rdi
callq 0x34e8
leaq -0x100(%rbp), %rdi
callq 0x34e8
jmp 0x1986a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x1976c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x19763
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xee40
leaq -0x38(%rbp), %rdi
callq 0x34e8
leaq -0x39(%rbp), %rdi
callq 0x32d0
jmp 0x19873
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x197ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x197ae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0xee40
leaq -0xa8(%rbp), %rdi
callq 0x34e8
leaq -0xa9(%rbp), %rdi
callq 0x32d0
jmp 0x19873
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x34e8
jmp 0x19873
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x1985c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x34e8
jmp 0x1985c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x19850
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x190(%rbp), %rdi
callq 0xee40
jmp 0x19850
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0xee40
leaq -0x140(%rbp), %rdi
callq 0x34e8
leaq -0x100(%rbp), %rdi
callq 0x34e8
jmp 0x19873
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x3470
nopl (%rax)