// File: Merulo[P]CompilerJFTT/src/IRs/FifthIR.cpp
// Function: FifthIR::calculateJumps()
void FifthIR::calculateJumps()
{
    unsigned long long instructionCounter = 0;
    for(auto& b : _blocks)
    {
        for(auto& line : b.lines)
        {
            if (line.operation.find("#") == std::string::npos && !line.operation.empty())
            {
                line.operationNumber = instructionCounter;
                instructionCounter++;
            }
        }
    }

    for(auto& b : _blocks)
    {
        for(auto& line : b.lines)
        {
            
            if  (line.operation == "JUMP" 
                || line.operation == "JZERO"
                || line.operation == "JODD")
            {
                std::string argument = line.two;
                if (argument.find(MathOperations::getLabelName()) == std::string::npos)
                {
                    argument = argument.substr(1, std::string::npos);
                    int jumpTarget = getFirstInstructionInBlock(getBlockByName(argument, _blocks));
                    line.two = std::to_string(jumpTarget) + " " + line.two;
                }
                else
                {
                    int jumpTarget = getFirstInstructionAfter(b, argument);
                    line.two = std::to_string(jumpTarget) + " " + line.two;
                }
            }
        }
    }
}

// Assembly:
subq $0x258, %rsp            # imm = 0x258
movq %rdi, 0x250(%rsp)
movq 0x250(%rsp), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x248(%rsp)
addq $0x10, %rax
movq %rax, 0x240(%rsp)
movq 0x240(%rsp), %rdi
callq 0x29fd0
movq %rax, 0x238(%rsp)
movq 0x240(%rsp), %rdi
callq 0x2a870
movq %rax, 0x230(%rsp)
leaq 0x238(%rsp), %rdi
leaq 0x230(%rsp), %rsi
callq 0x30be0
testb $0x1, %al
jne 0x3ef6c
jmp 0x3f072
leaq 0x238(%rsp), %rdi
callq 0x2bda0
movq %rax, 0x228(%rsp)
movq 0x228(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x220(%rsp)
movq 0x220(%rsp), %rdi
callq 0xe3f0
movq %rax, 0x218(%rsp)
movq 0x220(%rsp), %rdi
callq 0xe380
movq %rax, 0x210(%rsp)
leaq 0x218(%rsp), %rdi
leaq 0x210(%rsp), %rsi
callq 0xec10
testb $0x1, %al
jne 0x3efdd
jmp 0x3f05e
leaq 0x218(%rsp), %rdi
callq 0xfb10
movq %rax, 0x208(%rsp)
movq 0x208(%rsp), %rdi
leaq 0x1259b(%rip), %rsi     # 0x5159c
xorl %eax, %eax
movl %eax, %edx
callq 0x46b0
cmpq $-0x1, %rax
jne 0x3f04a
movq 0x208(%rsp), %rdi
callq 0x45b0
testb $0x1, %al
jne 0x3f04a
movq 0x248(%rsp), %rax
movl %eax, %ecx
movq 0x208(%rsp), %rax
movl %ecx, 0x60(%rax)
movq 0x248(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x248(%rsp)
jmp 0x3f04c
leaq 0x218(%rsp), %rdi
callq 0xf950
jmp 0x3efbf
jmp 0x3f060
leaq 0x238(%rsp), %rdi
callq 0x30c20
jmp 0x3ef4e
movq 0x40(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x200(%rsp)
movq 0x200(%rsp), %rdi
callq 0x29fd0
movq %rax, 0x1f8(%rsp)
movq 0x200(%rsp), %rdi
callq 0x2a870
movq %rax, 0x1f0(%rsp)
leaq 0x1f8(%rsp), %rdi
leaq 0x1f0(%rsp), %rsi
callq 0x30be0
testb $0x1, %al
jne 0x3f0cb
jmp 0x3f55a
leaq 0x1f8(%rsp), %rdi
callq 0x2bda0
movq %rax, 0x1e8(%rsp)
movq 0x1e8(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x1e0(%rsp)
movq 0x1e0(%rsp), %rdi
callq 0xe3f0
movq %rax, 0x1d8(%rsp)
movq 0x1e0(%rsp), %rdi
callq 0xe380
movq %rax, 0x1d0(%rsp)
leaq 0x1d8(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0xec10
testb $0x1, %al
jne 0x3f13c
jmp 0x3f546
leaq 0x1d8(%rsp), %rdi
callq 0xfb10
movq %rax, 0x1c8(%rsp)
movq 0x1c8(%rsp), %rdi
leaq 0x12064(%rip), %rsi     # 0x511c4
callq 0xdd10
testb $0x1, %al
jne 0x3f19e
movq 0x1c8(%rsp), %rdi
leaq 0x1203c(%rip), %rsi     # 0x511b4
callq 0xdd10
testb $0x1, %al
jne 0x3f19e
movq 0x1c8(%rsp), %rdi
leaq 0x1202a(%rip), %rsi     # 0x511ba
callq 0xdd10
testb $0x1, %al
jne 0x3f19e
jmp 0x3f532
movq 0x1c8(%rsp), %rsi
addq $0x40, %rsi
leaq 0x1a8(%rsp), %rdi
callq 0x4180
leaq 0x188(%rsp), %rdi
callq 0x13120
jmp 0x3f1c6
leaq 0x1a8(%rsp), %rdi
leaq 0x188(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x46d0
movq %rax, 0x38(%rsp)
leaq 0x188(%rsp), %rdi
callq 0x46e8
movq 0x38(%rsp), %rax
cmpq $-0x1, %rax
jne 0x3f3d1
leaq 0x158(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
movl $0x1, %edx
movq $-0x1, %rcx
callq 0x4350
jmp 0x3f223
leaq 0x1a8(%rsp), %rdi
movq %rdi, 0x30(%rsp)
leaq 0x158(%rsp), %rsi
movq %rsi, 0x28(%rsp)
callq 0x4440
movq 0x28(%rsp), %rdi
callq 0x46e8
movq 0x30(%rsp), %rsi
leaq 0x130(%rsp), %rdi
callq 0x4180
jmp 0x3f260
movq 0x40(%rsp), %rdi
movq %rdi, %rdx
addq $0x10, %rdx
leaq 0x130(%rsp), %rsi
callq 0x3d720
movq %rax, 0x20(%rsp)
jmp 0x3f280
movq 0x20(%rsp), %rsi
movq 0x40(%rsp), %rdi
callq 0x3f570
movl %eax, 0x1c(%rsp)
jmp 0x3f295
leaq 0x130(%rsp), %rdi
callq 0x46e8
movl 0x1c(%rsp), %eax
movl %eax, 0x154(%rsp)
movl 0x154(%rsp), %esi
leaq 0xd0(%rsp), %rdi
callq 0x3f9a0
jmp 0x3f2c3
leaq 0x132b3(%rip), %rdx     # 0x5257d
leaq 0xf0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x4fd0
jmp 0x3f2e1
movq 0x1c8(%rsp), %rdx
addq $0x40, %rdx
leaq 0x110(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x5020
jmp 0x3f304
movq 0x1c8(%rsp), %rdi
addq $0x40, %rdi
leaq 0x110(%rsp), %rsi
callq 0x4440
leaq 0x110(%rsp), %rdi
callq 0x46e8
leaq 0xf0(%rsp), %rdi
callq 0x46e8
leaq 0xd0(%rsp), %rdi
callq 0x46e8
jmp 0x3f514
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0x3f523
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0x130(%rsp), %rdi
callq 0x46e8
jmp 0x3f523
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0x3f3bf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x46e8
leaq 0xd0(%rsp), %rdi
callq 0x46e8
jmp 0x3f523
movq 0x1e8(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xa8(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
callq 0x4180
jmp 0x3f3f5
movq 0x10(%rsp), %rsi
movq 0x40(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0x3f620
movl %eax, 0xc(%rsp)
jmp 0x3f412
leaq 0xa8(%rsp), %rdi
callq 0x46e8
movl 0xc(%rsp), %eax
movl %eax, 0xcc(%rsp)
movl 0xcc(%rsp), %esi
leaq 0x48(%rsp), %rdi
callq 0x3f9a0
jmp 0x3f43d
leaq 0x13139(%rip), %rdx     # 0x5257d
leaq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x4fd0
jmp 0x3f455
movq 0x1c8(%rsp), %rdx
addq $0x40, %rdx
leaq 0x88(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x5020
jmp 0x3f475
movq 0x1c8(%rsp), %rdi
addq $0x40, %rdi
leaq 0x88(%rsp), %rsi
callq 0x4440
leaq 0x88(%rsp), %rdi
callq 0x46e8
leaq 0x68(%rsp), %rdi
callq 0x46e8
leaq 0x48(%rsp), %rdi
callq 0x46e8
jmp 0x3f514
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x46e8
jmp 0x3f523
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0x3f508
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x46e8
leaq 0x48(%rsp), %rdi
callq 0x46e8
jmp 0x3f523
leaq 0x1a8(%rsp), %rdi
callq 0x46e8
jmp 0x3f532
leaq 0x1a8(%rsp), %rdi
callq 0x46e8
jmp 0x3f562
jmp 0x3f534
leaq 0x1d8(%rsp), %rdi
callq 0xf950
jmp 0x3f11e
jmp 0x3f548
leaq 0x1f8(%rsp), %rdi
callq 0x30c20
jmp 0x3f0ad
addq $0x258, %rsp            # imm = 0x258
retq
movq 0x180(%rsp), %rdi
callq 0x45f0
nop