// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movq %rdx, 0x10(%rsp)
movq %rsi, %r14
movq %rdi, %r15
movq (%rsi), %rax
cmpb $0x0, 0x15(%rax)
jne 0x12212
leaq 0x4ba8(%r15), %rdi
movl $0x3c, %esi
callq 0x1637d
cmpq $0x0, 0x10(%rsp)
je 0x12232
leaq 0xf0(%r15), %rsi
movq (%r14), %rax
movq 0x8(%rax), %rdx
leaq 0x48(%rsp), %rdi
callq 0xa7fb
leaq 0x4e18(%r15), %rbx
leaq 0x4220(%r15), %rsi
movq %rbx, %rdi
movq %rsi, 0x18(%rsp)
movl $0x12, %edx
callq 0x19a70
movq %rax, %r13
movq %rax, 0x40(%rsp)
movq (%r14), %rax
movq 0x8(%rax), %rsi
movq %rsi, 0x8(%r13)
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
callq 0x19e29
movl %eax, %ebx
movq (%r14), %rax
movzbl 0x15(%rax), %eax
movl %eax, %ecx
xorl $0x1, %ecx
leal 0x39(,%rcx,4), %ecx
leaq 0x4ba8(%r15), %rdx
movq %rdx, 0x8(%rsp)
cmpl %ecx, 0x4bd0(%r15)
jne 0x122a3
xorl %ecx, %ecx
jmp 0x123af
movl %ebx, 0x20(%rsp)
leaq 0x30(%rsp), %rbx
testb $0x1, %al
jne 0x122be
cmpl $0x3d, 0x4bd0(%r15)
je 0x123a9
movq 0x8(%r13), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x122ea
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0xa82a
movslq 0x10(%r13), %r12
shlq $0x3, %r12
addq 0x50(%rsp), %r12
jmp 0x122ed
xorl %r12d, %r12d
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x111c3
movq (%r14), %rdi
cmpb $0x1, 0x15(%rdi)
jne 0x1231d
movq 0x30(%rsp), %rsi
callq 0x1a220
testb %al, %al
je 0x1231d
cmpl $0x0, 0x10(%r13)
je 0x12532
movq 0x8(%r13), %rbp
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xcabc
movq 0x30(%rsp), %rdx
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x1a277
cmpl $0x12, (%r13)
jne 0x1237a
movl 0x10(%r13), %eax
incl %eax
movl %eax, 0x10(%r13)
movq 0x8(%r13), %rcx
imull 0xc0(%rcx), %eax
movl %eax, 0xc0(%r13)
movl 0xc4(%rcx), %eax
movl %eax, 0xc4(%r13)
movl 0x4bd0(%r15), %ecx
cmpl $0x3f, %ecx
jne 0x1239b
movq 0x8(%rsp), %rdi
callq 0x15914
movq (%r14), %rax
movb 0x15(%rax), %al
jmp 0x122ac
movq (%r14), %rax
movb 0x15(%rax), %al
cmpl $0x4a, %ecx
sete %cl
jmp 0x123ab
xorl %ecx, %ecx
movl 0x20(%rsp), %ebx
testb $0x1, %al
jne 0x123d0
testb %cl, %cl
je 0x123c1
movq 0x8(%rsp), %rdi
callq 0x15914
movq 0x8(%rsp), %rdi
movl $0x3d, %esi
callq 0x1637d
movq %r14, 0x20(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x12488
movq %r15, 0x18(%rsp)
leaq 0xf0(%r15), %rdi
movslq 0x10(%r13), %rsi
movslq %ebx, %rax
movq %rax, %rbp
imulq %rax, %rsi
callq 0x7a70
movq %rax, 0x38(%rsp)
movslq 0x10(%r13), %rax
testq %rax, %rax
jle 0x1246a
movl %ebp, 0x8(%rsp)
movq 0x18(%rsp), %rcx
leaq 0x4e68(%rcx), %rbx
leaq 0x1(%rax), %r14
leaq -0x8(,%rax,8), %r15
decq %rax
imulq %rbp, %rax
movq 0x38(%rsp), %r12
addq %rax, %r12
negq %rbp
movq 0x50(%rsp), %rdx
addq %r15, %rdx
movq 0x8(%r13), %rax
movl (%rax), %ecx
movq %rbx, %rdi
movq %r12, %rsi
movl 0x8(%rsp), %r8d
callq 0x9d0e
decq %r14
addq $-0x8, %r15
addq %rbp, %r12
cmpq $0x1, %r14
ja 0x1243c
leaq 0x48(%rsp), %rdi
callq 0xa945
movq 0x10(%rsp), %rcx
movq 0x38(%rsp), %rax
movq %rax, (%rcx)
movq 0x18(%rsp), %rdi
jmp 0x12504
leaq 0x4e38(%r15), %rdi
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r13, %rcx
callq 0x15267
movl %eax, 0x8(%rsp)
movl 0x10(%r13), %eax
testl %eax, %eax
jle 0x124ec
leaq 0x4e70(%r15), %r12
leal 0x1(%rax), %r14d
decl %eax
imull %ebx, %eax
movl 0x8(%rsp), %ebp
addl %eax, %ebp
movq %r12, %rdi
movl %ebp, %esi
callq 0x12f97
movq 0x8(%r13), %rax
movl (%rax), %esi
movl 0xc0(%rax), %edx
movq %r12, %rdi
callq 0x133bf
decl %r14d
subl %ebx, %ebp
cmpl $0x1, %r14d
ja 0x124c3
leaq 0x4e70(%r15), %rdi
movl 0x8(%rsp), %esi
callq 0x12f97
movq 0x10(%rsp), %rcx
movq %r15, %rdi
movq 0x20(%rsp), %rax
movq (%rax), %rbx
leaq 0x40(%rsp), %r15
movq %rbx, %rsi
movq %r15, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xcabc
movq (%r15), %rdx
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a277
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq