// File: /workspace/github_c_makefile_star2_O3/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, %ebx
movq %rsi, %r14
movq %rdi, %r15
cmpl $0x0, 0x4(%rdi)
je 0x1ae3b
movl (%r15), %ebp
cmpq $0x12, %rbp
jne 0x1ae09
movl 0x10(%r15), %ecx
leaq 0x5cef5(%rip), %rdx     # 0x77cea
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
xorl %eax, %eax
callq 0x3170
jmp 0x1b1ba
leal -0x4(%rbp), %eax
cmpl $0x7, %eax
ja 0x1ae63
cmpb $0x1, 0x16(%r15)
jne 0x1b14e
leaq 0x6bcfd(%rip), %rax     # 0x86b20
movq (%rax,%rbp,8), %rcx
leaq 0x5cec1(%rip), %rdx     # 0x77cef
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
jmp 0x1b1b3
movq 0x18(%r15), %rcx
testq %rcx, %rcx
je 0x1ade1
addq $0x4, %rcx
leaq 0x5cec8(%rip), %rdx     # 0x77d17
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
xorl %eax, %eax
callq 0x3170
jmp 0x1b21a
leal -0x15(%rbp), %eax
cmpl $0x5, %eax
ja 0x1b14e
leaq 0x5cbea(%rip), %rcx     # 0x77a60
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq %r14, 0x8(%rsp)
jmpq *%rax
cmpl $0x18, %ebp
jne 0x1ae97
movq 0x28(%r15), %rax
movq (%rax), %rax
movq 0x108(%rax), %r15
movb $0x0, 0x4(%r14)
movl $0x28206e66, (%r14)     # imm = 0x28206E66
movl $0x4, %eax
cmpb $0x1, 0x30(%r15)
jne 0x1aeef
movq 0x8(%rsp), %rax
leaq 0x4(%rax), %r12
movq 0x38(%r15), %rax
movq 0x108(%rax), %rdi
leal -0x1(%rbx), %edx
leaq 0x30(%rsp), %r13
movq %r13, %rsi
callq 0x1adc2
leaq 0x5ce32(%rip), %rdx     # 0x77d0c
movl $0xfc, %esi
movq %r12, %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0x3170
addl $0x4, %eax
movq 0xb8(%r15), %rcx
movl (%rcx), %ecx
addl $-0x12, %ecx
xorl %esi, %esi
cmpl $0x6, %ecx
ja 0x1af0e
movl %ecx, %ecx
leaq 0x5cbcd(%rip), %rdx     # 0x77ad8
movl (%rdx,%rcx,4), %esi
movl 0x28(%r15), %ecx
addl %esi, %ecx
cmpl $0x2, %ecx
movq %rbx, 0x18(%rsp)
movq %rbp, 0x28(%rsp)
jl 0x1afee
leal -0x1(%rbx), %ecx
movl %ecx, 0x14(%rsp)
movl $0x8, %ebx
movl %esi, 0x24(%rsp)
leaq -0x7(%rbx), %rcx
cmpq $0x2, %rcx
jb 0x1af74
movslq %eax, %r14
movq 0x8(%rsp), %r12
leaq (%r12,%r14), %rdi
movl $0x100, %esi            # imm = 0x100
subl %r14d, %esi
testl %esi, %esi
movl $0x0, %eax
cmovlel %eax, %esi
leaq 0x5cdac(%rip), %rdx     # 0x77d12
xorl %eax, %eax
callq 0x3170
xorl %ecx, %ecx
addl %r14d, %eax
jmp 0x1af7b
movq 0x8(%rsp), %r12
xorl %ecx, %ecx
movslq %eax, %rbp
leaq (%r12,%rbp), %r13
movl $0x100, %r12d           # imm = 0x100
subl %ebp, %r12d
testl %r12d, %r12d
cmovlel %ecx, %r12d
movq (%r15,%rbx,8), %rax
movq 0x108(%rax), %rdi
movq %r15, %r14
leaq 0x30(%rsp), %r15
movq %r15, %rsi
movl 0x14(%rsp), %edx
callq 0x1adc2
movq %r13, %rdi
movq %r12, %rsi
leaq 0x5cd59(%rip), %rdx     # 0x77d17
movq %r15, %rcx
movq %r14, %r15
xorl %eax, %eax
callq 0x3170
addl %ebp, %eax
movl 0x28(%r14), %ecx
movl 0x24(%rsp), %esi
addl %esi, %ecx
movslq %ecx, %rcx
leaq 0x1(%rbx), %rdx
addq $-0x6, %rbx
cmpq %rcx, %rbx
movq %rdx, %rbx
jl 0x1af37
movslq %eax, %r14
movq 0x8(%rsp), %rbx
leaq (%rbx,%r14), %rdi
movl $0x100, %r12d           # imm = 0x100
movl $0x100, %esi            # imm = 0x100
subl %r14d, %esi
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
leaq 0x603bb(%rip), %rdx     # 0x7b3d1
xorl %eax, %eax
callq 0x3170
movl %eax, %ebp
addl %r14d, %ebp
movq 0xb8(%r15), %rdi
cmpl $0x2, (%rdi)
je 0x1b072
xorl %eax, %eax
movslq %ebp, %r13
leaq (%rbx,%r13), %r14
subl %r13d, %r12d
testl %r12d, %r12d
cmovlel %eax, %r12d
movq 0x18(%rsp), %rax
leal -0x1(%rax), %edx
leaq 0x30(%rsp), %rbx
movq %rbx, %rsi
callq 0x1adc2
leaq 0x5ccb8(%rip), %rdx     # 0x77d15
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x3170
movl %eax, %ebp
addl %r13d, %ebp
cmpl $0x18, 0x28(%rsp)
jne 0x1b22f
movslq %ebp, %rbx
movq 0x8(%rsp), %r14
leaq (%r14,%rbx), %rdi
movl $0x100, %esi            # imm = 0x100
subl %ebx, %esi
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
leaq 0x5cc7c(%rip), %rdx     # 0x77d1a
jmp 0x1b25c
cmpb $0x1, 0x14(%r15)
jne 0x1b14e
cmpl $0x0, 0x10(%r15)
jle 0x1b14e
movb $0x0, 0x2(%r14)
movw $0x207b, (%r14)         # imm = 0x207B
cmpl $0x0, 0x10(%r15)
movq %rbx, 0x18(%rsp)
jle 0x1b236
leal -0x1(%rbx), %eax
movl %eax, 0x14(%rsp)
movl $0x2, %eax
xorl %r14d, %r14d
movslq %eax, %rbx
movl $0x100, %r13d           # imm = 0x100
subl %ebx, %r13d
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
movq 0x8(%rsp), %rax
leaq (%rax,%rbx), %rbp
movq 0x28(%r15), %rax
movq (%rax,%r14,8), %rax
movq 0x108(%rax), %rdi
leaq 0x30(%rsp), %r12
movq %r12, %rsi
movl 0x14(%rsp), %edx
callq 0x1adc2
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x5cbd2(%rip), %rdx     # 0x77d03
movq %r12, %rcx
xorl %eax, %eax
callq 0x3170
addl %ebx, %eax
incq %r14
movslq 0x10(%r15), %rcx
cmpq %rcx, %r14
jl 0x1b0e3
jmp 0x1b23b
leaq 0x6b9cb(%rip), %rax     # 0x86b20
movq (%rax,%rbp,8), %rcx
leaq 0x5cbb7(%rip), %rdx     # 0x77d17
xorl %ebp, %ebp
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
xorl %eax, %eax
callq 0x3170
jmp 0x1b1bc
movq 0x8(%r15), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rax
movq 0x108(%rax), %rax
movq 0x8(%rax), %rdi
leal -0x1(%rbx), %edx
leaq 0x30(%rsp), %r12
movq %r12, %rsi
callq 0x1adc2
leaq 0x5cb57(%rip), %rdx     # 0x77cf8
movl $0x100, %esi            # imm = 0x100
movq 0x8(%rsp), %rdi
movq %r12, %rcx
movq 0x8(%rsp), %r14
xorl %eax, %eax
callq 0x3170
movl %eax, %ebp
movl (%r15), %eax
leal -0x10(%rax), %ecx
cmpl $0x4, %ecx
jb 0x1b1df
cmpl $0x15, %eax
jne 0x1b21a
movq 0x8(%r15), %rax
movq 0x28(%rax), %rax
movq 0x10(%rax), %rax
movq 0x108(%rax), %r15
testl %ebx, %ebx
jle 0x1b1fa
movq 0x8(%r15), %rdi
decl %ebx
leaq 0x30(%rsp), %r15
movq %r15, %rsi
movl %ebx, %edx
callq 0x1adc2
jmp 0x1b201
leaq 0x5cb20(%rip), %r15     # 0x77d21
movl $0xff, %eax
subl %ebp, %eax
xorl %edx, %edx
testl %eax, %eax
cmovgl %eax, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x31b0
movq %r14, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %r14
jmp 0x1b267
movl $0x2, %eax
movslq %eax, %rbx
movq 0x8(%rsp), %r14
leaq (%r14,%rbx), %rdi
movl $0x100, %esi            # imm = 0x100
subl %ebx, %esi
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
leaq 0x5c231(%rip), %rdx     # 0x7748d
xorl %eax, %eax
callq 0x3170
movl %eax, %ebp
addl %ebx, %ebp
movq 0x18(%rsp), %rbx
jmp 0x1b1bc