// File: /workspace/github_cpp_makefile_star2_O1/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 0x1ac28
movq 0x8(%r15), %rsi
movq %r12, %rdi
callq 0x10ae0
movq %rax, %r14
movq 0x10(%r15), %rsi
movq %r12, %rdi
callq 0x10ae0
movl 0x20(%rax), %ebx
testl %ebx, %ebx
movq %r14, -0x58(%rbp)
movq %rax, -0x50(%rbp)
movq %r15, -0x48(%rbp)
je 0x15e6a
cmpl $0x0, 0x20(%r14)
jne 0x15f56
movq -0x48(%rbp), %r15
leaq 0x8(%r15), %rax
jmp 0x15e6e
leaq 0x10(%r15), %rax
movq (%rax), %rdi
leaq 0x11760(%rip), %rsi     # 0x275d8
leaq 0x11309(%rip), %rdx     # 0x27188
xorl %ecx, %ecx
callq 0x5240
testq %rax, %rax
je 0x16469
movq 0x8(%rax), %r13
movq %r13, %r15
negq %r15
cmovsq %r13, %r15
cmpq $0x9, %r15
movq -0x50(%rbp), %rax
ja 0x15f56
testl %ebx, %ebx
je 0x16342
movq -0x48(%rbp), %rcx
cmpl $0x1, 0x18(%rcx)
ja 0x15f56
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rdi
movq %rax, %r14
callq 0x1acfc
movq %rax, -0x38(%rbp)
movq %r14, %rax
addq $0x18, %rax
leaq 0x11a35(%rip), %rcx     # 0x27918
addq $0x10, %rcx
leaq 0x1198a(%rip), %rbx     # 0x27878
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 0x1adbe
testq %r13, %r13
movq -0x30(%rbp), %r13
movq -0x40(%rbp), %r14
je 0x160fb
jns 0x1641b
leaq 0x11d87(%rip), %rbx     # 0x27cd8
jmp 0x16422
movb 0x1(%rax), %bl
movl $0x20, %edi
movq %rax, %r13
callq 0x51f0
movq %rax, %r14
cmpb $0x1, %bl
jne 0x16054
movq 0x18(%r12), %rbx
movq $-0x1, %rax
movq %rax, 0x8(%r14)
xorl %eax, %eax
movb %al, 0x10(%r14)
movq %rbx, 0x18(%r14)
leaq 0x11935(%rip), %rax     # 0x278c8
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 0x11a45(%rip), %rax     # 0x27a08
addq $0x10, %rax
movq %rax, (%r15)
movq 0x10(%r13), %rsi
movq -0x30(%rbp), %rdi
callq 0x1acfc
movq %rax, -0x38(%rbp)
movq %r13, %rax
addq $0x18, %rax
movb 0x1(%r13), %cl
leaq 0x1188b(%rip), %rdx     # 0x27878
addq $0x10, %rdx
leaq 0x11920(%rip), %r13     # 0x27918
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 0x1adbe
movq %rax, %rdi
movq %r14, %rsi
callq 0x1adbe
movq %rax, %rdi
movq %r15, %r14
jmp 0x1607b
movq 0x18(%r13), %rax
movq $-0x1, 0x8(%r14)
movb $0x0, 0x10(%r14)
movq %rax, 0x18(%r14)
leaq 0x11998(%rip), %rax     # 0x27a08
addq $0x10, %rax
movq %rax, (%r14)
movq -0x30(%rbp), %rdi
movq -0x58(%rbp), %r13
movq 0x10(%r13), %rsi
callq 0x1acfc
movq %rax, %r15
movq %r13, %rax
addq $0x18, %rax
leaq 0x1187f(%rip), %rcx     # 0x27918
addq $0x10, %rcx
leaq 0x117d4(%rip), %rbx     # 0x27878
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 0x1adbe
movq %rax, %rdi
movq %r14, %rsi
callq 0x1adbe
movq -0x40(%rbp), %r14
movq -0x30(%rbp), %r13
movq -0x48(%rbp), %rax
movl 0x18(%rax), %eax
cmpq $0x5, %rax
movq -0x58(%rbp), %rbx
ja 0x16316
leaq 0xa31d(%rip), %rcx      # 0x20434
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x20, %edi
callq 0x51f0
movq %rax, %r15
movq %r14, %rdi
callq 0x1acbe
movq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0x119fb(%rip), %rax     # 0x27b48
addq $0x10, %rax
movq %rax, (%r15)
movl $0x20, %edi
callq 0x51f0
movq %rax, %r12
movq %r14, %rdi
callq 0x1acb4
movq $-0x1, 0x8(%r12)
movb $0x0, 0x10(%r12)
leaq 0x11929(%rip), %rcx     # 0x27aa8
addq $0x10, %rcx
movq %rcx, (%r12)
movq %rax, 0x18(%r12)
jmp 0x16295
movl $0x20, %edi
callq 0x51f0
movq %rax, %r15
movq %r14, %rdi
callq 0x1acb4
movq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0x1193a(%rip), %rax     # 0x27af8
jmp 0x16304
movl $0x20, %edi
callq 0x51f0
movq %rax, %r15
movq %r14, %rdi
callq 0x1acb4
movq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0x11958(%rip), %rax     # 0x27b48
addq $0x10, %rax
movq %rax, (%r15)
movl $0x20, %edi
callq 0x51f0
movq %rax, %r12
movq %r14, %rdi
callq 0x1acb4
movq $-0x1, 0x8(%r12)
movb $0x0, 0x10(%r12)
movq %rax, 0x18(%r12)
leaq 0x118d1(%rip), %rax     # 0x27af8
jmp 0x1628d
movl $0x20, %edi
callq 0x51f0
movq %rax, %r15
movq %r14, %rdi
callq 0x1acb4
movq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0x118f2(%rip), %rax     # 0x27b48
addq $0x10, %rax
movq %rax, (%r15)
movl $0x20, %edi
callq 0x51f0
movq %rax, %r12
movq %r14, %rdi
callq 0x1acb4
movq $-0x1, 0x8(%r12)
movb $0x0, 0x10(%r12)
movq %rax, 0x18(%r12)
leaq 0x1190b(%rip), %rax     # 0x27b98
addq $0x10, %rax
movq %rax, (%r12)
movq %r13, %rdi
movq %r15, %rsi
callq 0x1adbe
movq %rax, %rdi
movq %r12, %rsi
jmp 0x16311
movl $0x20, %edi
callq 0x51f0
movq %rax, %r15
movq %r14, %rdi
callq 0x1acb4
movq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0x11873(%rip), %rax     # 0x27b48
jmp 0x16304
movl $0x20, %edi
callq 0x51f0
movq %rax, %r15
movq %r14, %rdi
callq 0x1acb4
movq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0x11894(%rip), %rax     # 0x27b98
addq $0x10, %rax
movq %rax, (%r15)
movq %r13, %rdi
movq %r15, %rsi
callq 0x1adbe
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 0x1acfc
movq %rax, -0x38(%rbp)
movq %r14, %rax
addq $0x18, %rax
leaq 0x115b7(%rip), %rcx     # 0x27918
addq $0x10, %rcx
leaq 0x1150c(%rip), %rbx     # 0x27878
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 0x1adbe
testq %r13, %r13
movq -0x30(%rbp), %r13
movq -0x40(%rbp), %r14
je 0x160fb
jns 0x163cd
leaq 0x1190d(%rip), %rbx     # 0x27cd8
jmp 0x163d4
leaq 0x11954(%rip), %rbx     # 0x27d28
addq $0x10, %rbx
incq %r15
movl $0x18, %edi
callq 0x51f0
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rbx, (%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x1adbe
decq %r15
cmpq $0x1, %r15
jg 0x163db
jmp 0x160fb
leaq 0x11906(%rip), %rbx     # 0x27d28
addq $0x10, %rbx
incq %r15
movl $0x18, %edi
callq 0x51f0
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rbx, (%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x1adbe
decq %r15
cmpq $0x1, %r15
jg 0x16429
jmp 0x160fb
callq 0x5410
jmp 0x16472
jmp 0x16472
movq %rax, %r14
movq %r12, -0x30(%rbp)
jmp 0x16491
jmp 0x16485
jmp 0x16485
jmp 0x16485
jmp 0x16485
jmp 0x16485
movq %rax, %r14
movq %r15, -0x30(%rbp)
jmp 0x16491
movq %rax, %r14
movl $0x20, %esi
movq -0x30(%rbp), %rdi
callq 0x5200
movq %r14, %rdi
callq 0x53d0
nop