// File: Merulo[P]CompilerJFTT/src/IRs/FourthIR.cpp
// Function: FourthIR::alignRegisters(Pair&, Pair&, RegisterBlock&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void FourthIR::alignRegisters(Pair& pair, Pair& next, RegisterBlock& rb, std::string name)
{
    Block handle = generateBlock();
    RegisterBlock copy(pair.endRegisterBlock);
    handle.blockJump = next.block.blockName;
    copy.exitBlock(handle, next.startRegisterBlock, *this);

    Block& grr = getBlockByName(pair.block.blockName, _blocks);

    for(auto& l : grr.lines)
    {
        if (l.two == "#" + name)
        {
            l.two = "#" + handle.blockName;
        }
        if (l.operation.find(name) != std::string::npos)
        {
            l.operation.replace(l.operation.find(name), name.length(), handle.blockName);
        }        
    }
    handle.lines.push_back({"JUMP", "", "#" +  next.block.blockName});
    _blocks.push_back(handle);
            
    convertBlockToAssembler(next, copy);
}

// Assembly:
subq $0x318, %rsp            # imm = 0x318
movq %r8, 0x48(%rsp)
movq %rdi, 0x310(%rsp)
movq %rsi, 0x308(%rsp)
movq %rdx, 0x300(%rsp)
movq %rcx, 0x2f8(%rsp)
movq %r8, 0x2f0(%rsp)
movq 0x310(%rsp), %rsi
movq %rsi, 0x50(%rsp)
leaq 0x250(%rsp), %rdi
callq 0x3d200
movq 0x308(%rsp), %rsi
addq $0xa0, %rsi
leaq 0x1c8(%rsp), %rdi
callq 0x5560
jmp 0x383bc
movq 0x300(%rsp), %rsi
leaq 0x2c8(%rsp), %rdi
callq 0x4050
jmp 0x383d3
movq 0x50(%rsp), %rcx
movq 0x300(%rsp), %rdx
addq $0x128, %rdx            # imm = 0x128
leaq 0x1c8(%rsp), %rdi
leaq 0x250(%rsp), %rsi
callq 0x5830
jmp 0x383fe
movq 0x308(%rsp), %rsi
leaq 0x190(%rsp), %rdi
callq 0x4180
jmp 0x38415
movq 0x50(%rsp), %rdi
movq %rdi, %rdx
addq $0x10, %rdx
leaq 0x190(%rsp), %rsi
callq 0x3d720
movq %rax, 0x40(%rsp)
jmp 0x38435
leaq 0x190(%rsp), %rdi
callq 0x46e8
movq 0x40(%rsp), %rax
movq %rax, 0x1b0(%rsp)
movq 0x1b0(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x188(%rsp)
movq 0x188(%rsp), %rdi
callq 0xe3f0
movq %rax, 0x180(%rsp)
movq 0x188(%rsp), %rdi
callq 0xe380
movq %rax, 0x178(%rsp)
leaq 0x180(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0xec10
testb $0x1, %al
jne 0x384ab
jmp 0x38632
leaq 0x180(%rsp), %rdi
callq 0xfb10
movq 0x48(%rsp), %rdx
movq %rax, 0x170(%rsp)
movq 0x170(%rsp), %rax
addq $0x40, %rax
movq %rax, 0x38(%rsp)
leaq 0x190bf(%rip), %rsi     # 0x5159c
leaq 0x150(%rsp), %rdi
callq 0x5070
jmp 0x384ec
movq 0x38(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0xd2c0
movb %al, 0x37(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x46e8
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0x3851c
jmp 0x385ba
leaq 0x19079(%rip), %rsi     # 0x5159c
leaq 0x130(%rsp), %rdi
leaq 0x250(%rsp), %rdx
callq 0x5070
jmp 0x3853a
movq 0x170(%rsp), %rdi
addq $0x40, %rdi
leaq 0x130(%rsp), %rsi
callq 0x4440
leaq 0x130(%rsp), %rdi
callq 0x46e8
jmp 0x385ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0x3884b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0x3883e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x46e8
jmp 0x3883e
movq 0x48(%rsp), %rsi
movq 0x170(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
callq 0x46d0
cmpq $-0x1, %rax
je 0x3861e
movq 0x48(%rsp), %rsi
movq 0x170(%rsp), %rdi
movq %rdi, 0x20(%rsp)
xorl %eax, %eax
movl %eax, %edx
callq 0x46d0
movq 0x48(%rsp), %rdi
movq %rax, 0x28(%rsp)
callq 0x4520
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %rax, %rdx
leaq 0x250(%rsp), %rcx
callq 0x4130
jmp 0x3861c
jmp 0x3861e
jmp 0x38620
leaq 0x180(%rsp), %rdi
callq 0xf950
jmp 0x3848d
leaq 0x270(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xa7(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x4600
movq 0x18(%rsp), %rdx
leaq 0x18b67(%rip), %rsi     # 0x511c4
leaq 0xa8(%rsp), %rdi
callq 0x4f10
jmp 0x3866c
leaq 0x7f(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x4600
movq 0x8(%rsp), %rdx
leaq 0x18a94(%rip), %rsi     # 0x5111b
leaq 0x80(%rsp), %rdi
callq 0x4f10
jmp 0x38696
movq 0x300(%rsp), %rdx
leaq 0x18ef7(%rip), %rsi     # 0x5159c
leaq 0x58(%rsp), %rdi
callq 0x5070
jmp 0x386b1
leaq 0xc8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x80(%rsp), %rdx
leaq 0x58(%rsp), %rcx
callq 0xd3b0
jmp 0x386d5
movq 0x10(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0xd380
jmp 0x386e9
leaq 0xc8(%rsp), %rdi
callq 0xd490
leaq 0x58(%rsp), %rdi
callq 0x46e8
leaq 0x80(%rsp), %rdi
callq 0x46e8
leaq 0x7f(%rsp), %rdi
callq 0x43b0
leaq 0xa8(%rsp), %rdi
callq 0x46e8
leaq 0xa7(%rsp), %rdi
callq 0x43b0
movq 0x50(%rsp), %rdi
addq $0x10, %rdi
leaq 0x250(%rsp), %rsi
callq 0x29850
jmp 0x38749
movq 0x50(%rsp), %rdi
movq 0x300(%rsp), %rsi
leaq 0x1c8(%rsp), %rdx
callq 0x32610
jmp 0x38765
leaq 0x1c8(%rsp), %rdi
callq 0x38fe0
leaq 0x250(%rsp), %rdi
callq 0x29a50
addq $0x318, %rsp            # imm = 0x318
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0x38831
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0x3881a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0x3880d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0x38803
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0xd490
leaq 0x58(%rsp), %rdi
callq 0x46e8
leaq 0x80(%rsp), %rdi
callq 0x46e8
leaq 0x7f(%rsp), %rdi
callq 0x43b0
leaq 0xa8(%rsp), %rdi
callq 0x46e8
leaq 0xa7(%rsp), %rdi
callq 0x43b0
leaq 0x1c8(%rsp), %rdi
callq 0x38fe0
leaq 0x250(%rsp), %rdi
callq 0x29a50
movq 0x1c0(%rsp), %rdi
callq 0x45f0
nopw %cs:(%rax,%rax)