// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
leaq 0x10(%rdi), %rax
movq %rax, 0x20(%rsp)
movq 0x10(%rdi), %r15
movq %rdi, 0x8(%rsp)
movq 0x18(%rdi), %r12
xorl %r13d, %r13d
leaq 0x79b0(%rip), %rbx      # 0x2355b
cmpq %r12, %r15
je 0x1bbed
movq 0x20(%r15), %r14
movq 0x28(%r15), %rbp
cmpq %rbp, %r14
je 0x1bbe4
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x4520
cmpq $-0x1, %rax
jne 0x1bbde
cmpq $0x0, 0x8(%r14)
je 0x1bbde
movl %r13d, 0x60(%r14)
incl %r13d
addq $0x68, %r14
jmp 0x1bbb8
addq $0xa0, %r15
jmp 0x1bbab
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rcx
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
leaq 0xd8(%rsp), %r14
leaq 0xb8(%rsp), %rbp
cmpq 0x18(%rsp), %rcx
je 0x1be33
movq 0x20(%rcx), %rbx
movq %rcx, 0x10(%rsp)
movq 0x28(%rcx), %rax
movq %rax, 0x28(%rsp)
cmpq 0x28(%rsp), %rbx
je 0x1be22
movq %rbx, %rdi
leaq 0x754b(%rip), %rsi      # 0x2318c
callq 0x8453
testb %al, %al
jne 0x1bc74
movq %rbx, %rdi
leaq 0x7528(%rip), %rsi      # 0x2317c
callq 0x8453
testb %al, %al
jne 0x1bc74
movq %rbx, %rdi
leaq 0x751b(%rip), %rsi      # 0x23182
callq 0x8453
testb %al, %al
je 0x1be19
leaq 0x40(%rbx), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x4390
movq %rbp, %rdi
callq 0x94bc
movq %rbx, 0x30(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x4530
movq %rax, %r12
movq %rbp, %rdi
callq 0x4548
cmpq $-0x1, %r12
je 0x1bd44
movq %r14, %rsi
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %rsi, %rbx
callq 0x4390
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x1bef2
movl %eax, %r12d
movq %r14, %rdi
callq 0x4548
leaq 0x38(%rsp), %r13
movq %r13, %rdi
movl %r12d, %esi
callq 0x1c088
leaq 0x58(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
leaq 0x8767(%rip), %rdx      # 0x2446b
callq 0x4a8f
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4aae
movq %r15, %rdi
movq %rbp, %rsi
callq 0x4340
movq %rbp, %rdi
callq 0x4548
movq %r14, %rdi
callq 0x4548
leaq 0x38(%rsp), %rdi
callq 0x4548
movq %rbx, %r14
jmp 0x1be0c
movq %rbp, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
pushq $-0x1
popq %rcx
callq 0x42a0
movq %r14, %r12
leaq 0x38(%rsp), %r13
movq %r14, %rdi
movq %rbp, %rsi
callq 0x4340
movq %rbp, %rdi
callq 0x4548
leaq 0x98(%rsp), %r14
movq %r14, %rdi
movq %r12, %rbp
movq %r12, %rsi
callq 0x4390
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq 0x20(%rsp), %rdx
callq 0x1b1b0
movq %rax, %rsi
callq 0x1bea0
movl %eax, %r12d
movq %r14, %rdi
callq 0x4548
movq %r13, %rdi
movl %r12d, %esi
callq 0x1c088
leaq 0x58(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
leaq 0x86a3(%rip), %rdx      # 0x2446b
callq 0x4a8f
leaq 0xb8(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4aae
movq %r15, %rdi
movq %r12, %rsi
callq 0x4340
movq %r12, %rdi
callq 0x4548
movq %r14, %rdi
callq 0x4548
movq %r13, %rdi
callq 0x4548
movq %rbp, %r14
movq %r12, %rbp
movq %r14, %rdi
callq 0x4548
movq 0x30(%rsp), %rbx
addq $0x68, %rbx
jmp 0x1bc2c
movq 0x10(%rsp), %rcx
addq $0xa0, %rcx
jmp 0x1bc0f
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1be88
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
jmp 0x1be81
jmp 0x1be88
jmp 0x1be88
jmp 0x1be5c
jmp 0x1be6f
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x4548
jmp 0x1be72
jmp 0x1be88
jmp 0x1be88
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
jmp 0x1be81
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x4548
jmp 0x1be8b
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x4548
movq %rbx, %rdi
callq 0x4490