// File: Merulo[P]CompilerJFTT/src/IRs/ThirdIR.cpp
// Function: ThirdIR::legalizeJumps()
void ThirdIR::legalizeJumps()
{
    for (size_t i = 0; i < _blocks.size(); i++)
    {
        if (!_blocks[i].blockJump.empty())
        {
            Line l;
            l.operation = "JUMP";
            l.two = "#" + _blocks[i].blockJump;
            _blocks[i].lines.push_back(l);
            continue;
        }
        if (_blocks[i].lines.back().operation == "JLS")
        {
            legalizeJLS(_blocks[i], false);
        }
        if(_blocks[i].lines.back().operation == "JMR")
        {
            legalizeJMR(_blocks[i], false);
        }
        if (_blocks[i].lines.back().operation == "JLE")
        {
            legalizeJLS(_blocks[i], true);
        }
        if(_blocks[i].lines.back().operation == "JME")
        {
            legalizeJMR(_blocks[i], true);
        }
        if(_blocks[i].lines.back().operation == "JEQ")
        {
            legalizeEquality(_blocks[i]);
        }
        if(_blocks[i].lines.back().operation == "JNE")
        {
            std::swap(_blocks[i].blockIfFalse, _blocks[i].blockIfTrue);
            legalizeEquality(_blocks[i]);
        }           
    }
}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0xc8(%rsp)
movq 0x28(%rsp), %rdi
movq 0xc8(%rsp), %rax
movq %rax, 0x20(%rsp)
addq $0x10, %rdi
callq 0x29d30
movq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rcx, %rax
jae 0x40a01
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq %rax, %rdi
addq $0x78, %rdi
callq 0x45b0
testb $0x1, %al
jne 0x40776
leaq 0x60(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0xda30
movq 0x18(%rsp), %rdi
leaq 0x10afb(%rip), %rsi     # 0x511c4
callq 0x44a0
jmp 0x406d0
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq %rax, %rdx
addq $0x78, %rdx
leaq 0x10ea8(%rip), %rsi     # 0x5159c
leaq 0x30(%rsp), %rdi
callq 0x5070
jmp 0x40700
leaq 0xa0(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq %rsi, 0x10(%rsp)
callq 0x4440
movq 0x10(%rsp), %rdi
callq 0x46e8
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq %rax, %rdi
addq $0x20, %rdi
leaq 0x60(%rsp), %rsi
callq 0x297e0
jmp 0x4074a
leaq 0x60(%rsp), %rdi
callq 0xd490
jmp 0x409e8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
leaq 0x60(%rsp), %rdi
callq 0xd490
jmp 0x40a09
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq %rax, %rdi
addq $0x20, %rdi
callq 0x11660
movq %rax, %rdi
leaq 0x10ec8(%rip), %rsi     # 0x5166a
callq 0xdd10
testb $0x1, %al
jne 0x407ad
jmp 0x407d2
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq 0x28(%rsp), %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x40b70
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq %rax, %rdi
addq $0x20, %rdi
callq 0x11660
movq %rax, %rdi
leaq 0x10e70(%rip), %rsi     # 0x5166e
callq 0xdd10
testb $0x1, %al
jne 0x40809
jmp 0x4082e
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq 0x28(%rsp), %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x40e20
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq %rax, %rdi
addq $0x20, %rdi
callq 0x11660
movq %rax, %rdi
leaq 0x10e18(%rip), %rsi     # 0x51672
callq 0xdd10
testb $0x1, %al
jne 0x40865
jmp 0x4088d
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq 0x28(%rsp), %rdi
movq %rax, %rsi
movl $0x1, %edx
callq 0x40b70
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq %rax, %rdi
addq $0x20, %rdi
callq 0x11660
movq %rax, %rdi
leaq 0x10dbd(%rip), %rsi     # 0x51676
callq 0xdd10
testb $0x1, %al
jne 0x408c4
jmp 0x408ec
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq 0x28(%rsp), %rdi
movq %rax, %rsi
movl $0x1, %edx
callq 0x40e20
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq %rax, %rdi
addq $0x20, %rdi
callq 0x11660
movq %rax, %rdi
leaq 0x10d62(%rip), %rsi     # 0x5167a
callq 0xdd10
testb $0x1, %al
jne 0x40923
jmp 0x40946
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x41060
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq %rax, %rdi
addq $0x20, %rdi
callq 0x11660
movq %rax, %rdi
leaq 0x10d0c(%rip), %rsi     # 0x5167e
callq 0xdd10
testb $0x1, %al
jne 0x4097d
jmp 0x409e6
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq 0x28(%rsp), %rdi
addq $0x58, %rax
movq %rax, 0x8(%rsp)
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq 0x8(%rsp), %rdi
movq %rax, %rsi
addq $0x38, %rsi
callq 0x2a080
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq 0xc8(%rsp), %rsi
callq 0x29d50
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x41060
jmp 0x409e8
movq 0xc8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xc8(%rsp)
jmp 0x40658
addq $0xd8, %rsp
retq
movq 0x58(%rsp), %rdi
callq 0x45f0
nopw %cs:(%rax,%rax)