// File: /workspace/github_cpp_makefile_star2_O2/Bartor[P]kompilator/middle/abstract_assembler/AbstractAssembler.cpp
// Function: AbstractAssembler::resolve(AbstractIdentifier&, bool)
Resolution *AbstractAssembler::resolve(AbstractIdentifier &identifier, bool checkInit = true) {
    Variable *var = scopedVariables->resolveVariable(identifier.name);
    if (checkInit && !var->initialized) {
        std::cout << "   [w] Variable " << var->name << " may not have been initialized" << std::endl;
        warning = true;
    }
    var->initialized = true; // assume it was initialized at this point

    if (auto numVar = dynamic_cast<NumberVariable *>(var)) {
        try {
            VariableIdentifier &varId = dynamic_cast<VariableIdentifier &>(identifier); // check if the identifier was actually a var id

            return new Resolution(
                    *new InstructionList(),
                    numVar->getAddress(),
                    VARIABLE,
                    false,
                    0, // zero temp vars used
                    !numVar->readOnly // if it's readOnly, it's not writable -- used for iterators
            );
        } catch (std::bad_cast _) {
            throw "Trying to access a number variable like an array";
        }
    } else if (auto arrayVar = dynamic_cast<NumberArrayVariable *>(var)) {
        try { // ACCESS VALUE - a[0]
            AccessIdentifier &accId = dynamic_cast<AccessIdentifier &>(identifier);

            if ((accId.index < arrayVar->start || accId.index > arrayVar->end) && !arrayVar->warned) {
                std::cout << "   [w] Trying to access " + arrayVar->toString() + " at index " + std::to_string(accId.index) << "; you won't be warned about this array anymore" << std::endl;
                warning = true;
                arrayVar->warned = true;
            }

            ResolvableAddress &address = *new ResolvableAddress(arrayVar->getAddress().getAddress()); // copy address
            address.setOffset(accId.index - arrayVar->start); // set proper offset

            return new Resolution(
                    *new InstructionList(),
                    address,
                    CONSTANT_ARRAY,
                    false
            );
        } catch (std::bad_cast _) {
            try { // VARIABLE ACCESS VALUE - a[b]
                VariableAccessIdentifier &varAccId = dynamic_cast<VariableAccessIdentifier &>(identifier);

                ResolvableAddress &startValueAddress = constants->getConstant(arrayVar->start)->getAddress(); // arr start
                Variable *variable = scopedVariables->resolveVariable(varAccId.accessName); // "b" variable
                if (!variable->initialized) {
                    std::cout << "   [w] Variable " << variable->name << " may not have been initialized" << std::endl;
                    warning = true;
                }
                variable->initialized = true; // assume it was initialized at this point

                ResolvableAddress &arrAddressAddress = constants->getConstant(arrayVar->getAddress().getAddress())->getAddress();

                InstructionList &instructionList = *new InstructionList();

                Load *load = new Load(variable->getAddress()); // load value of b
                Sub *sub = new Sub(startValueAddress); // subtract value of starting index
                Add *add = new Add(arrAddressAddress); // add value of array address

                instructionList.append(load)
                        .append(sub)
                        .append(add);

                return new Resolution(
                        instructionList,
                        *new ResolvableAddress(),
                        VARIABLE_ARRAY,
                        true
                );
            } catch (std::bad_cast __) {
                throw "Trying to use array identifier as variable";
            }
        }
    } else throw "Variable resolution error";
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r12
movq 0x28(%rdi), %rdi
movq 0x8(%rsi), %rsi
callq 0x12b82
movq %rax, %r14
testl %ebx, %ebx
je 0xbbbf
cmpb $0x0, 0x10(%r14)
jne 0xbbbf
movq 0x14442(%rip), %rdi     # 0x1ffc8
leaq 0xc909(%rip), %rsi      # 0x18496
callq 0x51b0
movq 0x18(%r14), %rsi
movq %rax, %rdi
callq 0x5190
leaq 0xc902(%rip), %rsi      # 0x184a7
movq %rax, %rdi
callq 0x51b0
movq %rax, %rdi
callq 0x5100
leaq 0x147e6(%rip), %rax     # 0x203a2
movb $0x1, (%rax)
movb $0x1, 0x10(%r14)
leaq 0x13afd(%rip), %rsi     # 0x1f6c8
leaq 0x13b2e(%rip), %rdx     # 0x1f700
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0xbc54
movq %rax, %rbx
leaq 0x13a3d(%rip), %rsi     # 0x1f628
leaq 0x134fe(%rip), %rdx     # 0x1f0f0
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0xbe13
pushq $0x28
popq %rdi
callq 0x51c0
movq %rax, %r13
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r14
movq %rax, %rdi
callq 0x14d30
movq %rbx, %rdi
callq 0x12e14
movb 0x28(%rbx), %cl
xorb $0x1, %cl
movb %cl, (%r13)
movb $0x0, 0x1(%r13)
andq $0x0, 0x8(%r13)
movq %r14, 0x10(%r13)
movq %rax, 0x18(%r13)
movl $0x1, 0x20(%r13)
jmp 0xbdfe
movq %r12, -0x40(%rbp)
leaq 0x13a69(%rip), %rsi     # 0x1f6c8
leaq 0x13ada(%rip), %rdx     # 0x1f740
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0xbe18
movq %rax, %r14
leaq 0x139a5(%rip), %rsi     # 0x1f628
leaq 0x134ae(%rip), %rdx     # 0x1f138
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0xbe3b
movq %rax, %r12
movq 0x10(%rax), %rax
cmpq 0x30(%r14), %rax
jl 0xbcb4
cmpq 0x38(%r14), %rax
jle 0xbd90
cmpb $0x0, 0x40(%r14)
jne 0xbd90
movq (%r14), %rax
leaq -0xc0(%rbp), %rdi
movq %r14, %rsi
callq *(%rax)
leaq 0xc822(%rip), %rsi      # 0x184f7
leaq -0xe0(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
callq 0x9838
leaq 0xc821(%rip), %rdx      # 0x18510
leaq -0x80(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x9715
movq 0x10(%r12), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x97d8
leaq -0x60(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
callq 0x9767
movq 0x1429d(%rip), %rdi     # 0x1ffc8
leaq -0x60(%rbp), %rsi
callq 0x5190
leaq 0xc7e0(%rip), %rsi      # 0x1851b
movq %rax, %rdi
callq 0x51b0
movq %rax, %rdi
callq 0x5100
leaq -0x60(%rbp), %rdi
callq 0x5418
leaq -0xa0(%rbp), %rdi
callq 0x5418
leaq -0x80(%rbp), %rdi
callq 0x5418
leaq -0xe0(%rbp), %rdi
callq 0x5418
leaq -0xc0(%rbp), %rdi
callq 0x5418
leaq 0x1461a(%rip), %rax     # 0x203a2
movb $0x1, %cl
movb %cl, (%rax)
movb %cl, 0x40(%r14)
pushq $0x10
popq %rdi
callq 0x51c0
movq %rax, %rbx
movq %r14, %rdi
callq 0x12e14
movq %rax, %rdi
callq 0x12a60
andq $0x0, (%rbx)
movq %rax, 0x8(%rbx)
movq 0x10(%r12), %rsi
subq 0x30(%r14), %rsi
movq %rbx, %rdi
callq 0x12a56
pushq $0x28
popq %rdi
callq 0x51c0
movq %rax, %r13
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r12
movq %rax, %rdi
callq 0x14d30
movw $0x1, (%r13)
andq $0x0, 0x8(%r13)
movq %r12, 0x10(%r13)
movq %rbx, 0x18(%r13)
movl $0x2, 0x20(%r13)
movq %r13, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x53d0
pushq $0x8
popq %rdi
callq 0x5110
leaq 0xc74e(%rip), %rcx      # 0x18575
movq %rcx, (%rax)
movq 0x14177(%rip), %rsi     # 0x1ffa8
movq %rax, %rdi
xorl %edx, %edx
callq 0x5360
callq 0x53d0
movq %rdx, -0x38(%rbp)
movq %rax, -0x30(%rbp)
jmp 0xbea5
movq %rdx, -0x38(%rbp)
movq %rax, -0x30(%rbp)
jmp 0xbeb1
movq %rdx, -0x38(%rbp)
movq %rax, -0x30(%rbp)
jmp 0xbeba
movq %rdx, -0x38(%rbp)
movq %rax, -0x30(%rbp)
jmp 0xbec6
jmp 0xbf00
movq %rdx, -0x38(%rbp)
movq %rax, -0x30(%rbp)
pushq $0x20
popq %rsi
movq %r12, %rdi
callq 0x51d0
jmp 0xbe8a
movq %rdx, -0x38(%rbp)
movq %rax, -0x30(%rbp)
pushq $0x28
popq %rsi
movq %r13, %rdi
jmp 0xbef9
jmp 0xbf00
movq %rdx, -0x38(%rbp)
movq %rax, -0x30(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x5418
leaq -0xa0(%rbp), %rdi
callq 0x5418
leaq -0x80(%rbp), %rdi
callq 0x5418
leaq -0xe0(%rbp), %rdi
callq 0x5418
leaq -0xc0(%rbp), %rdi
callq 0x5418
jmp 0xbf08
movq %rdx, %rbx
movq %rax, -0x30(%rbp)
pushq $0x20
popq %rsi
movq %r14, %rdi
callq 0x51d0
jmp 0xc1be
movq %rdx, -0x38(%rbp)
movq %rax, -0x30(%rbp)
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0x51d0
jmp 0xbf08
movq %rdx, -0x38(%rbp)
movq %rax, -0x30(%rbp)
cmpl $0x1, -0x38(%rbp)
jne 0xc22f
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
callq 0x5250
movq 0x1408b(%rip), %rax     # 0x1ffb0
addq $0x10, %rax
movq %rax, -0x38(%rbp)
movq %rax, -0x60(%rbp)
movq %rbx, %rdi
callq 0x50d0
leaq 0x136e8(%rip), %rsi     # 0x1f628
leaq 0x13239(%rip), %rdx     # 0x1f180
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0xc101
movq %rax, %rbx
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rdi
movq 0x30(%r14), %rsi
callq 0x12924
movq %rax, %rdi
callq 0x124f6
movq %rax, %r15
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rdi
movq 0x10(%rbx), %rsi
callq 0x12b82
movq %rax, %r12
cmpb $0x0, 0x10(%rax)
jne 0xbfd4
movq 0x1402e(%rip), %rdi     # 0x1ffc8
leaq 0xc4f5(%rip), %rsi      # 0x18496
callq 0x51b0
movq 0x18(%r12), %rsi
movq %rax, %rdi
callq 0x5190
leaq 0xc4ed(%rip), %rsi      # 0x184a7
movq %rax, %rdi
callq 0x51b0
movq %rax, %rdi
callq 0x5100
leaq 0x143d1(%rip), %rax     # 0x203a2
movb $0x1, (%rax)
movb $0x1, 0x10(%r12)
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rbx
movq %r14, %rdi
callq 0x12e14
movq %rax, %rdi
callq 0x12a60
movq %rbx, %rdi
movq %rax, %rsi
callq 0x12924
movq %rax, %rdi
callq 0x124f6
movq %rax, %r14
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %rbx
movq %rax, %rdi
callq 0x14d30
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r13
movq %r12, %rdi
callq 0x12e14
orq $-0x1, 0x8(%r13)
movb $0x0, 0x10(%r13)
movq %rax, 0x18(%r13)
leaq 0x1383d(%rip), %rax     # 0x1f880
addq $0x10, %rax
movq %rax, (%r13)
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r12
orq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r15, 0x18(%rax)
leaq 0x139a6(%rip), %rax     # 0x1fa10
addq $0x10, %rax
movq %rax, (%r12)
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r15
orq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r14, 0x18(%rax)
leaq 0x1392f(%rip), %rax     # 0x1f9c0
addq $0x10, %rax
movq %rax, (%r15)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x14e9c
movq %rax, %rdi
movq %r12, %rsi
callq 0x14e9c
movq %rax, %rdi
movq %r15, %rsi
callq 0x14e9c
pushq $0x28
popq %rdi
callq 0x51c0
movq %rax, %r13
pushq $0x10
popq %rdi
callq 0x51c0
xorps %xmm0, %xmm0
movw $0x101, (%r13)          # imm = 0x101
andq $0x0, 0x8(%r13)
movups %xmm0, (%rax)
movq %rbx, 0x10(%r13)
movq %rax, 0x18(%r13)
movl $0x3, 0x20(%r13)
leaq -0x60(%rbp), %rdi
callq 0x53b0
callq 0x5320
jmp 0xbdfe
callq 0x53d0
movq %rdx, %r14
movq %rax, -0x30(%rbp)
pushq $0x28
jmp 0xc11c
jmp 0xc142
movq %rdx, %r14
movq %rax, -0x30(%rbp)
pushq $0x20
popq %rsi
movq %r13, %rdi
jmp 0xc131
jmp 0xc142
movq %rdx, %r14
movq %rax, -0x30(%rbp)
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0x51d0
jmp 0xc149
jmp 0xc142
jmp 0xc142
jmp 0xc142
jmp 0xc142
jmp 0xc142
movq %rdx, %r14
movq %rax, -0x30(%rbp)
pushq $0x1
popq %rax
cmpl %eax, %r14d
jne 0xc1a7
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
callq 0x5250
movq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
movq %rbx, %rdi
callq 0x50d0
pushq $0x8
popq %rdi
callq 0x5110
leaq 0xc3ce(%rip), %rcx      # 0x1854a
movq %rcx, (%rax)
movq 0x13e22(%rip), %rsi     # 0x1ffa8
movq %rax, %rdi
xorl %edx, %edx
callq 0x5360
jmp 0xc21d
movq %rax, -0x30(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x53b0
callq 0x5320
leaq -0x60(%rbp), %rdi
callq 0x53b0
callq 0x5320
jmp 0xc22f
movq %rdx, %rbx
movq %rax, -0x30(%rbp)
pushq $0x28
popq %rsi
movq %r13, %rdi
callq 0x51d0
jmp 0xc1d2
movq %rdx, %rbx
movq %rax, -0x30(%rbp)
cmpl $0x1, %ebx
jne 0xc22f
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
callq 0x5250
movq 0x13dc6(%rip), %rax     # 0x1ffb0
addq $0x10, %rax
movq %rax, -0x60(%rbp)
movq %rbx, %rdi
callq 0x50d0
pushq $0x8
popq %rdi
callq 0x5110
leaq 0xc2bd(%rip), %rcx      # 0x184c6
movq %rcx, (%rax)
movq 0x13d95(%rip), %rsi     # 0x1ffa8
movq %rax, %rdi
xorl %edx, %edx
callq 0x5360
movq %rax, -0x30(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x53b0
callq 0x5320
movq -0x30(%rbp), %rdi
callq 0x5390
movq %rax, %rdi
callq 0xa8e7