// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, -0x48(%rbp)
movq %rdi, %rbx
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r13
movq %rax, %rdi
callq 0x14d30
cmpq $0x0, 0x8(%rbx)
je 0x11fdc
movq %r12, -0x58(%rbp)
movq %r14, -0x50(%rbp)
movq %r13, -0x38(%rbp)
movq -0x48(%rbp), %rax
leaq 0x10(%rax), %rsi
leaq -0x88(%rbp), %r15
movq %r15, %rdi
callq 0x1188c
movq (%r15), %r10
movq 0x8(%rbx), %r12
movq %r12, %r11
negq %r11
cmovsq %r12, %r11
movq 0x8(%r15), %rax
movq %rax, -0x68(%rbp)
xorl %r8d, %r8d
xorl %r14d, %r14d
xorl %r13d, %r13d
movq %rbx, %rdi
movq %rbx, -0x30(%rbp)
movq %r11, -0x60(%rbp)
cmpq -0x68(%rbp), %r10
je 0x11e62
movq (%r10), %r15
cmpq %rdi, %r15
je 0x11e62
movq 0x8(%r15), %rax
movq %rax, %rcx
negq %rcx
cmovsq %rax, %rcx
cmpq %r11, %rcx
ja 0x11e62
testq %r12, %r12
setg %dl
testq %rax, %rax
setg %sil
testb %dl, %sil
movq %r14, -0x70(%rbp)
movq %r10, -0x40(%rbp)
je 0x11dd7
cmpq %rcx, %r11
ja 0x11de7
movq %r8, %r14
jmp 0x11e1b
testq %rax, %r12
sets %dl
cmpq %rcx, %r11
seta %cl
testb %cl, %dl
je 0x11dd2
movq %r8, %r14
movq %r15, %rbx
testq %r13, %r13
je 0x11e1e
movq %r12, %rcx
subq %rax, %rcx
movq %rcx, %rdx
negq %rdx
cmovsq %rcx, %rdx
movq %r12, %rcx
subq 0x8(%r13), %rcx
movq %rcx, %rsi
negq %rsi
cmovsq %rcx, %rsi
movq %r15, %rbx
cmpq %rsi, %rdx
jb 0x11e1e
movq %r13, %rbx
testq %rax, %rax
setg %r13b
movq %r15, %rsi
callq 0x11c16
cmpq %r14, %rax
setg %cl
sete %dl
andb %r13b, %dl
orb %cl, %dl
jne 0x11e44
movq %r14, %rax
movq -0x70(%rbp), %r15
movq -0x40(%rbp), %r10
addq $0x8, %r10
movq %rax, %r8
movq %r15, %r14
movq %rbx, %r13
movq -0x30(%rbp), %rdi
movq -0x60(%rbp), %r11
jmp 0x11d86
movq %r8, -0x40(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x118e4
movq -0x30(%rbp), %r10
testq %r13, %r13
je 0x11f37
movq 0x8(%r10), %rax
subq 0x8(%r13), %rax
movq %rax, %rcx
negq %rcx
cmovsq %rax, %rcx
cmpq $0x9, %rcx
ja 0x11f37
pushq $0x20
popq %rdi
callq 0x51c0
movq (%r13), %rcx
orq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
leaq 0xd9c5(%rip), %rcx      # 0x1f880
addq $0x10, %rcx
movq %rcx, (%rax)
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x14e9c
xorl %r14d, %r14d
pushq $0x18
popq %r15
leaq 0xde04(%rip), %rbx      # 0x1fce0
movq -0x30(%rbp), %rax
movq 0x8(%rax), %r12
movq %r12, %rax
subq 0x8(%r13), %rax
movq %rax, %rcx
negq %rcx
cmovsq %rax, %rcx
cmpq %rcx, %r14
jae 0x12042
movq %r15, %rdi
callq 0x51c0
andq $0x0, 0x10(%rax)
orq $-0x1, 0x8(%rax)
movq %rbx, %rcx
testq %r12, %r12
jns 0x11f1f
leaq 0xde11(%rip), %rcx      # 0x1fd30
addq $0x10, %rcx
movq %rcx, (%rax)
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x14e9c
incq %r14
jmp 0x11edc
testq %r14, %r14
movq -0x38(%rbp), %r13
movq -0x58(%rbp), %r12
je 0x120ff
movq %r14, %r15
movq 0x20(%r10), %rax
movq -0x40(%rbp), %rdi
cmpq %rax, %rdi
jne 0x12075
pushq $0x20
popq %rbx
movq %rbx, %rdi
callq 0x51c0
orq $-0x1, 0x8(%rax)
xorl %ecx, %ecx
movb %cl, 0x10(%rax)
movq %r12, 0x18(%rax)
leaq 0xda94(%rip), %r14      # 0x1fa10
addq $0x10, %r14
movq %r14, (%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x14e9c
movq %rax, %r13
movq %rbx, %rdi
callq 0x51c0
movq (%r15), %rcx
orq $-0x1, 0x8(%rax)
xorl %edx, %edx
movb %dl, 0x10(%rax)
xorl %r15d, %r15d
movq %rcx, 0x18(%rax)
movq %r14, (%rax)
movq %r13, %rdi
movq -0x38(%rbp), %r13
movq %rax, %rsi
callq 0x14e9c
movq %rax, %r12
movq %rbx, %rdi
callq 0x51c0
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
orq $-0x1, 0x8(%rax)
movb %r15b, 0x10(%rax)
jmp 0x12028
pushq $0x20
popq %r15
movq %r15, %rdi
callq 0x51c0
orq $-0x1, 0x8(%rax)
xorl %r14d, %r14d
movb %r14b, 0x10(%rax)
movq %r12, 0x18(%rax)
leaq 0xda11(%rip), %rcx      # 0x1fa10
addq $0x10, %rcx
movq %rcx, (%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x14e9c
movq %rax, %r12
movq %r15, %rdi
callq 0x51c0
movq (%rbx), %rcx
orq $-0x1, 0x8(%rax)
movb %r14b, 0x10(%rax)
movq %rcx, 0x18(%rax)
leaq 0xd89d(%rip), %rcx      # 0x1f8d0
addq $0x10, %rcx
movq %rcx, (%rax)
movq %r12, %rdi
jmp 0x124b4
pushq $0x20
popq %rdi
callq 0x51c0
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
orq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
leaq 0xd86b(%rip), %rcx      # 0x1f8d0
addq $0x10, %rcx
movq %rcx, (%rax)
movq -0x38(%rbp), %r13
jmp 0x124b1
movq 0x8(%r15), %r14
movq 0x20(%r15), %rbx
subq %rdi, %rbx
leaq (%rbx,%rbx,4), %rcx
addq %r14, %rcx
addq $-0x5, %rcx
pushq $0x14
popq %rdx
pushq $0xa
popq %rcx
cmovsq %rdx, %rcx
movq 0x18(%r10), %rdx
leaq -0x1(%rax), %rsi
cmpq %rax, %rdi
jae 0x120c2
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
jmp 0x1209d
cmpq 0x10(%r10), %rcx
jge 0x120ff
pushq $0x20
popq %rdi
callq 0x51c0
testq %r14, %r14
jle 0x12284
movq (%r15), %rcx
orq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
leaq 0xd790(%rip), %rcx      # 0x1f880
addq $0x10, %rcx
movq %rcx, (%rax)
movq %r13, %rdi
jmp 0x122cd
pushq $0x20
popq %rdi
callq 0x51c0
orq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r12, 0x18(%rax)
leaq 0xd8f5(%rip), %rcx      # 0x1fa10
addq $0x10, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, (%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x14e9c
movq -0x30(%rbp), %rdx
xorl %r14d, %r14d
leaq 0xdba1(%rip), %rbx      # 0x1fce0
addq $0x10, %rbx
pushq $0x1
popq %r12
leaq 0xd912(%rip), %r13      # 0x1fa60
addq $0x10, %r13
movq 0x20(%rdx), %rax
cmpq %rax, %r14
jae 0x121da
movq 0x18(%rdx), %rcx
cmpb $0x31, (%rcx,%r14)
jne 0x1218f
pushq $0x18
popq %rdi
callq 0x51c0
andq $0x0, 0x10(%rax)
orq $-0x1, 0x8(%rax)
movq %rbx, (%rax)
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x14e9c
movq -0x30(%rbp), %rdx
movq 0x20(%rdx), %rax
decq %rax
cmpq %rax, %r14
je 0x121d2
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r15
movq -0x48(%rbp), %rdi
movq %r12, %rsi
callq 0x12924
movq (%rax), %rax
orq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
movq %r13, (%r15)
movq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x14e9c
movq -0x30(%rbp), %rdx
incq %r14
jmp 0x12152
cmpq $0x0, 0x8(%rdx)
movq -0x38(%rbp), %r13
movq %rdx, %r14
jns 0x12274
pushq $0x20
popq %rbx
movq %rbx, %rdi
callq 0x51c0
orq $-0x1, 0x8(%rax)
xorl %ecx, %ecx
movb %cl, 0x10(%rax)
movq -0x50(%rbp), %r15
movq %r15, 0x18(%rax)
leaq 0xd6c0(%rip), %rcx      # 0x1f8d0
addq $0x10, %rcx
movq %rcx, (%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x14e9c
movq %rax, %r12
movq %rbx, %rdi
callq 0x51c0
orq $-0x1, 0x8(%rax)
xorl %ecx, %ecx
movb %cl, 0x10(%rax)
movq %r15, 0x18(%rax)
movq -0x40(%rbp), %rbx
movq %rbx, (%rax)
movq %r12, %rdi
movq %rax, %rsi
callq 0x14e9c
movq %rax, %r12
pushq $0x20
popq %rdi
callq 0x51c0
orq $-0x1, 0x8(%rax)
xorl %ecx, %ecx
movb %cl, 0x10(%rax)
movq %r15, 0x18(%rax)
movq %rbx, (%rax)
movq %r12, %rdi
movq %rax, %rsi
callq 0x14e9c
pushq $0x20
popq %rdi
callq 0x51c0
movq (%r14), %rcx
jmp 0x12496
orq $-0x1, 0x8(%rax)
xorl %ecx, %ecx
movb %cl, 0x10(%rax)
movq %r12, 0x18(%rax)
leaq 0xd777(%rip), %r12      # 0x1fa10
addq $0x10, %r12
movq %r12, (%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x14e9c
movq %rax, %r14
pushq $0x20
popq %rdi
callq 0x51c0
movq (%r15), %rcx
orq $-0x1, 0x8(%rax)
xorl %edx, %edx
movb %dl, 0x10(%rax)
movq %rcx, 0x18(%rax)
movq %r12, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x14e9c
xorl %r14d, %r14d
testq %rbx, %rbx
cmovgq %rbx, %r14
pushq $0x20
popq %r12
leaq 0xd776(%rip), %rbx      # 0x1fa60
addq $0x10, %rbx
movq %r12, %rdi
callq 0x51c0
movq %rax, %r15
subq $0x1, %r14
jb 0x1232c
movq -0x48(%rbp), %rdi
pushq $-0x1
popq %rsi
callq 0x12924
movq (%rax), %rax
orq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
movq %rbx, (%r15)
movq %r13, %rdi
movq %r15, %rsi
callq 0x14e9c
jmp 0x122ee
pushq $0x1
popq %rsi
movq -0x48(%rbp), %rdi
callq 0x12924
movq (%rax), %rax
orq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
movq %rbx, (%r15)
movq %r13, %rdi
movq %r15, %rsi
callq 0x14e9c
leaq 0xd982(%rip), %r14      # 0x1fce0
addq $0x10, %r14
movq -0x30(%rbp), %r15
movq -0x40(%rbp), %r12
movq 0x20(%r15), %rax
cmpq %rax, %r12
jae 0x123f0
movq 0x18(%r15), %rcx
cmpb $0x31, (%rcx,%r12)
jne 0x123a6
pushq $0x18
popq %rdi
callq 0x51c0
andq $0x0, 0x10(%rax)
orq $-0x1, 0x8(%rax)
movq %r14, (%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x14e9c
movq -0x30(%rbp), %r15
movq 0x20(%r15), %rax
decq %rax
cmpq %rax, %r12
je 0x123e8
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r15
movq -0x48(%rbp), %rdi
pushq $0x1
popq %rsi
callq 0x12924
movq (%rax), %rax
orq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
movq %rbx, (%r15)
movq %r13, %rdi
movq %r15, %rsi
callq 0x14e9c
movq -0x30(%rbp), %r15
incq %r12
jmp 0x1236a
cmpq $0x0, 0x8(%r15)
jns 0x1248b
pushq $0x20
popq %r14
movq %r14, %rdi
callq 0x51c0
orq $-0x1, 0x8(%rax)
xorl %ecx, %ecx
movb %cl, 0x10(%rax)
movq -0x50(%rbp), %rbx
movq %rbx, 0x18(%rax)
leaq 0xd4b0(%rip), %rcx      # 0x1f8d0
addq $0x10, %rcx
movq %rcx, (%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x14e9c
movq %rax, %r12
movq %r14, %rdi
callq 0x51c0
orq $-0x1, 0x8(%rax)
xorl %ecx, %ecx
movb %cl, 0x10(%rax)
movq %rbx, 0x18(%rax)
leaq 0xd5be(%rip), %r14      # 0x1fa10
addq $0x10, %r14
movq %r14, (%rax)
movq %r12, %rdi
movq %rax, %rsi
callq 0x14e9c
movq %rax, %r12
pushq $0x20
popq %rdi
callq 0x51c0
orq $-0x1, 0x8(%rax)
xorl %ecx, %ecx
movb %cl, 0x10(%rax)
movq %rbx, 0x18(%rax)
movq %r14, (%rax)
movq %r12, %rdi
movq %rax, %rsi
callq 0x14e9c
pushq $0x20
popq %rdi
callq 0x51c0
movq (%r15), %rcx
orq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
leaq 0xd426(%rip), %rcx      # 0x1f8d0
addq $0x10, %rcx
movq %rcx, (%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x14e9c
movq %r13, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x124df
movq %rax, %r14
pushq $0x20
popq %rsi
movq %r13, %rdi
jmp 0x124e8
jmp 0x124df
jmp 0x124df
movq %rax, %r14
pushq $0x20
popq %rsi
movq %r15, %rdi
callq 0x51d0
movq %r14, %rdi
callq 0x5390
nop