// File: /workspace/github_c_makefile_star2_O2/vtereshkov[P]umka-lang/src/umka_const.c
// Function: constCompare
static int64_t constCompare(const Consts *consts, Const *lhs, const Const *rhs, const Type *type)
{
    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:
        {
            const char *lhsStr = (const char *)lhs->ptrVal;
            if (!lhsStr)
                lhsStr = "";

            const char *rhsStr = (const char *)rhs->ptrVal;
            if (!rhsStr)
                rhsStr = "";

            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;                
                
                Const lhsItem = {.ptrVal = (char *)lhs->ptrVal + itemOffset};
                Const rhsItem = {.ptrVal = (char *)rhs->ptrVal + itemOffset};
            
                constDeref(consts, &lhsItem, itemType->kind);
                constDeref(consts, &rhsItem, itemType->kind);

                const int64_t itemDiff = constCompare(consts, &lhsItem, &rhsItem, itemType);
                if (itemDiff != 0)
                    return itemDiff;
            }
            return 0;
        }

        default: consts->error->handler(consts->error->context, "Illegal type"); return 0;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, 0x10(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, %r13
movl (%rcx), %eax
addl $-0x4, %eax
cmpl $0x12, %eax
ja 0x6aa1
movq %rcx, %r12
leaq 0x63553(%rip), %rcx     # 0x69ee8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%rsp), %rax
movq (%rax), %rbx
movq 0x10(%rsp), %rax
subq (%rax), %rbx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
movslq 0x10(%r12), %rax
cmpq %rax, %rbx
jge 0x6a9a
cmpl $0x12, (%r12)
jne 0x69e7
movq 0x8(%r12), %rbp
movl 0xc0(%rbp), %eax
imull %ebx, %eax
jmp 0x69fd
movq 0x28(%r12), %rax
movq (%rax,%rbx,8), %rax
movq 0x108(%rax), %rbp
movl 0x110(%rax), %eax
cltq
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
addq %rax, %rcx
movq %rcx, 0x20(%rsp)
movq 0x10(%rsp), %rcx
addq (%rcx), %rax
movq %rax, 0x18(%rsp)
movl (%rbp), %edx
movq %r13, %rdi
leaq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x613a
movl (%rbp), %edx
movq %r13, %rdi
leaq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x613a
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x6962
incq %rbx
testq %rax, %rax
je 0x69c2
movq %rax, %rbx
jmp 0x69ae
movq 0x8(%rsp), %rax
movsd (%rax), %xmm0
movq 0x10(%rsp), %rax
subsd (%rax), %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 0x69ae
xorl %ebx, %ebx
jmp 0x69ae
movq (%r13), %rcx
movq 0x20(%rcx), %rdi
leaq 0x63560(%rip), %rsi     # 0x6a010
xorl %ebx, %ebx
xorl %eax, %eax
callq *(%rcx)
jmp 0x69ae
movq 0x8(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
leaq 0x64649(%rip), %rax     # 0x6b116
cmoveq %rax, %rdi
movq 0x10(%rsp), %rcx
movq (%rcx), %rsi
testq %rsi, %rsi
cmoveq %rax, %rsi
callq 0x3210
movslq %eax, %rbx
jmp 0x69ae