// File: Bartor[P]kompilator/middle/abstract_assembler/Constants.cpp
// Function: Constants::addConstant(long long)
Constant *Constants::addConstant(long long value) {
    if (value == 1 || value == -1) return nullptr;

    for (const auto &constant : constants) {
        if (constant->value == value) {
            return nullptr;
        }
    }

    ResolvableAddress &address = *new ResolvableAddress(currentAddress++); // assign a new address to it
    Constant *constant = new Constant(value, address); // create it with new value

    if (constants.size() == 0) {
        constants.push_back(constant);
    } else {
        for (long long i = 0; i < constants.size(); i++) {
            if (llabs(constants[i]->value) > llabs(value)) {
                constants.insert(constants.begin() + i, constant);
                break;
            }
            if (i == constants.size() - 1) {
                constants.push_back(constant);
                break;
            }
        }
    }
    return constant;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x1, -0x18(%rbp)
je 0x2334c
cmpq $-0x1, -0x18(%rbp)
jne 0x23359
movq $0x0, -0x8(%rbp)
jmp 0x235d8
movq -0x88(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1f8d0
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1f900
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1f930
testb $0x1, %al
jne 0x23395
jmp 0x233cd
leaq -0x28(%rbp), %rdi
callq 0x1f970
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
cmpq -0x18(%rbp), %rax
jne 0x233c0
movq $0x0, -0x8(%rbp)
jmp 0x235d8
jmp 0x233c2
leaq -0x28(%rbp), %rdi
callq 0x1f990
jmp 0x23382
movl $0x10, %edi
callq 0x6260
movq %rax, %rdi
movq -0x88(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x90(%rbp)
movq 0x28(%rax), %rsi
movq %rsi, %rcx
incq %rcx
movq %rcx, 0x28(%rax)
callq 0x1fa40
jmp 0x23400
movq -0x90(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x38, %edi
callq 0x6260
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x21d60
jmp 0x23431
movq -0x88(%rbp), %rdi
movq -0x98(%rbp), %rax
movq %rax, -0x58(%rbp)
addq $0x10, %rdi
callq 0x20600
cmpq $0x0, %rax
jne 0x234af
movq -0x88(%rbp), %rdi
addq $0x10, %rdi
leaq -0x58(%rbp), %rsi
callq 0x1fe80
jmp 0x235d0
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x10, %esi
callq 0x6270
jmp 0x235e5
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x38, %esi
callq 0x6270
jmp 0x235e5
movq $0x0, -0x60(%rbp)
movq -0x88(%rbp), %rdi
movq -0x60(%rbp), %rax
movq %rax, -0xa0(%rbp)
addq $0x10, %rdi
callq 0x20600
movq %rax, %rcx
movq -0xa0(%rbp), %rax
cmpq %rcx, %rax
jae 0x235ce
movq -0x88(%rbp), %rdi
addq $0x10, %rdi
movq -0x60(%rbp), %rsi
callq 0x239e0
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, %rcx
negq %rcx
cmovnsq %rcx, %rax
movq -0x18(%rbp), %rcx
movq %rcx, %rdx
negq %rdx
cmovnsq %rdx, %rcx
cmpq %rcx, %rax
jle 0x23577
movq -0x88(%rbp), %rdi
movq %rdi, %rax
addq $0x10, %rax
movq %rax, -0xa8(%rbp)
addq $0x10, %rdi
callq 0x1f8d0
movq %rax, -0x78(%rbp)
movq -0x60(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x23c00
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x23c40
movq -0xa8(%rbp), %rdi
movq -0x68(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0x23a00
movq %rax, -0x80(%rbp)
jmp 0x235ce
movq -0x88(%rbp), %rdi
movq -0x60(%rbp), %rax
movq %rax, -0xb0(%rbp)
addq $0x10, %rdi
callq 0x20600
movq %rax, %rcx
movq -0xb0(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x235bb
movq -0x88(%rbp), %rdi
addq $0x10, %rdi
leaq -0x58(%rbp), %rsi
callq 0x1fe80
jmp 0x235ce
jmp 0x235bd
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x234b7
jmp 0x235d0
movq -0x58(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x64d0
nop