// File: /workspace/github_cpp_makefile_star2_O3/Bartor[P]kompilator/middle/abstract_assembler/AbstractAssembler.cpp
// Function: AbstractAssembler::assembleCondition(Condition&, InstructionList&)
SimpleResolution *AbstractAssembler::assembleCondition(Condition &condition, InstructionList &codeBlock) {
    InstructionList &instructions = *new InstructionList();

    Resolution *lhsResolution = resolve(condition.lhs);
    Resolution *rhsResolution = resolve(condition.rhs);

    bool incResolved = false;
    if (rhsResolution->type == CONSTANT || lhsResolution->type == CONSTANT) {
        bool rhsFlag = rhsResolution->type == CONSTANT;

        NumberValue &constantValue = dynamic_cast<NumberValue &>(rhsFlag ? condition.rhs : condition.lhs);
        bool negative = constantValue.value < 0;
        long long valCopy = llabs(constantValue.value);

        if (valCopy < 10) {
            if (rhsFlag) {
                instructions.append(lhsResolution->instructions)
                        .append(lhsResolution->indirect ? static_cast<Instruction *>(new Loadi(primaryAccumulator)) : static_cast<Instruction *>(new Load(lhsResolution->address)));
                while (valCopy-- > 0) {
                    instructions.append(negative ? static_cast<Instruction *>(new Inc()) : static_cast<Instruction *>(new Dec()));
                }
                incResolved = true;
            } else if (condition.type == EQUAL || condition.type == NOT_EQUAL) {
                incResolved = true;
                instructions.append(rhsResolution->instructions)
                        .append(rhsResolution->indirect ? static_cast<Instruction *>(new Loadi(primaryAccumulator)) : static_cast<Instruction *>(new Load(rhsResolution->address)));
                while (valCopy-- > 0) {
                    instructions.append(negative ? static_cast<Instruction *>(new Inc()) : static_cast<Instruction *>(new Dec()));
                }
            }
        }
    }

    if (!incResolved) {
        if (rhsResolution->indirect) {
            Store *store = new Store(expressionAccumulator);
            Sub *sub = new Sub(expressionAccumulator);

            instructions.append(rhsResolution->instructions)
                    .append(rhsResolution->indirect ? static_cast<Instruction *>(new Loadi(primaryAccumulator)) : static_cast<Instruction *>(new Load(rhsResolution->address)))
                    .append(store)
                    .append(lhsResolution->instructions)
                    .append(lhsResolution->indirect ? static_cast<Instruction *>(new Loadi(primaryAccumulator)) : static_cast<Instruction *>(new Load(lhsResolution->address)))
                    .append(sub);
        } else {
            Sub *sub = new Sub(rhsResolution->address);
            instructions.append(lhsResolution->instructions)
                    .append(lhsResolution->indirect ? static_cast<Instruction *>(new Loadi(primaryAccumulator)) : static_cast<Instruction *>(new Load(lhsResolution->address)))
                    .append(sub);
        }
    }

    switch (condition.type) {
        case NOT_EQUAL: {
            Jzero *jzero = new Jzero(codeBlock.end()); // jump to end of code block if they don't subtruct to zero

            instructions.append(jzero);
        }
            break;
        case EQUAL: {
            Jzero *jzero = new Jzero(codeBlock.start()); // jump to start of code block if the subtract to zero
            Jump *jump = new Jump(codeBlock.end()); // otherwise, jump to its end

            instructions.append(jzero)
                    .append(jump);
        }
            break;
        case LESS: {
            Jzero *jzero = new Jzero(codeBlock.end()); // jump if it's zero
            Jpos *jpos = new Jpos(codeBlock.end()); // jump to end if a - b < 0 => a < b

            instructions.append(jzero)
                    .append(jpos);
        }
            break;
        case GREATER: {
            Jzero *jzero = new Jzero(codeBlock.end()); // jump if it's zero
            Jneg *jneg = new Jneg(codeBlock.end()); // jump to end if a - b > 0 => a > b

            instructions.append(jzero)
                    .append(jneg);
        }
            break;
        case LESS_OR_EQUAL: {
            Jpos *jpos = new Jpos(codeBlock.end()); // jump to end if a - b < 0 => a < b

            instructions.append(jpos);
        }
            break;
        case GREATER_OR_EQUAL: {
            Jneg *jneg = new Jneg(codeBlock.end()); // jump to end if a - b > 0 => a > b

            instructions.append(jneg);
        }
            break;
    }

    return new SimpleResolution(
            instructions,
            lhsResolution->temporaryVars + rhsResolution->temporaryVars
    );
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, -0x40(%rbp)
movq %rsi, %r15
movq %rdi, %r12
movl $0x20, %edi
callq 0x51f0
movq %rax, -0x30(%rbp)
movq %rax, %rdi
callq 0x1a830
movq 0x8(%r15), %rsi
movq %r12, %rdi
callq 0x1092e
movq %rax, %r14
movq 0x10(%r15), %rsi
movq %r12, %rdi
callq 0x1092e
movl 0x20(%rax), %ebx
testl %ebx, %ebx
movq %r14, -0x58(%rbp)
movq %rax, -0x50(%rbp)
movq %r15, -0x48(%rbp)
je 0x15bea
cmpl $0x0, 0x20(%r14)
jne 0x15cd6
movq -0x48(%rbp), %r15
leaq 0x8(%r15), %rax
jmp 0x15bee
leaq 0x10(%r15), %rax
movq (%rax), %rdi
leaq 0x10a40(%rip), %rsi     # 0x26638
leaq 0x105c1(%rip), %rdx     # 0x261c0
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x161d5
movq 0x8(%rax), %r13
movq %r13, %r15
negq %r15
cmovsq %r13, %r15
cmpq $0x9, %r15
movq -0x50(%rbp), %rax
ja 0x15cd6
testl %ebx, %ebx
je 0x160c2
movq -0x48(%rbp), %rcx
cmpl $0x1, 0x18(%rcx)
ja 0x15cd6
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rdi
movq %rax, %r14
callq 0x1a902
movq %rax, -0x38(%rbp)
movq %r14, %rax
addq $0x18, %rax
leaq 0x10cb5(%rip), %rcx     # 0x26918
addq $0x10, %rcx
leaq 0x10c0a(%rip), %rbx     # 0x26878
addq $0x10, %rbx
movb 0x1(%r14), %dl
testb %dl, %dl
cmovneq %rcx, %rbx
addq $0x8, %r12
testb %dl, %dl
cmoveq %rax, %r12
movl $0x20, %edi
callq 0x51f0
movq (%r12), %rcx
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
movq %rbx, (%rax)
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x1a9ba
testq %r13, %r13
movq -0x30(%rbp), %r13
movq -0x40(%rbp), %r14
je 0x15e7b
jns 0x16191
leaq 0x11007(%rip), %rbx     # 0x26cd8
jmp 0x16198
movb 0x1(%rax), %bl
movl $0x20, %edi
movq %rax, %r13
callq 0x51f0
movq %rax, %r14
cmpb $0x1, %bl
jne 0x15dd4
movq 0x18(%r12), %rbx
movq $-0x1, %rax
movq %rax, 0x8(%r14)
xorl %eax, %eax
movb %al, 0x10(%r14)
movq %rbx, 0x18(%r14)
leaq 0x10bb5(%rip), %rax     # 0x268c8
addq $0x10, %rax
movq %rax, (%r14)
movl $0x20, %edi
callq 0x51f0
movq %rax, %r15
movq $-0x1, %rax
movq %rax, 0x8(%r15)
xorl %eax, %eax
movb %al, 0x10(%r15)
movq %rbx, 0x18(%r15)
leaq 0x10cc5(%rip), %rax     # 0x26a08
addq $0x10, %rax
movq %rax, (%r15)
movq 0x10(%r13), %rsi
movq -0x30(%rbp), %rdi
callq 0x1a902
movq %rax, -0x38(%rbp)
movq %r13, %rax
addq $0x18, %rax
movb 0x1(%r13), %cl
leaq 0x10b0b(%rip), %rdx     # 0x26878
addq $0x10, %rdx
leaq 0x10ba0(%rip), %r13     # 0x26918
addq $0x10, %r13
xorl %esi, %esi
cmpb %sil, %cl
cmoveq %rdx, %r13
leaq 0x8(%r12), %rbx
cmpb %sil, %cl
cmoveq %rax, %rbx
movl $0x20, %edi
callq 0x51f0
movq (%rbx), %rcx
movq $-0x1, %rdx
movq %rdx, 0x8(%rax)
xorl %edx, %edx
movb %dl, 0x10(%rax)
movq %rcx, 0x18(%rax)
movq %r13, (%rax)
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x1a9ba
movq %rax, %rdi
movq %r14, %rsi
callq 0x1a9ba
movq %rax, %rdi
movq %r15, %r14
jmp 0x15dfb
movq 0x18(%r13), %rax
movq $-0x1, 0x8(%r14)
movb $0x0, 0x10(%r14)
movq %rax, 0x18(%r14)
leaq 0x10c18(%rip), %rax     # 0x26a08
addq $0x10, %rax
movq %rax, (%r14)
movq -0x30(%rbp), %rdi
movq -0x58(%rbp), %r13
movq 0x10(%r13), %rsi
callq 0x1a902
movq %rax, %r15
movq %r13, %rax
addq $0x18, %rax
leaq 0x10aff(%rip), %rcx     # 0x26918
addq $0x10, %rcx
leaq 0x10a54(%rip), %rbx     # 0x26878
addq $0x10, %rbx
movb 0x1(%r13), %dl
testb %dl, %dl
cmovneq %rcx, %rbx
addq $0x8, %r12
testb %dl, %dl
cmoveq %rax, %r12
movl $0x20, %edi
callq 0x51f0
movq (%r12), %rcx
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
movq %rbx, (%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x1a9ba
movq %rax, %rdi
movq %r14, %rsi
callq 0x1a9ba
movq -0x40(%rbp), %r14
movq -0x30(%rbp), %r13
movq -0x48(%rbp), %rax
movl 0x18(%rax), %eax
cmpq $0x5, %rax
movq -0x58(%rbp), %rbx
ja 0x16096
leaq 0x958d(%rip), %rcx      # 0x1f424
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x20, %edi
callq 0x51f0
movq %rax, %r15
movq %r14, %rdi
callq 0x1a8c4
movq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0x10c7b(%rip), %rax     # 0x26b48
addq $0x10, %rax
movq %rax, (%r15)
movl $0x20, %edi
callq 0x51f0
movq %rax, %r12
movq %r14, %rdi
callq 0x1a8ba
movq $-0x1, 0x8(%r12)
movb $0x0, 0x10(%r12)
leaq 0x10ba9(%rip), %rcx     # 0x26aa8
addq $0x10, %rcx
movq %rcx, (%r12)
movq %rax, 0x18(%r12)
jmp 0x16015
movl $0x20, %edi
callq 0x51f0
movq %rax, %r15
movq %r14, %rdi
callq 0x1a8ba
movq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0x10bba(%rip), %rax     # 0x26af8
jmp 0x16084
movl $0x20, %edi
callq 0x51f0
movq %rax, %r15
movq %r14, %rdi
callq 0x1a8ba
movq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0x10bd8(%rip), %rax     # 0x26b48
addq $0x10, %rax
movq %rax, (%r15)
movl $0x20, %edi
callq 0x51f0
movq %rax, %r12
movq %r14, %rdi
callq 0x1a8ba
movq $-0x1, 0x8(%r12)
movb $0x0, 0x10(%r12)
movq %rax, 0x18(%r12)
leaq 0x10b51(%rip), %rax     # 0x26af8
jmp 0x1600d
movl $0x20, %edi
callq 0x51f0
movq %rax, %r15
movq %r14, %rdi
callq 0x1a8ba
movq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0x10b72(%rip), %rax     # 0x26b48
addq $0x10, %rax
movq %rax, (%r15)
movl $0x20, %edi
callq 0x51f0
movq %rax, %r12
movq %r14, %rdi
callq 0x1a8ba
movq $-0x1, 0x8(%r12)
movb $0x0, 0x10(%r12)
movq %rax, 0x18(%r12)
leaq 0x10b8b(%rip), %rax     # 0x26b98
addq $0x10, %rax
movq %rax, (%r12)
movq %r13, %rdi
movq %r15, %rsi
callq 0x1a9ba
movq %rax, %rdi
movq %r12, %rsi
jmp 0x16091
movl $0x20, %edi
callq 0x51f0
movq %rax, %r15
movq %r14, %rdi
callq 0x1a8ba
movq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0x10af3(%rip), %rax     # 0x26b48
jmp 0x16084
movl $0x20, %edi
callq 0x51f0
movq %rax, %r15
movq %r14, %rdi
callq 0x1a8ba
movq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0x10b14(%rip), %rax     # 0x26b98
addq $0x10, %rax
movq %rax, (%r15)
movq %r13, %rdi
movq %r15, %rsi
callq 0x1a9ba
movl $0x10, %edi
callq 0x51f0
movq -0x50(%rbp), %rcx
movq 0x8(%rcx), %rcx
addq 0x8(%rbx), %rcx
movq %rcx, (%rax)
movq %r13, 0x8(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r14), %rsi
movq -0x30(%rbp), %rdi
callq 0x1a902
movq %rax, -0x38(%rbp)
movq %r14, %rax
addq $0x18, %rax
leaq 0x10837(%rip), %rcx     # 0x26918
addq $0x10, %rcx
leaq 0x1078c(%rip), %rbx     # 0x26878
addq $0x10, %rbx
movb 0x1(%r14), %dl
testb %dl, %dl
cmovneq %rcx, %rbx
addq $0x8, %r12
testb %dl, %dl
cmoveq %rax, %r12
movl $0x20, %edi
callq 0x51f0
movq (%r12), %rcx
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
movq %rbx, (%rax)
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x1a9ba
testq %r13, %r13
movq -0x30(%rbp), %r13
movq -0x40(%rbp), %r14
je 0x15e7b
jns 0x1614d
leaq 0x10b8d(%rip), %rbx     # 0x26cd8
jmp 0x16154
leaq 0x10bd4(%rip), %rbx     # 0x26d28
addq $0x10, %rbx
incq %r15
movl $0x18, %edi
callq 0x51f0
movq $0x0, 0x10(%rax)
movq $-0x1, 0x8(%rax)
movq %rbx, (%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x1a9ba
decq %r15
cmpq $0x1, %r15
ja 0x1615b
jmp 0x15e7b
leaq 0x10b90(%rip), %rbx     # 0x26d28
addq $0x10, %rbx
incq %r15
movl $0x18, %edi
callq 0x51f0
movq $0x0, 0x10(%rax)
movq $-0x1, 0x8(%rax)
movq %rbx, (%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x1a9ba
decq %r15
cmpq $0x1, %r15
ja 0x1619f
jmp 0x15e7b
callq 0x5410
jmp 0x161de
jmp 0x161de
movq %rax, %r14
movq %r12, -0x30(%rbp)
jmp 0x161fd
jmp 0x161f1
jmp 0x161f1
jmp 0x161f1
jmp 0x161f1
jmp 0x161f1
movq %rax, %r14
movq %r15, -0x30(%rbp)
jmp 0x161fd
movq %rax, %r14
movl $0x20, %esi
movq -0x30(%rbp), %rdi
callq 0x5200
movq %r14, %rdi
callq 0x53d0
nop