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

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

    TryPrint (PrintArith, left);
    TryPrint (PrintArith, right);

    // arithm_op xmm(№-1), xmm№
    // arithm_op = {addsd, subsd, mulsd, divsd} 

    if (elf->xmm_counter < 8)
        ElfAddBytes (elf, "\xF2\x0F", 2); // arithm_op xmm[0..6], xmm[1..7]
    else if (elf->xmm_counter == 8)
        ElfAddBytes (elf, "\xF2\x41\x0F", 3); // arithm_op xmm7, xmm8
    else
        ElfAddBytes (elf, "\xF2\x45\x0F", 3); // arithm_op xmm[8..14], xmm[9..15]

    switch (el->ind[0])
    {
        case '+':
            ElfAddBytes (elf, "\x58", 1); // arithm_op = addsd
            break;
        case '-':
            ElfAddBytes (elf, "\x5C", 1); // arithm_op = subsd
            break;
        case '*':
            ElfAddBytes (elf, "\x59", 1); // arithm_op = mulsd
            break;
        case '/':
            ElfAddBytes (elf, "\x5E", 1); // arithm_op = divsd
            break;
        // case '^': // ToDo: Deleted operator
            // fprintf (asm_text, "pow\n");
            // return 0;
        default:
            return 1;
    }

    char num_registers = ((elf->xmm_counter - 2) % 8) * 8 + (elf->xmm_counter - 1) % 8 + 0xC0;
    elf->xmm_counter -= 1;
    ElfAddBytes (elf, &num_registers, 1);
    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 0x331d
jmp 0x333c
leaq 0x3038(%rip), %rdi      # 0x635c
leaq 0x2deb(%rip), %rsi      # 0x6116
movl $0x2d3, %edx            # imm = 0x2D3
leaq 0x3603(%rip), %rcx      # 0x693a
callq 0x1080
cmpq $0x0, -0x18(%rbp)
je 0x3345
jmp 0x3364
leaq 0x31b1(%rip), %rdi      # 0x64fd
leaq 0x2dc3(%rip), %rsi      # 0x6116
movl $0x2d3, %edx            # imm = 0x2D3
leaq 0x35db(%rip), %rcx      # 0x693a
callq 0x1080
cmpq $0x0, -0x20(%rbp)
je 0x336d
jmp 0x338c
leaq 0x31f5(%rip), %rdi      # 0x6569
leaq 0x2d9b(%rip), %rsi      # 0x6116
movl $0x2d3, %edx            # imm = 0x2D3
leaq 0x35b3(%rip), %rcx      # 0x693a
callq 0x1080
movq -0x18(%rbp), %rax
cmpl $0x6, (%rax)
je 0x339e
movb $0x1, -0x1(%rbp)
jmp 0x3599
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x2f30
testb $0x1, %al
jne 0x33b9
jmp 0x33f6
leaq 0x3161(%rip), %rdi      # 0x6521
leaq 0x3573(%rip), %rsi      # 0x693a
movl $0x2d8, %edx            # imm = 0x2D8
movb $0x0, %al
callq 0x1040
movq -0x18(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x3160(%rip), %rdi      # 0x6546
movb $0x0, %al
callq 0x1040
movb $0x1, -0x1(%rbp)
jmp 0x3599
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x2f30
testb $0x1, %al
jne 0x3411
jmp 0x344e
leaq 0x3109(%rip), %rdi      # 0x6521
leaq 0x351b(%rip), %rsi      # 0x693a
movl $0x2d9, %edx            # imm = 0x2D9
movb $0x0, %al
callq 0x1040
movq -0x18(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x3108(%rip), %rdi      # 0x6546
movb $0x0, %al
callq 0x1040
movb $0x1, -0x1(%rbp)
jmp 0x3599
movq -0x10(%rbp), %rax
movsbl 0x60(%rax), %eax
cmpl $0x8, %eax
jge 0x3472
movq -0x10(%rbp), %rdi
leaq 0x3503(%rip), %rsi      # 0x6969
movl $0x2, %edx
callq 0x1690
jmp 0x34ad
movq -0x10(%rbp), %rax
movsbl 0x60(%rax), %eax
cmpl $0x8, %eax
jne 0x3496
movq -0x10(%rbp), %rdi
leaq 0x34e2(%rip), %rsi      # 0x696c
movl $0x3, %edx
callq 0x1690
jmp 0x34ab
movq -0x10(%rbp), %rdi
leaq 0x34cf(%rip), %rsi      # 0x6970
movl $0x3, %edx
callq 0x1690
jmp 0x34ad
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movsbl (%rax), %eax
addl $-0x2a, %eax
movl %eax, %ecx
movq %rcx, -0x30(%rbp)
subl $0x5, %eax
ja 0x3536
movq -0x30(%rbp), %rax
leaq 0x2b4b(%rip), %rcx      # 0x601c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rdi
leaq 0x348f(%rip), %rsi      # 0x6974
movl $0x1, %edx
callq 0x1690
jmp 0x353c
movq -0x10(%rbp), %rdi
leaq 0x347a(%rip), %rsi      # 0x6976
movl $0x1, %edx
callq 0x1690
jmp 0x353c
movq -0x10(%rbp), %rdi
leaq 0x3504(%rip), %rsi      # 0x6a17
movl $0x1, %edx
callq 0x1690
jmp 0x353c
movq -0x10(%rbp), %rdi
leaq 0x344e(%rip), %rsi      # 0x6978
movl $0x1, %edx
callq 0x1690
jmp 0x353c
movb $0x1, -0x1(%rbp)
jmp 0x3599
movq -0x10(%rbp), %rax
movsbl 0x60(%rax), %eax
subl $0x2, %eax
movl $0x8, %ecx
cltd
idivl %ecx
shll $0x3, %edx
movl %edx, -0x34(%rbp)
movq -0x10(%rbp), %rax
movsbl 0x60(%rax), %eax
subl $0x1, %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl -0x34(%rbp), %eax
addl %edx, %eax
addl $0xc0, %eax
movb %al, -0x21(%rbp)
movq -0x10(%rbp), %rax
movsbl 0x60(%rax), %ecx
subl $0x1, %ecx
movb %cl, 0x60(%rax)
movq -0x10(%rbp), %rdi
leaq -0x21(%rbp), %rsi
movl $0x1, %edx
callq 0x1690
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)