// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rdx, %r12
movq %rsi, -0x48(%rbp)
movq %rdi, %r14
movl $0x20, %edi
callq 0x51f0
movq %rax, %rbx
movq %rax, %rdi
callq 0x1ac28
cmpq $0x0, 0x8(%r14)
je 0x16f4d
movq %r12, -0x40(%rbp)
movq %r15, -0x50(%rbp)
movq %rbx, -0x30(%rbp)
movq -0x48(%rbp), %rax
leaq 0x10(%rax), %rsi
leaq -0x68(%rbp), %r15
movq %r15, %rdi
callq 0x1698c
movq (%r15), %rdi
movq 0x8(%r15), %rax
cmpq %rax, %rdi
movq %r14, -0x38(%rbp)
je 0x16f7d
movq 0x8(%r14), %rcx
movq 0x18(%r14), %rdx
movq %rcx, %rsi
negq %rsi
cmovsq %rcx, %rsi
xorl %ebx, %ebx
xorl %r9d, %r9d
xorl %r10d, %r10d
movq %rdi, %r8
movq (%r8), %r13
movb $0x1, %r14b
cmpq -0x38(%rbp), %r13
je 0x16e84
movq 0x8(%r13), %r11
movq %r11, %r15
negq %r15
cmovsq %r11, %r15
cmpq %rsi, %r15
jbe 0x16e92
movq %r10, %r15
movq %r9, %r13
movq %rbx, %r12
jmp 0x16f30
testq %rcx, %rcx
setg %r14b
testq %r11, %r11
setg %r12b
testb %r14b, %r12b
je 0x16eac
cmpq %r15, %rsi
ja 0x16ebf
jmp 0x16eef
testq %r11, %rcx
sets %r14b
cmpq %r15, %rsi
seta %r15b
testb %r15b, %r14b
je 0x16eef
movq %r13, %r12
testq %rbx, %rbx
je 0x16ef2
movq %rcx, %r14
subq %r11, %r14
movq %r14, %r15
negq %r15
cmovsq %r14, %r15
movq %rcx, %r14
subq 0x8(%rbx), %r14
movq %r14, %r12
negq %r12
cmovsq %r14, %r12
cmpq %r12, %r15
cmovbq %r13, %rbx
movq %rbx, %r12
movb (%rdx), %bl
testb %bl, %bl
je 0x16f13
movq 0x18(%r13), %r14
xorl %r15d, %r15d
cmpb (%r14,%r15), %bl
jne 0x16f16
movb 0x1(%rdx,%r15), %bl
incq %r15
testb %bl, %bl
jne 0x16eff
jmp 0x16f16
xorl %r15d, %r15d
cmpq %r10, %r15
jg 0x16f22
jne 0x16f27
testq %r11, %r11
jle 0x16f27
xorl %r14d, %r14d
jmp 0x16f30
xorl %r14d, %r14d
movq %r10, %r15
movq %r9, %r13
testb %r14b, %r14b
jne 0x16f86
addq $0x8, %r8
movq %r12, %rbx
movq %r13, %r9
movq %r15, %r10
cmpq %rax, %r8
jne 0x16e65
jmp 0x16f86
movl $0x20, %edi
callq 0x51f0
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r12, 0x18(%rax)
leaq 0x10a9a(%rip), %rcx     # 0x27a08
addq $0x10, %rcx
movq %rcx, (%rax)
movq %rbx, %rdi
jmp 0x17113
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
testq %rdi, %rdi
je 0x16f97
movq -0x58(%rbp), %rsi
subq %rdi, %rsi
callq 0x5200
testq %r12, %r12
movq -0x38(%rbp), %r14
je 0x17092
movq 0x8(%r14), %rax
subq 0x8(%r12), %rax
movq %rax, %rcx
negq %rcx
cmovsq %rax, %rcx
cmpq $0x9, %rcx
ja 0x17092
movl $0x20, %edi
callq 0x51f0
movq (%r12), %rcx
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
leaq 0x10892(%rip), %rcx     # 0x27878
addq $0x10, %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1adbe
movq 0x8(%r14), %r13
movq %rbx, %r15
cmpq 0x8(%r12), %r13
je 0x175a3
xorl %ebx, %ebx
leaq 0x10cc1(%rip), %r14     # 0x27cd8
leaq 0x10d0a(%rip), %r15     # 0x27d28
movl $0x18, %edi
callq 0x51f0
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r14, %rcx
testq %r13, %r13
jns 0x1704d
movq %r15, %rcx
addq $0x10, %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x1adbe
incq %rbx
movq -0x38(%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 0x1701e
movq -0x30(%rbp), %rbx
movq %rbx, %r15
movq -0x38(%rbp), %r14
jmp 0x175a3
testq %r13, %r13
je 0x171c1
movq 0x20(%r14), %rax
cmpq %rax, %r15
jne 0x17123
movl $0x20, %edi
callq 0x51f0
movq $-0x1, %rcx
movq %rcx, 0x8(%rax)
xorl %r14d, %r14d
movb %r14b, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x18(%rax)
leaq 0x10939(%rip), %r12     # 0x27a08
addq $0x10, %r12
movq %r12, (%rax)
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1adbe
movq %rax, %r15
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 %r12, (%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x1adbe
movq %rax, %r15
jmp 0x175a3
movq 0x8(%r13), %r12
movq 0x20(%r13), %rbx
subq %r15, %rbx
leaq (%rbx,%rbx,4), %rcx
addq %r12, %rcx
addq $-0x5, %rcx
movl $0x14, %edx
movl $0xa, %ecx
cmovsq %rdx, %rcx
cmpq %rax, %r15
jae 0x1717a
movq 0x18(%r14), %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 0x17157
cmpq 0x10(%r14), %rcx
jge 0x171c1
movl $0x20, %edi
callq 0x51f0
testq %r12, %r12
jle 0x1732f
movq (%r13), %rcx
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
leaq 0x106ca(%rip), %rcx     # 0x27878
addq $0x10, %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %r12
movq %r12, %rdi
jmp 0x17391
movl $0x20, %edi
callq 0x51f0
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x18(%rax)
leaq 0x10822(%rip), %rcx     # 0x27a08
addq $0x10, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, (%rax)
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x1adbe
cmpq $0x0, 0x20(%r14)
je 0x172b7
xorl %r15d, %r15d
leaq 0x10ac6(%rip), %r12     # 0x27cd8
addq $0x10, %r12
leaq 0x1083b(%rip), %rbx     # 0x27a58
addq $0x10, %rbx
movq 0x18(%r14), %rax
cmpb $0x31, (%rax,%r15)
jne 0x1725f
movl $0x18, %edi
callq 0x51f0
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r12, (%rax)
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x1adbe
movq 0x20(%r14), %rax
decq %rax
cmpq %rax, %r15
je 0x172aa
movl $0x20, %edi
callq 0x51f0
movq %rax, %r13
movl $0x1, %esi
movq -0x48(%rbp), %rdi
callq 0x17c56
movq (%rax), %rax
movq $-0x1, 0x8(%r13)
movb $0x0, 0x10(%r13)
movq %rax, 0x18(%r13)
movq %rbx, (%r13)
movq -0x30(%rbp), %rdi
movq %r13, %rsi
callq 0x1adbe
incq %r15
cmpq 0x20(%r14), %r15
jb 0x17221
cmpq $0x0, 0x8(%r14)
movq -0x30(%rbp), %rbx
movq %rbx, %r15
jns 0x175a3
movl $0x20, %edi
callq 0x51f0
movq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq $-0x1, %r12
xorl %r14d, %r14d
movb %r14b, 0x10(%rax)
movq -0x50(%rbp), %r13
movq %r13, 0x18(%rax)
leaq 0x105cd(%rip), %rcx     # 0x278c8
addq $0x10, %rcx
movq %rcx, (%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1adbe
movq %rax, %r15
movl $0x20, %edi
callq 0x51f0
movq %r12, 0x8(%rax)
movb %r14b, 0x10(%rax)
movq %r13, 0x18(%rax)
movq -0x40(%rbp), %r12
jmp 0x17560
movq $-0x1, %rcx
movq %rcx, 0x8(%rax)
xorl %ecx, %ecx
movb %cl, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x18(%rax)
leaq 0x106ba(%rip), %r14     # 0x27a08
addq $0x10, %r14
movq %r14, (%rax)
movq -0x30(%rbp), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0x1adbe
movq %rax, -0x40(%rbp)
movl $0x20, %edi
callq 0x51f0
movq (%r13), %rcx
movq $-0x1, %rdx
movq %rdx, 0x8(%rax)
xorl %edx, %edx
movb %dl, 0x10(%rax)
movq %rcx, 0x18(%rax)
movq %r14, (%rax)
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x1adbe
testq %rbx, %rbx
jle 0x173ee
leaq 0x106b3(%rip), %r14     # 0x27a58
addq $0x10, %r14
movl $0x20, %edi
callq 0x51f0
movq %rax, %r13
movq -0x48(%rbp), %rdi
movq $-0x1, %rsi
callq 0x17c56
movq (%rax), %rax
movq $-0x1, 0x8(%r13)
movb $0x0, 0x10(%r13)
movq %rax, 0x18(%r13)
movq %r14, (%r13)
movq %r12, %rdi
movq %r13, %rsi
callq 0x1adbe
decq %rbx
jne 0x173a9
movl $0x20, %edi
callq 0x51f0
movq %rax, %r13
movl $0x1, %esi
movq -0x48(%rbp), %rdi
callq 0x17c56
movq (%rax), %rax
movq $-0x1, 0x8(%r13)
movb $0x0, 0x10(%r13)
movq %rax, 0x18(%r13)
leaq 0x10634(%rip), %rbx     # 0x27a58
addq $0x10, %rbx
movq %rbx, (%r13)
movq %r12, %rdi
movq %r13, %rsi
callq 0x1adbe
movq -0x38(%rbp), %r14
cmpq 0x20(%r14), %r15
jae 0x174e6
leaq 0x1088c(%rip), %r12     # 0x27cd8
addq $0x10, %r12
movq 0x18(%r14), %rax
cmpb $0x31, (%rax,%r15)
jne 0x1748e
movl $0x18, %edi
callq 0x51f0
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r12, (%rax)
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x1adbe
movq 0x20(%r14), %rax
decq %rax
cmpq %rax, %r15
je 0x174d9
movl $0x20, %edi
callq 0x51f0
movq %rax, %r13
movl $0x1, %esi
movq -0x48(%rbp), %rdi
callq 0x17c56
movq (%rax), %rax
movq $-0x1, 0x8(%r13)
movb $0x0, 0x10(%r13)
movq %rax, 0x18(%r13)
movq %rbx, (%r13)
movq -0x30(%rbp), %rdi
movq %r13, %rsi
callq 0x1adbe
incq %r15
cmpq 0x20(%r14), %r15
jb 0x17450
cmpq $0x0, 0x8(%r14)
movq -0x30(%rbp), %rbx
movq %rbx, %r15
jns 0x175a3
movl $0x20, %edi
callq 0x51f0
movq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq $-0x1, %r12
xorl %r14d, %r14d
movb %r14b, 0x10(%rax)
movq -0x50(%rbp), %r13
movq %r13, 0x18(%rax)
leaq 0x1039e(%rip), %rcx     # 0x278c8
addq $0x10, %rcx
movq %rcx, (%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1adbe
movq %rax, %r15
movl $0x20, %edi
callq 0x51f0
movq %r12, 0x8(%rax)
movb %r14b, 0x10(%rax)
movq %r13, 0x18(%rax)
leaq 0x104ac(%rip), %r12     # 0x27a08
addq $0x10, %r12
movq %r12, (%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x1adbe
movq %rax, %r15
movl $0x20, %edi
callq 0x51f0
movq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movb %r14b, 0x10(%rax)
movq -0x38(%rbp), %r14
movq %r13, 0x18(%rax)
movq %r12, (%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x1adbe
movq %rbx, %r15
movl $0x20, %edi
callq 0x51f0
movq (%r14), %rcx
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
leaq 0x10301(%rip), %rcx     # 0x278c8
addq $0x10, %rcx
movq %rcx, (%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x1adbe
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x175f9
jmp 0x175f9
movq %rbx, %rdi
movq %rax, %r14
jmp 0x175ff
jmp 0x175f9
movq %rax, %r14
movq %r13, %rdi
movl $0x20, %esi
callq 0x5200
movq %r14, %rdi
callq 0x53d0
nop