// File: Merulo[P]CompilerJFTT/src/IRs/FirstIR.cpp
// Function: FirstIR::insertFor(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Data, Data, bool)
void FirstIR::insertFor(std::string iterator, Data from, Data to, bool isForTo)
{
    closeForBlock();
    Block b1 = _forBlocks.top();
    _forBlocks.pop();
    Block b2 = _forBlocks.top();
    _forBlocks.pop();

    std::string name = generateForName();
    Block firstControlBlock = createBeforeForBlock(iterator, from, to, isForTo, name);
    firstControlBlock.blockIfFalse = b2.blockName;
    firstControlBlock.blockIfTrue = b1.blockName;

    Block secondControlBlock = createSecondControlBlock(iterator, isForTo, name);
    secondControlBlock.blockIfFalse = b2.blockName;
    secondControlBlock.blockIfTrue = b1.blockName;        
    
    for(size_t i = 0; i < _blocks.size(); i++)
    {
        if (_blocks[i] == b2)
        {
            _blocks.insert(_blocks.begin() + i, firstControlBlock);
            break;
        }
    }

    _blocks.push_back(_currentBlock.top());
    _currentBlock.pop();
    Block testing = generateBlock();
    _currentBlock.push(testing);

    for(size_t i = 0; i < _blocks.size(); i++)
    {
        if (_blocks[i] == b1)
        {
            _blocks.insert(_blocks.begin() + i, secondControlBlock);
            return;
        }
    }        

}

