// File: /workspace/github_c_makefile_star2_O2/vtereshkov[P]umka-lang/src/umka_expr.c
// Function: parseDynArrayLiteral
static void parseDynArrayLiteral(Compiler *comp, const Type **type, Const *constant)
{
    if (!(*type)->isVariadicParamList)
        lexEat(&comp->lex, TOK_LBRACE);

    ConstArray constItems;
    if (constant)
        constArrayAlloc(&constItems, &comp->storage, (*type)->base);

    // Dynamic array is first parsed as a static array of unknown length, then converted to a dynamic array
    Type *staticArrayType = typeAdd(&comp->types, &comp->blocks, TYPE_ARRAY);
    staticArrayType->base = (*type)->base;
    const int itemSize = typeSize(&comp->types, staticArrayType->base);

    // Parse array
    const TokenKind rightEndTok = (*type)->isVariadicParamList ? TOK_RPAR : TOK_RBRACE;
    if (comp->lex.tok.kind != rightEndTok)
    {
        while ((*type)->isVariadicParamList || comp->lex.tok.kind != TOK_RBRACE)
        {
            const Type *itemType = staticArrayType->base;

            Const *constItem = NULL;
            if (constant)
            {
                constArrayAppend(&constItems, (Const){0});
                constItem = &constItems.data[staticArrayType->numItems];
            }

            parseExpr(comp, &itemType, constItem);

            // Special case: variadic parameter list's first item is already a dynamic array compatible with the variadic parameter list
            if ((*type)->isVariadicParamList && typeCompatible(*type, itemType) && staticArrayType->numItems == 0)
                return;

            doAssertImplicitTypeConv(comp, staticArrayType->base, &itemType, constItem);

            typeResizeArray(staticArrayType, staticArrayType->numItems + 1);

            if (comp->lex.tok.kind != TOK_COMMA)
                break;
            lexNext(&comp->lex);
        }
    }

    if (!(*type)->isVariadicParamList)
    {
        // Allow closing brace on a new line
        if (comp->lex.tok.kind == TOK_IMPLICIT_SEMICOLON)
            lexNext(&comp->lex);

        lexEat(&comp->lex, TOK_RBRACE);
    }

    if (constant)
    {
        // Allocate array
        Const constStaticArray = {.ptrVal = storageAdd(&comp->storage, staticArrayType->numItems * itemSize)};

        // Assign items
        for (int i = staticArrayType->numItems - 1; i >= 0; i--)
            constAssign(&comp->consts, (char *)constStaticArray.ptrVal + i * itemSize, &constItems.data[i], staticArrayType->base->kind, itemSize);

        constArrayFree(&constItems);

        *constant = constStaticArray;
    }
    else
    {
        // Allocate array
        const int staticArrayOffset = identAllocStack(&comp->idents, &comp->types, &comp->blocks, staticArrayType);

        // Assign items
        for (int i = staticArrayType->numItems - 1; i >= 0; i--)
        {
            genPushLocalPtr(&comp->gen, staticArrayOffset + i * itemSize);
            genSwapAssign(&comp->gen, staticArrayType->base->kind, staticArrayType->base->size);
        }

        genPushLocalPtr(&comp->gen, staticArrayOffset);
    }

    // Convert to dynamic array
    doAssertImplicitTypeConv(comp, *type, (const Type **)&staticArrayType, constant);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbp
movq (%rsi), %rax
cmpb $0x0, 0x15(%rax)
jne 0xde69
leaq 0x4ba8(%rbp), %rdi
pushq $0x3c
popq %rsi
callq 0x118d3
testq %r15, %r15
je 0xde86
leaq 0xf0(%rbp), %rsi
movq (%r14), %rax
movq 0x8(%rax), %rdx
leaq 0x38(%rsp), %rdi
callq 0x6ce0
leaq 0x4e18(%rbp), %rbx
leaq 0x4220(%rbp), %rsi
pushq $0x12
popq %rdx
movq %rbx, %rdi
movq %rsi, 0x8(%rsp)
callq 0x14da9
movq %rax, %r12
movq %rax, 0x30(%rsp)
movq (%r14), %rax
movq 0x8(%rax), %rsi
movq %rsi, 0x8(%r12)
movq %rbx, %rdi
callq 0x15124
movl %eax, 0x4(%rsp)
movq (%r14), %rax
movzbl 0x15(%rax), %eax
movl %eax, %ecx
xorl $0x1, %ecx
leal 0x39(,%rcx,4), %ecx
leaq 0x4ba8(%rbp), %rdx
movq %rdx, 0x10(%rsp)
cmpl %ecx, 0x4bd0(%rbp)
jne 0xdef2
xorl %ecx, %ecx
jmp 0xdfe9
movq %rbx, 0x18(%rsp)
leaq 0x20(%rsp), %rbx
testb $0x1, %al
jne 0xdf0d
cmpl $0x3d, 0x4bd0(%rbp)
je 0xdfe2
movq 0x8(%r12), %rax
movq %rax, 0x20(%rsp)
testq %r15, %r15
je 0xdf38
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x6d0d
movslq 0x10(%r12), %r13
shlq $0x3, %r13
addq 0x40(%rsp), %r13
jmp 0xdf3b
xorl %r13d, %r13d
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0xd3a5
movq (%r14), %rdi
cmpb $0x1, 0x15(%rdi)
jne 0xdf6c
movq 0x20(%rsp), %rsi
callq 0x155ab
testb %al, %al
je 0xdf6c
cmpl $0x0, 0x10(%r12)
je 0xe135
movq 0x8(%r12), %rsi
movq %rbp, %rdi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x97b3
cmpl $0x12, (%r12)
jne 0xdfb4
movl 0x10(%r12), %eax
incl %eax
movl %eax, 0x10(%r12)
movq 0x8(%r12), %rcx
imull 0xc0(%rcx), %eax
movl %eax, 0xc0(%r12)
movl 0xc4(%rcx), %eax
movl %eax, 0xc4(%r12)
movl 0x4bd0(%rbp), %ecx
cmpl $0x3f, %ecx
jne 0xdfd4
movq 0x10(%rsp), %rdi
callq 0x10f6e
movq (%r14), %rax
movb 0x15(%rax), %al
jmp 0xdefc
movq (%r14), %rax
movb 0x15(%rax), %al
cmpl $0x4a, %ecx
sete %cl
jmp 0xdfe4
xorl %ecx, %ecx
movq 0x18(%rsp), %rbx
testb $0x1, %al
jne 0xe008
testb %cl, %cl
je 0xdffb
movq 0x10(%rsp), %rdi
callq 0x10f6e
pushq $0x3d
popq %rsi
movq 0x10(%rsp), %rdi
callq 0x118d3
movq %r14, 0x10(%rsp)
testq %r15, %r15
je 0xe0b2
movq %r15, 0x8(%rsp)
leaq 0xf0(%rbp), %rdi
movslq 0x10(%r12), %rsi
movslq 0x4(%rsp), %r15
imulq %r15, %rsi
callq 0x4144
movq %rbp, %rcx
movslq 0x10(%r12), %rbp
movq %rcx, 0x18(%rsp)
leaq 0x4e68(%rcx), %r13
leaq -0x8(,%rbp,8), %r14
leaq -0x1(%rbp), %rbx
imulq %r15, %rbx
movq %rax, 0x28(%rsp)
addq %rax, %rbx
negq %r15
testq %rbp, %rbp
jle 0xe094
decq %rbp
movq 0x40(%rsp), %rdx
addq %r14, %rdx
movq 0x8(%r12), %rax
movl (%rax), %ecx
movq %r13, %rdi
movq %rbx, %rsi
movl 0x4(%rsp), %r8d
callq 0x61ea
addq $-0x8, %r14
addq %r15, %rbx
jmp 0xe064
leaq 0x38(%rsp), %rdi
callq 0x6e18
movq 0x8(%rsp), %r15
movq 0x28(%rsp), %rax
movq %rax, (%r15)
movq 0x18(%rsp), %rbp
jmp 0xe11d
leaq 0x4e38(%rbp), %rdi
movq %rbx, %rsi
movq 0x8(%rsp), %rdx
movq %r12, %rcx
callq 0x10996
movl 0x10(%r12), %r14d
leaq 0x4e70(%rbp), %r13
leal -0x1(%r14), %ebx
imull 0x4(%rsp), %ebx
movl %eax, 0x8(%rsp)
addl %eax, %ebx
testl %r14d, %r14d
jle 0xe111
decl %r14d
movq %r13, %rdi
movl %ebx, %esi
callq 0xeb38
movq 0x8(%r12), %rax
movl (%rax), %esi
movl 0xc0(%rax), %edx
movq %r13, %rdi
callq 0xeee9
subl 0x4(%rsp), %ebx
jmp 0xe0e4
movq %r13, %rdi
movl 0x8(%rsp), %esi
callq 0xeb38
movq 0x10(%rsp), %rax
movq (%rax), %rsi
leaq 0x30(%rsp), %rdx
movq %rbp, %rdi
movq %r15, %rcx
callq 0x97b3
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq