// File: /workspace/github_c_makefile_star2_O3/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 (char *)lhs->ptrVal - (char *)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 %rdi, 0x8(%rsp)
movl (%rcx), %eax
addl $-0x4, %eax
cmpl $0x12, %eax
ja 0xa5ba
leaq 0x6ba35(%rip), %rdi     # 0x75ee8
movslq (%rdi,%rax,4), %rax
addq %rdi, %rax
jmpq *%rax
movq (%rsi), %rbx
subq (%rdx), %rbx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x10(%rcx)
jle 0xa58a
xorl %r14d, %r14d
movq %rcx, 0x18(%rsp)
movq 0x8(%rsp), %r12
cmpl $0x12, (%rcx)
jne 0xa500
movq 0x8(%rcx), %rbp
movl 0xc0(%rbp), %eax
imull %r14d, %eax
jmp 0xa515
movq 0x28(%rcx), %rax
movq (%rax,%r14,8), %rax
movq 0x108(%rax), %rbp
movl 0x110(%rax), %eax
cltq
movq %rsi, %r13
movq (%rsi), %rcx
addq %rax, %rcx
movq %rcx, 0x10(%rsp)
movq %rdx, %rbx
addq (%rdx), %rax
movq %rax, 0x20(%rsp)
movl (%rbp), %edx
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x9c5e
movl (%rbp), %edx
movq %r12, %rdi
leaq 0x20(%rsp), %r15
movq %r15, %rsi
callq 0x9c5e
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0xa48b
testq %rax, %rax
jne 0xa600
incq %r14
movq 0x18(%rsp), %rcx
movslq 0x10(%rcx), %rax
cmpq %rax, %r14
movq %rbx, %rdx
movq %r13, %rsi
jl 0xa4eb
xorl %ebx, %ebx
jmp 0xa4c2
movsd (%rsi), %xmm0
subsd (%rdx), %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 0xa4c2
movq 0x8(%rsp), %rax
movq (%rax), %rcx
movq 0x20(%rcx), %rdi
leaq 0x6ba43(%rip), %rsi     # 0x76010
xorl %ebx, %ebx
xorl %eax, %eax
callq *(%rcx)
jmp 0xa4c2
movq (%rsi), %rdi
testq %rdi, %rdi
leaq 0x6cb91(%rip), %rax     # 0x77176
cmoveq %rax, %rdi
movq (%rdx), %rsi
testq %rsi, %rsi
cmoveq %rax, %rsi
callq 0x3210
movslq %eax, %rbx
jmp 0xa4c2
movq %rax, %rbx
jmp 0xa4c2