// File: /workspace/github_c_makefile_star2_O2/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
movq %rdx, %rax
testq %rax, %rax
je 0x15297
cmpq %r14, (%rax)
jne 0x15291
cmpq %rbx, 0x8(%rax)
je 0x15315
movq 0x10(%rax), %rax
jmp 0x1527d
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movb $0x1, %al
cmpq %rbx, %r14
je 0x15574
movq 0x18(%r14), %rcx
testq %rcx, %rcx
je 0x152db
movq 0x18(%rbx), %rdx
testq %rdx, %rdx
je 0x152db
cmpq %rdx, %rcx
jne 0x15572
movl 0x4(%r14), %eax
cmpl 0x4(%rbx), %eax
sete %al
jmp 0x15574
movl (%r14), %ecx
cmpl (%rbx), %ecx
jne 0x15572
leal -0x10(%rcx), %edx
cmpl $0x4, %edx
ja 0x1532a
leaq 0x5669b(%rip), %rcx     # 0x6b990
movslq (%rcx,%rdx,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
movq 0x8(%r14), %rdi
movq 0x8(%rbx), %rsi
leaq 0x8(%rsp), %rdx
callq 0x15266
jmp 0x15574
movb $0x1, %al
jmp 0x15574
movl 0x10(%r14), %eax
cmpl 0x10(%rbx), %eax
je 0x152fe
jmp 0x15572
leal -0x4(%rcx), %edx
cmpl $0x7, %edx
ja 0x15347
cmpb $0x0, 0x16(%r14)
jne 0x15572
cmpb $0x0, 0x16(%rbx)
jne 0x15572
leal -0x16(%rcx), %edx
cmpl $0x3, %edx
jae 0x153e2
movslq 0x10(%r14), %rcx
xorl %eax, %eax
movq %rcx, (%rsp)
cmpl 0x10(%rbx), %ecx
jne 0x15574
movq (%rsp), %rax
testl %eax, %eax
movl $0x0, %r15d
cmovgl %eax, %r15d
xorl %ebp, %ebp
cmpq %rbp, %r15
je 0x15583
movq 0x28(%r14), %rax
movq (%rax,%rbp,8), %r12
movl 0x100(%r12), %eax
movq 0x28(%rbx), %rcx
movq (%rcx,%rbp,8), %r13
cmpl 0x100(%r13), %eax
jne 0x15572
movq %r12, %rdi
movq %r13, %rsi
callq 0x3210
testl %eax, %eax
jne 0x15572
movq 0x108(%r12), %rdi
movq 0x108(%r13), %rsi
leaq 0x8(%rsp), %rdx
callq 0x15266
movl %eax, %ecx
incq %rbp
xorl %eax, %eax
testb %cl, %cl
jne 0x15378
jmp 0x15574
cmpl $0x1a, %ecx
je 0x15440
cmpl $0x15, %ecx
jne 0x15574
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
leaq 0x8(%rsp), %rdx
callq 0x15266
testb %al, %al
je 0x15572
leaq 0x8(%rsp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
jmp 0x1530b
movslq 0x28(%r14), %rax
movq %rax, (%rsp)
cmpl 0x28(%rbx), %eax
jne 0x15572
movl 0x2c(%r14), %eax
cmpl 0x2c(%rbx), %eax
jne 0x15572
movb 0x30(%r14), %cl
cmpb 0x30(%rbx), %cl
jne 0x15572
xorl %ebp, %ebp
cmpl $0x0, 0x34(%r14)
setne %bpl
movq (%rsp), %rcx
subl %eax, %ecx
movslq %ecx, %rax
movq %rax, 0x20(%rsp)
cmpq (%rsp), %rbp
jge 0x1558c
movq 0x38(%r14,%rbp,8), %r13
movq 0x108(%r13), %r12
movq 0x38(%rbx,%rbp,8), %r15
movq 0x108(%r15), %rsi
movq %r12, %rdi
leaq 0x8(%rsp), %rdx
callq 0x15266
testb %al, %al
je 0x15572
cmpq 0x20(%rsp), %rbp
jl 0x1556a
movl (%r12), %ecx
leal -0x4(%rcx), %eax
cmpl $0xa, %eax
setae %al
cmpl $0x1a, %ecx
setne %dl
testb %al, %dl
jne 0x154f2
movq 0x110(%r13), %rax
cmpq 0x110(%r15), %rax
je 0x1556a
jmp 0x15572
movl %ecx, %eax
andl $-0x2, %eax
cmpl $0xe, %eax
jne 0x15514
movsd 0x110(%r13), %xmm0
ucomisd 0x110(%r15), %xmm0
jne 0x15572
jnp 0x1556a
jmp 0x15572
cmpl $0x11, %ecx
je 0x154dd
movq 0x110(%r13), %rdi
movq 0x110(%r15), %rsi
testq %rdi, %rdi
je 0x1555a
testq %rsi, %rsi
je 0x1555a
xorl %eax, %eax
addl $-0x10, %ecx
cmpl $0x8, %ecx
ja 0x15574
leaq 0x56462(%rip), %rdx     # 0x6b9a4
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movslq 0xc0(%r12), %rdx
callq 0x32c0
jmp 0x15566
cmpq %rsi, %rdi
je 0x1556a
jmp 0x15572
callq 0x3210
testl %eax, %eax
jne 0x15572
incq %rbp
jmp 0x15484
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq (%rsp), %rbp
setge %al
jmp 0x15574
movq 0xb8(%r14), %rdi
movq 0xb8(%rbx), %rsi
jmp 0x15306