// File: /workspace/github_c_makefile_star2_O2/vtereshkov[P]umka-lang/src/umka_types.c
// Function: typeSpellingRecursive
static char *typeSpellingRecursive(const Type *type, char *buf, int size, int depth)
{
    if (type->block == 0 && type->typeIdent)
        snprintf(buf, size, "%s", type->typeIdent->name);
    else
    {
        int len = 0;

        if (type->kind == TYPE_ARRAY)
        {
            len += snprintf(buf + len, nonneg(size - len), "[%d]", type->numItems);
        }
        else if (typeEnum(type))
        {
            len += snprintf(buf + len, nonneg(size - len), "enum(%s)", typeKindSpelling(type->kind));
        }
        else if (type->kind == TYPE_MAP)
        {
            char keyBuf[DEFAULT_STR_LEN + 1];
            len += snprintf(buf + len, nonneg(size - len), "map[%s]", typeSpellingRecursive(typeMapKey(type), keyBuf, DEFAULT_STR_LEN + 1, depth - 1));
        }
        else if (typeExprListStruct(type))
        {
            len += snprintf(buf + len, nonneg(size - len), "{ ");
            for (int i = 0; i < type->numItems; i++)
            {
                char fieldBuf[DEFAULT_STR_LEN + 1];
                len += snprintf(buf + len, nonneg(size - len), "%s ", typeSpellingRecursive(type->field[i]->type, fieldBuf, DEFAULT_STR_LEN + 1, depth - 1));
            }
            len += snprintf(buf + len, nonneg(size - len), "}");
        }
        else if (type->kind == TYPE_FN || type->kind == TYPE_CLOSURE)
        {
            const bool isClosure = type->kind == TYPE_CLOSURE;
            if (isClosure)
                type = type->field[0]->type;

            len += snprintf(buf + len, nonneg(size - len), "fn (");

            if (type->sig.isMethod)
            {
                char paramBuf[DEFAULT_STR_LEN + 1];
                len += snprintf(buf + len, nonneg(size - len), "%s) (", typeSpellingRecursive(type->sig.param[0]->type, paramBuf, DEFAULT_STR_LEN + 1, depth - 1));
            }

            const int numPreHiddenParams = 1;                                                 // #self or #upvalues
            const int numPostHiddenParams = typeStructured(type->sig.resultType) ? 1 : 0;     // #result

            for (int i = numPreHiddenParams; i < type->sig.numParams - numPostHiddenParams; i++)
            {
                if (i > numPreHiddenParams)
                    len += snprintf(buf + len, nonneg(size - len), ", ");

                char paramBuf[DEFAULT_STR_LEN + 1];
                len += snprintf(buf + len, nonneg(size - len), "%s", typeSpellingRecursive(type->sig.param[i]->type, paramBuf, DEFAULT_STR_LEN + 1, depth - 1));
            }

            len += snprintf(buf + len, nonneg(size - len), ")");

            if (type->sig.resultType->kind != TYPE_VOID)
            {
                char resultBuf[DEFAULT_STR_LEN + 1];
                len += snprintf(buf + len, nonneg(size - len), ": %s", typeSpellingRecursive(type->sig.resultType, resultBuf, DEFAULT_STR_LEN + 1, depth - 1));
            }

            if (isClosure)
                len += snprintf(buf + len, nonneg(size - len), " |...|");
        }
        else
        {
            snprintf(buf + len, nonneg(size - len), "%s", spelling[type->kind]);
        }

        if (type->kind == TYPE_PTR || type->kind == TYPE_WEAKPTR || type->kind == TYPE_ARRAY || type->kind == TYPE_DYNARRAY || type->kind == TYPE_MAP)
        {
            const Type *itemType = (type->kind == TYPE_MAP) ? typeMapItem(type) : type->base;

            char itemBuf[DEFAULT_STR_LEN + 1];
            if (depth > 0)
                strncat(buf, typeSpellingRecursive(itemType, itemBuf, DEFAULT_STR_LEN + 1, depth - 1), nonneg(size - len - 1));
            else
                strncat(buf, "...", nonneg(size - len - 1));
        }
    }
    return buf;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %edx, %r13d
movq %rsi, %r15
cmpl $0x0, 0x4(%rdi)
je 0x160a6
movl (%rdi), %edx
cmpq $0x12, %rdx
jne 0x16072
movl 0x10(%rdi), %ecx
leaq 0x55c6f(%rip), %rdx     # 0x6bcca
movl $0x100, %esi            # imm = 0x100
movq %rdi, %rbx
movq %r15, %rdi
xorl %eax, %eax
callq 0x3170
jmp 0x1627b
leal -0x4(%rdx), %eax
cmpl $0x7, %eax
ja 0x160ce
cmpb $0x1, 0x16(%rdi)
jne 0x1620b
leaq 0x64a95(%rip), %rax     # 0x7ab20
movq (%rax,%rdx,8), %rcx
leaq 0x55c39(%rip), %rdx     # 0x6bccf
movl $0x100, %esi            # imm = 0x100
movq %rdi, %rbx
movq %r15, %rdi
jmp 0x16274
movq 0x18(%rdi), %rcx
testq %rcx, %rcx
je 0x16049
addq $0x4, %rcx
leaq 0x55c3d(%rip), %rdx     # 0x6bcf7
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
xorl %eax, %eax
callq 0x3170
jmp 0x162e4
leal -0x15(%rdx), %eax
cmpl $0x5, %eax
ja 0x1620b
leaq 0x55987(%rip), %rcx     # 0x6ba68
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x18, %edx
jne 0x160fd
movq 0x28(%rdi), %rax
movq (%rax), %rax
movq 0x108(%rax), %rdi
movb $0x0, 0x4(%r15)
movl $0x28206e66, (%r15)     # imm = 0x28206E66
cmpb $0x1, 0x30(%rdi)
movq %rdi, 0x10(%rsp)
movq %rdx, 0x28(%rsp)
jne 0x162f9
leaq 0x4(%r15), %r14
movq 0x38(%rdi), %rax
movq 0x108(%rax), %rdi
leal -0x1(%r13), %edx
leaq 0x30(%rsp), %r12
movq %r12, %rsi
callq 0x1602c
leaq 0x55ba8(%rip), %rdx     # 0x6bcec
movl $0xfc, %esi
movq %r14, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x3170
movq 0x10(%rsp), %rdi
addl $0x4, %eax
jmp 0x162fc
cmpb $0x1, 0x14(%rdi)
jne 0x1620b
cmpl $0x0, 0x10(%rdi)
jle 0x1620b
movb $0x0, 0x2(%r15)
movq %r15, 0x8(%rsp)
movw $0x207b, (%r15)         # imm = 0x207B
movq %r13, 0x20(%rsp)
leal -0x1(%r13), %eax
movl %eax, 0x10(%rsp)
pushq $0x2
popq %rax
leaq 0x30(%rsp), %r14
xorl %ebp, %ebp
movslq 0x10(%rdi), %rcx
movslq %eax, %rbx
cmpq %rcx, %rbp
jge 0x164b1
movl $0x100, %r13d           # imm = 0x100
subl %ebx, %r13d
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
movq 0x8(%rsp), %rax
leaq (%rax,%rbx), %r12
movq 0x28(%rdi), %rax
movq (%rax,%rbp,8), %rax
movq %rdi, %r15
movq 0x108(%rax), %rdi
movq %r14, %rsi
movl 0x10(%rsp), %edx
callq 0x1602c
movq %r12, %rdi
movq %r13, %rsi
leaq 0x55aec(%rip), %rdx     # 0x6bce3
movq %r14, %rcx
xorl %eax, %eax
callq 0x3170
movq %r15, %rdi
addl %ebx, %eax
incq %rbp
jmp 0x1619e
leaq 0x6490e(%rip), %rax     # 0x7ab20
movq (%rax,%rdx,8), %rcx
leaq 0x55ada(%rip), %rdx     # 0x6bcf7
xorl %r12d, %r12d
movl $0x100, %esi            # imm = 0x100
movq %rdi, %rbx
movq %r15, %rdi
xorl %eax, %eax
callq 0x3170
movq %rbx, %rdx
jmp 0x16281
movq 0x8(%rdi), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rax
movq 0x108(%rax), %rax
movq %rdi, %rbx
movq 0x8(%rax), %rdi
leal -0x1(%r13), %edx
leaq 0x30(%rsp), %r14
movq %r14, %rsi
callq 0x1602c
leaq 0x55a6f(%rip), %rdx     # 0x6bcd8
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x3170
movq %rbx, %rdx
movl %eax, %r12d
movl (%rdx), %eax
leal -0x10(%rax), %ecx
cmpl $0x4, %ecx
jae 0x162a9
testl %r13d, %r13d
jle 0x162c3
movq 0x8(%rdx), %rdi
decl %r13d
leaq 0x30(%rsp), %r14
movq %r14, %rsi
movl %r13d, %edx
callq 0x1602c
jmp 0x162ca
cmpl $0x15, %eax
jne 0x162e4
movq 0x8(%rdx), %rax
movq 0x28(%rax), %rax
movq 0x10(%rax), %rax
movq 0x108(%rax), %rdx
jmp 0x1628b
leaq 0x55a37(%rip), %r14     # 0x6bd01
movl $0xff, %eax
subl %r12d, %eax
xorl %edx, %edx
testl %eax, %eax
cmovgl %eax, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x31b0
movq %r15, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x4
popq %rax
movq %r15, 0x8(%rsp)
movq 0xb8(%rdi), %rcx
movl (%rcx), %ecx
addl $-0x12, %ecx
movl $0x0, %ebx
cmpl $0x6, %ecx
ja 0x16323
movl %ecx, %ecx
leaq 0x55798(%rip), %rdx     # 0x6bab8
movl (%rdx,%rcx,4), %ebx
movq %r13, 0x20(%rsp)
leal -0x1(%r13), %ecx
movl %ecx, 0x1c(%rsp)
pushq $0x8
popq %rbp
leaq 0x30(%rsp), %r13
leaq -0x7(%rbp), %rcx
movl 0x28(%rdi), %edx
addl %ebx, %edx
movslq %edx, %rdx
movslq %eax, %r14
cmpq %rdx, %rcx
jge 0x163e6
cmpq $0x2, %rcx
jb 0x16387
movq 0x8(%rsp), %rax
leaq (%rax,%r14), %rdi
movl $0x100, %esi            # imm = 0x100
subl %r14d, %esi
testl %esi, %esi
movl $0x0, %eax
cmovlel %eax, %esi
leaq 0x5597a(%rip), %rdx     # 0x6bcf2
xorl %eax, %eax
callq 0x3170
movq 0x10(%rsp), %rdi
addl %r14d, %eax
movslq %eax, %r15
movq 0x8(%rsp), %rax
leaq (%rax,%r15), %r12
movl $0x100, %r14d           # imm = 0x100
subl %r15d, %r14d
testl %r14d, %r14d
movl $0x0, %eax
cmovlel %eax, %r14d
movq (%rdi,%rbp,8), %rax
movq 0x108(%rax), %rdi
movq %r13, %rsi
movl 0x1c(%rsp), %edx
callq 0x1602c
movq %r12, %rdi
movq %r14, %rsi
leaq 0x5592b(%rip), %rdx     # 0x6bcf7
movq %r13, %rcx
xorl %eax, %eax
callq 0x3170
movq 0x10(%rsp), %rdi
addl %r15d, %eax
incq %rbp
jmp 0x16338
movq 0x8(%rsp), %r15
movq %rdi, %r13
leaq (%r15,%r14), %rdi
movl $0x100, %ebx            # imm = 0x100
movl $0x100, %esi            # imm = 0x100
subl %r14d, %esi
xorl %ebp, %ebp
testl %esi, %esi
cmovlel %ebp, %esi
leaq 0x588bc(%rip), %rdx     # 0x6ecc9
xorl %eax, %eax
callq 0x3170
movl %eax, %r12d
addl %r14d, %r12d
movq 0xb8(%r13), %rdi
cmpl $0x2, (%rdi)
je 0x16463
movslq %r12d, %r14
leaq (%r15,%r14), %r12
subl %r14d, %ebx
testl %ebx, %ebx
cmovlel %ebp, %ebx
leaq 0x30(%rsp), %r13
movq %r13, %rsi
movl 0x1c(%rsp), %edx
callq 0x1602c
leaq 0x558a8(%rip), %rdx     # 0x6bcf5
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rcx
xorl %eax, %eax
callq 0x3170
movl %eax, %r12d
addl %r14d, %r12d
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rax
cmpl $0x18, %eax
jne 0x164a2
movslq %r12d, %rbx
movq 0x8(%rsp), %r15
leaq (%r15,%rbx), %rdi
movl $0x100, %esi            # imm = 0x100
subl %ebx, %esi
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
leaq 0x55867(%rip), %rdx     # 0x6bcfa
xorl %eax, %eax
callq 0x3170
movl %eax, %r12d
addl %ebx, %r12d
jmp 0x164a7
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %rdx
jmp 0x16281
movq 0x8(%rsp), %r15
movq %rdi, %r14
leaq (%r15,%rbx), %rdi
movl $0x100, %esi            # imm = 0x100
subl %ebx, %esi
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
leaq 0x54f5b(%rip), %rdx     # 0x6b42d
xorl %eax, %eax
callq 0x3170
movq %r14, %rdx
movl %eax, %r12d
addl %ebx, %r12d
movq 0x20(%rsp), %r13
jmp 0x16281