// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq %rsi, %rbp
movq %rdi, %r15
movq (%rsi), %rax
cmpb $0x0, 0x15(%rax)
jne 0x171d9
leaq 0x4ba8(%r15), %rdi
movl $0x3c, %esi
callq 0x1b41f
testq %r14, %r14
je 0x171f7
leaq 0xf0(%r15), %rsi
movq (%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x48(%rsp), %rdi
callq 0xf9da
leaq 0x4e18(%r15), %rbx
leaq 0x4220(%r15), %rsi
movq %rbx, %rdi
movq %rsi, 0x8(%rsp)
movl $0x12, %edx
callq 0x1ea48
movq %rax, %r13
movq %rax, 0x40(%rsp)
movq (%rbp), %rax
movq 0x8(%rax), %rsi
movq %rsi, 0x8(%r13)
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
callq 0x1ede7
movl %eax, %ebx
movq (%rbp), %rax
movzbl 0x15(%rax), %eax
xorl $0x1, %eax
leal 0x39(,%rax,4), %eax
leaq 0x4ba8(%r15), %rcx
movq %rcx, 0x18(%rsp)
cmpl %eax, 0x4bd0(%r15)
je 0x17386
movl %ebx, 0x14(%rsp)
movq (%rbp), %rax
cmpb $0x0, 0x15(%rax)
jne 0x17281
cmpl $0x3d, 0x4bd0(%r15)
je 0x17386
movq 0x8(%r13), %rax
movq %rax, 0x20(%rsp)
testq %r14, %r14
je 0x172aa
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0xfa09
movslq 0x10(%r13), %r12
shlq $0x3, %r12
addq 0x50(%rsp), %r12
jmp 0x172ad
xorl %r12d, %r12d
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
movq %r12, %rdx
callq 0x1617b
movq (%rbp), %rdi
cmpb $0x1, 0x15(%rdi)
jne 0x172e0
movq 0x20(%rsp), %rsi
callq 0x1f24f
testb %al, %al
je 0x172e0
cmpl $0x0, 0x10(%r13)
je 0x17376
movq %r15, %rdi
movq %r14, %rbx
movq %rbp, %r14
movq 0x8(%r13), %rbp
movq %rbp, %rsi
leaq 0x20(%rsp), %rdx
movq %r12, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x11c15
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x1f2a6
movq %r13, %rdx
cmpl $0x12, (%r13)
jne 0x17344
movl 0x10(%rdx), %eax
incl %eax
movl %eax, 0x10(%rdx)
movq 0x8(%rdx), %rcx
imull 0xc0(%rcx), %eax
movl %eax, 0xc0(%rdx)
movl 0xc4(%rcx), %eax
movl %eax, 0xc4(%rdx)
movl $0x3, %eax
cmpl $0x3f, 0x4bd0(%r15)
movq %r14, %rbp
movq %rbx, %r14
movl 0x14(%rsp), %ebx
movq %rdx, %r13
jne 0x1736c
movq 0x18(%rsp), %rdi
callq 0x1a984
xorl %eax, %eax
testl %eax, %eax
je 0x17269
jmp 0x1737d
movl $0x1, %eax
jmp 0x1736c
cmpl $0x3, %eax
jne 0x17518
movq (%rbp), %rax
cmpb $0x0, 0x15(%rax)
jne 0x173b3
cmpl $0x4a, 0x4bd0(%r15)
jne 0x173a4
movq 0x18(%rsp), %rdi
callq 0x1a984
movq 0x18(%rsp), %rdi
movl $0x3d, %esi
callq 0x1b41f
movq %rbp, 0x18(%rsp)
testq %r14, %r14
movq %r14, 0x38(%rsp)
je 0x1746d
movq %r15, 0x8(%rsp)
leaq 0xf0(%r15), %rdi
movslq 0x10(%r13), %rsi
movslq %ebx, %rax
movq %rax, %rbp
imulq %rax, %rsi
callq 0xcca8
movq %rax, 0x30(%rsp)
movslq 0x10(%r13), %rax
testq %rax, %rax
jle 0x1744f
movl %ebp, 0x14(%rsp)
movq 0x8(%rsp), %rcx
leaq 0x4e68(%rcx), %rbx
leaq 0x1(%rax), %r14
leaq -0x8(,%rax,8), %r15
decq %rax
imulq %rbp, %rax
movq 0x30(%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 0x14(%rsp), %r8d
callq 0xef72
decq %r14
addq $-0x8, %r15
addq %rbp, %r12
cmpq $0x1, %r14
jg 0x17421
leaq 0x48(%rsp), %rdi
callq 0xfb24
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rax, (%rcx)
movq 0x8(%rsp), %rdi
jmp 0x174ea
leaq 0x4e38(%r15), %rdi
movq 0x28(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r13, %rcx
callq 0x1a2d1
movl %eax, 0x8(%rsp)
movl 0x10(%r13), %eax
testl %eax, %eax
jle 0x174d2
movl %ebx, %r14d
leaq 0x4e70(%r15), %r12
leal 0x1(%rax), %ebp
decl %eax
imull %ebx, %eax
movl 0x8(%rsp), %ebx
addl %eax, %ebx
movq %r12, %rdi
movl %ebx, %esi
callq 0x17f78
movq 0x8(%r13), %rax
movl (%rax), %esi
movl 0xc0(%rax), %edx
movq %r12, %rdi
callq 0x183d5
decl %ebp
subl %r14d, %ebx
cmpl $0x1, %ebp
jg 0x174aa
leaq 0x4e70(%r15), %rdi
movl 0x8(%rsp), %esi
callq 0x17f78
movq %r15, %rdi
movq 0x38(%rsp), %rcx
movq 0x18(%rsp), %rax
movq (%rax), %rbx
leaq 0x40(%rsp), %r15
movq %rbx, %rsi
movq %r15, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x11c15
movq (%r15), %rdx
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x1f2a6
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq