// File: /workspace/github_cpp_makefile_star2_O3/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 $0x158, %rsp            # imm = 0x158
movq %rcx, 0x28(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r14
movq 0x68(%rdi), %r13
movq 0x70(%rdi), %rax
cmpq %r13, %rax
je 0x5a07
leaq 0xe0(%rsp), %r15
leaq 0x10(%r14), %rax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
xorl %ebp, %ebp
movq %r14, 0x20(%rsp)
movq %r15, %r14
movq 0x8(%rsp), %rax
movq 0x68(%rax), %r15
movq 0x38(%r13,%rbx), %r12
cmpq 0x38(%r15,%rbx), %r12
jne 0x593b
testq %r12, %r12
je 0x594c
movq 0x30(%r15,%rbx), %rsi
movq 0x30(%r13,%rbx), %rdi
movq %r12, %rdx
callq 0x42b0
testl %eax, %eax
je 0x594c
cmpl $0x0, (%r15,%rbx)
je 0x5959
movq %r14, %r15
movq 0x20(%rsp), %r14
jmp 0x59bf
movq %r14, %r15
movq 0x20(%rsp), %r14
jmp 0x59db
movq %r14, %r15
movq %r14, 0xd0(%rsp)
movq 0x30(%r13,%rbx), %rsi
addq %rsi, %r12
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
movq %r12, %rdx
callq 0xc7aa
movq 0x28(%rsp), %rdi
movq %r14, %rsi
movq 0x10(%rsp), %rdx
callq 0x28d5a
movl %eax, %r12d
movq 0xd0(%rsp), %rdi
cmpq %r15, %rdi
movq 0x20(%rsp), %r14
je 0x59b6
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x42a0
movq 0x68(%r14), %r13
testb %r12b, %r12b
jne 0x59db
addq %rbx, %r13
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
movq 0x18(%rsp), %rcx
callq 0x5ede
movq 0x68(%r14), %r13
incq %rbp
movq 0x70(%r14), %rax
movq %rax, %rcx
subq %r13, %rcx
sarq $0x3, %rcx
movabsq $0x2e8ba2e8ba2e8ba3, %rdx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rdx, %rcx
addq $0x58, %rbx
cmpq %rcx, %rbp
jb 0x5908
cmpq %r13, %rax
je 0x5c43
leaq 0x10(%r14), %rax
movq %rax, 0x18(%rsp)
movl $0x30, %ebx
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
movq 0x68(%rax), %r15
movq 0x8(%r13,%rbx), %r12
addq %rbx, %r13
cmpq 0x8(%r15,%rbx), %r12
jne 0x5a59
testq %r12, %r12
je 0x5afb
movq (%r15,%rbx), %rsi
movq (%r13), %rdi
movq %r12, %rdx
callq 0x42b0
testl %eax, %eax
je 0x5afb
cmpl $0x3, -0x30(%r15,%rbx)
jne 0x5afb
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
leaq 0x3166f(%rip), %rcx     # 0x370e3
xorl %r8d, %r8d
callq 0x4440
movq 0x68(%r14), %rax
movl $0x0, -0x30(%rax,%rbx)
movq 0x8(%rsp), %rax
movq 0x68(%rax), %rax
leaq 0xc0(%rsp), %r15
movq %r15, 0xb0(%rsp)
movq (%rax,%rbx), %rsi
movq 0x8(%rax,%rbx), %rdx
addq %rsi, %rdx
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
callq 0xc7aa
movq 0x68(%r14), %rax
leaq (%rax,%rbx), %rcx
addq $-0x30, %rcx
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq %r12, %rdx
movq 0x18(%rsp), %r8
callq 0x60ee
movq 0xb0(%rsp), %rdi
cmpq %r15, %rdi
je 0x5afb
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x42a0
incq %rbp
movq 0x68(%r14), %r13
movq 0x70(%r14), %rax
movq %rax, %rcx
subq %r13, %rcx
sarq $0x3, %rcx
movabsq $0x2e8ba2e8ba2e8ba3, %rdx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rdx, %rcx
addq $0x58, %rbx
cmpq %rcx, %rbp
jb 0x5a20
cmpq %r13, %rax
je 0x5c43
movl $0x30, %ebx
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
movq 0x68(%rax), %r15
movq 0x8(%r13,%rbx), %r12
addq %rbx, %r13
cmpq 0x8(%r15,%rbx), %r12
jne 0x5b74
testq %r12, %r12
je 0x5c16
movq (%r15,%rbx), %rsi
movq (%r13), %rdi
movq %r12, %rdx
callq 0x42b0
testl %eax, %eax
je 0x5c16
cmpl $0x3, -0x30(%r15,%rbx)
je 0x5c16
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
leaq 0x31554(%rip), %rcx     # 0x370e3
xorl %r8d, %r8d
callq 0x4440
movq 0x68(%r14), %rax
movl $0x0, -0x30(%rax,%rbx)
movq 0x8(%rsp), %rax
movq 0x68(%rax), %rax
leaq 0xa0(%rsp), %r15
movq %r15, 0x90(%rsp)
movq (%rax,%rbx), %rsi
movq 0x8(%rax,%rbx), %rdx
addq %rsi, %rdx
leaq 0x90(%rsp), %r12
movq %r12, %rdi
callq 0xc7aa
movq 0x68(%r14), %rax
leaq (%rax,%rbx), %rcx
addq $-0x30, %rcx
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq %r12, %rdx
movq 0x18(%rsp), %r8
callq 0x60ee
movq 0x90(%rsp), %rdi
cmpq %r15, %rdi
je 0x5c16
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x42a0
incq %rbp
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, %rbp
jb 0x5b3b
movq 0x8(%rsp), %rax
cmpl $0x0, 0x10(%rax)
je 0x5df7
cmpl $0x0, 0x10(%r14)
jne 0x5db1
leaq 0x80(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x31425(%rip), %rsi     # 0x37096
leaq 0x31421(%rip), %rdx     # 0x37099
leaq 0x70(%rsp), %rdi
callq 0x53cc
leaq 0x60(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x31402(%rip), %rsi     # 0x37094
leaq 0x313fc(%rip), %rdx     # 0x37095
leaq 0x50(%rsp), %rdi
callq 0x53cc
leaq 0x40(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x313e1(%rip), %rsi     # 0x37094
leaq 0x313db(%rip), %rdx     # 0x37095
leaq 0x30(%rsp), %rdi
callq 0x53cc
leaq 0xf0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0xbbea
movq 0x10(%rsp), %rax
leaq 0x20(%rax), %rdi
leaq 0xf0(%rsp), %rsi
callq 0xccac
leaq 0x140(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5d17
movq 0x140(%rsp), %rsi
incq %rsi
callq 0x42a0
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5d38
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x42a0
leaq 0x100(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5d59
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x42a0
movq 0x30(%rsp), %rdi
cmpq %rbx, %rdi
je 0x5d70
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x42a0
movq 0x50(%rsp), %rdi
cmpq %r13, %rdi
je 0x5d87
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x42a0
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0x5da1
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x42a0
movl $0x2, 0x10(%r14)
movq $0x0, 0x18(%r14)
addq $0x18, %r14
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rcx
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rdx
callq 0x64be
movq 0x10(%rsp), %rdi
movq 0x28(%rdi), %rsi
addq $0x20, %rdi
movq (%r15), %rdx
movq 0x8(%r15), %rcx
callq 0xc18c
leaq 0xf0(%rsp), %rdi
callq 0xbcf0
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq 0xf0(%rsp), %rdi
callq 0xbca2
jmp 0x5e1e
movq %rax, %r14
movq 0x30(%rsp), %rdi
cmpq %rbx, %rdi
je 0x5e3a
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x42a0
jmp 0x5e3a
movq %rax, %r14
movq 0x50(%rsp), %rdi
cmpq %r13, %rdi
je 0x5e56
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x42a0
jmp 0x5e56
movq %rax, %r14
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0x5ed5
movq 0x80(%rsp), %rsi
jmp 0x5ecd
movq %rax, %r14
jmp 0x5ed5
movq %rax, %r14
leaq 0xf0(%rsp), %rdi
callq 0xbcf0
jmp 0x5ed5
movq %rax, %r14
movq 0x90(%rsp), %rdi
cmpq %r15, %rdi
je 0x5ed5
movq 0xa0(%rsp), %rsi
jmp 0x5ecd
movq %rax, %r14
movq 0xd0(%rsp), %rdi
cmpq %r15, %rdi
je 0x5ed5
movq 0xe0(%rsp), %rsi
jmp 0x5ecd
movq %rax, %r14
movq 0xb0(%rsp), %rdi
cmpq %r15, %rdi
je 0x5ed5
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x42a0
movq %r14, %rdi
callq 0x44c0
nop