// File: 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:
subq $0x2b8, %rsp            # imm = 0x2B8
movq %r8, 0x60(%rsp)
movq %rdi, 0x2b0(%rsp)
movq %rsi, 0x2a8(%rsp)
movq %rdx, 0x2a0(%rsp)
movq %rcx, 0x298(%rsp)
movq %r8, 0x290(%rsp)
movq 0x2b0(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x2a0(%rsp), %rdi
addq $0x30, %rdi
movq %rdi, 0x68(%rsp)
leaq 0x48606(%rip), %rsi     # 0x526cf
xorl %eax, %eax
movl %eax, %edx
movq %rdx, 0x70(%rsp)
callq 0x46b0
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq %rax, %rcx
leaq 0x270(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x4350
movq 0x78(%rsp), %rdi
callq 0xdde0
movq 0x80(%rsp), %rsi
movq %rax, 0x88(%rsp)
leaq 0x248(%rsp), %rdi
callq 0x4180
jmp 0xa122
movq 0x88(%rsp), %rdi
leaq 0x248(%rsp), %rsi
callq 0x20200
movq %rax, 0x58(%rsp)
jmp 0xa13e
leaq 0x248(%rsp), %rdi
callq 0x46e8
movq 0x58(%rsp), %rax
movq 0x78(%rsp), %rdi
movq %rax, 0x268(%rsp)
callq 0xdde0
movq %rax, 0x50(%rsp)
leaq 0x210(%rsp), %rdi
leaq 0x270(%rsp), %rsi
callq 0x4180
jmp 0xa17e
movq 0x50(%rsp), %rdi
leaq 0x210(%rsp), %rsi
callq 0x21630
movq %rax, 0x48(%rsp)
jmp 0xa197
leaq 0x210(%rsp), %rdi
callq 0x46e8
movq 0x48(%rsp), %rax
movq %rax, 0x230(%rsp)
leaq 0x1e7(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x4600
movq 0x40(%rsp), %rdx
leaq 0x46f4c(%rip), %rsi     # 0x5111b
leaq 0x1e8(%rsp), %rdi
callq 0x4f10
jmp 0xa1de
movq 0x2a8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x2a0(%rsp), %rsi
leaq 0x1a8(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0xddf0
leaq 0x1b0(%rsp), %rdi
movq 0x298(%rsp), %rsi
callq 0xddf0
movq 0x30(%rsp), %rax
movq %rax, 0x1b8(%rsp)
movq $0x2, 0x1c0(%rsp)
leaq 0x1a7(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0xde20
movq 0x38(%rsp), %rcx
movq 0x1b8(%rsp), %rsi
movq 0x1c0(%rsp), %rdx
leaq 0x1c8(%rsp), %rdi
callq 0xde30
jmp 0xa269
movq 0x28(%rsp), %rdx
movq 0x78(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
leaq 0x1c8(%rsp), %rcx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x7ac0
movq %rax, 0x20(%rsp)
jmp 0xa298
leaq 0x1c8(%rsp), %rdi
callq 0xd9c0
leaq 0x1a7(%rsp), %rdi
callq 0xdec0
leaq 0x1e8(%rsp), %rdi
callq 0x46e8
leaq 0x1e7(%rsp), %rdi
callq 0x43b0
movq 0x20(%rsp), %rax
movq 0x60(%rsp), %rsi
movq %rax, 0x208(%rsp)
movq 0x2a8(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0x180(%rsp), %rdi
callq 0x4180
jmp 0xa2fa
movq 0x18(%rsp), %rsi
movq 0x78(%rsp), %rdi
movq 0x298(%rsp), %rcx
movq 0x2a0(%rsp), %r8
leaq 0x180(%rsp), %rdx
callq 0xa6e0
jmp 0xa323
leaq 0x180(%rsp), %rdi
callq 0x46e8
movq 0x208(%rsp), %rdi
addq $0x30, %rdi
leaq 0x46dd8(%rip), %rsi     # 0x5111b
callq 0x44a0
jmp 0xa34a
movq 0x78(%rsp), %rsi
movq 0x208(%rsp), %rax
movl $0x0, (%rax)
movq 0x268(%rsp), %rcx
movq 0x208(%rsp), %r8
leaq 0x168(%rsp), %rdi
leaq 0x230(%rsp), %rdx
callq 0x8830
jmp 0xa384
movq 0x78(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x2a8(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0xd4d0
jmp 0xa3a7
movq 0x2a8(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x8(%rsp)
leaq 0xdf(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x4600
movq 0x10(%rsp), %rdx
leaq 0x46d1c(%rip), %rsi     # 0x510f2
leaq 0xe0(%rsp), %rdi
callq 0x4f10
jmp 0xa3e5
movq 0x2a0(%rsp), %rsi
addq $0x10, %rsi
leaq 0xb8(%rsp), %rdi
callq 0x4180
jmp 0xa400
leaq 0x97(%rsp), %rdi
movq %rdi, (%rsp)
callq 0x4600
movq (%rsp), %rdx
leaq 0x46cff(%rip), %rsi     # 0x5111b
leaq 0x98(%rsp), %rdi
callq 0x4f10
jmp 0xa42b
leaq 0x100(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
leaq 0x98(%rsp), %rcx
callq 0xd3b0
jmp 0xa452
movq 0x8(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0xd380
jmp 0xa466
leaq 0x100(%rsp), %rdi
callq 0xd490
leaq 0x98(%rsp), %rdi
callq 0x46e8
leaq 0x97(%rsp), %rdi
callq 0x43b0
leaq 0xb8(%rsp), %rdi
callq 0x46e8
leaq 0xe0(%rsp), %rdi
callq 0x46e8
leaq 0xdf(%rsp), %rdi
callq 0x43b0
leaq 0x168(%rsp), %rdi
callq 0xd550
leaq 0x270(%rsp), %rdi
callq 0x46e8
addq $0x2b8, %rsp            # imm = 0x2B8
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
jmp 0xa6bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
leaq 0x248(%rsp), %rdi
callq 0x46e8
jmp 0xa6bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
leaq 0x210(%rsp), %rdi
callq 0x46e8
jmp 0xa6bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
jmp 0xa5a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
jmp 0xa588
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
leaq 0x1c8(%rsp), %rdi
callq 0xd9c0
leaq 0x1a7(%rsp), %rdi
callq 0xdec0
leaq 0x1e8(%rsp), %rdi
callq 0x46e8
leaq 0x1e7(%rsp), %rdi
callq 0x43b0
jmp 0xa6bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
leaq 0x180(%rsp), %rdi
callq 0x46e8
jmp 0xa6bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
jmp 0xa6b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
jmp 0xa6a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
jmp 0xa696
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
jmp 0xa67c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
jmp 0xa66f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
leaq 0x100(%rsp), %rdi
callq 0xd490
leaq 0x98(%rsp), %rdi
callq 0x46e8
leaq 0x97(%rsp), %rdi
callq 0x43b0
leaq 0xb8(%rsp), %rdi
callq 0x46e8
leaq 0xe0(%rsp), %rdi
callq 0x46e8
leaq 0xdf(%rsp), %rdi
callq 0x43b0
leaq 0x168(%rsp), %rdi
callq 0xd550
leaq 0x270(%rsp), %rdi
callq 0x46e8
movq 0x240(%rsp), %rdi
callq 0x45f0
nopw (%rax,%rax)