// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movq %rcx, (%rsp)
movl (%rdx), %eax
addl $-0x4, %eax
cmpl $0x12, %eax
ja 0x3d558
movq %rdi, %r15
leaq 0x2fca6(%rip), %rcx     # 0x6cffc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
subq %rsi, %r15
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r12
xorl %r14d, %r14d
movq %rsi, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movslq 0x10(%r12), %rax
cmpq %rax, %r14
jge 0x3d550
cmpl $0x12, (%r12)
jne 0x3d3ab
movq 0x8(%r12), %r13
movl 0xc0(%r13), %ebp
imull %r14d, %ebp
jmp 0x3d3c1
movq 0x28(%r12), %rax
movq (%rax,%r14,8), %rax
movq 0x108(%rax), %r13
movl 0x110(%rax), %ebp
movl (%r13), %ebx
testq %r15, %r15
je 0x3d4de
movslq %ebp, %rbp
addq %rbp, %r15
addl $-0x4, %ebx
cmpl $0x16, %ebx
ja 0x3d41f
leaq 0x2fc65(%rip), %rcx     # 0x6d048
movslq (%rcx,%rbx,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%r15), %r15
jmp 0x3d43b
movzbl (%r15), %r15d
jmp 0x3d43b
movslq (%r15), %r15
jmp 0x3d43b
movzwl (%r15), %r15d
jmp 0x3d43b
movl (%r15), %r15d
jmp 0x3d43b
movsbq (%r15), %r15
jmp 0x3d43b
movswq (%r15), %r15
jmp 0x3d43b
cvtss2sd (%r15), %xmm0
movq %xmm0, %r15
jmp 0x3d43b
movq (%rsp), %rcx
movq 0x20(%rcx), %rdi
pushq $-0x1
popq %rsi
leaq 0x2cbdf(%rip), %rdx     # 0x6a010
xorl %eax, %eax
callq *0x8(%rcx)
movq 0x8(%rsp), %rsi
movl (%r13), %ebx
testq %rsi, %rsi
je 0x3d4ff
addq %rsi, %rbp
addl $-0x4, %ebx
cmpl $0x16, %ebx
ja 0x3d49a
leaq 0x2fc4a(%rip), %rcx     # 0x6d0a4
movslq (%rcx,%rbx,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%rbp), %rbp
jmp 0x3d4b1
movzbl (%rbp), %ebp
jmp 0x3d4b1
movslq (%rbp), %rbp
jmp 0x3d4b1
movzwl (%rbp), %ebp
jmp 0x3d4b1
movl (%rbp), %ebp
jmp 0x3d4b1
movsbq (%rbp), %rbp
jmp 0x3d4b1
movswq (%rbp), %rbp
jmp 0x3d4b1
cvtss2sd (%rbp), %xmm0
movq %xmm0, %rbp
jmp 0x3d4b1
movq (%rsp), %rcx
movq 0x20(%rcx), %rdi
pushq $-0x1
popq %rsi
leaq 0x2cb64(%rip), %rdx     # 0x6a010
xorl %eax, %eax
callq *0x8(%rcx)
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq (%rsp), %rcx
callq 0x3d32c
incq %r14
testq %rax, %rax
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %rsi
je 0x3d384
jmp 0x3d5cd
movq (%rsp), %rcx
movq 0x20(%rcx), %rdi
pushq $-0x1
popq %rsi
leaq 0x2cb10(%rip), %rdx     # 0x6a000
xorl %eax, %eax
callq *0x8(%rcx)
movq 0x8(%rsp), %rsi
jmp 0x3d3ce
movq (%rsp), %rcx
movq 0x20(%rcx), %rdi
pushq $-0x1
popq %rsi
leaq 0x2caef(%rip), %rdx     # 0x6a000
xorl %eax, %eax
callq *0x8(%rcx)
movq 0x8(%rsp), %rsi
jmp 0x3d448
movq %r15, %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 0x3d362
xorl %r15d, %r15d
jmp 0x3d362
movq (%rsp), %rcx
movq 0x20(%rcx), %rdi
leaq 0x2caa9(%rip), %rdx     # 0x6a010
xorl %r15d, %r15d
pushq $-0x1
popq %rsi
xorl %eax, %eax
callq *0x8(%rcx)
jmp 0x3d362
testq %r15, %r15
jne 0x3d59a
movupd 0x2e7bc(%rip), %xmm0    # 0x6bd40
movapd %xmm0, 0x3dd44(%rip)    # 0x7b2d0
movb $0x0, 0x3dd4d(%rip)     # 0x7b2e0
leaq 0x3dd46(%rip), %r15     # 0x7b2e0
testq %rsi, %rsi
jne 0x3d5bd
leaq 0x3dd3a(%rip), %rsi     # 0x7b2e0
movupd 0x2e792(%rip), %xmm0    # 0x6bd40
movapd %xmm0, 0x3dd1a(%rip)    # 0x7b2d0
movb $0x0, 0x3dd23(%rip)     # 0x7b2e0
movq %r15, %rdi
callq 0x3210
movslq %eax, %r15
jmp 0x3d362
movq %rax, %r15
jmp 0x3d362