// File: johan-bjareholt[P]lua-compiler/src/convert.cc
// Function: outFunctionBlock(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, BBlock&, std::ostream&, std::set<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::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>&, std::set<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::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>&)
void outFunctionBlock(std::string funcname, BBlock& block, std::ostream& ss, std::set<std::string>& outputSymbols, std::set<std::string>& inputSymbols){
    //std::cout << "Translating function " << funcname << std::endl;
    addVar(funcname, VT_FUNCTION, "");
    
    varoffset = 0;
    localVars.clear();
    
    for(auto iter = block.instructions.begin(); iter->op=='a' && iter!=block.instructions.end(); iter++){
        ThreeAd& arg = *iter;
        localVars.insert(std::make_pair(arg.lhs, varoffset));
        varoffset+=8;
    }
    std::stringstream bodyss;
    std::string retlabel = outBlock(block, bodyss, outputSymbols, inputSymbols, false, true);
    
    ss << std::endl;
    ss << pre_in << funcname << ":" << post_in;
    
    // Push bp
    ss << pre_in << "pushq %%rbp" << post_in;
    // Set new bp
    ss << pre_in << "movq %%rsp, %%rbp" << post_in;
    // Allocate local var space
    ss << pre_in << "subq $" << varoffset << ", %%rsp" << post_in;

    // Insert body
    ss << bodyss.str();
    
    varoffset = 0;
    localVars.clear();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x340, %rsp            # imm = 0x340
movq %rsi, %rax
movq %rdi, %rsi
movq %rsi, -0x2b0(%rbp)
movq %rsi, -0x8(%rbp)
movq %rax, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x3100
leaq -0x69(%rbp), %rdi
movq %rdi, -0x2a8(%rbp)
callq 0x34c0
movq -0x2a8(%rbp), %rdx
leaq 0x180e7(%rip), %rsi     # 0x2b51a
leaq -0x68(%rbp), %rdi
callq 0x3390
jmp 0x1343e
leaq -0x48(%rbp), %rdi
movl $0x3, %esi
leaq -0x68(%rbp), %rdx
callq 0x11910
jmp 0x13452
leaq -0x68(%rbp), %rdi
callq 0x3528
leaq -0x69(%rbp), %rdi
callq 0x32c0
leaq -0x48(%rbp), %rdi
callq 0x3528
movl $0x0, 0x2afcd(%rip)     # 0x3e444
leaq 0x2afca(%rip), %rdi     # 0x3e448
callq 0x15bb0
movq -0x10(%rbp), %rdi
callq 0x9c40
movq %rax, -0x88(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x15bd0
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x61, %ecx
movb %al, -0x2b1(%rbp)
jne 0x134d8
movq -0x10(%rbp), %rdi
callq 0x9c70
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x9ca0
movb %al, -0x2b1(%rbp)
movb -0x2b1(%rbp), %al
testb $0x1, %al
jne 0x134e7
jmp 0x135e7
leaq -0x88(%rbp), %rdi
callq 0x9cd0
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rsi
addq $0x28, %rsi
leaq 0x2af38(%rip), %rdx     # 0x3e444
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x2d0(%rbp)
callq 0x15c30
movq -0x2d0(%rbp), %rsi
leaq 0x2af1b(%rip), %rdi     # 0x3e448
callq 0x15bf0
movb %dl, -0x2c1(%rbp)
movq %rax, -0x2c0(%rbp)
jmp 0x13541
movb -0x2c1(%rbp), %al
movq -0x2c0(%rbp), %rcx
movq %rcx, -0xd0(%rbp)
movb %al, -0xc8(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x15c70
movl 0x2aed7(%rip), %eax     # 0x3e444
addl $0x8, %eax
movl %eax, 0x2aece(%rip)     # 0x3e444
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
callq 0x15c90
movq %rax, -0xd8(%rbp)
jmp 0x13493
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x135b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x3528
leaq -0x69(%rbp), %rdi
callq 0x32c0
leaq -0x48(%rbp), %rdi
callq 0x3528
jmp 0x13871
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x15c70
jmp 0x13871
leaq -0x260(%rbp), %rdi
callq 0x31d0
movq -0x10(%rbp), %rsi
leaq -0x250(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movq %rsp, %rax
movl $0x1, (%rax)
leaq -0x280(%rbp), %rdi
xorl %r9d, %r9d
callq 0x12b90
jmp 0x13620
movq -0x18(%rbp), %rdi
movq 0x2a975(%rip), %rsi     # 0x3dfa0
callq 0x32a0
jmp 0x13632
movq -0x18(%rbp), %rdi
leaq 0x2ad93(%rip), %rsi     # 0x3e3d0
callq 0x3220
movq %rax, -0x2d8(%rbp)
jmp 0x1364b
movq -0x2b0(%rbp), %rsi
movq -0x2d8(%rbp), %rdi
callq 0x3220
movq %rax, -0x2e0(%rbp)
jmp 0x13667
movq -0x2e0(%rbp), %rdi
leaq 0x1804d(%rip), %rsi     # 0x2b6c2
callq 0x3260
movq %rax, -0x2e8(%rbp)
jmp 0x13683
movq -0x2e8(%rbp), %rdi
leaq 0x2ad5f(%rip), %rsi     # 0x3e3f0
callq 0x3220
jmp 0x13698
movq -0x18(%rbp), %rdi
leaq 0x2ad2d(%rip), %rsi     # 0x3e3d0
callq 0x3220
movq %rax, -0x2f0(%rbp)
jmp 0x136b1
movq -0x2f0(%rbp), %rdi
leaq 0x18167(%rip), %rsi     # 0x2b826
callq 0x3260
movq %rax, -0x2f8(%rbp)
jmp 0x136cd
movq -0x2f8(%rbp), %rdi
leaq 0x2ad15(%rip), %rsi     # 0x3e3f0
callq 0x3220
jmp 0x136e2
movq -0x18(%rbp), %rdi
leaq 0x2ace3(%rip), %rsi     # 0x3e3d0
callq 0x3220
movq %rax, -0x300(%rbp)
jmp 0x136fb
movq -0x300(%rbp), %rdi
leaq 0x18129(%rip), %rsi     # 0x2b832
callq 0x3260
movq %rax, -0x308(%rbp)
jmp 0x13717
movq -0x308(%rbp), %rdi
leaq 0x2accb(%rip), %rsi     # 0x3e3f0
callq 0x3220
jmp 0x1372c
movq -0x18(%rbp), %rdi
leaq 0x2ac99(%rip), %rsi     # 0x3e3d0
callq 0x3220
movq %rax, -0x310(%rbp)
jmp 0x13745
movq -0x310(%rbp), %rdi
leaq 0x180f1(%rip), %rsi     # 0x2b844
callq 0x3260
movq %rax, -0x318(%rbp)
jmp 0x13761
movq -0x318(%rbp), %rdi
movl 0x2acd6(%rip), %esi     # 0x3e444
callq 0x3490
movq %rax, -0x320(%rbp)
jmp 0x1377c
movq -0x320(%rbp), %rdi
leaq 0x18085(%rip), %rsi     # 0x2b80f
callq 0x3260
movq %rax, -0x328(%rbp)
jmp 0x13798
movq -0x328(%rbp), %rdi
leaq 0x2ac4a(%rip), %rsi     # 0x3e3f0
callq 0x3220
jmp 0x137ad
movq -0x18(%rbp), %rax
movq %rax, -0x330(%rbp)
leaq -0x2a0(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x3250
jmp 0x137cd
movq -0x330(%rbp), %rdi
leaq -0x2a0(%rbp), %rsi
callq 0x3220
jmp 0x137e2
leaq -0x2a0(%rbp), %rdi
callq 0x3528
movl $0x0, 0x2ac4c(%rip)     # 0x3e444
leaq 0x2ac49(%rip), %rdi     # 0x3e448
callq 0x15bb0
leaq -0x280(%rbp), %rdi
callq 0x3528
leaq -0x260(%rbp), %rdi
callq 0x31e0
addq $0x340, %rsp            # imm = 0x340
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x13865
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x13859
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x2a0(%rbp), %rdi
callq 0x3528
leaq -0x280(%rbp), %rdi
callq 0x3528
leaq -0x260(%rbp), %rdi
callq 0x31e0
movq -0x78(%rbp), %rdi
callq 0x34b0
nopw (%rax,%rax)