// File: johan-bjareholt[P]lua-compiler/src/ir.cc
// Function: For(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Expression*, Expression*, Expression*, Statement*)
Statement *For(std::string varname, Expression* varval, Expression* boundry, Expression* step, Statement* body){
    // Get statement pointer as address
    std::ostringstream address;
    address << "_l";
    address << (void const *)body;
    std:string labelname = address.str();

    // Loop start (assign)
    Statement *pre = new Statement('S');
	pre->children.push_back(Assign(varname, varval));
	// Loop body (if,code,increment,if etc.)
    char checktoken = '=';
    if (step->value > 0)
        checktoken = '<';
    else if (step->value < 0)
        checktoken = '>';
    else {
        std::cout << "Step value in for loop cannot be 0" << std::endl;
        exit(0);
    }
    Expression* check = BinOp(checktoken,Variable(varname),boundry);
    // Increment
	Statement *post = Assign(varname, BinOp('+',Variable(varname),step));
    // Return
	return Loop(pre, check, body, post);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2e0, %rsp            # imm = 0x2E0
movq %rdi, -0x288(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
leaq -0x1a0(%rbp), %rdi
movq %rdi, -0x280(%rbp)
callq 0x3370
movq -0x280(%rbp), %rdi
leaq 0x25f42(%rip), %rsi     # 0x2d9b9
callq 0x3260
jmp 0x7a7e
movq -0x28(%rbp), %rsi
leaq -0x1a0(%rbp), %rdi
callq 0x3200
jmp 0x7a90
jmp 0x7a92
leaq -0x1d0(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
callq 0x3280
jmp 0x7aa7
movl $0x38, %edi
callq 0x3270
movq %rax, -0x290(%rbp)
jmp 0x7aba
movq -0x290(%rbp), %rdi
movl $0x53, %esi
callq 0x75a0
jmp 0x7acd
movq -0x288(%rbp), %rsi
movq -0x290(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movq -0x1d8(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x298(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x3100
jmp 0x7b02
movq -0x10(%rbp), %rsi
leaq -0x200(%rbp), %rdi
callq 0x7740
movq %rax, -0x2a0(%rbp)
jmp 0x7b1b
movq -0x298(%rbp), %rdi
movq -0x2a0(%rbp), %rax
movq %rax, -0x1e0(%rbp)
leaq -0x1e0(%rbp), %rsi
callq 0xa4f0
jmp 0x7b3e
leaq -0x200(%rbp), %rdi
callq 0x3528
movb $0x3d, -0x201(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
jle 0x7bdf
movb $0x3c, -0x201(%rbp)
jmp 0x7c2c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
jmp 0x7df6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
jmp 0x7dea
movq -0x290(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
callq 0x3230
jmp 0x7dea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x3528
jmp 0x7dea
movq -0x20(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
jge 0x7bf2
movb $0x3e, -0x201(%rbp)
jmp 0x7c2a
movq 0x363cf(%rip), %rdi     # 0x3dfc8
leaq 0x2364d(%rip), %rsi     # 0x2b24d
callq 0x3260
movq %rax, -0x2a8(%rbp)
jmp 0x7c0e
movq -0x2a8(%rbp), %rdi
movq 0x36384(%rip), %rsi     # 0x3dfa0
callq 0x32a0
jmp 0x7c23
xorl %edi, %edi
callq 0x3330
jmp 0x7c2c
movq -0x288(%rbp), %rsi
movb -0x201(%rbp), %al
movb %al, -0x2a9(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x3100
jmp 0x7c4d
leaq -0x230(%rbp), %rdi
callq 0x7320
movq %rax, -0x2b8(%rbp)
jmp 0x7c62
movq -0x2b8(%rbp), %rsi
movb -0x2a9(%rbp), %al
movq -0x18(%rbp), %rdx
movsbl %al, %edi
callq 0x7220
movq %rax, -0x2c0(%rbp)
jmp 0x7c84
leaq -0x230(%rbp), %rdi
callq 0x3528
movq -0x2c0(%rbp), %rax
movq -0x288(%rbp), %rsi
movq %rax, -0x210(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x3100
jmp 0x7cb3
movq -0x288(%rbp), %rsi
leaq -0x278(%rbp), %rdi
callq 0x3100
jmp 0x7cc8
leaq -0x278(%rbp), %rdi
callq 0x7320
movq %rax, -0x2c8(%rbp)
jmp 0x7cdd
movq -0x2c8(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl $0x2b, %edi
callq 0x7220
movq %rax, -0x2d0(%rbp)
jmp 0x7cfb
movq -0x2d0(%rbp), %rsi
leaq -0x258(%rbp), %rdi
callq 0x7740
movq %rax, -0x2d8(%rbp)
jmp 0x7d17
leaq -0x278(%rbp), %rdi
callq 0x3528
leaq -0x258(%rbp), %rdi
callq 0x3528
movq -0x2d8(%rbp), %rax
movq %rax, -0x238(%rbp)
movq -0x1d8(%rbp), %rdi
movq -0x210(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x238(%rbp), %rcx
callq 0x7980
movq %rax, -0x2e0(%rbp)
jmp 0x7d64
leaq -0x1d0(%rbp), %rdi
callq 0x3528
leaq -0x1a0(%rbp), %rdi
callq 0x3090
movq -0x2e0(%rbp), %rax
addq $0x2e0, %rsp            # imm = 0x2E0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x3528
jmp 0x7dea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
jmp 0x7dde
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
leaq -0x278(%rbp), %rdi
callq 0x3528
leaq -0x258(%rbp), %rdi
callq 0x3528
leaq -0x1d0(%rbp), %rdi
callq 0x3528
leaq -0x1a0(%rbp), %rdi
callq 0x3090
movq -0x1a8(%rbp), %rdi
callq 0x34b0
nop