// File: /workspace/github_cpp_makefile_star2_O2/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, -0x38(%rbp)
movq %rsi, %r13
movq %rdi, %r12
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %rbx
movq %rax, %rdi
callq 0x14d30
movq 0x8(%r13), %rsi
movq %r12, %rdi
callq 0xc240
movq %rax, %r15
movq 0x10(%r13), %rsi
movq %r12, %rdi
callq 0xc240
movl 0x20(%rax), %r14d
testl %r14d, %r14d
movq %rax, -0x50(%rbp)
movq %r15, -0x40(%rbp)
movq %r13, -0x48(%rbp)
je 0x10fff
cmpl $0x0, 0x20(%r15)
jne 0x110da
movq -0x48(%rbp), %r13
leaq 0x8(%r13), %rax
jmp 0x11003
leaq 0x10(%r13), %rax
movq (%rax), %rdi
leaq 0xe633(%rip), %rsi      # 0x1f640
leaq 0xe1b4(%rip), %rdx      # 0x1f1c8
xorl %ecx, %ecx
callq 0x51f0
testq %rax, %rax
je 0x11582
movq 0x8(%rax), %r13
movq %r13, %r15
negq %r15
cmovsq %r13, %r15
cmpq $0x9, %r15
movq -0x50(%rbp), %rax
ja 0x110da
testl %r14d, %r14d
je 0x11483
movq -0x48(%rbp), %rcx
cmpl $0x1, 0x18(%rcx)
ja 0x110da
movq 0x10(%rax), %rsi
movq %rbx, %rdi
movq %rax, %r14
callq 0x14dee
movq %rax, -0x30(%rbp)
movq %r14, %rax
movq %r14, %rdx
addq $0x18, %rax
leaq 0xe8a5(%rip), %rcx      # 0x1f920
addq $0x10, %rcx
leaq 0xe7fa(%rip), %r14      # 0x1f880
addq $0x10, %r14
movb 0x1(%rdx), %dl
testb %dl, %dl
cmovneq %rcx, %r14
addq $0x8, %r12
testb %dl, %dl
cmoveq %rax, %r12
pushq $0x20
popq %rdi
callq 0x51c0
movq (%r12), %rcx
orq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
movq %r14, (%rax)
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x14e9c
testq %r13, %r13
jns 0x11541
leaq 0xec0b(%rip), %r12      # 0x1fce0
jmp 0x11548
movq %rbx, -0x30(%rbp)
movb 0x1(%rax), %r15b
pushq $0x20
popq %rdi
movq %rax, %rbx
callq 0x51c0
movq %rax, %r14
cmpb $0x1, %r15b
jne 0x111c5
movq 0x18(%r12), %r13
orq $-0x1, 0x8(%r14)
xorl %eax, %eax
movb %al, 0x10(%r14)
movq %r13, 0x18(%r14)
leaq 0xe7bb(%rip), %rax      # 0x1f8d0
addq $0x10, %rax
movq %rax, (%r14)
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r15
orq $-0x1, 0x8(%rax)
xorl %eax, %eax
movb %al, 0x10(%r15)
movq %r13, 0x18(%r15)
leaq 0xe8d3(%rip), %rax      # 0x1fa10
addq $0x10, %rax
movq %rax, (%r15)
movq 0x10(%rbx), %rsi
movq -0x30(%rbp), %rdi
callq 0x14dee
movq %rax, -0x58(%rbp)
movq %rbx, %rax
addq $0x18, %rax
movb 0x1(%rbx), %cl
leaq 0xe71a(%rip), %rdx      # 0x1f880
addq $0x10, %rdx
leaq 0xe7af(%rip), %r13      # 0x1f920
addq $0x10, %r13
xorl %esi, %esi
cmpb %sil, %cl
cmoveq %rdx, %r13
leaq 0x8(%r12), %rbx
cmpb %sil, %cl
cmoveq %rax, %rbx
pushq $0x20
popq %rdi
callq 0x51c0
movq (%rbx), %rcx
orq $-0x1, 0x8(%rax)
xorl %edx, %edx
movb %dl, 0x10(%rax)
movq %rcx, 0x18(%rax)
movq %r13, (%rax)
movq -0x58(%rbp), %rdi
movq %rax, %rsi
callq 0x14e9c
movq %rax, %rdi
movq %r14, %rsi
callq 0x14e9c
movq %rax, %rdi
movq %r15, %r14
jmp 0x111e9
movq 0x18(%rbx), %rax
orq $-0x1, 0x8(%r14)
movb $0x0, 0x10(%r14)
movq %rax, 0x18(%r14)
leaq 0xe832(%rip), %rax      # 0x1fa10
addq $0x10, %rax
movq %rax, (%r14)
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %r13
movq 0x10(%r13), %rsi
callq 0x14dee
movq %rax, %r15
movq %r13, %rax
addq $0x18, %rax
leaq 0xe719(%rip), %rcx      # 0x1f920
addq $0x10, %rcx
leaq 0xe66e(%rip), %rbx      # 0x1f880
addq $0x10, %rbx
movb 0x1(%r13), %dl
testb %dl, %dl
cmovneq %rcx, %rbx
addq $0x8, %r12
testb %dl, %dl
cmoveq %rax, %r12
pushq $0x20
popq %rdi
callq 0x51c0
movq (%r12), %rcx
orq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
movq %rbx, (%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x14e9c
movq %rax, %rdi
movq %r14, %rsi
callq 0x14e9c
movq -0x30(%rbp), %rbx
movq -0x48(%rbp), %rax
movl 0x18(%rax), %eax
cmpq $0x5, %rax
ja 0x11459
leaq 0x71ac(%rip), %rcx      # 0x18424
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r15
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x14db2
orq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0xe8a3(%rip), %rax      # 0x1fb50
addq $0x10, %rax
movq %rax, (%r15)
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r12
movq %r14, %rdi
callq 0x14da8
orq $-0x1, 0x8(%r12)
movb $0x0, 0x10(%r12)
leaq 0xe7d6(%rip), %rcx      # 0x1fab0
addq $0x10, %rcx
movq %rcx, (%r12)
movq %rax, 0x18(%r12)
jmp 0x113e0
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r15
movq -0x38(%rbp), %rdi
callq 0x14da8
orq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0xe7eb(%rip), %rax      # 0x1fb00
jmp 0x11447
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r15
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x14da8
orq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0xe80a(%rip), %rax      # 0x1fb50
addq $0x10, %rax
movq %rax, (%r15)
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r12
movq %r14, %rdi
callq 0x14da8
orq $-0x1, 0x8(%r12)
movb $0x0, 0x10(%r12)
movq %rax, 0x18(%r12)
leaq 0xe788(%rip), %rax      # 0x1fb00
jmp 0x113d8
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r15
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x14da8
orq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0xe7aa(%rip), %rax      # 0x1fb50
addq $0x10, %rax
movq %rax, (%r15)
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r12
movq %r14, %rdi
callq 0x14da8
orq $-0x1, 0x8(%r12)
movb $0x0, 0x10(%r12)
movq %rax, 0x18(%r12)
leaq 0xe7c8(%rip), %rax      # 0x1fba0
addq $0x10, %rax
movq %rax, (%r12)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x14e9c
movq %rax, %rdi
movq %r12, %rsi
jmp 0x11454
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r15
movq -0x38(%rbp), %rdi
callq 0x14da8
orq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0xe734(%rip), %rax      # 0x1fb50
jmp 0x11447
pushq $0x20
popq %rdi
callq 0x51c0
movq %rax, %r15
movq -0x38(%rbp), %rdi
callq 0x14da8
orq $-0x1, 0x8(%r15)
movb $0x0, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0xe759(%rip), %rax      # 0x1fba0
addq $0x10, %rax
movq %rax, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x14e9c
pushq $0x10
popq %rdi
callq 0x51c0
movq -0x50(%rbp), %rcx
movq 0x8(%rcx), %rcx
addq 0x8(%r13), %rcx
movq %rcx, (%rax)
movq %rbx, 0x8(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x40(%rbp), %r14
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x14dee
movq %rax, -0x30(%rbp)
movq %r14, %rax
movq %r14, %rdx
addq $0x18, %rax
leaq 0xe478(%rip), %rcx      # 0x1f920
addq $0x10, %rcx
leaq 0xe3cd(%rip), %r14      # 0x1f880
addq $0x10, %r14
movb 0x1(%rdx), %dl
testb %dl, %dl
cmovneq %rcx, %r14
addq $0x8, %r12
testb %dl, %dl
cmoveq %rax, %r12
pushq $0x20
popq %rdi
callq 0x51c0
movq (%r12), %rcx
orq $-0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
movq %r14, (%rax)
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x14e9c
testq %r13, %r13
jns 0x11500
leaq 0xe7e2(%rip), %r12      # 0x1fce0
jmp 0x11507
leaq 0xe829(%rip), %r12      # 0x1fd30
addq $0x10, %r12
pushq $0x18
popq %r14
movq -0x40(%rbp), %r13
testq %r15, %r15
jle 0x11260
decq %r15
movq %r14, %rdi
callq 0x51c0
andq $0x0, 0x10(%rax)
orq $-0x1, 0x8(%rax)
movq %r12, (%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x14e9c
jmp 0x11513
leaq 0xe7e8(%rip), %r12      # 0x1fd30
addq $0x10, %r12
pushq $0x18
popq %r14
movq -0x40(%rbp), %r13
testq %r15, %r15
jle 0x11260
decq %r15
movq %r14, %rdi
callq 0x51c0
andq $0x0, 0x10(%rax)
orq $-0x1, 0x8(%rax)
movq %r12, (%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x14e9c
jmp 0x11554
callq 0x53d0
jmp 0x1158b
jmp 0x1158b
movq %rax, %r14
movq %r12, %rdi
jmp 0x115ab
jmp 0x1159d
jmp 0x1159d
jmp 0x1159d
jmp 0x1159d
jmp 0x1159d
movq %rax, %r14
movq %r15, %rdi
jmp 0x115ab
movq %rbx, %rdi
movq %rax, %r14
pushq $0x20
popq %rsi
callq 0x51d0
movq %r14, %rdi
callq 0x5390
nop