// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movq %rsi, 0x10(%rsp)
movq %rdi, %r14
leaq 0x10(%rdi), %rax
movq %rax, 0x18(%rsp)
pushq $0x30
popq %r12
xorl %ebp, %ebp
pushq $0x58
popq %rbx
leaq 0x138(%rsp), %r13
movq %rdx, 0x20(%rsp)
movq 0x68(%r14), %rdi
movq 0x70(%r14), %rcx
movq %rcx, %rax
subq %rdi, %rax
cqto
idivq %rbx
cmpq %rax, %rbp
jae 0x4ee3
addq %r12, %rdi
movq 0x68(%r15), %rsi
addq %r12, %rsi
callq 0x8141
testb %al, %al
jne 0x4ed7
movq 0x68(%r15), %rax
cmpl $0x0, -0x30(%rax,%r12)
jne 0x4eb9
movq %rbx, %r15
movq 0x68(%r14), %rsi
addq %r12, %rsi
movq %r13, %rdi
callq 0x4390
movq 0x28(%rsp), %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rdx
callq 0x1934a
movl %eax, %ebx
movq %r13, %rdi
callq 0x4548
testb %bl, %bl
movq %r15, %rbx
movq 0x20(%rsp), %r15
jne 0x4ed7
movq 0x68(%r14), %rax
leaq (%rax,%r12), %rdx
addq $-0x30, %rdx
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rcx
callq 0x51c8
incq %rbp
addq $0x58, %r12
jmp 0x4e44
pushq $0x30
popq %r13
xorl %r12d, %r12d
leaq 0x118(%rsp), %rbp
movq %rcx, %rax
subq %rdi, %rax
cqto
idivq %rbx
cmpq %rax, %r12
jae 0x4f8e
addq %r13, %rdi
movq 0x68(%r15), %rsi
addq %r13, %rsi
callq 0x8141
testb %al, %al
jne 0x4f7a
movq 0x68(%r15), %rax
cmpl $0x3, -0x30(%rax,%r13)
jne 0x4f7a
movq 0x68(%r14), %rdi
addq %r13, %rdi
leaq 0x1e1b0(%rip), %rsi     # 0x230e3
callq 0x44e0
movq 0x68(%r14), %rax
andl $0x0, -0x30(%rax,%r13)
movq 0x68(%r15), %rsi
addq %r13, %rsi
movq %rbp, %rdi
callq 0x4390
movq 0x68(%r14), %rax
leaq (%rax,%r13), %rcx
addq $-0x30, %rcx
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq 0x18(%rsp), %r8
callq 0x5316
movq %rbp, %rdi
callq 0x4548
incq %r12
movq 0x68(%r14), %rdi
movq 0x70(%r14), %rcx
addq $0x58, %r13
jmp 0x4ef2
pushq $0x30
popq %r13
xorl %r12d, %r12d
leaq 0xf8(%rsp), %rbp
subq %rdi, %rcx
movq %rcx, %rax
cqto
idivq %rbx
cmpq %rax, %r12
jae 0x5039
addq %r13, %rdi
movq 0x68(%r15), %rsi
addq %r13, %rsi
callq 0x8141
testb %al, %al
jne 0x5025
movq 0x68(%r15), %rax
cmpl $0x3, -0x30(%rax,%r13)
je 0x5025
movq 0x68(%r14), %rdi
addq %r13, %rdi
leaq 0x1e105(%rip), %rsi     # 0x230e3
callq 0x44e0
movq 0x68(%r14), %rax
andl $0x0, -0x30(%rax,%r13)
movq 0x68(%r15), %rsi
addq %r13, %rsi
movq %rbp, %rdi
callq 0x4390
movq 0x68(%r14), %rax
leaq (%rax,%r13), %rcx
addq $-0x30, %rcx
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq 0x18(%rsp), %r8
callq 0x5316
movq %rbp, %rdi
callq 0x4548
incq %r12
movq 0x68(%r14), %rdi
movq 0x70(%r14), %rcx
addq $0x58, %r13
jmp 0x4f9d
cmpl $0x0, 0x10(%r15)
je 0x5133
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x50fe
leaq 0x1e03d(%rip), %rsi     # 0x23096
leaq 0x70(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x4a54
leaq 0x1e025(%rip), %rsi     # 0x23094
leaq 0x50(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x4a54
leaq 0x1e00f(%rip), %rsi     # 0x23094
leaq 0x30(%rsp), %rdi
leaq 0xd(%rsp), %rdx
callq 0x4a54
leaq 0x90(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x816a
movq 0x10(%rsp), %rax
leaq 0x20(%rax), %rdi
leaq 0x90(%rsp), %rsi
callq 0x8f90
leaq 0x90(%rsp), %rdi
callq 0x81fe
leaq 0x30(%rsp), %rdi
callq 0x4548
leaq 0x50(%rsp), %rdi
callq 0x4548
leaq 0x70(%rsp), %rdi
callq 0x4548
movl $0x2, 0x10(%r14)
andq $0x0, 0x18(%r14)
addq $0x18, %r14
movq 0x18(%r15), %rcx
leaq 0x90(%rsp), %r15
movq %r15, %rdi
movq %r14, %rdx
callq 0x5530
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x821e
leaq 0x90(%rsp), %rdi
callq 0x8236
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x81fe
jmp 0x515a
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x4548
jmp 0x5169
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4548
jmp 0x5178
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x51bb
movq %rax, %rbx
jmp 0x51c0
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x8236
jmp 0x51c0
movq %rax, %rbx
leaq 0x138(%rsp), %rdi
jmp 0x51bb
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
jmp 0x51bb
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x4548
movq %rbx, %rdi
callq 0x4490