// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq %rdi, %r15
testq %rdx, %rdx
je 0x1ef9c
movq %rdx, %rax
cmpq %r15, (%rax)
jne 0x1ef93
cmpq %r14, 0x8(%rax)
je 0x1f020
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x1ef84
movq %r15, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movb $0x1, %bl
cmpq %r14, %r15
je 0x1f22f
movq 0x18(%r15), %rax
testq %rax, %rax
je 0x1efe1
movq 0x18(%r14), %rcx
testq %rcx, %rcx
je 0x1efe1
cmpq %rcx, %rax
jne 0x1f22d
movl 0x4(%r15), %eax
cmpl 0x4(%r14), %eax
sete %bl
jmp 0x1f22f
movl (%r15), %eax
movl (%r14), %ecx
cmpl %ecx, %eax
jne 0x1f22d
leal -0x10(%rax), %edx
cmpl $0x4, %edx
ja 0x1f036
leaq 0x599ca(%rip), %rax     # 0x789c8
movslq (%rax,%rdx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq 0x8(%r15), %rdi
movq 0x8(%r14), %rsi
leaq 0x8(%rsp), %rdx
callq 0x1ef68
movl %eax, %ebx
jmp 0x1f22f
movb $0x1, %bl
jmp 0x1f22f
movl 0x10(%r15), %eax
cmpl 0x10(%r14), %eax
je 0x1f007
jmp 0x1f22d
leal -0x4(%rax), %edx
cmpl $0x7, %edx
ja 0x1f049
cmpb $0x0, 0x16(%r15)
jne 0x1f22d
addl $-0x4, %ecx
cmpl $0x7, %ecx
ja 0x1f05c
cmpb $0x0, 0x16(%r14)
jne 0x1f22d
leal -0x16(%rax), %ecx
cmpl $0x3, %ecx
jae 0x1f113
movl 0x10(%r15), %r13d
cmpl 0x10(%r14), %r13d
jne 0x1f22d
testl %r13d, %r13d
setle %bl
jle 0x1f22f
movq 0x28(%r15), %rax
movq 0x28(%r14), %r14
movq %rax, (%rsp)
movq (%rax), %r15
movl 0x100(%r15), %eax
movq (%r14), %r12
cmpl 0x100(%r12), %eax
jne 0x1f22f
movl $0x1, %ebp
movq %r15, %rdi
movq %r12, %rsi
callq 0x3210
testl %eax, %eax
jne 0x1f22f
movq 0x108(%r15), %rdi
movq 0x108(%r12), %rsi
leaq 0x8(%rsp), %rdx
callq 0x1ef68
testb %al, %al
je 0x1f22f
cmpq %r13, %rbp
setae %bl
je 0x1f22f
movq (%rsp), %rax
movq (%rax,%rbp,8), %r15
movl 0x100(%r15), %eax
movq (%r14,%rbp,8), %r12
incq %rbp
cmpl 0x100(%r12), %eax
je 0x1f0ae
jmp 0x1f22f
cmpl $0x1a, %eax
je 0x1f171
cmpl $0x15, %eax
jne 0x1f22f
movq 0x8(%r15), %rbx
movq 0x28(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x108(%rax), %rax
movq 0x8(%rax), %rdi
movq 0x8(%r14), %r14
movq 0x28(%r14), %rax
movq 0x8(%rax), %rax
movq 0x108(%rax), %rax
movq 0x8(%rax), %rsi
leaq 0x8(%rsp), %rdx
callq 0x1ef68
testb %al, %al
je 0x1f22d
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x1f014
movl 0x28(%r15), %esi
cmpl 0x28(%r14), %esi
jne 0x1f22d
movl 0x2c(%r15), %eax
cmpl 0x2c(%r14), %eax
jne 0x1f22d
movb 0x30(%r15), %cl
cmpb 0x30(%r14), %cl
jne 0x1f22d
movl 0x34(%r15), %ecx
xorl %edx, %edx
testl %ecx, %ecx
setne %dl
cmpl %edx, %esi
jle 0x1f21a
xorl %ebx, %ebx
testl %ecx, %ecx
setne %bl
movl %esi, %ecx
subl %eax, %ecx
movslq %ecx, %rax
movq %rax, (%rsp)
movq %rsi, 0x20(%rsp)
movq 0x38(%r15,%rbx,8), %r13
movq 0x108(%r13), %r12
movq 0x38(%r14,%rbx,8), %rbp
movq 0x108(%rbp), %rsi
movq %r12, %rdi
leaq 0x8(%rsp), %rdx
callq 0x1ef68
testb %al, %al
je 0x1f22d
cmpq (%rsp), %rbx
jl 0x1f20d
movl $0x110, %eax            # imm = 0x110
addq %rax, %r13
addq %rax, %rbp
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x20326
testb %al, %al
je 0x1f22d
incq %rbx
movq 0x20(%rsp), %rsi
cmpq %rbx, %rsi
jne 0x1f1c1
movq 0xb8(%r15), %rdi
movq 0xb8(%r14), %rsi
jmp 0x1f00f
xorl %ebx, %ebx
andb $0x1, %bl
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq