// File: /workspace/github_cpp_makefile_star2_O3/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 0x17c58
movq %rax, %r14
testl %ebx, %ebx
je 0xffc1
cmpb $0x0, 0x10(%r14)
jne 0xffc1
movq 0x17076(%rip), %rbx     # 0x26fc8
leaq 0xf53d(%rip), %rsi      # 0x1f496
movl $0x10, %edx
movq %rbx, %rdi
callq 0x5250
movq 0x18(%r14), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %rbx, %rdi
callq 0x5250
movq %rax, %rbx
leaq 0xf524(%rip), %rsi      # 0x1f4a7
movl $0x1e, %edx
movq %rax, %rdi
callq 0x5250
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x51d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5140
leaq 0x173f4(%rip), %rax     # 0x273b2
movb $0x1, (%rax)
movb $0x1, 0x10(%r14)
leaq 0x166f3(%rip), %rsi     # 0x266c0
leaq 0x16724(%rip), %rdx     # 0x266f8
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x1005d
movq %rax, %rbx
leaq 0x16633(%rip), %rsi     # 0x26620
leaq 0x160f4(%rip), %rdx     # 0x260e8
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x10469
movl $0x28, %edi
callq 0x51f0
movq %rax, %r13
movl $0x20, %edi
callq 0x51f0
movq %rax, %r14
movq %rax, %rdi
callq 0x1a830
movq %rbx, %rdi
callq 0x17e78
movb 0x28(%rbx), %cl
xorb $0x1, %cl
movb %cl, (%r13)
movb $0x0, 0x1(%r13)
movq $0x0, 0x8(%r13)
movq %r14, 0x10(%r13)
movq %rax, 0x18(%r13)
movl $0x1, 0x20(%r13)
jmp 0x10454
movq %r12, -0x60(%rbp)
leaq 0x16658(%rip), %rsi     # 0x266c0
leaq 0x166c9(%rip), %rdx     # 0x26738
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x5240
movq %rax, -0x38(%rbp)
testq %rax, %rax
je 0x1046e
leaq 0x16593(%rip), %rsi     # 0x26620
leaq 0x1609c(%rip), %rdx     # 0x26130
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x10493
movq %rax, %r12
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rsi
cmpq 0x30(%rsi), %rax
jl 0x100c2
cmpq 0x38(%rsi), %rax
jle 0x103d5
cmpb $0x0, 0x40(%rsi)
jne 0x103d5
movq (%rsi), %rax
leaq -0xe0(%rbp), %rdi
callq *(%rax)
leaq 0xf418(%rip), %rcx      # 0x1f4f7
leaq -0xe0(%rbp), %rdi
movl $0x18, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x5230
leaq -0xb0(%rbp), %rsi
movq %rsi, -0x10(%rsi)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x10122
movq %rdx, -0xc0(%rbp)
movq (%rcx), %rdx
movq %rdx, -0xb0(%rbp)
jmp 0x10128
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
leaq -0xc0(%rbp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0xf3c3(%rip), %rsi      # 0x1f510
callq 0x5420
leaq -0x70(%rbp), %rsi
movq %rsi, -0x10(%rsi)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x10176
movq %rdx, -0x80(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x70(%rbp)
jmp 0x1017c
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, -0x78(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x10(%r12), %r13
movq %r13, %rbx
negq %rbx
cmovsq %r13, %rbx
movl $0x1, %r14d
cmpq $0xa, %rbx
jb 0x10202
movl $0x4, %r14d
movabsq $0x346dc5d63886594b, %rsi # imm = 0x346DC5D63886594B
movq %rbx, %rcx
cmpq $0x63, %rcx
jbe 0x101f9
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x101ff
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x10202
movq %rcx, %rax
mulq %rsi
shrq $0xb, %rdx
addl $0x4, %r14d
cmpq $0x1869f, %rcx          # imm = 0x1869F
movq %rdx, %rcx
ja 0x101c1
addl $-0x3, %r14d
jmp 0x10202
addl $-0x2, %r14d
jmp 0x10202
decl %r14d
shrq $0x3f, %r13
leal (%r14,%r13), %esi
leaq -0x90(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0xa0(%rbp), %rdi
movl $0x2d, %edx
callq 0x5260
addq -0xa0(%rbp), %r13
movq %r13, %rdi
movl %r14d, %esi
movq %rbx, %rdx
callq 0xe9b3
movq -0x80(%rbp), %rcx
movq -0x78(%rbp), %r8
movq -0x98(%rbp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %edi
leaq -0x70(%rbp), %r14
cmpq %r14, %rcx
je 0x10260
movq -0x70(%rbp), %rdi
movq -0xa0(%rbp), %rsi
cmpq %rdi, %rax
leaq -0x90(%rbp), %r13
jbe 0x10289
movl $0xf, %edi
cmpq %r13, %rsi
je 0x10284
movq -0x90(%rbp), %rdi
cmpq %rdi, %rax
jbe 0x10294
leaq -0x80(%rbp), %rdi
callq 0x50a0
jmp 0x102a4
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5230
leaq -0x48(%rbp), %rdx
movq %rdx, -0x10(%rdx)
movq (%rax), %rsi
leaq 0x10(%rax), %rcx
cmpq %rcx, %rsi
je 0x102c5
movq %rsi, -0x58(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x48(%rbp)
jmp 0x102cb
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, -0x50(%rbp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq 0x16cd2(%rip), %rdi     # 0x26fc8
callq 0x5250
movq %rax, %rbx
leaq 0xf216(%rip), %rsi      # 0x1f51b
movl $0x2e, %edx
movq %rax, %rdi
callq 0x5250
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x51d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5140
leaq -0x48(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x10352
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x5200
movq -0xa0(%rbp), %rdi
cmpq %r13, %rdi
je 0x1036d
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x5200
movq -0x80(%rbp), %rdi
cmpq %r14, %rdi
je 0x10382
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x5200
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
cmpq %rax, %rdi
je 0x103a4
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0x5200
leaq -0xd0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x103c3
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0x5200
leaq 0x16fe8(%rip), %rax     # 0x273b2
movb $0x1, %cl
movb %cl, (%rax)
movq -0x38(%rbp), %rax
movb %cl, 0x40(%rax)
movl $0x10, %edi
callq 0x51f0
movq %rax, %rbx
movq -0x38(%rbp), %rdi
callq 0x17e78
movq %rax, %rdi
callq 0x17aec
movq $0x0, (%rbx)
movq %rax, 0x8(%rbx)
movq 0x10(%r12), %rsi
movq -0x38(%rbp), %rax
subq 0x30(%rax), %rsi
movq %rbx, %rdi
callq 0x17ae2
movl $0x28, %edi
callq 0x51f0
movq %rax, %r13
movl $0x20, %edi
callq 0x51f0
movq %rax, %r14
movq %rax, %rdi
callq 0x1a830
movw $0x1, (%r13)
movq $0x0, 0x8(%r13)
movq %r14, 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 0x5410
movl $0x8, %edi
callq 0x5120
leaq 0xf0f6(%rip), %rcx      # 0x1f575
movq %rcx, (%rax)
movq 0x16b17(%rip), %rsi     # 0x26fa0
movq %rax, %rdi
xorl %edx, %edx
callq 0x53a0
callq 0x5410
movq %rdx, -0x30(%rbp)
movq %rax, %r12
jmp 0x10534
movq %rdx, -0x30(%rbp)
movq %rax, %r12
jmp 0x10556
movq %rdx, -0x30(%rbp)
movq %rax, %r12
jmp 0x1056f
movq %rdx, -0x30(%rbp)
movq %rax, %r12
jmp 0x10591
jmp 0x105c3
movq %rdx, -0x30(%rbp)
movq %rax, %r12
movl $0x20, %esi
movq %r14, %rdi
callq 0x5200
jmp 0x104ea
movq %rdx, -0x30(%rbp)
movq %rax, %r12
movl $0x28, %esi
movq %r13, %rdi
jmp 0x105bc
jmp 0x105c3
movq %rdx, %rbx
movq %rax, %r12
movl $0x20, %esi
movq %r14, %rdi
callq 0x5200
jmp 0x108ae
movq %rdx, -0x30(%rbp)
movq %rax, %r12
leaq -0x48(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x10534
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x5200
movq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rax
cmpq %rax, %rdi
je 0x10556
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x5200
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x1056f
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x5200
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
cmpq %rax, %rdi
je 0x10591
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0x5200
leaq -0xd0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x105ca
movq -0xd0(%rbp), %rsi
incq %rsi
jmp 0x105bc
movq %rdx, -0x30(%rbp)
movq %rax, %r12
movl $0x10, %esi
movq %rbx, %rdi
callq 0x5200
jmp 0x105ca
movq %rdx, -0x30(%rbp)
movq %rax, %r12
cmpl $0x1, -0x30(%rbp)
jne 0x1091d
movq %r12, %rdi
callq 0x52b0
movq 0x169c5(%rip), %rax     # 0x26fa8
addq $0x10, %rax
movq %rax, -0x30(%rbp)
movq %rax, -0x58(%rbp)
movq %r12, %rdi
callq 0x50f0
leaq 0x16022(%rip), %rsi     # 0x26620
leaq 0x15b73(%rip), %rdx     # 0x26178
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x107f4
movq %rax, %rbx
movq -0x60(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x17a14
movq %rax, %rdi
callq 0x173fa
movq %rax, %r15
movq -0x60(%rbp), %rax
movq 0x28(%rax), %rdi
movq 0x10(%rbx), %rsi
callq 0x17c58
movq %rax, %r12
cmpb $0x0, 0x10(%rax)
jne 0x106ae
movq 0x1696c(%rip), %rdi     # 0x26fc8
leaq 0xee33(%rip), %rsi      # 0x1f496
movl $0x10, %edx
callq 0x5250
movq 0x18(%r12), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq 0x16948(%rip), %rdi     # 0x26fc8
callq 0x5250
movq %rax, %rbx
leaq 0xee18(%rip), %rsi      # 0x1f4a7
movl $0x1e, %edx
movq %rax, %rdi
callq 0x5250
movq %rbx, %rdi
callq 0x5110
leaq 0x16d07(%rip), %rax     # 0x273b2
movb $0x1, (%rax)
movb $0x1, 0x10(%r12)
movq -0x60(%rbp), %rax
movq 0x30(%rax), %rbx
movq -0x38(%rbp), %rdi
callq 0x17e78
movq %rax, %rdi
callq 0x17aec
movq %rbx, %rdi
movq %rax, %rsi
callq 0x17a14
movq %rax, %rdi
callq 0x173fa
movq %rax, %r14
movl $0x20, %edi
callq 0x51f0
movq %rax, %rbx
movq %rax, %rdi
callq 0x1a830
movl $0x20, %edi
callq 0x51f0
movq %rax, %r13
movq %r12, %rdi
callq 0x17e78
movq $-0x1, 0x8(%r13)
movb $0x0, 0x10(%r13)
movq %rax, 0x18(%r13)
leaq 0x16153(%rip), %rax     # 0x26878
addq $0x10, %rax
movq %rax, (%r13)
movl $0x20, %edi
callq 0x51f0
movq %rax, %r12
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r15, 0x18(%rax)
leaq 0x162b7(%rip), %rax     # 0x26a08
addq $0x10, %rax
movq %rax, (%r12)
movl $0x20, %edi
callq 0x51f0
movq %rax, %r15
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r14, 0x18(%rax)
leaq 0x1623b(%rip), %rax     # 0x269b8
addq $0x10, %rax
movq %rax, (%r15)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1a9ba
movq %rax, %rdi
movq %r12, %rsi
callq 0x1a9ba
movq %rax, %rdi
movq %r15, %rsi
callq 0x1a9ba
movl $0x28, %edi
callq 0x51f0
movq %rax, %r13
movl $0x10, %edi
callq 0x51f0
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movw $0x101, (%r13)          # imm = 0x101
movq $0x0, 0x8(%r13)
movq %rbx, 0x10(%r13)
movq %rax, 0x18(%r13)
movl $0x3, 0x20(%r13)
leaq -0x58(%rbp), %rdi
callq 0x53f0
callq 0x5350
jmp 0x10454
callq 0x5410
movq %rdx, %r14
movq %rax, %r12
movl $0x28, %esi
jmp 0x10813
jmp 0x10839
movq %rdx, %r14
movq %rax, %r12
movl $0x20, %esi
movq %r13, %rdi
jmp 0x10828
jmp 0x10839
movq %rdx, %r14
movq %rax, %r12
movl $0x20, %esi
movq %rbx, %rdi
callq 0x5200
jmp 0x1083f
jmp 0x10839
jmp 0x10839
jmp 0x10839
jmp 0x10839
jmp 0x10839
movq %rdx, %r14
movq %rax, %r12
cmpl $0x1, %r14d
jne 0x10898
movq %r12, %rdi
callq 0x52b0
movq -0x30(%rbp), %rax
movq %rax, -0x80(%rbp)
movq %r12, %rdi
callq 0x50f0
movl $0x8, %edi
callq 0x5120
leaq 0xecdc(%rip), %rcx      # 0x1f54a
movq %rcx, (%rax)
movq 0x16728(%rip), %rsi     # 0x26fa0
movq %rax, %rdi
xorl %edx, %edx
callq 0x53a0
jmp 0x1090c
movq %rax, %r12
leaq -0x80(%rbp), %rdi
callq 0x53f0
callq 0x5350
leaq -0x58(%rbp), %rdi
callq 0x53f0
callq 0x5350
jmp 0x1091d
movq %rdx, %rbx
movq %rax, %r12
movl $0x28, %esi
movq %r13, %rdi
callq 0x5200
jmp 0x108c3
movq %rdx, %rbx
movq %rax, %r12
cmpl $0x1, %ebx
jne 0x1091d
movq %r12, %rdi
callq 0x52b0
movq 0x166d1(%rip), %rax     # 0x26fa8
addq $0x10, %rax
movq %rax, -0x58(%rbp)
movq %r12, %rdi
callq 0x50f0
movl $0x8, %edi
callq 0x5120
leaq 0xebce(%rip), %rcx      # 0x1f4c6
movq %rcx, (%rax)
movq 0x1669e(%rip), %rsi     # 0x26fa0
movq %rax, %rdi
xorl %edx, %edx
callq 0x53a0
movq %rax, %r12
leaq -0x58(%rbp), %rdi
callq 0x53f0
callq 0x5350
movq %r12, %rdi
callq 0x53d0
movq %rax, %rdi
callq 0xeb19
nop