// File: /workspace/github_cpp_makefile_star2_O3/Bartor[P]kompilator/middle/abstract_assembler/Constant.cpp
// Function: Constant::generateConstant(Constants*, ResolvableAddress&, ResolvableAddress&)
InstructionList &Constant::generateConstant(Constants *constantsData, ResolvableAddress &primaryAccumulator, ResolvableAddress &secondaryAccumulator) {
    InstructionList &instructions = *new InstructionList();

    if (value == 0) {
        instructions.append(new Sub(primaryAccumulator))
                .append(new Store(address));

        return instructions;
    } // 1 and -1 are covered separately

    long long bestPrefix = 0;
    Constant *bestFit = nullptr;

    Constant *closest = nullptr;
    for (auto const &constant : constantsData->getConstants()) {
        if (constant == this) break;
        if (llabs(constant->value) > llabs(value)) break;

        if ((value > 0 && constant->value > 0) || (value < 0 && constant->value < 0)) {
            if (llabs(value) > llabs(constant->value)) {
                if (closest) {
                    if (llabs(value - constant->value) < llabs(value - closest->value)) {
                        closest = constant;
                    }
                } else {
                    closest = constant;
                }
            }
        }

        long long prefix = calculatePrefix(this, constant);
        if (prefix > bestPrefix || (prefix == bestPrefix && constant->value > 0)) {
            bestPrefix = prefix;
            bestFit = constant;
        }
    }

    if (closest && llabs(value - closest->value) < 10) {
        instructions.append(new Load(closest->getAddress()));

        for (long long i = 0; i < llabs(value - closest->value); i++) {
            if (value < 0) {
                instructions.append(new Dec());
            } else {
                instructions.append(new Inc());
            }
        }

        instructions.append(new Store(address));

        return instructions;
    }

    bool byBestFit = false;
    if (bestFit) {
        if (bestPrefix == binaryString.size()) { // the number is actually the same, just with different sign
            byBestFit = true;

            instructions.append(new Sub(primaryAccumulator))
                    .append(new Sub(bestFit->address))
                    .append(new Store(address));
        } else { // numbers differ by last (binaryString.length - bestPrefix) bits
            long long backShifts = bestFit->binaryString.size() - bestPrefix;
            long long cost = (backShifts - 1) * 5 + bestFit->value < 0 ? 20 : 10; // load + shift [-1]s cost
            for (long long i = bestPrefix; i < binaryString.size(); i++) {
                if (binaryString[i] == '1') cost++;
                if (i != binaryString.size() - 1) cost += 5;
            }

            if (cost < generationCost) {
                byBestFit = true;

                if (bestFit->value > 0) {
                    instructions.append(new Load(bestFit->address));
                } else {
                    bestFit->value < 0;
                    instructions.append(new Sub(primaryAccumulator))
                            .append(new Sub(bestFit->getAddress()));
                }

                for (long long i = 0; i < backShifts; i++) instructions.append(new Shift(constantsData->getConstant(-1)->getAddress()));
                instructions.append(new Shift(constantsData->getConstant(1)->getAddress()));
                for (long long i = bestPrefix; i < binaryString.size(); i++) {
                    if (binaryString[i] == '1') instructions.append(new Inc());
                    if (i != binaryString.size() - 1) instructions.append(new Shift(constantsData->getConstant(1)->getAddress()));
                }
                if (value < 0) {
                    instructions.append(new Store(secondaryAccumulator))
                            .append(new Sub(secondaryAccumulator))
                            .append(new Sub(secondaryAccumulator));
                }
                instructions.append(new Store(address));
            } else {
                byBestFit = false;
            }
        }
    }


    if (!byBestFit) {
        instructions.append(new Sub(primaryAccumulator));
        for (long long i = 0; i < binaryString.size(); i++) {
            if (binaryString[i] == '1')instructions.append(new Inc());
            if (i != binaryString.size() - 1) instructions.append(new Shift(constantsData->getConstant(1)->getAddress()));
        }
        if (value < 0)
            instructions.append(new Store(secondaryAccumulator))
                    .append(new Sub(secondaryAccumulator))
                    .append(new Sub(secondaryAccumulator));
        instructions.append(new Store(address));
    }

    return instructions;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, -0x48(%rbp)
movq %rdi, %r12
movl $0x20, %edi
callq 0x51f0
movq %rax, %r14
movq %rax, %rdi
callq 0x1a830
cmpq $0x0, 0x8(%r12)
movq %r14, -0x38(%rbp)
je 0x16cb7
movq %r15, -0x40(%rbp)
movq %rbx, -0x50(%rbp)
movq -0x48(%rbp), %rax
leaq 0x10(%rax), %rsi
leaq -0x68(%rbp), %r15
movq %r15, %rdi
callq 0x1670a
movq (%r15), %rdi
movq 0x8(%r15), %rax
cmpq %rax, %rdi
movq %r12, -0x30(%rbp)
je 0x16d18
movq 0x8(%r12), %rcx
movq 0x18(%r12), %rdx
movq %rcx, %rsi
negq %rsi
cmovsq %rcx, %rsi
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %rdi, %r8
movq %r12, %r11
movq (%r8), %r9
cmpq -0x30(%rbp), %r9
je 0x16d13
movq 0x8(%r9), %r10
movq %r10, %rbx
negq %rbx
cmovsq %r10, %rbx
cmpq %rsi, %rbx
ja 0x16d13
testq %rcx, %rcx
setg %r14b
testq %r10, %r10
setg %r12b
testb %r14b, %r12b
je 0x16c1c
cmpq %rbx, %rsi
ja 0x16c2e
jmp 0x16c5e
testq %r10, %rcx
sets %r14b
cmpq %rbx, %rsi
seta %bl
testb %bl, %r14b
je 0x16c5e
movq %r9, %r12
testq %r11, %r11
je 0x16c61
movq %rcx, %rbx
subq %r10, %rbx
movq %rbx, %r14
negq %r14
cmovsq %rbx, %r14
movq %rcx, %rbx
subq 0x8(%r11), %rbx
movq %rbx, %r12
negq %r12
cmovsq %rbx, %r12
cmpq %r12, %r14
cmovbq %r9, %r11
movq %r11, %r12
movb (%rdx), %bl
testb %bl, %bl
je 0x16c82
movq 0x18(%r9), %r14
xorl %r11d, %r11d
cmpb (%r14,%r11), %bl
jne 0x16c85
movb 0x1(%rdx,%r11), %bl
incq %r11
testb %bl, %bl
jne 0x16c6e
jmp 0x16c85
xorl %r11d, %r11d
testq %r10, %r10
setg %r10b
cmpq %r15, %r11
setg %bl
sete %r14b
andb %r10b, %r14b
orb %bl, %r14b
cmovneq %r11, %r15
cmovneq %r9, %r13
addq $0x8, %r8
cmpq %rax, %r8
movq -0x38(%rbp), %r14
jne 0x16bdb
jmp 0x16d21
movl $0x20, %edi
callq 0x51f0
movq $-0x1, %rbx
movq %rbx, 0x8(%rax)
xorl %r14d, %r14d
movb %r14b, 0x10(%rax)
movq %r15, 0x18(%rax)
leaq 0xfd2a(%rip), %rcx      # 0x26a08
addq $0x10, %rcx
movq %rcx, (%rax)
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x1a9ba
movq %rax, %r15
movl $0x20, %edi
callq 0x51f0
movq (%r12), %rcx
movq %rbx, 0x8(%rax)
movb %r14b, 0x10(%rax)
movq -0x38(%rbp), %r14
jmp 0x16ec4
movq %r11, %r12
jmp 0x16d21
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
testq %rdi, %rdi
je 0x16d32
movq -0x58(%rbp), %rsi
subq %rdi, %rsi
callq 0x5200
testq %r12, %r12
je 0x16e1e
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
subq 0x8(%r12), %rax
movq %rax, %rcx
negq %rcx
cmovsq %rax, %rcx
cmpq $0x9, %rcx
ja 0x16e1e
movl $0x20, %edi
callq 0x51f0
movq (%r12), %rcx
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
leaq 0xfaf7(%rip), %rcx      # 0x26878
addq $0x10, %rcx
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x1a9ba
movq -0x30(%rbp), %rax
movq 0x8(%rax), %r13
cmpq 0x8(%r12), %r13
je 0x16e08
xorl %ebx, %ebx
leaq 0xff2d(%rip), %r15      # 0x26cd8
movl $0x18, %edi
callq 0x51f0
movq $0x0, 0x10(%rax)
movq $-0x1, 0x8(%rax)
movq %r15, %rcx
testq %r13, %r13
jns 0x16dd4
leaq 0xff54(%rip), %rcx      # 0x26d28
addq $0x10, %rcx
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x1a9ba
incq %rbx
movq -0x30(%rbp), %rax
movq 0x8(%rax), %r13
movq %r13, %rax
subq 0x8(%r12), %rax
movq %rax, %rcx
negq %rcx
cmovsq %rax, %rcx
cmpq %rcx, %rbx
jb 0x16dab
movl $0x20, %edi
callq 0x51f0
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
jmp 0x17394
testq %r13, %r13
je 0x16f80
movq -0x30(%rbp), %rbx
movq 0x20(%rbx), %rax
cmpq %rax, %r15
jne 0x16ede
movl $0x20, %edi
callq 0x51f0
movq $-0x1, %rcx
movq %rcx, 0x8(%rax)
xorl %r12d, %r12d
movb %r12b, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x18(%rax)
leaq 0xfba5(%rip), %r14      # 0x26a08
addq $0x10, %r14
movq %r14, (%rax)
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x1a9ba
movq %rax, %r15
movl $0x20, %edi
callq 0x51f0
movq (%r13), %rcx
movq $-0x1, %r13
movq %r13, 0x8(%rax)
movb %r12b, 0x10(%rax)
movq %rcx, 0x18(%rax)
movq %r14, (%rax)
movq -0x38(%rbp), %r14
movq %r15, %rdi
movq %rax, %rsi
callq 0x1a9ba
movq %rax, %r15
movl $0x20, %edi
callq 0x51f0
movq (%rbx), %rcx
movq %r13, 0x8(%rax)
movb %r12b, 0x10(%rax)
movq %rcx, 0x18(%rax)
leaq 0xf9f9(%rip), %rcx      # 0x268c8
addq $0x10, %rcx
movq %rcx, (%rax)
movq %r15, %rdi
jmp 0x173b5
movq 0x8(%r13), %rbx
movq 0x20(%r13), %r12
subq %r15, %r12
leaq (%r12,%r12,4), %rcx
addq %rbx, %rcx
addq $-0x5, %rcx
movl $0x14, %edx
movl $0xa, %ecx
cmovsq %rdx, %rcx
cmpq %rax, %r15
jae 0x16f39
movq -0x30(%rbp), %rdx
movq 0x18(%rdx), %rdx
leaq -0x1(%rax), %rsi
movq %r15, %rdi
xorl %r8d, %r8d
cmpb $0x31, (%rdx,%rdi)
sete %r8b
leaq (%rcx,%r8), %r9
cmpq %rdi, %rsi
leaq 0x5(%rcx,%r8), %rcx
cmoveq %r9, %rcx
incq %rdi
cmpq %rdi, %rax
jne 0x16f16
movq -0x30(%rbp), %rax
cmpq 0x10(%rax), %rcx
jge 0x16f80
movl $0x20, %edi
callq 0x51f0
testq %rbx, %rbx
jle 0x1711b
movq (%r13), %rcx
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
leaq 0xf907(%rip), %rcx      # 0x26878
addq $0x10, %rcx
movq %rcx, (%rax)
movq %r14, %rdi
jmp 0x1717f
movl $0x20, %edi
callq 0x51f0
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x18(%rax)
leaq 0xfa63(%rip), %rcx      # 0x26a08
addq $0x10, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x1a9ba
movq -0x30(%rbp), %r13
movq 0x20(%r13), %rax
testq %rax, %rax
je 0x17077
xorl %r15d, %r15d
leaq 0xfd02(%rip), %r12      # 0x26cd8
addq $0x10, %r12
leaq 0xfa77(%rip), %rbx      # 0x26a58
addq $0x10, %rbx
movq 0x18(%r13), %rcx
cmpb $0x31, (%rcx,%r15)
jne 0x1701c
movl $0x18, %edi
callq 0x51f0
movq $0x0, 0x10(%rax)
movq $-0x1, 0x8(%rax)
movq %r12, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x1a9ba
movq 0x20(%r13), %rax
leaq -0x1(%rax), %rcx
cmpq %rcx, %r15
je 0x1706b
movl $0x20, %edi
callq 0x51f0
movq %rax, %r13
movl $0x1, %esi
movq -0x48(%rbp), %rdi
callq 0x17a14
movq (%rax), %rax
movq $-0x1, 0x8(%r13)
movb $0x0, 0x10(%r13)
movq %rax, 0x18(%r13)
movq %rbx, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x1a9ba
movq -0x30(%rbp), %r13
movq 0x20(%r13), %rax
incq %r15
cmpq %rax, %r15
jb 0x16fe5
cmpq $0x0, 0x8(%r13)
jns 0x17386
movl $0x20, %edi
callq 0x51f0
movq $-0x1, %rcx
movq %rcx, 0x8(%rax)
xorl %ecx, %ecx
movb %cl, 0x10(%rax)
xorl %r12d, %r12d
movq -0x50(%rbp), %rbx
movq %rbx, 0x18(%rax)
leaq 0xf81a(%rip), %rcx      # 0x268c8
addq $0x10, %rcx
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x1a9ba
movq %rax, %r15
movl $0x20, %edi
callq 0x51f0
movq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movb %r12b, 0x10(%rax)
movq %rbx, 0x18(%rax)
movq -0x38(%rbp), %r12
movq %r12, (%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x1a9ba
movq %rax, %r15
movl $0x20, %edi
callq 0x51f0
movq $-0x1, %rcx
movq %rcx, 0x8(%rax)
xorl %ecx, %ecx
movb %cl, 0x10(%rax)
movq %rbx, 0x18(%rax)
movq %r12, (%rax)
jmp 0x1737b
movq $-0x1, %rcx
movq %rcx, 0x8(%rax)
xorl %r14d, %r14d
movb %r14b, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x18(%rax)
leaq 0xf8cc(%rip), %rbx      # 0x26a08
addq $0x10, %rbx
movq %rbx, (%rax)
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x1a9ba
movq %rax, -0x40(%rbp)
movl $0x20, %edi
callq 0x51f0
movq (%r13), %rcx
movq $-0x1, %rdx
movq %rdx, 0x8(%rax)
movb %r14b, 0x10(%rax)
movq -0x38(%rbp), %r14
movq %rcx, 0x18(%rax)
movq %rbx, (%rax)
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x1a9ba
testq %r12, %r12
jle 0x171dc
leaq 0xf8c5(%rip), %rbx      # 0x26a58
addq $0x10, %rbx
movl $0x20, %edi
callq 0x51f0
movq %rax, %r13
movq -0x48(%rbp), %rdi
movq $-0x1, %rsi
callq 0x17a14
movq (%rax), %rax
movq $-0x1, 0x8(%r13)
movb $0x0, 0x10(%r13)
movq %rax, 0x18(%r13)
movq %rbx, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x1a9ba
decq %r12
jne 0x17197
movl $0x20, %edi
callq 0x51f0
movq %rax, %r13
movl $0x1, %esi
movq -0x48(%rbp), %rdi
callq 0x17a14
movq (%rax), %rax
movq $-0x1, 0x8(%r13)
movb $0x0, 0x10(%r13)
movq %rax, 0x18(%r13)
leaq 0xf846(%rip), %r12      # 0x26a58
addq $0x10, %r12
movq %r12, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x1a9ba
movq -0x30(%rbp), %r13
movq 0x20(%r13), %rax
cmpq %rax, %r15
jae 0x172d3
leaq 0xfa9b(%rip), %rbx      # 0x26cd8
addq $0x10, %rbx
movq 0x18(%r13), %rcx
cmpb $0x31, (%rcx,%r15)
jne 0x17278
movl $0x18, %edi
callq 0x51f0
movq $0x0, 0x10(%rax)
movq $-0x1, 0x8(%rax)
movq %rbx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x1a9ba
movq 0x20(%r13), %rax
leaq -0x1(%rax), %rcx
cmpq %rcx, %r15
je 0x172c7
movl $0x20, %edi
callq 0x51f0
movq %rax, %r13
movl $0x1, %esi
movq -0x48(%rbp), %rdi
callq 0x17a14
movq (%rax), %rax
movq $-0x1, 0x8(%r13)
movb $0x0, 0x10(%r13)
movq %rax, 0x18(%r13)
movq %r12, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x1a9ba
movq -0x30(%rbp), %r13
movq 0x20(%r13), %rax
incq %r15
cmpq %rax, %r15
jb 0x17241
cmpq $0x0, 0x8(%r13)
jns 0x17386
movl $0x20, %edi
callq 0x51f0
movq $-0x1, %rcx
movq %rcx, 0x8(%rax)
xorl %r12d, %r12d
movb %r12b, 0x10(%rax)
movq -0x50(%rbp), %rbx
movq %rbx, 0x18(%rax)
leaq 0xf5bf(%rip), %rcx      # 0x268c8
addq $0x10, %rcx
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x1a9ba
movq %rax, %r15
movl $0x20, %edi
callq 0x51f0
movq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movb %r12b, 0x10(%rax)
movq %rbx, 0x18(%rax)
leaq 0xf6c6(%rip), %r14      # 0x26a08
addq $0x10, %r14
movq %r14, (%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x1a9ba
movq %rax, %r15
movl $0x20, %edi
callq 0x51f0
movq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movb %r12b, 0x10(%rax)
movq %rbx, 0x18(%rax)
movq %r14, (%rax)
movq -0x38(%rbp), %r14
movq %r15, %rdi
movq %rax, %rsi
callq 0x1a9ba
movl $0x20, %edi
callq 0x51f0
movq (%r13), %rcx
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
leaq 0xf51d(%rip), %rcx      # 0x268c8
addq $0x10, %rcx
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x1a9ba
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x173e2
jmp 0x173e2
movq %r14, %rdi
movq %rax, %r14
movl $0x20, %esi
jmp 0x173ed
jmp 0x173e2
movq %rax, %r14
movl $0x20, %esi
movq %r13, %rdi
callq 0x5200
movq %r14, %rdi
callq 0x53d0