// Assembly:
subq $0x538, %rsp            # imm = 0x538
movq %rcx, 0x70(%rsp)
movq %rdx, 0x78(%rsp)
movq %rsi, 0x80(%rsp)
movb %r8b, %al
movq %rdi, 0x530(%rsp)
movq %rsi, 0x528(%rsp)
movq %rdx, 0x520(%rsp)
movq %rcx, 0x518(%rsp)
andb $0x1, %al
movb %al, 0x517(%rsp)
movq 0x530(%rsp), %rdi
movq %rdi, 0x88(%rsp)
callq 0x28360
movq 0x88(%rsp), %rdi
addq $0x168, %rdi            # imm = 0x168
movq %rdi, 0x90(%rsp)
callq 0x297d0
movq %rax, %rsi
leaq 0x470(%rsp), %rdi
callq 0x29ab0
movq 0x90(%rsp), %rdi
callq 0x29900
jmp 0x285d5
movq 0x88(%rsp), %rdi
addq $0x168, %rdi            # imm = 0x168
callq 0x297d0
movq %rax, 0x68(%rsp)
jmp 0x285f0
movq 0x68(%rsp), %rsi
leaq 0x3c0(%rsp), %rdi
callq 0x29ab0
jmp 0x28604
movq 0x88(%rsp), %rdi
addq $0x168, %rdi            # imm = 0x168
callq 0x29900
jmp 0x2861a
movq 0x88(%rsp), %rsi
leaq 0x3a0(%rsp), %rdi
callq 0x3d500
jmp 0x28631
movq 0x80(%rsp), %rsi
leaq 0x2e0(%rsp), %rdi
callq 0x4180
jmp 0x28648
movq 0x78(%rsp), %rsi
leaq 0x2b8(%rsp), %rdi
callq 0x29cd0
jmp 0x2865c
movq 0x70(%rsp), %rsi
leaq 0x290(%rsp), %rdi
callq 0x29cd0
jmp 0x28670
movb 0x517(%rsp), %al
movb %al, 0x67(%rsp)
leaq 0x270(%rsp), %rdi
leaq 0x3a0(%rsp), %rsi
callq 0x4180
jmp 0x28692
movq 0x88(%rsp), %rsi
movb 0x67(%rsp), %al
movq %rsp, %rcx
leaq 0x270(%rsp), %rdx
movq %rdx, (%rcx)
movzbl %al, %r9d
andl $0x1, %r9d
leaq 0x300(%rsp), %rdi
leaq 0x2e0(%rsp), %rdx
leaq 0x2b8(%rsp), %rcx
leaq 0x290(%rsp), %r8
callq 0x28cd0
jmp 0x286db
leaq 0x270(%rsp), %rdi
callq 0x46e8
leaq 0x290(%rsp), %rdi
callq 0x29d10
leaq 0x2b8(%rsp), %rdi
callq 0x29d10
leaq 0x2e0(%rsp), %rdi
callq 0x46e8
leaq 0x358(%rsp), %rdi
leaq 0x3c0(%rsp), %rsi
callq 0x4050
jmp 0x28726
leaq 0x338(%rsp), %rdi
leaq 0x470(%rsp), %rsi
callq 0x4050
jmp 0x2873d
movq 0x80(%rsp), %rsi
leaq 0x1b0(%rsp), %rdi
callq 0x4180
jmp 0x28754
movb 0x517(%rsp), %al
movb %al, 0x66(%rsp)
leaq 0x190(%rsp), %rdi
leaq 0x3a0(%rsp), %rsi
callq 0x4180
jmp 0x28776
movq 0x88(%rsp), %rsi
movb 0x66(%rsp), %al
movzbl %al, %ecx
andl $0x1, %ecx
leaq 0x1d0(%rsp), %rdi
leaq 0x1b0(%rsp), %rdx
leaq 0x190(%rsp), %r8
callq 0x29350
jmp 0x287a7
leaq 0x190(%rsp), %rdi
callq 0x46e8
leaq 0x1b0(%rsp), %rdi
callq 0x46e8
leaq 0x228(%rsp), %rdi
leaq 0x3c0(%rsp), %rsi
callq 0x4050
jmp 0x287d8
leaq 0x208(%rsp), %rdi
leaq 0x470(%rsp), %rsi
callq 0x4050
jmp 0x287ef
movq $0x0, 0x188(%rsp)
movq 0x88(%rsp), %rdi
movq 0x188(%rsp), %rax
movq %rax, 0x58(%rsp)
addq $0x10, %rdi
callq 0x29d30
movq %rax, %rcx
movq 0x58(%rsp), %rax
cmpq %rcx, %rax
jae 0x28a59
movq 0x88(%rsp), %rdi
addq $0x10, %rdi
movq 0x188(%rsp), %rsi
callq 0x29d50
movq %rax, %rdi
leaq 0x3c0(%rsp), %rsi
callq 0x29d70
movb %al, 0x57(%rsp)
jmp 0x28859
movb 0x57(%rsp), %al
testb $0x1, %al
jne 0x28866
jmp 0x28a3e
movq 0x88(%rsp), %rdi
addq $0x10, %rdi
movq %rdi, 0x40(%rsp)
callq 0x29fd0
movq %rax, 0x170(%rsp)
movq 0x188(%rsp), %rsi
leaq 0x170(%rsp), %rdi
callq 0x2a000
movq %rax, 0x178(%rsp)
leaq 0x180(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x2a040
movq 0x40(%rsp), %rdi
movq 0x180(%rsp), %rsi
leaq 0x300(%rsp), %rdx
callq 0x29da0
movq %rax, 0x48(%rsp)
jmp 0x288d7
movq 0x48(%rsp), %rax
movq %rax, 0x168(%rsp)
jmp 0x28a59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x468(%rsp)
movl %eax, 0x464(%rsp)
jmp 0x28ca8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x468(%rsp)
movl %eax, 0x464(%rsp)
jmp 0x28c9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x468(%rsp)
movl %eax, 0x464(%rsp)
jmp 0x28c8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x468(%rsp)
movl %eax, 0x464(%rsp)
jmp 0x289b1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x468(%rsp)
movl %eax, 0x464(%rsp)
jmp 0x289a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x468(%rsp)
movl %eax, 0x464(%rsp)
jmp 0x28997
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x468(%rsp)
movl %eax, 0x464(%rsp)
leaq 0x270(%rsp), %rdi
callq 0x46e8
leaq 0x290(%rsp), %rdi
callq 0x29d10
leaq 0x2b8(%rsp), %rdi
callq 0x29d10
leaq 0x2e0(%rsp), %rdi
callq 0x46e8
jmp 0x28c8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x468(%rsp)
movl %eax, 0x464(%rsp)
jmp 0x28c81
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x468(%rsp)
movl %eax, 0x464(%rsp)
jmp 0x28a13
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x468(%rsp)
movl %eax, 0x464(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x46e8
leaq 0x1b0(%rsp), %rdi
callq 0x46e8
jmp 0x28c81
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x468(%rsp)
movl %eax, 0x464(%rsp)
jmp 0x28c74
jmp 0x28a40
movq 0x188(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x188(%rsp)
jmp 0x287fb
movq 0x88(%rsp), %rdi
movq %rdi, %rax
addq $0x10, %rax
movq %rax, 0x30(%rsp)
addq $0x28, %rdi
callq 0x297d0
movq %rax, 0x38(%rsp)
jmp 0x28a7d
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdi
callq 0x29850
jmp 0x28a8e
movq 0x88(%rsp), %rdi
addq $0x28, %rdi
callq 0x29900
jmp 0x28aa1
movq 0x88(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x3d200
jmp 0x28ab8
movq 0x88(%rsp), %rdi
addq $0x28, %rdi
leaq 0xc8(%rsp), %rsi
callq 0x298d0
jmp 0x28ad3
movq $0x0, 0xc0(%rsp)
movq 0x88(%rsp), %rdi
movq 0xc0(%rsp), %rax
movq %rax, 0x28(%rsp)
addq $0x10, %rdi
callq 0x29d30
movq %rax, %rcx
movq 0x28(%rsp), %rax
cmpq %rcx, %rax
jae 0x28c13
movq 0x88(%rsp), %rdi
addq $0x10, %rdi
movq 0xc0(%rsp), %rsi
callq 0x29d50
movq %rax, %rdi
leaq 0x470(%rsp), %rsi
callq 0x29d70
movb %al, 0x27(%rsp)
jmp 0x28b3d
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x28b4a
jmp 0x28bf8
movq 0x88(%rsp), %rdi
addq $0x10, %rdi
movq %rdi, 0x10(%rsp)
callq 0x29fd0
movq %rax, 0xa8(%rsp)
movq 0xc0(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x2a000
movq %rax, 0xb0(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x2a040
movq 0x10(%rsp), %rdi
movq 0xb8(%rsp), %rsi
leaq 0x1d0(%rsp), %rdx
callq 0x29da0
movq %rax, 0x18(%rsp)
jmp 0x28bbb
movq 0x18(%rsp), %rax
movq %rax, 0xa0(%rsp)
movl $0x1, 0x9c(%rsp)
jmp 0x28c1e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x468(%rsp)
movl %eax, 0x464(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x29a50
jmp 0x28c74
jmp 0x28bfa
movq 0xc0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xc0(%rsp)
jmp 0x28adf
movl $0x0, 0x9c(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x29a50
leaq 0x1d0(%rsp), %rdi
callq 0x29a50
leaq 0x300(%rsp), %rdi
callq 0x29a50
leaq 0x3a0(%rsp), %rdi
callq 0x46e8
leaq 0x3c0(%rsp), %rdi
callq 0x29a50
leaq 0x470(%rsp), %rdi
callq 0x29a50
addq $0x538, %rsp            # imm = 0x538
retq
leaq 0x1d0(%rsp), %rdi
callq 0x29a50
leaq 0x300(%rsp), %rdi
callq 0x29a50
leaq 0x3a0(%rsp), %rdi
callq 0x46e8
leaq 0x3c0(%rsp), %rdi
callq 0x29a50
leaq 0x470(%rsp), %rdi
callq 0x29a50
movq 0x468(%rsp), %rdi
callq 0x45f0
nopw %cs:(%rax,%rax)