// File: johan-bjareholt[P]lua-compiler/src/ir.cc
// Function: convertLoop(Statement*, BBlock**)
void convertLoop(Statement *in, BBlock **current){
    Statement* pre = in->children.at(0);
    convertStatement(pre, current);
    Expression* check = in->expressions.at(0);
    Statement* body = in->children.at(1);
    Statement* post = in->children.at(2);

    BBlock* loopBlock = new BBlock();
    BBlock* bodyBlock = new BBlock();
    BBlock* nextBlock = new BBlock();

    (*current)->trueExit = loopBlock;
    // Set loop block
    (*current) = loopBlock;
    Expression* comparitor = in->expressions.back();
    convertComparitor(comparitor, *current);
    (*current)->falseExit = bodyBlock;
    (*current)->trueExit = nextBlock;

    // Set body block
    (*current) = bodyBlock;
    convertStatement(body, current);
    convertStatement(post, current);
    (*current)->trueExit = loopBlock;

    // Set next block
    (*current) = nextBlock;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %esi
movq %rsi, -0x70(%rbp)
callq 0xab70
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x8f20
movq -0x70(%rbp), %rsi
movq -0x8(%rbp), %rdi
callq 0xaaf0
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
addq $0x18, %rdi
movl $0x1, %esi
callq 0xab70
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
addq $0x18, %rdi
movl $0x2, %esi
callq 0xab70
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movl $0x48, %edi
callq 0x3270
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x68(%rbp)
callq 0x66a0
jmp 0x956d
movq -0x68(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x48, %edi
callq 0x3270
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x78(%rbp)
callq 0x66a0
jmp 0x9590
movq -0x78(%rbp), %rax
movq %rax, -0x50(%rbp)
movl $0x48, %edi
callq 0x3270
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x80(%rbp)
callq 0x66a0
jmp 0x95b3
movq -0x80(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x18(%rax)
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
callq 0xab30
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x8bc0
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x20(%rax)
movq -0x58(%rbp), %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x18(%rax)
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x8f20
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x8f20
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x18(%rax)
movq -0x58(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
addq $0x80, %rsp
popq %rbp
retq
movq -0x68(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x3230
jmp 0x969e
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x3230
jmp 0x969e
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x3230
movq -0x40(%rbp), %rdi
callq 0x34b0
nopw (%rax,%rax)