// File: Merulo[P]CompilerJFTT/src/Register/RegisterBlock.cpp
// Function: RegisterBlock::loadVariableFromMemory(Block&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Register&, Register&)
void RegisterBlock::loadVariableFromMemory(Block& b, std::string name, Register& r, Register& freeRegister)
{
    unsigned long long memoryCell = _symbolTable->getMemoryCell(name);

    bool found = false;
    for (auto& regs : _registers)
    {
        if ((regs.state == RegisterState::VARIABLE || regs.state == RegisterState::CONSTVARIABLE) && regs.variableName == name)
        {
            found = true;
            b.lines.push_back({"COPY", r.name, regs.name});
        }
    }

    if (!found)
    {
        if (_addressRegister.state == RegisterState::UNKNOWN)
        {
            b.lines.push_back({"SUB", "A", "A"});
            _addressRegister.state = RegisterState::CONST;
            _addressRegister.constValue = 0;
        }
        auto lines = generateNumberFrom(_addressRegister.constValue, memoryCell, freeRegister);

        lines.push_back({"LOAD", r.name});
        b.insert(lines);
    }
}

// Assembly:
subq $0x388, %rsp            # imm = 0x388
movq %rdx, 0x48(%rsp)
movq %rdi, 0x380(%rsp)
movq %rsi, 0x378(%rsp)
movq %rdx, 0x370(%rsp)
movq %rcx, 0x368(%rsp)
movq %r8, 0x360(%rsp)
movq 0x380(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0xdde0
movq 0x48(%rsp), %rsi
movq %rax, 0x50(%rsp)
leaq 0x338(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x4180
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x20200
movq %rax, 0x60(%rsp)
jmp 0xa758
leaq 0x338(%rsp), %rdi
callq 0x46e8
movq 0x60(%rsp), %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x358(%rsp)
movb $0x0, 0x32b(%rsp)
addq $0x68, %rax
movq %rax, 0x320(%rsp)
movq 0x320(%rsp), %rdi
callq 0xd710
movq %rax, 0x318(%rsp)
movq 0x320(%rsp), %rdi
callq 0xd740
movq %rax, 0x310(%rsp)
leaq 0x318(%rsp), %rdi
leaq 0x310(%rsp), %rsi
callq 0xd770
testb $0x1, %al
jne 0xa7d3
jmp 0xaa11
leaq 0x318(%rsp), %rdi
callq 0xd7b0
movq %rax, 0x308(%rsp)
movq 0x308(%rsp), %rax
cmpl $0x1, (%rax)
je 0xa806
movq 0x308(%rsp), %rax
cmpl $0x4, (%rax)
jne 0xa9fd
movq 0x48(%rsp), %rsi
movq 0x308(%rsp), %rdi
addq $0x30, %rdi
callq 0xd2c0
testb $0x1, %al
jne 0xa825
jmp 0xa9fd
movb $0x1, 0x32b(%rsp)
movq 0x378(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x30(%rsp)
leaq 0x27f(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x4600
movq 0x38(%rsp), %rdx
leaq 0x468a1(%rip), %rsi     # 0x510fd
leaq 0x280(%rsp), %rdi
callq 0x4f10
jmp 0xa86b
movq 0x368(%rsp), %rsi
addq $0x10, %rsi
leaq 0x258(%rsp), %rdi
callq 0x4180
jmp 0xa886
movq 0x308(%rsp), %rsi
addq $0x10, %rsi
leaq 0x238(%rsp), %rdi
callq 0x4180
jmp 0xa8a1
leaq 0x2a0(%rsp), %rdi
leaq 0x280(%rsp), %rsi
leaq 0x258(%rsp), %rdx
leaq 0x238(%rsp), %rcx
callq 0xd3b0
jmp 0xa8c8
movq 0x30(%rsp), %rdi
leaq 0x2a0(%rsp), %rsi
callq 0xd380
jmp 0xa8dc
leaq 0x2a0(%rsp), %rdi
callq 0xd490
leaq 0x238(%rsp), %rdi
callq 0x46e8
leaq 0x258(%rsp), %rdi
callq 0x46e8
leaq 0x280(%rsp), %rdi
callq 0x46e8
leaq 0x27f(%rsp), %rdi
callq 0x43b0
jmp 0xa9fd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
leaq 0x338(%rsp), %rdi
callq 0x46e8
jmp 0xae6a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0xa9eb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0xa9de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0xa9d1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0xa9c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
leaq 0x2a0(%rsp), %rdi
callq 0xd490
leaq 0x238(%rsp), %rdi
callq 0x46e8
leaq 0x258(%rsp), %rdi
callq 0x46e8
leaq 0x280(%rsp), %rdi
callq 0x46e8
leaq 0x27f(%rsp), %rdi
callq 0x43b0
jmp 0xae6a
jmp 0xa9ff
leaq 0x318(%rsp), %rdi
callq 0xd7c0
jmp 0xa7b5
testb $0x1, 0x32b(%rsp)
jne 0xae62
movq 0x40(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jne 0xac44
movq 0x378(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x20(%rsp)
leaq 0x1af(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x4600
movq 0x28(%rsp), %rdx
leaq 0x46671(%rip), %rsi     # 0x510ce
leaq 0x1b0(%rsp), %rdi
callq 0x4f10
jmp 0xaa6c
leaq 0x187(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x4600
movq 0x18(%rsp), %rdx
leaq 0x46642(%rip), %rsi     # 0x510cc
leaq 0x188(%rsp), %rdi
callq 0x4f10
jmp 0xaa99
leaq 0x15f(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x4600
movq 0x10(%rsp), %rdx
leaq 0x46615(%rip), %rsi     # 0x510cc
leaq 0x160(%rsp), %rdi
callq 0x4f10
jmp 0xaac6
leaq 0x1d0(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
leaq 0x188(%rsp), %rdx
leaq 0x160(%rsp), %rcx
callq 0xd3b0
jmp 0xaaed
movq 0x20(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0xd380
jmp 0xab01
leaq 0x1d0(%rsp), %rdi
callq 0xd490
leaq 0x160(%rsp), %rdi
callq 0x46e8
leaq 0x15f(%rsp), %rdi
callq 0x43b0
leaq 0x188(%rsp), %rdi
callq 0x46e8
leaq 0x187(%rsp), %rdi
callq 0x43b0
leaq 0x1b0(%rsp), %rdi
callq 0x46e8
leaq 0x1af(%rsp), %rdi
callq 0x43b0
movq 0x40(%rsp), %rax
movl $0x2, 0x10(%rax)
movq $0x0, 0x18(%rax)
jmp 0xac44
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0xac32
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0xac18
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0xabfe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0xabf1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0xd490
leaq 0x160(%rsp), %rdi
callq 0x46e8
leaq 0x15f(%rsp), %rdi
callq 0x43b0
leaq 0x188(%rsp), %rdi
callq 0x46e8
leaq 0x187(%rsp), %rdi
callq 0x43b0
leaq 0x1b0(%rsp), %rdi
callq 0x46e8
leaq 0x1af(%rsp), %rdi
callq 0x43b0
jmp 0xae6a
movq 0x40(%rsp), %rsi
movq %rsi, %rdx
addq $0x18, %rdx
movq 0x358(%rsp), %rcx
movq 0x360(%rsp), %r8
leaq 0x140(%rsp), %rdi
callq 0x6770
leaq 0xb7(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x4600
movq 0x8(%rsp), %rdx
leaq 0x4646d(%rip), %rsi     # 0x510f8
leaq 0xb8(%rsp), %rdi
callq 0x4f10
jmp 0xac9a
movq 0x368(%rsp), %rsi
addq $0x10, %rsi
leaq 0x90(%rsp), %rdi
callq 0x4180
jmp 0xacb5
leaq 0x6f(%rsp), %rdi
movq %rdi, (%rsp)
callq 0x4600
movq (%rsp), %rdx
leaq 0x4644d(%rip), %rsi     # 0x5111b
leaq 0x70(%rsp), %rdi
callq 0x4f10
jmp 0xacda
leaq 0xd8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
leaq 0x90(%rsp), %rdx
leaq 0x70(%rsp), %rcx
callq 0xd3b0
jmp 0xacfe
leaq 0x140(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0xd380
jmp 0xad15
leaq 0xd8(%rsp), %rdi
callq 0xd490
leaq 0x70(%rsp), %rdi
callq 0x46e8
leaq 0x6f(%rsp), %rdi
callq 0x43b0
leaq 0x90(%rsp), %rdi
callq 0x46e8
leaq 0xb8(%rsp), %rdi
callq 0x46e8
leaq 0xb7(%rsp), %rdi
callq 0x43b0
movq 0x378(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0xd4d0
jmp 0xad74
leaq 0x140(%rsp), %rdi
callq 0xd550
jmp 0xae62
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0xae30
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0xae23
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0xae0c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
jmp 0xae02
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0xd490
leaq 0x70(%rsp), %rdi
callq 0x46e8
leaq 0x6f(%rsp), %rdi
callq 0x43b0
leaq 0x90(%rsp), %rdi
callq 0x46e8
leaq 0xb8(%rsp), %rdi
callq 0x46e8
leaq 0xb7(%rsp), %rdi
callq 0x43b0
jmp 0xae53
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x330(%rsp)
movl %eax, 0x32c(%rsp)
leaq 0x140(%rsp), %rdi
callq 0xd550
jmp 0xae6a
addq $0x388, %rsp            # imm = 0x388
retq
movq 0x330(%rsp), %rdi
callq 0x45f0
nopw (%rax,%rax)