// File: AndreyVV-100[P]GachiLang/Backend/Backend.cpp
// Function: PrintComp(Elf*, element*, Stack*)
bool PrintComp  (Elf* elf, element* el, Stack* vars)
{
    print_ass;

    if (el->type != COND)
        return 1;

    TryPrint (PrintArith, left);
    ElfAddBytes (elf, "\x66\x48\x0F\x7E\xC2", 5); // movq rdx, xmm0

    elf->xmm_counter = 0;
    TryPrint (PrintArith, right);
    ElfAddBytes (elf, "\x66\x48\x0F\x6E\xCA", 5); // movq xmm1, rdx

    switch (el->len)
    {
        case 1:
        {
            switch (el->ind[0])
            {
                case '>':
                    ElfAddBytes (elf, "\xC5\xF3\xC2\xD0\x02", 5); // vcmpsd xmm2, xmm1, xmm0, 2
                    break;

                case '<':
                    ElfAddBytes (elf, "\xC5\xF3\xC2\xD0\x0D", 5); // vcmpsd xmm2, xmm1, xmm0, 0x0D
                    break;

                default:
                    return 1;
            }
            break;
        }

        case 2:
        {
            switch (el->ind[0])
            {
                case '>':
                    ElfAddBytes (elf, "\xC5\xF3\xC2\xD0\x01", 5); // vcmpsd xmm2, xmm1, xmm0, 1
                    break;

                case '<':
                    ElfAddBytes (elf, "\xC5\xF3\xC2\xD0\x0E", 5); // vcmpsd xmm2, xmm1, xmm0, 0x0E
                    break;

                case '!':
                    ElfAddBytes (elf, "\xC5\xF3\xC2\xD0\x00", 5); // vcmpsd xmm2, xmm1, xmm0, 0
                    break;

                case '=':
                    ElfAddBytes (elf, "\xC5\xF3\xC2\xD0\x04", 5); // vcmpsd xmm2, xmm1, xmm0, 4
                    break;

                default:
                    return 1;
            }
            break;
        }

        default:
            return 1;
    }

    ElfAddBytes (elf, "\x66\x48\x0F\x7E\xD0"        // movq rax, xmm2
                      "\x48\x83\xF8\x00"            // cmp rax, 0
                      "\x0F\x85\x00\x00\x00\x00",   // jne 00 00 00 00 (will be patched)
                      15);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x3aed
jmp 0x3b0c
leaq 0x2868(%rip), %rdi      # 0x635c
leaq 0x261b(%rip), %rsi      # 0x6116
movl $0x23d, %edx            # imm = 0x23D
leaq 0x2cf1(%rip), %rcx      # 0x67f8
callq 0x1080
cmpq $0x0, -0x18(%rbp)
je 0x3b15
jmp 0x3b34
leaq 0x29e1(%rip), %rdi      # 0x64fd
leaq 0x25f3(%rip), %rsi      # 0x6116
movl $0x23d, %edx            # imm = 0x23D
leaq 0x2cc9(%rip), %rcx      # 0x67f8
callq 0x1080
cmpq $0x0, -0x20(%rbp)
je 0x3b3d
jmp 0x3b5c
leaq 0x2a25(%rip), %rdi      # 0x6569
leaq 0x25cb(%rip), %rsi      # 0x6116
movl $0x23d, %edx            # imm = 0x23D
leaq 0x2ca1(%rip), %rcx      # 0x67f8
callq 0x1080
movq -0x18(%rbp), %rax
cmpl $0xf, (%rax)
je 0x3b6e
movb $0x1, -0x1(%rbp)
jmp 0x3d7e
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x2f30
testb $0x1, %al
jne 0x3b89
jmp 0x3bc6
leaq 0x2991(%rip), %rdi      # 0x6521
leaq 0x2c61(%rip), %rsi      # 0x67f8
movl $0x242, %edx            # imm = 0x242
movb $0x0, %al
callq 0x1040
movq -0x18(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x2990(%rip), %rdi      # 0x6546
movb $0x0, %al
callq 0x1040
movb $0x1, -0x1(%rbp)
jmp 0x3d7e
movq -0x10(%rbp), %rdi
leaq 0x2c51(%rip), %rsi      # 0x6822
movl $0x5, %edx
callq 0x1690
movq -0x10(%rbp), %rax
movb $0x0, 0x60(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x2f30
testb $0x1, %al
jne 0x3bfe
jmp 0x3c3b
leaq 0x291c(%rip), %rdi      # 0x6521
leaq 0x2bec(%rip), %rsi      # 0x67f8
movl $0x246, %edx            # imm = 0x246
movb $0x0, %al
callq 0x1040
movq -0x18(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x291b(%rip), %rdi      # 0x6546
movb $0x0, %al
callq 0x1040
movb $0x1, -0x1(%rbp)
jmp 0x3d7e
movq -0x10(%rbp), %rdi
leaq 0x2be2(%rip), %rsi      # 0x6828
movl $0x5, %edx
callq 0x1690
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x28(%rbp)
subq $0x1, %rax
je 0x3c73
jmp 0x3c64
movq -0x28(%rbp), %rax
subq $0x2, %rax
je 0x3cce
jmp 0x3d5f
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movsbl (%rax), %eax
movl %eax, -0x2c(%rbp)
subl $0x3c, %eax
je 0x3ca9
jmp 0x3c88
movl -0x2c(%rbp), %eax
subl $0x3e, %eax
jne 0x3cc0
jmp 0x3c92
movq -0x10(%rbp), %rdi
leaq 0x2b91(%rip), %rsi      # 0x682e
movl $0x5, %edx
callq 0x1690
jmp 0x3cc9
movq -0x10(%rbp), %rdi
leaq 0x2b80(%rip), %rsi      # 0x6834
movl $0x5, %edx
callq 0x1690
jmp 0x3cc9
movb $0x1, -0x1(%rbp)
jmp 0x3d7e
jmp 0x3d65
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movsbl (%rax), %eax
addl $-0x21, %eax
movl %eax, %ecx
movq %rcx, -0x38(%rbp)
subl $0x1d, %eax
ja 0x3d57
movq -0x38(%rbp), %rax
leaq 0x2342(%rip), %rcx      # 0x6034
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rdi
leaq 0x2b34(%rip), %rsi      # 0x683a
movl $0x5, %edx
callq 0x1690
jmp 0x3d5d
movq -0x10(%rbp), %rdi
leaq 0x2b23(%rip), %rsi      # 0x6840
movl $0x5, %edx
callq 0x1690
jmp 0x3d5d
movq -0x10(%rbp), %rdi
leaq 0x23dc(%rip), %rsi      # 0x6110
movl $0x5, %edx
callq 0x1690
jmp 0x3d5d
movq -0x10(%rbp), %rdi
leaq 0x2afb(%rip), %rsi      # 0x6846
movl $0x5, %edx
callq 0x1690
jmp 0x3d5d
movb $0x1, -0x1(%rbp)
jmp 0x3d7e
jmp 0x3d65
movb $0x1, -0x1(%rbp)
jmp 0x3d7e
movq -0x10(%rbp), %rdi
leaq 0x2c7e(%rip), %rsi      # 0x69ee
movl $0xf, %edx
callq 0x1690
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)