// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rcx, %r12
movq %rdx, %rbp
movq %rsi, %r15
movq %rdi, %r13
movq (%rdi), %rbx
leaq 0x198(%rsp), %r14
movq %r14, %rdi
movq %rdx, %rsi
callq 0x4390
movq %rbx, %rdi
movq %r14, %rsi
callq 0x100f2
movq %rax, 0x48(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x4548
movq %r13, %rax
movq 0x68(%r13), %r13
movq %rax, 0x38(%rsp)
movq 0x70(%rax), %rbx
movq %r15, 0x40(%rsp)
leaq 0x20(%r15), %rax
movq %rax, 0x10(%rsp)
addq $0x10, %r12
movq %r12, 0x18(%rsp)
xorl %r12d, %r12d
leaq 0x1b8(%rsp), %r15
leaq 0x130(%rsp), %r14
cmpq %rbx, %r13
je 0x7649
movl (%r13), %eax
cmpl $0x4, %eax
je 0x75a7
cmpl $0x1, %eax
jne 0x7640
leaq 0x30(%r13), %rdi
movq %rbp, %rsi
callq 0x8141
testb %al, %al
je 0x7640
leaq 0x1d8(%rsp), %r12
movq %r12, %rdi
leaq 0x1baf8(%rip), %rsi     # 0x230c5
leaq 0x20(%rsp), %rdx
callq 0x4a54
leaq 0x110(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x4390
leaq 0x10(%r13), %rsi
movq %r15, %rdi
callq 0x4390
movq %r14, %rdi
movq %r12, %rsi
leaq 0x110(%rsp), %rdx
movq %r15, %rcx
callq 0x816a
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x8f90
movq %r14, %rdi
callq 0x81fe
movq %r15, %rdi
callq 0x4548
leaq 0x110(%rsp), %rdi
callq 0x4548
movq %r12, %rdi
callq 0x4548
movb $0x1, %r12b
addq $0x58, %r13
jmp 0x758c
testb $0x1, %r12b
jne 0x77f8
movq 0x38(%rsp), %r15
cmpl $0x0, 0x10(%r15)
movq 0x40(%rsp), %rbx
movq 0x18(%rsp), %r14
jne 0x7730
leaq 0x1ba22(%rip), %rsi     # 0x23096
leaq 0xf0(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x4a54
leaq 0x1ba07(%rip), %rsi     # 0x23094
leaq 0xd0(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x4a54
leaq 0x1b9ee(%rip), %rsi     # 0x23094
leaq 0xb0(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x4a54
leaq 0x130(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
callq 0x816a
leaq 0x130(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x8f90
leaq 0x130(%rsp), %rdi
callq 0x81fe
leaq 0xb0(%rsp), %rdi
callq 0x4548
leaq 0xd0(%rsp), %rdi
callq 0x4548
leaq 0xf0(%rsp), %rdi
callq 0x4548
movl $0x2, 0x10(%r15)
andq $0x0, 0x18(%r15)
addq $0x18, %r15
leaq 0x20(%rsp), %rdi
movq %r15, %rdx
movq 0x48(%rsp), %rcx
callq 0x5530
leaq 0x1b973(%rip), %rsi     # 0x230c0
leaq 0x90(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x4a54
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x4390
leaq 0x1b970(%rip), %rsi     # 0x230e3
leaq 0x50(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x4a54
leaq 0x130(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0x816a
leaq 0x20(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x8f90
leaq 0x130(%rsp), %rdi
callq 0x81fe
leaq 0x50(%rsp), %rdi
callq 0x4548
leaq 0x70(%rsp), %rdi
callq 0x4548
leaq 0x90(%rsp), %rdi
callq 0x4548
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x821e
leaq 0x20(%rsp), %rdi
callq 0x8236
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x81fe
jmp 0x781f
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x4548
jmp 0x7831
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x4548
jmp 0x7843
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
jmp 0x78f8
jmp 0x78bc
jmp 0x7896
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x81fe
jmp 0x7869
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4548
jmp 0x7878
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x4548
jmp 0x7887
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x4548
jmp 0x7899
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x8236
jmp 0x78fd
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
jmp 0x78f8
movq %rax, %rbx
jmp 0x78e3
movq %rax, %rbx
jmp 0x78f0
movq %rax, %rbx
jmp 0x78fd
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x81fe
jmp 0x78d6
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
callq 0x4548
leaq 0x110(%rsp), %rdi
callq 0x4548
leaq 0x1d8(%rsp), %rdi
callq 0x4548
movq %rbx, %rdi
callq 0x4490
nop