// File: /workspace/github_c_makefile_star2_O1/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 lhs.ptrVal - 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 $0x28, %rsp
movq %rcx, (%rsp)
movl (%rdx), %eax
addl $-0x4, %eax
cmpl $0x12, %eax
ja 0x48fa4
leaq 0x31548(%rip), %rcx     # 0x7a2a0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
subq %rsi, %rdi
movq %rdi, %r15
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x10(%rdx)
jle 0x48f9c
leaq 0x8(%rdx), %rax
movq %rax, 0x20(%rsp)
xorl %ebx, %ebx
movq %rdi, 0x10(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdx, 0x18(%rsp)
movl (%rdx), %eax
movq 0x20(%rsp), %rcx
cmpl $0x12, %eax
je 0x48dba
movq 0x28(%rdx), %rcx
movq (%rcx,%rbx,8), %rcx
movl $0x108, %r8d            # imm = 0x108
addq %r8, %rcx
movq (%rcx), %rbp
cmpl $0x12, %eax
jne 0x48dcf
movl 0xc0(%rbp), %r14d
imull %ebx, %r14d
jmp 0x48dde
movq 0x28(%rdx), %rax
movq (%rax,%rbx,8), %rax
movl 0x110(%rax), %r14d
movl (%rbp), %r13d
testq %rdi, %rdi
je 0x48f21
movslq %r14d, %r12
leaq (%rdi,%r12), %r14
addl $-0x4, %r13d
cmpl $0x16, %r13d
ja 0x48e42
leaq 0x314e9(%rip), %rcx     # 0x7a2ec
movslq (%rcx,%r13,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%r14), %r14
jmp 0x48e60
movzbl (%r14), %r14d
jmp 0x48e60
movslq (%r14), %r14
jmp 0x48e60
movzwl (%r14), %r14d
jmp 0x48e60
movl (%r14), %r14d
jmp 0x48e60
movsbq (%r14), %r14
jmp 0x48e60
movswq (%r14), %r14
jmp 0x48e60
xorps %xmm0, %xmm0
cvtss2sd (%r14), %xmm0
movq %xmm0, %r14
jmp 0x48e60
movq (%rsp), %rcx
movq 0x20(%rcx), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x2e1ba(%rip), %rdx     # 0x77010
xorl %eax, %eax
callq *0x8(%rcx)
movq 0x8(%rsp), %rsi
movl (%rbp), %r13d
testq %rsi, %rsi
je 0x48f49
addq %rsi, %r12
addl $-0x4, %r13d
cmpl $0x16, %r13d
ja 0x48ec8
leaq 0x314c7(%rip), %rcx     # 0x7a348
movslq (%rcx,%r13,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%r12), %r12
jmp 0x48ee1
movzbl (%r12), %r12d
jmp 0x48ee1
movslq (%r12), %r12
jmp 0x48ee1
movzwl (%r12), %r12d
jmp 0x48ee1
movl (%r12), %r12d
jmp 0x48ee1
movsbq (%r12), %r12
jmp 0x48ee1
movswq (%r12), %r12
jmp 0x48ee1
xorps %xmm0, %xmm0
cvtss2sd (%r12), %xmm0
movq %xmm0, %r12
jmp 0x48ee1
movq (%rsp), %rcx
movq 0x20(%rcx), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x2e134(%rip), %rdx     # 0x77010
xorl %eax, %eax
callq *0x8(%rcx)
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq (%rsp), %rcx
callq 0x48d31
testq %rax, %rax
cmovneq %rax, %r15
jne 0x48d67
incq %rbx
movq 0x18(%rsp), %rdx
movslq 0x10(%rdx), %rax
cmpq %rax, %rbx
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
jl 0x48d9d
jmp 0x48f9c
movq (%rsp), %rcx
movq 0x20(%rcx), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x2e0cb(%rip), %rdx     # 0x77000
xorl %eax, %eax
callq *0x8(%rcx)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
jmp 0x48deb
movq (%rsp), %rcx
movq 0x20(%rcx), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x2e0a3(%rip), %rdx     # 0x77000
xorl %eax, %eax
callq *0x8(%rcx)
movq 0x8(%rsp), %rsi
jmp 0x48e6d
movq %rdi, %xmm0
movq %rsi, %xmm1
subsd %xmm1, %xmm0
xorl %r15d, %r15d
xorpd %xmm1, %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm0
seta %al
leaq -0x1(%rax,%rax), %rax
cmovneq %rax, %r15
cmovpq %rax, %r15
jmp 0x48d67
xorl %r15d, %r15d
jmp 0x48d67
movq (%rsp), %rcx
movq 0x20(%rcx), %rdi
leaq 0x2e05d(%rip), %rdx     # 0x77010
xorl %r15d, %r15d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %eax, %eax
callq *0x8(%rcx)
jmp 0x48d67
testq %rdi, %rdi
jne 0x48fe8
movupd 0x2fd8e(%rip), %xmm0    # 0x78d60
movapd %xmm0, 0x462f6(%rip)    # 0x8f2d0
movb $0x0, 0x462ff(%rip)     # 0x8f2e0
leaq 0x462f8(%rip), %rdi     # 0x8f2e0
testq %rsi, %rsi
jne 0x4900b
movupd 0x2fd6b(%rip), %xmm0    # 0x78d60
movapd %xmm0, 0x462d3(%rip)    # 0x8f2d0
movb $0x0, 0x462dc(%rip)     # 0x8f2e0
leaq 0x462d5(%rip), %rsi     # 0x8f2e0
callq 0x3210
movslq %eax, %r15
jmp 0x48d67