// File: /workspace/github_cpp_makefile_star2_O2/Merulo[P]CompilerJFTT/src/Register/RegisterBlock.cpp
// Function: RegisterBlock::getRegistersForOperation(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Block&, std::vector<std::reference_wrapper<Register>, std::allocator<std::reference_wrapper<Register>>>)
Register& RegisterBlock::getRegistersForOperation(std::string name, Block& b, std::vector<std::reference_wrapper<Register>> usedRegisters)
{
    for(auto& reg : _registers)
    {
        if (std::find_if(usedRegisters.begin(), usedRegisters.end(), [reg](auto i)
                {
                    return i.get() == reg;
                }) != usedRegisters.end())
        {      
            continue;
        }  
        if (reg.variableName == name)
        {
            saveToMemory(b, reg, _addressRegister);
            return reg;
        }
    }

    auto& reg = _registers[_currentRegister];
    _currentRegister++;
    if (_currentRegister == _registers.size())
    {
        _currentRegister = 0;
    }

    if (std::find_if(usedRegisters.begin(), usedRegisters.end(), [reg](auto i)
            {
                return i.get() == reg;
            }) == usedRegisters.end())
    {
        saveToMemory(b, reg, _addressRegister);
        loadFromMemory(b, name, reg, _addressRegister);
        return reg;
    }    

    return getRegistersForOperation(name, b, usedRegisters);     
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %rcx, %r13
movq %rdx, 0x20(%rsp)
movq %rsi, 0x28(%rsp)
movq 0x68(%rdi), %r12
movq %rdi, 0x18(%rsp)
movq 0x70(%rdi), %rax
movq %rax, 0x30(%rsp)
leaq 0xe8(%rsp), %r14
leaq 0x90(%rsp), %r15
movq %rcx, 0x8(%rsp)
cmpq 0x30(%rsp), %r12
je 0x5b61
movq %r14, %rbp
movq (%r13), %r14
movq 0x8(%r13), %rax
movq %rax, 0x10(%rsp)
leaq 0x1f0(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x8050
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x8050
movq %r12, %rbx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x8f56
leaq 0x140(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x8f56
movq %r15, %rdi
callq 0x80ca
movq %r15, %rdi
movq %r12, %rsi
callq 0x8050
movq 0x10(%rsp), %r13
subq %r14, %r13
movq %r13, %r12
sarq $0x5, %r12
testq %r12, %r12
jle 0x5a7f
movq %r15, %rdi
movq %r14, %rsi
callq 0x7f6c
testb %al, %al
jne 0x5ab2
leaq 0x8(%r14), %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7f6c
testb %al, %al
jne 0x5ae2
leaq 0x10(%r14), %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7f6c
testb %al, %al
jne 0x5ae2
leaq 0x18(%r14), %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7f6c
testb %al, %al
jne 0x5ae2
addq $0x20, %r14
decq %r12
addq $-0x20, %r13
jmp 0x5a19
sarq $0x3, %r13
cmpq $0x1, %r13
je 0x5ad3
cmpq $0x2, %r13
je 0x5ab7
cmpq $0x3, %r13
jne 0x5add
movq %r15, %rdi
movq %r14, %rsi
callq 0x7f6c
testb %al, %al
movq %rbx, %r12
movq 0x8(%rsp), %r13
jne 0x5ace
addq $0x8, %r14
jmp 0x5abf
movq %r14, %rbp
jmp 0x5ae2
movq %rbx, %r12
movq 0x8(%rsp), %r13
movq %r15, %rdi
movq %r14, %rsi
callq 0x7f6c
testb %al, %al
je 0x5b45
movq %r14, %rbp
jmp 0x5aea
movq %rbx, %r12
movq 0x8(%rsp), %r13
jmp 0x5b49
movq 0x10(%rsp), %rbp
movq %rbx, %r12
movq 0x8(%rsp), %r13
movq %r15, %rdi
callq 0x80ca
leaq 0x140(%rsp), %rdi
callq 0x80ca
leaq 0xe8(%rsp), %r14
movq %r14, %rdi
callq 0x80ca
movq 0x8(%r13), %rbx
leaq 0x1f0(%rsp), %rdi
callq 0x80ca
cmpq %rbx, %rbp
jne 0x5b3c
leaq 0x30(%r12), %rdi
movq 0x28(%rsp), %rsi
callq 0x8141
testb %al, %al
jne 0x5c81
addq $0x58, %r12
jmp 0x599d
addq $0x8, %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x7f6c
testb %al, %al
movq 0x10(%rsp), %rbp
cmovneq %r14, %rbp
jmp 0x5aea
movq 0x18(%rsp), %rdi
movl 0x80(%rdi), %ecx
imulq $0x58, %rcx, %rbx
movq 0x68(%rdi), %rdx
movq 0x70(%rdi), %rax
addq %rdx, %rbx
incl %ecx
subq %rdx, %rax
pushq $0x58
popq %rsi
cqto
idivq %rsi
xorl %edx, %edx
cmpq %rcx, %rax
cmovnel %ecx, %edx
movl %edx, 0x80(%rdi)
movq (%r13), %r14
movq 0x8(%r13), %rbp
leaq 0x198(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8050
leaq 0xe8(%rsp), %rdi
movq %r15, %rsi
callq 0x8050
leaq 0x90(%rsp), %r15
leaq 0xe8(%rsp), %rsi
movq %r15, %rdi
callq 0x8f56
leaq 0x140(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x8f56
movq %r15, %rdi
callq 0x80ca
leaq 0x90(%rsp), %rdi
movq %r12, %rsi
callq 0x8050
movq %rbp, 0x10(%rsp)
subq %r14, %rbp
movq %rbp, %r13
sarq $0x5, %r13
leaq 0x90(%rsp), %r12
testq %r13, %r13
jle 0x5c9c
movq %r12, %rdi
movq %r14, %rsi
callq 0x7f80
testb %al, %al
jne 0x5cec
leaq 0x8(%r14), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x7f80
testb %al, %al
jne 0x5cef
leaq 0x10(%r14), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x7f80
testb %al, %al
jne 0x5cef
leaq 0x18(%r14), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x7f80
testb %al, %al
jne 0x5cef
addq $0x20, %r14
decq %r13
addq $-0x20, %rbp
jmp 0x5c1b
movq 0x18(%rsp), %rdi
leaq 0x10(%rdi), %rcx
movq 0x20(%rsp), %rsi
movq %r12, %rdx
callq 0x51c8
jmp 0x5dcc
sarq $0x3, %rbp
cmpq $0x1, %rbp
je 0x5e02
cmpq $0x2, %rbp
movq %rbx, %r12
movq 0x8(%rsp), %r13
je 0x5de1
cmpq $0x3, %rbp
jne 0x5e11
leaq 0x90(%rsp), %rdi
movq %r14, %rsi
callq 0x7f80
testb %al, %al
movq 0x10(%rsp), %r15
jne 0x5dfa
addq $0x8, %r14
jmp 0x5de6
movq %r14, %r15
movq %rbx, %r12
movq 0x8(%rsp), %r13
leaq 0x90(%rsp), %rdi
callq 0x80ca
leaq 0x140(%rsp), %rdi
callq 0x80ca
leaq 0xe8(%rsp), %rdi
callq 0x80ca
movq 0x8(%r13), %rbx
leaq 0x198(%rsp), %rdi
callq 0x80ca
cmpq %rbx, %r15
je 0x5d7d
leaq 0x50(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x4390
leaq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x834a
leaq 0x50(%rsp), %rsi
leaq 0x38(%rsp), %rcx
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
callq 0x5958
movq %rax, %r12
leaq 0x38(%rsp), %rdi
callq 0x9106
leaq 0x50(%rsp), %rdi
jmp 0x5dc7
movq 0x18(%rsp), %r13
leaq 0x10(%r13), %r14
movq %r13, %rdi
movq 0x20(%rsp), %rbx
movq %rbx, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x51c8
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq 0x28(%rsp), %rsi
callq 0x4390
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r14, %r8
callq 0x5316
leaq 0x70(%rsp), %rdi
callq 0x4548
movq %r12, %rax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %r15
leaq 0x90(%rsp), %rdi
movq %r14, %rsi
callq 0x7f80
testb %al, %al
je 0x5e1b
movq %r14, %r15
jmp 0x5cf7
movq %rbx, %r12
movq 0x8(%rsp), %r13
movq 0x10(%rsp), %r15
jmp 0x5e1f
movq 0x10(%rsp), %r15
jmp 0x5cf7
addq $0x8, %r14
leaq 0x90(%rsp), %rdi
movq %r14, %rsi
callq 0x7f80
testb %al, %al
cmovneq %r14, %r15
jmp 0x5cf7
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x5e5b
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x9106
jmp 0x5e56
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4548
jmp 0x5ebd
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x80ca
leaq 0xe8(%rsp), %rdi
callq 0x80ca
jmp 0x5e84
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
jmp 0x5eb8
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x80ca
leaq 0xe8(%rsp), %rdi
callq 0x80ca
jmp 0x5eb0
movq %rax, %rbx
leaq 0x1f0(%rsp), %rdi
callq 0x80ca
movq %rbx, %rdi
callq 0x4490
nop