// File: /workspace/github_c_makefile_star2_O1/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 %rdi, (%rsp)
movl (%rcx), %eax
addl $-0x4, %eax
cmpl $0x12, %eax
ja 0xf7b1
leaq 0x6785e(%rip), %rdi     # 0x76ee8
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 0xf781
leaq 0x8(%rcx), %rax
movq %rax, 0x20(%rsp)
xorl %r14d, %r14d
movq %rcx, 0x18(%rsp)
movq (%rsp), %r12
movl (%rcx), %eax
movq 0x20(%rsp), %rdi
cmpl $0x12, %eax
je 0xf6e7
movq 0x28(%rcx), %rdi
movq (%rdi,%r14,8), %rdi
movl $0x108, %r8d            # imm = 0x108
addq %r8, %rdi
movq (%rdi), %rbp
cmpl $0x12, %eax
jne 0xf6fb
movl 0xc0(%rbp), %eax
imull %r14d, %eax
jmp 0xf709
movq 0x28(%rcx), %rax
movq (%rax,%r14,8), %rax
movl 0x110(%rax), %eax
cltq
movq %rsi, %r13
movq (%rsi), %rcx
addq %rax, %rcx
movq %rcx, 0x10(%rsp)
movq %rdx, %r15
addq (%rdx), %rax
movq %rax, 0x8(%rsp)
movl (%rbp), %edx
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0xeec2
movl (%rbp), %edx
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0xeec2
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rbp, %rcx
callq 0xf663
testq %rax, %rax
cmovneq %rax, %rbx
jne 0xf699
incq %r14
movq 0x18(%rsp), %rcx
movslq 0x10(%rcx), %rax
cmpq %rax, %r14
movq %r15, %rdx
movq %r13, %rsi
jl 0xf6ca
xorl %ebx, %ebx
jmp 0xf699
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 0xf699
movq (%rsp), %rax
movq (%rax), %rcx
movq 0x20(%rcx), %rdi
leaq 0x6784d(%rip), %rsi     # 0x77010
xorl %ebx, %ebx
xorl %eax, %eax
callq *(%rcx)
jmp 0xf699
movq (%rsi), %rdi
testq %rdi, %rdi
leaq 0x6899b(%rip), %rax     # 0x78176
cmoveq %rax, %rdi
movq (%rdx), %rsi
testq %rsi, %rsi
cmoveq %rax, %rsi
callq 0x3210
movslq %eax, %rbx
jmp 0xf699