// File: /workspace/github_c_makefile_star2_O3/vtereshkov[P]umka-lang/src/umka_types.c
// Function: typeEquivalentRecursive
static bool typeEquivalentRecursive(const Type *left, const Type *right, VisitedTypePair *firstPair)
{
    // Recursively defined types visited before (need to check first in order to break a possible circular definition)
    VisitedTypePair *pair = firstPair;
    while (pair && !(pair->left == left && pair->right == right))
        pair = pair->next;

    if (pair)
        return true;

    VisitedTypePair newPair = {left, right, firstPair};

    // Same types
    if (left == right)
        return true;

    // Identically named types
    if (left->typeIdent && right->typeIdent)
        return left->typeIdent == right->typeIdent && left->block == right->block;

    if (left->kind == right->kind)
    {
        // Pointers or weak pointers
        if (left->kind == TYPE_PTR || left->kind == TYPE_WEAKPTR)
            return typeEquivalentRecursive(left->base, right->base, &newPair);

        // Arrays
        else if (left->kind == TYPE_ARRAY)
        {
            // Number of elements
            if (left->numItems != right->numItems)
                return false;

            return typeEquivalentRecursive(left->base, right->base, &newPair);
        }

        // Dynamic arrays
        else if (left->kind == TYPE_DYNARRAY)
            return typeEquivalentRecursive(left->base, right->base, &newPair);

        // Strings
        else if (left->kind == TYPE_STR)
            return true;

        // Enumerations
        else if (typeEnum(left) || typeEnum(right))
            return false;

        // Maps
        else if (left->kind == TYPE_MAP)
        {
            // Key type
            if (!typeEquivalentRecursive(typeMapKey(left), typeMapKey(right), &newPair))
                return false;

            return typeEquivalentRecursive(left->base, right->base, &newPair);
        }

        // Structures or interfaces
        else if (left->kind == TYPE_STRUCT || left->kind == TYPE_INTERFACE || left->kind == TYPE_CLOSURE)
        {
            // Number of fields
            if (left->numItems != right->numItems)
                return false;

            // Fields
            for (int i = 0; i < left->numItems; i++)
            {
                // Name
                if (left->field[i]->hash != right->field[i]->hash || strcmp(left->field[i]->name, right->field[i]->name) != 0)
                    return false;

                // Type
                if (!typeEquivalentRecursive(left->field[i]->type, right->field[i]->type, &newPair))
                    return false;
            }
            return true;
        }

        // Functions
        else if (left->kind == TYPE_FN)
        {
            // Number of parameters
            if (left->sig.numParams != right->sig.numParams)
                return false;

            // Number of default parameters
            if (left->sig.numDefaultParams != right->sig.numDefaultParams)
                return false;

            // Method flag
            if (left->sig.isMethod != right->sig.isMethod)
                return false;

            // Parameters (skip interface method receiver)
            int iStart = left->sig.offsetFromSelf == 0 ? 0 : 1;
            for (int i = iStart; i < left->sig.numParams; i++)
            {
                // Type
                if (!typeEquivalentRecursive(left->sig.param[i]->type, right->sig.param[i]->type, &newPair))
                    return false;

                // Default value
                if (i >= left->sig.numParams - left->sig.numDefaultParams)
                    if (!typeDefaultParamEqual(&left->sig.param[i]->defaultVal, &right->sig.param[i]->defaultVal, left->sig.param[i]->type))
                        return false;
            }

            // Result type
            if (!typeEquivalentRecursive(left->sig.resultType, right->sig.resultType, &newPair))
                return false;

            return true;
        }

        // Primitive types
        else
            return true;
    }
    return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %r14
testq %rdx, %rdx
je 0x19fb5
movq %rdx, %rax
cmpq %r14, (%rax)
jne 0x19fac
cmpq %rbx, 0x8(%rax)
je 0x1a030
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x19f9d
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movb $0x1, %al
cmpq %rbx, %r14
je 0x1a205
movq 0x18(%r14), %rcx
testq %rcx, %rcx
je 0x19ff8
movq 0x18(%rbx), %rdx
testq %rdx, %rdx
je 0x19ff8
cmpq %rdx, %rcx
jne 0x1a203
movl 0x4(%r14), %eax
cmpl 0x4(%rbx), %eax
sete %al
jmp 0x1a205
movl (%r14), %ecx
cmpl (%rbx), %ecx
jne 0x1a203
leal -0x10(%rcx), %edx
cmpl $0x4, %edx
ja 0x1a045
leaq 0x5d9b6(%rip), %rcx     # 0x779c8
movslq (%rcx,%rdx,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
movq 0x8(%r14), %rdi
movq 0x8(%rbx), %rsi
movq %rsp, %rdx
callq 0x19f81
jmp 0x1a205
movb $0x1, %al
jmp 0x1a205
movl 0x10(%r14), %eax
cmpl 0x10(%rbx), %eax
je 0x1a01b
jmp 0x1a203
leal -0x4(%rcx), %edx
cmpl $0x7, %edx
ja 0x1a062
cmpb $0x0, 0x16(%r14)
jne 0x1a203
cmpb $0x0, 0x16(%rbx)
jne 0x1a203
leal -0x16(%rcx), %edx
cmpl $0x3, %edx
jae 0x1a0ee
movl 0x10(%r14), %r12d
cmpl 0x10(%rbx), %r12d
jne 0x1a203
testl %r12d, %r12d
jle 0x1a205
movq 0x28(%r14), %r13
movq 0x28(%rbx), %rbp
xorl %ebx, %ebx
movq (%r13,%rbx,8), %r14
movl 0x100(%r14), %eax
movq (%rbp,%rbx,8), %r15
cmpl 0x100(%r15), %eax
jne 0x1a203
movq %r14, %rdi
movq %r15, %rsi
callq 0x3210
testl %eax, %eax
jne 0x1a203
movq 0x108(%r14), %rdi
movq 0x108(%r15), %rsi
movq %rsp, %rdx
callq 0x19f81
testb %al, %al
je 0x1a203
incq %rbx
cmpq %rbx, %r12
jne 0x1a08f
setbe %al
jmp 0x1a205
cmpl $0x1a, %ecx
je 0x1a148
cmpl $0x15, %ecx
jne 0x1a205
movq 0x8(%r14), %r14
movq 0x28(%r14), %rax
movq 0x8(%rax), %rax
movq 0x108(%rax), %rax
movq 0x8(%rax), %rdi
movq 0x8(%rbx), %rbx
movq 0x28(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x108(%rax), %rax
movq 0x8(%rax), %rsi
movq %rsp, %rdx
callq 0x19f81
testb %al, %al
je 0x1a203
movq %rsp, %rdx
movq %r14, %rdi
movq %rbx, %rsi
jmp 0x1a026
movl 0x28(%r14), %esi
cmpl 0x28(%rbx), %esi
jne 0x1a203
movl 0x2c(%r14), %eax
cmpl 0x2c(%rbx), %eax
jne 0x1a203
movb 0x30(%r14), %cl
cmpb 0x30(%rbx), %cl
jne 0x1a203
movl 0x34(%r14), %ecx
xorl %edx, %edx
testl %ecx, %ecx
setne %dl
cmpl %edx, %esi
jle 0x1a1f0
xorl %r15d, %r15d
testl %ecx, %ecx
setne %r15b
movl %esi, %ecx
subl %eax, %ecx
movslq %ecx, %rax
movq %rax, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq 0x38(%r14,%r15,8), %r13
movq 0x108(%r13), %r12
movq 0x38(%rbx,%r15,8), %rbp
movq 0x108(%rbp), %rsi
movq %r12, %rdi
movq %rsp, %rdx
callq 0x19f81
testb %al, %al
je 0x1a203
cmpq 0x20(%rsp), %r15
jl 0x1a1e3
movl $0x110, %eax            # imm = 0x110
addq %rax, %r13
addq %rax, %rbp
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x1b271
testb %al, %al
je 0x1a203
incq %r15
movq 0x18(%rsp), %rsi
cmpq %r15, %rsi
jne 0x1a198
movq 0xb8(%r14), %rdi
movq 0xb8(%rbx), %rsi
jmp 0x1a023
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq