// File: johan-bjareholt[P]lua-compiler/src/ir.cc
// Function: convertFuncDef(Statement*, BBlock*)
void convertFuncDef(Statement* in, BBlock *current){
	std::string name = in->expressions.at(0)->name;
    //std::cout << "Defining function " << name << std::endl;
	Statement* args = in->children.at(0);
	Statement* body = in->children.at(1);
	BBlock* bodyBlock = new BBlock();
    BBlock* currblock = bodyBlock;
    // TODO: Fix support for multiple arguments
    if (args->expressions.size() > 0){
        Expression* exp1 = args->expressions.at(0);
        ThreeAd ta (name, 'a', exp1->name, exp1->name);
        bodyBlock->instructions.push_back(ta);
    }
	convertStatement(body, &currblock);

    funcdefs.insert(std::make_pair(name, bodyBlock));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movq %rsi, -0x178(%rbp)
callq 0xaaf0
movq (%rax), %rsi
addq $0x30, %rsi
leaq -0x30(%rbp), %rdi
callq 0x3100
movq -0x178(%rbp), %rsi
movq -0x8(%rbp), %rdi
addq $0x18, %rdi
callq 0xab70
movq %rax, -0x170(%rbp)
jmp 0x9044
movq -0x170(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
addq $0x18, %rdi
movl $0x1, %esi
callq 0xab70
movq %rax, -0x180(%rbp)
jmp 0x906d
movq -0x180(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movl $0x48, %edi
callq 0x3270
movq %rax, -0x188(%rbp)
jmp 0x908e
movq -0x188(%rbp), %rdi
callq 0x66a0
jmp 0x909c
movq -0x188(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rdi
callq 0xabb0
cmpq $0x0, %rax
jbe 0x922f
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xaaf0
movq %rax, -0x190(%rbp)
jmp 0x90d8
movq -0x190(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x68(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x3100
jmp 0x90f8
movq -0x68(%rbp), %rsi
addq $0x30, %rsi
leaq -0x110(%rbp), %rdi
callq 0x3100
jmp 0x910e
movq -0x68(%rbp), %rsi
addq $0x30, %rsi
leaq -0x130(%rbp), %rdi
callq 0x3100
jmp 0x9124
leaq -0xd0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
movl $0x61, %edx
leaq -0x110(%rbp), %rcx
leaq -0x130(%rbp), %r8
callq 0x6550
jmp 0x914c
leaq -0x130(%rbp), %rdi
callq 0x3528
leaq -0x110(%rbp), %rdi
callq 0x3528
leaq -0xf0(%rbp), %rdi
callq 0x3528
movq -0x58(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0xaa40
jmp 0x9182
leaq -0xd0(%rbp), %rdi
callq 0x9db0
jmp 0x922f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x92c6
movq -0x188(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x3230
jmp 0x92c6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x9201
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x91f5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x3528
leaq -0x110(%rbp), %rdi
callq 0x3528
leaq -0xf0(%rbp), %rdi
callq 0x3528
jmp 0x92c6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x9db0
jmp 0x92c6
movq -0x50(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x8f20
jmp 0x923e
leaq -0x158(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0xac10
jmp 0x9254
leaq 0x35105(%rip), %rdi     # 0x3e360
leaq -0x158(%rbp), %rsi
callq 0xabd0
movb %dl, -0x199(%rbp)
movq %rax, -0x198(%rbp)
jmp 0x9276
movb -0x199(%rbp), %al
movq -0x198(%rbp), %rcx
movq %rcx, -0x168(%rbp)
movb %al, -0x160(%rbp)
leaq -0x158(%rbp), %rdi
callq 0xac50
leaq -0x30(%rbp), %rdi
callq 0x3528
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x158(%rbp), %rdi
callq 0xac50
leaq -0x30(%rbp), %rdi
callq 0x3528
movq -0x40(%rbp), %rdi
callq 0x34b0
nopl (%rax,%rax)