// File: Merulo[P]CompilerJFTT/src/IRs/FirstIR.cpp
// Function: FirstIR::createBeforeForBlock(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Data, Data, bool, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
Block FirstIR::createBeforeForBlock(std::string iterator, Data from, Data to, bool isForTo, std::string name)
{
    Block b = generateBlock();
    b.blockName = name + "_initialForControl";
    Line iteratorInit = getLine(from);
    iteratorInit.one = iterator;

    Line counterInit = getLine(to);
    counterInit.one = iterator + forControlName;

    Line counterFinal = getLine(from);
    counterFinal.operation = "SUB";
    counterFinal.one = counterInit.one;
    counterFinal.two = iterator;

    Line inclusiveFor;
    inclusiveFor.operation = "INC";
    inclusiveFor.one = counterInit.one;

    Line l;
    if (!isForTo)
    {
        std::swap(counterInit.two, counterFinal.two);
        if (counterInit.operation == "CONST")
        {
            counterInit.operation = "COPY";
            
            std::string var = getVariable(counterFinal.two);
            l.operation = "CONST";
            l.one = var;
            l.two = counterFinal.two;
            counterFinal.two = var;
        }
    }

    b.lines.push_back(iteratorInit);
    b.lines.push_back(counterInit);
    b.lines.push_back(inclusiveFor);

    if (!isForTo && !l.operation.empty())
    {
        b.lines.push_back(l);
    }
    
    b.lines.push_back(counterFinal);
    
    Line jump;
    jump.operation = "JZERO";
    jump.one = counterInit.one;
    b.lines.push_back(jump);
    return b;
}

