// File: /workspace/github_cpp_makefile_star2_O1/Merulo[P]CompilerJFTT/src/Register/RegisterBlock.cpp
// Function: RegisterBlock::exitBlock(Block&, RegisterBlock&, IRBase&)
void RegisterBlock::exitBlock(Block& b, RegisterBlock& other, IRBase& irBase)
{
    //saves to memory
    for(size_t i = 0; i < _registers.size(); i++)
    {
        if (_registers[i].variableName == other._registers[i].variableName)
        {
            continue;
        }
        if (other._registers[i].state == RegisterState::UNKNOWN)
        {
            FourthIR& fourthIR = static_cast<FourthIR&>(irBase);

            bool result = fourthIR.isThisVariableUsed(_registers[i].variableName, b);
            if (result)
            {
                continue;
            }
        }
        saveToMemory(b, _registers[i], _addressRegister);
    }

    //loads tables to registers
    for(size_t i = 0; i < _registers.size(); i++)
    {
        if (_registers[i].variableName == other._registers[i].variableName)
        {
            continue;
        } 
        if (other._registers[i].state != RegisterState::TABLE)
        {
            continue;
        }       
        _registers[i].variableName = "";
        _registers[i].state = RegisterState::UNKNOWN;
        loadFromMemory(b, other._registers[i].variableName, _registers[i], _addressRegister);
    }

    //loads variables to registers
    for(size_t i = 0; i < _registers.size(); i++)
    {
        if (_registers[i].variableName == other._registers[i].variableName)
        {
            continue;
        }
        if (other._registers[i].state == RegisterState::TABLE)
        {
            continue;
        }           
        _registers[i].variableName = "";
        _registers[i].state = RegisterState::UNKNOWN;
        loadFromMemory(b, other._registers[i].variableName, _registers[i], _addressRegister);
    }

    //sets A register
    if (other._addressRegister.state != RegisterState::UNKNOWN)
    {
        if (_addressRegister.state == RegisterState::UNKNOWN)
        {
            b.lines.push_back({"SUB", "A", "A"});
            _addressRegister.state = RegisterState::CONST;
            _addressRegister.constValue = 0;
        }
        auto lines = generateNumberFrom(_addressRegister.constValue, other._addressRegister.constValue, _addressRegister);
        b.insert(lines);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rcx, 0x18(%rsp)
movq %rdx, (%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, %r14
movq 0x68(%rdi), %r13
cmpq %r13, 0x70(%rdi)
je 0x5b05
leaq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
xorl %r12d, %r12d
movq (%rsp), %rax
movq 0x68(%rax), %r15
movq 0x38(%r13,%rbx), %rbp
cmpq 0x38(%r15,%rbx), %rbp
jne 0x5a57
testq %rbp, %rbp
je 0x5ad8
movq 0x30(%r15,%rbx), %rsi
movq 0x30(%r13,%rbx), %rdi
movq %rbp, %rdx
callq 0x42b0
testl %eax, %eax
je 0x5ad8
cmpl $0x0, (%r15,%rbx)
jne 0x5abf
leaq 0xd0(%rsp), %r15
movq %r15, 0xc0(%rsp)
movq 0x30(%r13,%rbx), %rsi
addq %rsi, %rbp
leaq 0xc0(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rdx
callq 0xca12
movq 0x18(%rsp), %rdi
movq %r13, %rsi
movq 0x8(%rsp), %rdx
callq 0x28fc8
movl %eax, %ebp
movq 0xc0(%rsp), %rdi
cmpq %r15, %rdi
je 0x5aba
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x42a0
testb %bpl, %bpl
jne 0x5ad8
movq 0x68(%r14), %rdx
addq %rbx, %rdx
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rcx
callq 0x5fde
incq %r12
movq 0x68(%r14), %r13
movq 0x70(%r14), %rax
subq %r13, %rax
sarq $0x3, %rax
movabsq $0x2e8ba2e8ba2e8ba3, %rcx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rcx, %rax
addq $0x58, %rbx
cmpq %rax, %r12
jb 0x5a20
movq 0x68(%r14), %r13
cmpq %r13, 0x70(%r14)
je 0x5c2a
leaq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movl $0x30, %ebx
xorl %r12d, %r12d
movq (%rsp), %rax
movq 0x68(%rax), %r15
movq 0x8(%r13,%rbx), %rbp
addq %rbx, %r13
cmpq 0x8(%r15,%rbx), %rbp
jne 0x5b5c
testq %rbp, %rbp
je 0x5bfd
movq (%r15,%rbx), %rsi
movq (%r13), %rdi
movq %rbp, %rdx
callq 0x42b0
testl %eax, %eax
je 0x5bfd
cmpl $0x3, -0x30(%r15,%rbx)
jne 0x5bfd
movq %r13, %rdi
xorl %esi, %esi
movq %rbp, %rdx
leaq 0x3256c(%rip), %rcx     # 0x380e3
xorl %r8d, %r8d
callq 0x4440
movq 0x68(%r14), %rax
movl $0x0, -0x30(%rax,%rbx)
movq (%rsp), %rax
movq 0x68(%rax), %rax
leaq 0xb0(%rsp), %r15
movq %r15, 0xa0(%rsp)
movq (%rax,%rbx), %rsi
movq 0x8(%rax,%rbx), %rdx
addq %rsi, %rdx
leaq 0xa0(%rsp), %r13
movq %r13, %rdi
callq 0xca12
movq 0x68(%r14), %rax
leaq (%rax,%rbx), %rcx
addq $-0x30, %rcx
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq %r13, %rdx
movq 0x10(%rsp), %r8
callq 0x61ee
movq 0xa0(%rsp), %rdi
cmpq %r15, %rdi
je 0x5bfd
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x42a0
incq %r12
movq 0x68(%r14), %r13
movq 0x70(%r14), %rax
subq %r13, %rax
sarq $0x3, %rax
movabsq $0x2e8ba2e8ba2e8ba3, %rcx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rcx, %rax
addq $0x58, %rbx
cmpq %rax, %r12
jb 0x5b24
movq 0x68(%r14), %r13
cmpq %r13, 0x70(%r14)
je 0x5d4f
leaq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movl $0x30, %ebx
xorl %r12d, %r12d
movq (%rsp), %rax
movq 0x68(%rax), %r15
movq 0x8(%r13,%rbx), %rbp
addq %rbx, %r13
cmpq 0x8(%r15,%rbx), %rbp
jne 0x5c81
testq %rbp, %rbp
je 0x5d22
movq (%r15,%rbx), %rsi
movq (%r13), %rdi
movq %rbp, %rdx
callq 0x42b0
testl %eax, %eax
je 0x5d22
cmpl $0x3, -0x30(%r15,%rbx)
je 0x5d22
movq %r13, %rdi
xorl %esi, %esi
movq %rbp, %rdx
leaq 0x32447(%rip), %rcx     # 0x380e3
xorl %r8d, %r8d
callq 0x4440
movq 0x68(%r14), %rax
movl $0x0, -0x30(%rax,%rbx)
movq (%rsp), %rax
movq 0x68(%rax), %rax
leaq 0x90(%rsp), %r15
movq %r15, 0x80(%rsp)
movq (%rax,%rbx), %rsi
movq 0x8(%rax,%rbx), %rdx
addq %rsi, %rdx
leaq 0x80(%rsp), %r13
movq %r13, %rdi
callq 0xca12
movq 0x68(%r14), %rax
leaq (%rax,%rbx), %rcx
addq $-0x30, %rcx
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq %r13, %rdx
movq 0x10(%rsp), %r8
callq 0x61ee
movq 0x80(%rsp), %rdi
cmpq %r15, %rdi
je 0x5d22
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x42a0
incq %r12
movq 0x68(%r14), %r13
movq 0x70(%r14), %rax
subq %r13, %rax
sarq $0x3, %rax
movabsq $0x2e8ba2e8ba2e8ba3, %rcx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rcx, %rax
addq $0x58, %rbx
cmpq %rax, %r12
jb 0x5c49
movq (%rsp), %rax
cmpl $0x0, 0x10(%rax)
je 0x5efb
cmpl $0x0, 0x10(%r14)
jne 0x5eb6
leaq 0x70(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x3231d(%rip), %rsi     # 0x38096
leaq 0x32319(%rip), %rdx     # 0x38099
leaq 0x60(%rsp), %rdi
callq 0x54fc
leaq 0x50(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x322fa(%rip), %rsi     # 0x38094
leaq 0x322f4(%rip), %rdx     # 0x38095
leaq 0x40(%rsp), %rdi
callq 0x54fc
leaq 0x30(%rsp), %rbp
movq %rbp, -0x10(%rbp)
leaq 0x322d9(%rip), %rsi     # 0x38094
leaq 0x322d3(%rip), %rdx     # 0x38095
leaq 0x20(%rsp), %rdi
callq 0x54fc
leaq 0xe0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x20(%rsp), %rcx
callq 0xbe58
movq 0x8(%rsp), %rax
leaq 0x20(%rax), %rdi
leaq 0xe0(%rsp), %rsi
callq 0xcf0e
leaq 0x130(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5e1f
movq 0x130(%rsp), %rsi
incq %rsi
callq 0x42a0
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5e40
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x42a0
leaq 0xf0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5e61
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x42a0
movq 0x20(%rsp), %rdi
cmpq %rbp, %rdi
je 0x5e78
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x42a0
movq 0x40(%rsp), %rdi
cmpq %r13, %rdi
je 0x5e8f
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x42a0
movq 0x60(%rsp), %rdi
cmpq %r12, %rdi
je 0x5ea6
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x42a0
movl $0x2, 0x10(%r14)
movq $0x0, 0x18(%r14)
addq $0x18, %r14
movq (%rsp), %rax
movq 0x18(%rax), %rcx
leaq 0xe0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rdx
callq 0x65be
movq 0x8(%rsp), %rdi
movq 0x28(%rdi), %rsi
addq $0x20, %rdi
movq (%r15), %rdx
movq 0x8(%r15), %rcx
callq 0xc3f4
leaq 0xe0(%rsp), %rdi
callq 0xbf5e
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0xbf10
jmp 0x5f22
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %rbp, %rdi
je 0x5f3e
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x42a0
jmp 0x5f3e
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r13, %rdi
je 0x5f5a
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x42a0
jmp 0x5f5a
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r12, %rdi
je 0x5fd6
movq 0x70(%rsp), %rsi
jmp 0x5fce
movq %rax, %rbx
jmp 0x5fd6
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0xbf5e
jmp 0x5fd6
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
cmpq %r15, %rdi
je 0x5fd6
movq 0xd0(%rsp), %rsi
jmp 0x5fce
movq %rax, %rbx
movq 0x80(%rsp), %rdi
cmpq %r15, %rdi
je 0x5fd6
movq 0x90(%rsp), %rsi
jmp 0x5fce
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
cmpq %r15, %rdi
je 0x5fd6
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x42a0
movq %rbx, %rdi
callq 0x44c0