// File: /workspace/github_c_makefile_star2_O3/vtereshkov[P]umka-lang/src/umka_vm.c
// Function: doCompare
static int64_t doCompare(Slot lhs, Slot rhs, const Type *type, Error *error)
{
    switch (type->kind)
    {
        case TYPE_INT8:
        case TYPE_INT16:
        case TYPE_INT32:
        case TYPE_INT:
        case TYPE_UINT8:
        case TYPE_UINT16:
        case TYPE_UINT32:   return lhs.intVal - rhs.intVal;
        case TYPE_UINT:     return lhs.uintVal - rhs.uintVal;
        case TYPE_BOOL:
        case TYPE_CHAR:     return lhs.intVal - rhs.intVal;
        case TYPE_REAL32:
        case TYPE_REAL:
        {
            const double diff = lhs.realVal - rhs.realVal;
            return (diff == 0.0) ? 0 : (diff > 0.0) ? 1 : -1;
        }
        case TYPE_PTR:      return (char *)lhs.ptrVal - (char *)rhs.ptrVal;
        case TYPE_WEAKPTR:  return lhs.weakPtrVal - rhs.weakPtrVal;
        case TYPE_STR:
        {
            char *lhsStr = (char *)lhs.ptrVal;
            if (!lhsStr)
                lhsStr = doGetEmptyStr();

            char *rhsStr = (char *)rhs.ptrVal;
            if (!rhsStr)
                rhsStr = doGetEmptyStr();

            doCheckStr(lhsStr, error);
            doCheckStr(rhsStr, error);

            return strcmp(lhsStr, rhsStr);
        }
        case TYPE_ARRAY:
        case TYPE_STRUCT:
        {
            for (int i = 0; i < type->numItems; i++)
            {
                const Type *itemType = (type->kind == TYPE_ARRAY) ? type->base : type->field[i]->type;
                const int itemOffset = (type->kind == TYPE_ARRAY) ? (i * itemType->size) : type->field[i]->offset;                
                
                Slot lhsItem = {.ptrVal = (char *)lhs.ptrVal + itemOffset};
                Slot rhsItem = {.ptrVal = (char *)rhs.ptrVal + itemOffset};
            
                doDerefImpl(&lhsItem, itemType->kind, error);
                doDerefImpl(&rhsItem, itemType->kind, error);

                const int64_t itemDiff = doCompare(lhsItem, rhsItem, itemType, error);
                if (itemDiff != 0)
                    return itemDiff;
            }
            return 0;
        }
        default: error->runtimeHandler(error->context, ERR_RUNTIME, "Illegal type"); return 0;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, (%rsp)
movl (%rdx), %eax
addl $-0x4, %eax
cmpl $0x12, %eax
ja 0x45e29
movq %rdi, %rbx
leaq 0x336ce(%rip), %rcx     # 0x792d8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
subq %rsi, %rbx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r12
cmpl $0x0, 0x10(%rdx)
jle 0x45e22
xorl %r14d, %r14d
movq %rsi, 0x8(%rsp)
cmpl $0x12, (%r12)
jne 0x45c56
movq 0x8(%r12), %r13
movl 0xc0(%r13), %ebp
imull %r14d, %ebp
jmp 0x45c6c
movq 0x28(%r12), %rax
movq (%rax,%r14,8), %rax
movq 0x108(%rax), %r13
movl 0x110(%rax), %ebp
movl (%r13), %r15d
testq %rbx, %rbx
je 0x45da3
movslq %ebp, %rbp
leaq (%rbx,%rbp), %rdi
addl $-0x4, %r15d
cmpl $0x16, %r15d
ja 0x45ccc
leaq 0x33693(%rip), %rcx     # 0x79324
movslq (%rcx,%r15,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%rdi), %rdi
jmp 0x45cf0
movzbl (%rdi), %edi
jmp 0x45cf0
movslq (%rdi), %rdi
jmp 0x45cf0
movzwl (%rdi), %edi
jmp 0x45cf0
movl (%rdi), %edi
jmp 0x45cf0
movsbq (%rdi), %rdi
jmp 0x45cf0
movswq (%rdi), %rdi
jmp 0x45cf0
xorps %xmm0, %xmm0
cvtss2sd (%rdi), %xmm0
movq %xmm0, %rdi
jmp 0x45cf0
movq (%rsp), %rcx
movq %rdi, %r15
movq 0x20(%rcx), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x3032d(%rip), %rdx     # 0x76010
xorl %eax, %eax
callq *0x8(%rcx)
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movl (%r13), %r15d
testq %rsi, %rsi
je 0x45dc6
addq %rsi, %rbp
addl $-0x4, %r15d
cmpl $0x16, %r15d
ja 0x45d54
leaq 0x3366f(%rip), %rcx     # 0x79380
movslq (%rcx,%r15,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%rbp), %rbp
jmp 0x45d73
movzbl (%rbp), %ebp
jmp 0x45d73
movslq (%rbp), %rbp
jmp 0x45d73
movzwl (%rbp), %ebp
jmp 0x45d73
movl (%rbp), %ebp
jmp 0x45d73
movsbq (%rbp), %rbp
jmp 0x45d73
movswq (%rbp), %rbp
jmp 0x45d73
xorps %xmm0, %xmm0
cvtss2sd (%rbp), %xmm0
movq %xmm0, %rbp
jmp 0x45d73
movq (%rsp), %rcx
movq %rdi, %r15
movq 0x20(%rcx), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x302a5(%rip), %rdx     # 0x76010
xorl %eax, %eax
callq *0x8(%rcx)
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq (%rsp), %rcx
callq 0x45be0
testq %rax, %rax
jne 0x45e9f
incq %r14
movslq 0x10(%r12), %rax
cmpq %rax, %r14
movq 0x8(%rsp), %rsi
jl 0x45c3d
jmp 0x45e22
movq (%rsp), %rcx
movq 0x20(%rcx), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x30249(%rip), %rdx     # 0x76000
xorl %eax, %eax
callq *0x8(%rcx)
movq 0x8(%rsp), %rsi
jmp 0x45c79
movq (%rsp), %rcx
movq %rdi, 0x10(%rsp)
movq 0x20(%rcx), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x30221(%rip), %rdx     # 0x76000
xorl %eax, %eax
callq *0x8(%rcx)
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
jmp 0x45cfd
movq %rbx, %xmm0
movq %rsi, %xmm1
subsd %xmm1, %xmm0
xorl %ebx, %ebx
xorpd %xmm1, %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm0
seta %al
leaq -0x1(%rax,%rax), %rax
cmovneq %rax, %rbx
cmovpq %rax, %rbx
jmp 0x45c16
xorl %ebx, %ebx
jmp 0x45c16
movq (%rsp), %rcx
movq 0x20(%rcx), %rdi
leaq 0x301d8(%rip), %rdx     # 0x76010
xorl %ebx, %ebx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %eax, %eax
callq *0x8(%rcx)
jmp 0x45c16
testq %rbx, %rbx
jne 0x45e6c
movupd 0x31f0a(%rip), %xmm0    # 0x77d60
movapd %xmm0, 0x41472(%rip)    # 0x872d0
movb $0x0, 0x4147b(%rip)     # 0x872e0
leaq 0x41474(%rip), %rbx     # 0x872e0
testq %rsi, %rsi
jne 0x45e8f
movupd 0x31ee7(%rip), %xmm0    # 0x77d60
movapd %xmm0, 0x4144f(%rip)    # 0x872d0
movb $0x0, 0x41458(%rip)     # 0x872e0
leaq 0x41451(%rip), %rsi     # 0x872e0
movq %rbx, %rdi
callq 0x3210
movslq %eax, %rbx
jmp 0x45c16
movq %rax, %rbx
jmp 0x45c16