// File: 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:
subq $0x268, %rsp            # imm = 0x268
movq %rdi, 0x260(%rsp)
movq %rsi, 0x258(%rsp)
movq %rdx, 0x250(%rsp)
movq %rcx, 0x248(%rsp)
movq 0x260(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq $0x0, 0x240(%rsp)
movq 0xb0(%rsp), %rdi
movq 0x240(%rsp), %rax
movq %rax, 0xa8(%rsp)
addq $0x68, %rdi
callq 0xd2a0
movq %rax, %rcx
movq 0xa8(%rsp), %rax
cmpq %rcx, %rax
jae 0x5a54
movq 0xb0(%rsp), %rdi
addq $0x68, %rdi
movq 0x240(%rsp), %rsi
callq 0xd360
addq $0x30, %rax
movq %rax, 0xa0(%rsp)
movq 0x250(%rsp), %rdi
addq $0x68, %rdi
movq 0x240(%rsp), %rsi
callq 0xd360
movq 0xa0(%rsp), %rdi
movq %rax, %rsi
addq $0x30, %rsi
callq 0xd2c0
testb $0x1, %al
jne 0x5900
jmp 0x5905
jmp 0x5a3b
movq 0x250(%rsp), %rdi
addq $0x68, %rdi
movq 0x240(%rsp), %rsi
callq 0xd360
cmpl $0x0, (%rax)
jne 0x59f3
movq 0xb0(%rsp), %rdi
movq 0x248(%rsp), %rax
movq %rax, 0x238(%rsp)
movq 0x238(%rsp), %rax
movq %rax, 0x88(%rsp)
addq $0x68, %rdi
movq 0x240(%rsp), %rsi
callq 0xd360
movq %rax, %rsi
addq $0x30, %rsi
leaq 0x210(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x4180
movq 0x88(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x258(%rsp), %rdx
callq 0x379c0
movb %al, 0x9f(%rsp)
jmp 0x59a2
leaq 0x210(%rsp), %rdi
callq 0x46e8
movb 0x9f(%rsp), %al
andb $0x1, %al
movb %al, 0x237(%rsp)
testb $0x1, 0x237(%rsp)
je 0x59f1
jmp 0x5a3b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x210(%rsp), %rdi
callq 0x46e8
jmp 0x608f
jmp 0x59f3
movq 0xb0(%rsp), %rdi
movq 0x258(%rsp), %rax
movq %rax, 0x80(%rsp)
addq $0x68, %rdi
movq 0x240(%rsp), %rsi
callq 0xd360
movq 0xb0(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq %rax, %rdx
movq %rdi, %rcx
addq $0x10, %rcx
callq 0x60a0
movq 0x240(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x240(%rsp)
jmp 0x5873
movq $0x0, 0x1f8(%rsp)
movq 0xb0(%rsp), %rdi
movq 0x1f8(%rsp), %rax
movq %rax, 0x78(%rsp)
addq $0x68, %rdi
callq 0xd2a0
movq %rax, %rcx
movq 0x78(%rsp), %rax
cmpq %rcx, %rax
jae 0x5c12
movq 0xb0(%rsp), %rdi
addq $0x68, %rdi
movq 0x1f8(%rsp), %rsi
callq 0xd360
addq $0x30, %rax
movq %rax, 0x70(%rsp)
movq 0x250(%rsp), %rdi
addq $0x68, %rdi
movq 0x1f8(%rsp), %rsi
callq 0xd360
movq 0x70(%rsp), %rdi
movq %rax, %rsi
addq $0x30, %rsi
callq 0xd2c0
testb $0x1, %al
jne 0x5ae1
jmp 0x5ae6
jmp 0x5bd3
movq 0x250(%rsp), %rdi
addq $0x68, %rdi
movq 0x1f8(%rsp), %rsi
callq 0xd360
cmpl $0x3, (%rax)
je 0x5b09
jmp 0x5bd3
movq 0xb0(%rsp), %rdi
addq $0x68, %rdi
movq %rdi, 0x58(%rsp)
movq 0x1f8(%rsp), %rsi
callq 0xd360
movq %rax, %rdi
addq $0x30, %rdi
leaq 0x4b5e6(%rip), %rsi     # 0x5111b
callq 0x44a0
movq 0x58(%rsp), %rdi
movq 0x1f8(%rsp), %rsi
callq 0xd360
movl $0x0, (%rax)
movq 0x258(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x250(%rsp), %rdi
addq $0x68, %rdi
movq 0x1f8(%rsp), %rsi
callq 0xd360
movq %rax, %rsi
addq $0x30, %rsi
leaq 0x1d8(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x4180
movq 0x58(%rsp), %rdi
movq 0x1f8(%rsp), %rsi
callq 0xd360
movq 0xb0(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq %rax, %rcx
movq %rdi, %r8
addq $0x10, %r8
callq 0x6310
jmp 0x5bc6
leaq 0x1d8(%rsp), %rdi
callq 0x46e8
movq 0x1f8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x1f8(%rsp)
jmp 0x5a60
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x1d8(%rsp), %rdi
callq 0x46e8
jmp 0x608f
movq $0x0, 0x1d0(%rsp)
movq 0xb0(%rsp), %rdi
movq 0x1d0(%rsp), %rax
movq %rax, 0x50(%rsp)
addq $0x68, %rdi
callq 0xd2a0
movq %rax, %rcx
movq 0x50(%rsp), %rax
cmpq %rcx, %rax
jae 0x5dd0
movq 0xb0(%rsp), %rdi
addq $0x68, %rdi
movq 0x1d0(%rsp), %rsi
callq 0xd360
addq $0x30, %rax
movq %rax, 0x48(%rsp)
movq 0x250(%rsp), %rdi
addq $0x68, %rdi
movq 0x1d0(%rsp), %rsi
callq 0xd360
movq 0x48(%rsp), %rdi
movq %rax, %rsi
addq $0x30, %rsi
callq 0xd2c0
testb $0x1, %al
jne 0x5c9f
jmp 0x5ca4
jmp 0x5d91
movq 0x250(%rsp), %rdi
addq $0x68, %rdi
movq 0x1d0(%rsp), %rsi
callq 0xd360
cmpl $0x3, (%rax)
jne 0x5cc7
jmp 0x5d91
movq 0xb0(%rsp), %rdi
addq $0x68, %rdi
movq %rdi, 0x30(%rsp)
movq 0x1d0(%rsp), %rsi
callq 0xd360
movq %rax, %rdi
addq $0x30, %rdi
leaq 0x4b428(%rip), %rsi     # 0x5111b
callq 0x44a0
movq 0x30(%rsp), %rdi
movq 0x1d0(%rsp), %rsi
callq 0xd360
movl $0x0, (%rax)
movq 0x258(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x250(%rsp), %rdi
addq $0x68, %rdi
movq 0x1d0(%rsp), %rsi
callq 0xd360
movq %rax, %rsi
addq $0x30, %rsi
leaq 0x1b0(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x4180
movq 0x30(%rsp), %rdi
movq 0x1d0(%rsp), %rsi
callq 0xd360
movq 0xb0(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %rax, %rcx
movq %rdi, %r8
addq $0x10, %r8
callq 0x6310
jmp 0x5d84
leaq 0x1b0(%rsp), %rdi
callq 0x46e8
movq 0x1d0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x1d0(%rsp)
jmp 0x5c1e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x46e8
jmp 0x608f
movq 0x250(%rsp), %rax
cmpl $0x0, 0x10(%rax)
je 0x6087
movq 0xb0(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x600d
movq 0x258(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x20(%rsp)
leaq 0x127(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x4600
movq 0x28(%rsp), %rdx
leaq 0x4b2ab(%rip), %rsi     # 0x510ce
leaq 0x128(%rsp), %rdi
callq 0x4f10
jmp 0x5e32
leaq 0xff(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x4600
movq 0x18(%rsp), %rdx
leaq 0x4b27c(%rip), %rsi     # 0x510cc
leaq 0x100(%rsp), %rdi
callq 0x4f10
jmp 0x5e5f
leaq 0xd7(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x4600
movq 0x10(%rsp), %rdx
leaq 0x4b24f(%rip), %rsi     # 0x510cc
leaq 0xd8(%rsp), %rdi
callq 0x4f10
jmp 0x5e8c
leaq 0x148(%rsp), %rdi
leaq 0x128(%rsp), %rsi
leaq 0x100(%rsp), %rdx
leaq 0xd8(%rsp), %rcx
callq 0xd3b0
jmp 0x5eb3
movq 0x20(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0xd380
jmp 0x5ec7
leaq 0x148(%rsp), %rdi
callq 0xd490
leaq 0xd8(%rsp), %rdi
callq 0x46e8
leaq 0xd7(%rsp), %rdi
callq 0x43b0
leaq 0x100(%rsp), %rdi
callq 0x46e8
leaq 0xff(%rsp), %rdi
callq 0x43b0
leaq 0x128(%rsp), %rdi
callq 0x46e8
leaq 0x127(%rsp), %rdi
callq 0x43b0
movq 0xb0(%rsp), %rax
movl $0x2, 0x10(%rax)
movq $0x0, 0x18(%rax)
jmp 0x600d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x5ffb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x5fe1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x5fc7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x5fba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x148(%rsp), %rdi
callq 0xd490
leaq 0xd8(%rsp), %rdi
callq 0x46e8
leaq 0xd7(%rsp), %rdi
callq 0x43b0
leaq 0x100(%rsp), %rdi
callq 0x46e8
leaq 0xff(%rsp), %rdi
callq 0x43b0
leaq 0x128(%rsp), %rdi
callq 0x46e8
leaq 0x127(%rsp), %rdi
callq 0x43b0
jmp 0x608f
movq 0xb0(%rsp), %rsi
movq %rsi, %r8
addq $0x10, %r8
movq %rsi, %rdx
addq $0x18, %rdx
movq 0x250(%rsp), %rax
movq 0x18(%rax), %rcx
leaq 0xb8(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x6770
movq 0x8(%rsp), %rsi
movq 0x258(%rsp), %rdi
callq 0xd4d0
jmp 0x6055
leaq 0xb8(%rsp), %rdi
callq 0xd550
jmp 0x6087
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0xd550
jmp 0x608f
addq $0x268, %rsp            # imm = 0x268
retq
movq 0x208(%rsp), %rdi
callq 0x45f0
nopl (%rax)