// File: /workspace/github_cpp_makefile_star2_O2/Merulo[P]CompilerJFTT/src/Register/RegisterBlock.cpp
// Function: RegisterBlock::saveVarTableToMemory(Block&, Register&, Register&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void RegisterBlock::saveVarTableToMemory(Block& b, Register& r, Register& freeRegister, std::string name)
{
    std::string array = r.variableName.substr(0, r.variableName.find("("));
    unsigned long memoryCell = _symbolTable->getMemoryCell(array);
    unsigned long long shift = _symbolTable->getTableShift(array);

    Register& regToSub = getRegister("", b, {r, freeRegister}, false, true);

    loadVariableFromMemory(b, name, freeRegister, r);
    regToSub.variableName = "";
    regToSub.state = RegisterState::UNKNOWN;

    auto linesToAdd = generateNumber(shift, memoryCell, regToSub);
    _addressRegister.state = RegisterState::UNKNOWN;    
    b.insert(linesToAdd);

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %r8, 0x10(%rsp)
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x30(%rdx), %r13
leaq 0x1d3ba(%rip), %rsi     # 0x245bd
movq %r13, %rdi
xorl %edx, %edx
callq 0x4520
leaq 0x130(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x42a0
movq (%r15), %r13
leaq 0x110(%rsp), %rdi
movq %rbp, %rsi
callq 0x4390
leaq 0x110(%rsp), %rsi
movq %r13, %rdi
callq 0x100f2
movq %rax, %rbp
leaq 0x110(%rsp), %rdi
callq 0x4548
movq (%r15), %r13
leaq 0xf0(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x4390
leaq 0xf0(%rsp), %rsi
movq %r13, %rdi
callq 0x109a6
movq %rax, %r13
leaq 0xf0(%rsp), %rdi
callq 0x4548
movq %r13, 0x18(%rsp)
leaq 0x1be47(%rip), %rsi     # 0x230e3
leaq 0xd0(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x4a54
leaq 0x150(%rsp), %rsi
movq %r14, (%rsi)
movq %r12, 0x8(%rsi)
leaq 0x38(%rsp), %rdi
pushq $0x2
popq %rdx
leaq 0xe(%rsp), %rcx
callq 0x8492
leaq 0xd0(%rsp), %rsi
leaq 0x38(%rsp), %rcx
pushq $0x1
popq %r9
movq %r15, %rdi
movq %rbx, %rdx
xorl %r8d, %r8d
callq 0x5ec6
movq %rax, %r13
leaq 0x38(%rsp), %rdi
callq 0x9106
leaq 0xd0(%rsp), %rdi
callq 0x4548
leaq 0xb0(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x4390
leaq 0xb0(%rsp), %rdx
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x7502
leaq 0xb0(%rsp), %rdi
callq 0x4548
leaq 0x30(%r13), %rdi
leaq 0x1bd9b(%rip), %rsi     # 0x230e3
callq 0x44e0
andl $0x0, (%r13)
leaq 0x20(%rsp), %rdi
leaq 0x18(%rsp), %rdx
movq %r15, %rsi
movq %rbp, %rcx
movq %r13, %r8
callq 0x6704
andl $0x0, 0x10(%r15)
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x821e
leaq 0x1bd37(%rip), %rsi     # 0x230ba
leaq 0x90(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x4a54
addq $0x10, %r14
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x4390
leaq 0x1bd36(%rip), %rsi     # 0x230e3
leaq 0x50(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x4a54
leaq 0x150(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0x816a
addq $0x20, %rbx
leaq 0x150(%rsp), %rsi
movq %rbx, %rdi
callq 0x8f90
leaq 0x150(%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), %rdi
callq 0x8236
leaq 0x130(%rsp), %rdi
callq 0x4548
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
callq 0x81fe
jmp 0x745b
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4548
jmp 0x746a
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x4548
jmp 0x7479
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x4548
jmp 0x748d
jmp 0x748a
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x8236
jmp 0x74ec
jmp 0x74e9
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
jmp 0x74e0
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x9106
jmp 0x74ba
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
jmp 0x74e0
jmp 0x74e9
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
jmp 0x74e0
jmp 0x74e9
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x4548
jmp 0x74ec
jmp 0x74e9
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x4548
movq %rbx, %rdi
callq 0x4490
nop