// File: johan-bjareholt[P]lua-compiler/src/ir.cc
// Function: emitPass(Expression*, std::map<Expression*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::less<Expression*>, std::allocator<std::pair<Expression* const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>>&, BBlock*)
void emitPass(Expression *tree, map<Expression*,string> &nameMap, BBlock *out)
{
    if (tree->kind == 'N'){
        emitPass(tree->left, nameMap, out);
        emitPass(tree->right, nameMap, out);
	    ThreeAd* ta = new ThreeAd(
            nameMap[tree],
	        tree->op, nameMap[tree->left],
	        nameMap[tree->right]
        );
        out->instructions.push_back(*ta);
    }
    else if (tree->kind == 'f' && tree->op == 'f'){
        for (Expression* exp : tree->subexp){
            emitPass(exp, nameMap, out);
        }
	    ThreeAd* ta = new ThreeAd(
            nameMap[tree],
	        tree->op, 
            tree->name, nameMap[tree->subexp.front()]
        );
        out->instructions.push_back(*ta);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movsbl 0x28(%rax), %eax
cmpl $0x4e, %eax
jne 0x86f9
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x8530
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x8530
movl $0x68, %edi
callq 0x3270
movq %rax, -0x138(%rbp)
movb $0x1, -0x91(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x8(%rbp), %rsi
callq 0xa960
movq %rax, -0x130(%rbp)
jmp 0x85af
movq -0x130(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x3100
jmp 0x85c1
movq -0x8(%rbp), %rsi
movb 0x29(%rsi), %al
movb %al, -0x141(%rbp)
movq -0x10(%rbp), %rdi
callq 0xa960
movq %rax, -0x140(%rbp)
jmp 0x85e0
movq -0x140(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x3100
jmp 0x85f2
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x8, %rsi
callq 0xa960
movq %rax, -0x150(%rbp)
jmp 0x860c
movq -0x150(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x3100
jmp 0x8621
movq -0x138(%rbp), %rdi
movb -0x141(%rbp), %al
movsbl %al, %edx
leaq -0x40(%rbp), %rsi
leaq -0x70(%rbp), %rcx
leaq -0x90(%rbp), %r8
callq 0x6550
jmp 0x8647
movb $0x0, -0x91(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x3528
leaq -0x70(%rbp), %rdi
callq 0x3528
leaq -0x40(%rbp), %rdi
callq 0x3528
movq -0x138(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xaa40
jmp 0x8934
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x86dd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x86d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x86cb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x3528
leaq -0x70(%rbp), %rdi
callq 0x3528
leaq -0x40(%rbp), %rdi
callq 0x3528
testb $0x1, -0x91(%rbp)
jne 0x86e8
jmp 0x86f4
movq -0x138(%rbp), %rdi
callq 0x3230
jmp 0x893d
movq -0x8(%rbp), %rax
movsbl 0x28(%rax), %eax
cmpl $0x66, %eax
jne 0x8932
movq -0x8(%rbp), %rax
movsbl 0x29(%rax), %eax
cmpl $0x66, %eax
jne 0x8932
movq -0x8(%rbp), %rax
addq $0x10, %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0xa040
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
callq 0xa070
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0xa0a0
testb $0x1, %al
jne 0x8769
jmp 0x87a1
leaq -0xa8(%rbp), %rdi
callq 0xa0d0
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x8530
leaq -0xa8(%rbp), %rdi
callq 0xa100
jmp 0x8750
movl $0x68, %edi
callq 0x3270
movq %rax, -0x160(%rbp)
movb $0x1, -0x121(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x8(%rbp), %rsi
callq 0xa960
movq %rax, -0x158(%rbp)
jmp 0x87cf
movq -0x158(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
callq 0x3100
jmp 0x87e4
movq -0x8(%rbp), %rsi
movb 0x29(%rsi), %al
movb %al, -0x161(%rbp)
addq $0x30, %rsi
leaq -0x100(%rbp), %rdi
callq 0x3100
jmp 0x8803
movq -0x10(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
callq 0xaa80
movq -0x178(%rbp), %rdi
movq %rax, %rsi
callq 0xa960
movq %rax, -0x170(%rbp)
jmp 0x8833
movq -0x170(%rbp), %rsi
leaq -0x120(%rbp), %rdi
callq 0x3100
jmp 0x8848
movq -0x160(%rbp), %rdi
movb -0x161(%rbp), %al
movsbl %al, %edx
leaq -0xe0(%rbp), %rsi
leaq -0x100(%rbp), %rcx
leaq -0x120(%rbp), %r8
callq 0x6550
jmp 0x8874
movb $0x0, -0x121(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x3528
leaq -0x100(%rbp), %rdi
callq 0x3528
leaq -0xe0(%rbp), %rdi
callq 0x3528
movq -0x160(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0xaa40
jmp 0x8932
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x8919
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x890d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x8901
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x3528
leaq -0x100(%rbp), %rdi
callq 0x3528
leaq -0xe0(%rbp), %rdi
callq 0x3528
testb $0x1, -0x121(%rbp)
jne 0x8924
jmp 0x8930
movq -0x160(%rbp), %rdi
callq 0x3230
jmp 0x893d
jmp 0x8934
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x34b0
nopw %cs:(%rax,%rax)