// Assembly:
subq $0x3f8, %rsp            # imm = 0x3F8
movq %r8, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
movq %rdi, 0x30(%rsp)
movb %r9b, %al
movq %rdi, %r9
movq %r9, 0x38(%rsp)
movq 0x400(%rsp), %r9
movq %r9, 0x48(%rsp)
movq %rdi, 0x3f0(%rsp)
movq %rsi, 0x3e8(%rsp)
movq %rdx, 0x3e0(%rsp)
movq %rcx, 0x3d8(%rsp)
movq %r8, 0x3d0(%rsp)
andb $0x1, %al
movb %al, 0x3cf(%rsp)
movq 0x3e8(%rsp), %rsi
movq %rsi, 0x40(%rsp)
movb $0x0, 0x3ce(%rsp)
callq 0x3d200
movq 0x48(%rsp), %rsi
leaq 0x2878a(%rip), %rdx     # 0x514e4
leaq 0x3a8(%rsp), %rdi
callq 0x299c0
jmp 0x28d69
movq 0x30(%rsp), %rdi
leaq 0x3a8(%rsp), %rsi
movq %rsi, 0x10(%rsp)
callq 0x4440
movq 0x10(%rsp), %rdi
callq 0x46e8
movq 0x20(%rsp), %rsi
leaq 0x308(%rsp), %rdi
callq 0x29cd0
jmp 0x28d9e
movq 0x40(%rsp), %rsi
leaq 0x330(%rsp), %rdi
leaq 0x308(%rsp), %rdx
callq 0x296e0
jmp 0x28dba
leaq 0x308(%rsp), %rdi
callq 0x29d10
movq 0x28(%rsp), %rsi
leaq 0x350(%rsp), %rdi
callq 0x4050
jmp 0x28ddb
movq 0x18(%rsp), %rsi
leaq 0x278(%rsp), %rdi
callq 0x29cd0
jmp 0x28def
movq 0x40(%rsp), %rsi
leaq 0x2a0(%rsp), %rdi
leaq 0x278(%rsp), %rdx
callq 0x296e0
jmp 0x28e0b
leaq 0x278(%rsp), %rdi
callq 0x29d10
movq 0x40(%rsp), %rdx
movq 0x28(%rsp), %rsi
addq $0x78, %rdx
leaq 0x258(%rsp), %rdi
callq 0x21f70
jmp 0x28e35
leaq 0x2c0(%rsp), %rdi
leaq 0x258(%rsp), %rsi
movq %rsi, 0x8(%rsp)
callq 0x4440
movq 0x8(%rsp), %rdi
callq 0x46e8
movq 0x20(%rsp), %rsi
leaq 0x1c8(%rsp), %rdi
callq 0x29cd0
jmp 0x28e6d
movq 0x40(%rsp), %rsi
leaq 0x1f0(%rsp), %rdi
leaq 0x1c8(%rsp), %rdx
callq 0x296e0
jmp 0x28e89
leaq 0x1c8(%rsp), %rdi
callq 0x29d10
leaq 0x28231(%rip), %rsi     # 0x510ce
leaq 0x1f0(%rsp), %rdi
callq 0x44a0
jmp 0x28eac
leaq 0x2c0(%rsp), %rsi
leaq 0x210(%rsp), %rdi
callq 0x4050
jmp 0x28ec3
movq 0x28(%rsp), %rsi
leaq 0x230(%rsp), %rdi
callq 0x4050
jmp 0x28ed7
leaq 0x160(%rsp), %rdi
callq 0xda30
jmp 0x28ee6
leaq 0x281e9(%rip), %rsi     # 0x510d6
leaq 0x160(%rsp), %rdi
callq 0x44a0
jmp 0x28efc
leaq 0x2c0(%rsp), %rsi
leaq 0x180(%rsp), %rdi
callq 0x4050
jmp 0x28f13
leaq 0xf8(%rsp), %rdi
callq 0xda30
jmp 0x28f22
testb $0x1, 0x3cf(%rsp)
jne 0x29185
leaq 0x2e0(%rsp), %rdi
leaq 0x230(%rsp), %rsi
callq 0x2a080
leaq 0x28616(%rip), %rsi     # 0x51562
leaq 0x2a0(%rsp), %rdi
callq 0xdd10
movb %al, 0x7(%rsp)
jmp 0x28f5f
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x28f6c
jmp 0x29183
leaq 0x2818a(%rip), %rsi     # 0x510fd
leaq 0x2a0(%rsp), %rdi
callq 0x44a0
jmp 0x28f82
leaq 0x230(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x4180
jmp 0x28f99
movq 0x40(%rsp), %rsi
leaq 0xd8(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0x3d2e0
jmp 0x28fb5
leaq 0xb8(%rsp), %rdi
callq 0x46e8
leaq 0x28599(%rip), %rsi     # 0x51562
leaq 0xf8(%rsp), %rdi
callq 0x44a0
jmp 0x28fd8
leaq 0x118(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x4050
jmp 0x28fef
leaq 0x230(%rsp), %rsi
leaq 0x138(%rsp), %rdi
callq 0x4050
jmp 0x29006
leaq 0x230(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x4050
jmp 0x2901d
leaq 0xd8(%rsp), %rdi
callq 0x46e8
jmp 0x29183
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3a0(%rsp)
movl %eax, 0x39c(%rsp)
jmp 0x29339
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3a0(%rsp)
movl %eax, 0x39c(%rsp)
leaq 0x308(%rsp), %rdi
callq 0x29d10
jmp 0x29339
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3a0(%rsp)
movl %eax, 0x39c(%rsp)
jmp 0x29313
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3a0(%rsp)
movl %eax, 0x39c(%rsp)
leaq 0x278(%rsp), %rdi
callq 0x29d10
jmp 0x29313
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3a0(%rsp)
movl %eax, 0x39c(%rsp)
jmp 0x29306
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3a0(%rsp)
movl %eax, 0x39c(%rsp)
leaq 0x1c8(%rsp), %rdi
callq 0x29d10
jmp 0x29306
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3a0(%rsp)
movl %eax, 0x39c(%rsp)
jmp 0x292f9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3a0(%rsp)
movl %eax, 0x39c(%rsp)
jmp 0x292ec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3a0(%rsp)
movl %eax, 0x39c(%rsp)
jmp 0x292df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3a0(%rsp)
movl %eax, 0x39c(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x46e8
jmp 0x292df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3a0(%rsp)
movl %eax, 0x39c(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x46e8
jmp 0x292df
jmp 0x29185
movq 0x30(%rsp), %rdi
addq $0x20, %rdi
leaq 0x330(%rsp), %rsi
callq 0x297e0
jmp 0x2919d
movq 0x30(%rsp), %rdi
addq $0x20, %rdi
leaq 0x2a0(%rsp), %rsi
callq 0x297e0
jmp 0x291b5
movq 0x30(%rsp), %rdi
addq $0x20, %rdi
leaq 0x160(%rsp), %rsi
callq 0x297e0
jmp 0x291cd
testb $0x1, 0x3cf(%rsp)
jne 0x29202
leaq 0xf8(%rsp), %rdi
callq 0x45b0
testb $0x1, %al
jne 0x29202
movq 0x30(%rsp), %rdi
addq $0x20, %rdi
leaq 0xf8(%rsp), %rsi
callq 0x297e0
jmp 0x29200
jmp 0x29202
movq 0x30(%rsp), %rdi
addq $0x20, %rdi
leaq 0x1f0(%rsp), %rsi
callq 0x297e0
jmp 0x2921a
leaq 0x50(%rsp), %rdi
callq 0xda30
jmp 0x29226
leaq 0x27f87(%rip), %rsi     # 0x511b4
leaq 0x50(%rsp), %rdi
callq 0x44a0
jmp 0x29239
leaq 0x2c0(%rsp), %rsi
leaq 0x70(%rsp), %rdi
callq 0x4050
jmp 0x2924d
movq 0x30(%rsp), %rdi
addq $0x20, %rdi
leaq 0x50(%rsp), %rsi
callq 0x297e0
jmp 0x29262
movb $0x1, 0x3ce(%rsp)
leaq 0x50(%rsp), %rdi
callq 0xd490
leaq 0xf8(%rsp), %rdi
callq 0xd490
leaq 0x160(%rsp), %rdi
callq 0xd490
leaq 0x1f0(%rsp), %rdi
callq 0xd490
leaq 0x2a0(%rsp), %rdi
callq 0xd490
leaq 0x330(%rsp), %rdi
callq 0xd490
testb $0x1, 0x3ce(%rsp)
jne 0x2932c
jmp 0x29322
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3a0(%rsp)
movl %eax, 0x39c(%rsp)
leaq 0x50(%rsp), %rdi
callq 0xd490
leaq 0xf8(%rsp), %rdi
callq 0xd490
leaq 0x160(%rsp), %rdi
callq 0xd490
leaq 0x1f0(%rsp), %rdi
callq 0xd490
leaq 0x2a0(%rsp), %rdi
callq 0xd490
leaq 0x330(%rsp), %rdi
callq 0xd490
jmp 0x29339
movq 0x30(%rsp), %rdi
callq 0x29a50
movq 0x38(%rsp), %rax
addq $0x3f8, %rsp            # imm = 0x3F8
retq
movq 0x30(%rsp), %rdi
callq 0x29a50
movq 0x3a0(%rsp), %rdi
callq 0x45f0