// File: /workspace/github_c_makefile_star2_O0/vtereshkov[P]umka-lang/src/umka_expr.c
// Function: parseIndexSelector
static void parseIndexSelector(Compiler *comp, const Type **type, bool *isVar, bool *isCall)
{
    // Implicit dereferencing: a^[i] == a[i]
    doTryImplicitDeref(comp, type);

    // Explicit dereferencing for a string, since it is just a pointer, not a structured type
    if ((*type)->kind == TYPE_PTR && (*type)->base->kind == TYPE_STR)
        genDeref(&comp->gen, TYPE_STR);

    if ((*type)->kind == TYPE_PTR &&
       ((*type)->base->kind == TYPE_ARRAY || (*type)->base->kind == TYPE_DYNARRAY || (*type)->base->kind == TYPE_STR || (*type)->base->kind == TYPE_MAP))
        *type = (*type)->base;

    if ((*type)->kind != TYPE_ARRAY && (*type)->kind != TYPE_DYNARRAY && (*type)->kind != TYPE_STR && (*type)->kind != TYPE_MAP)
        comp->error.handler(comp->error.context, "Array, string or map expected");

    // Index or key
    lexNext(&comp->lex);

    if ((*type)->kind == TYPE_MAP)
    {
        const Type *keyType = typeMapKey(*type);
        parseExpr(comp, &keyType, NULL);
        doAssertImplicitTypeConv(comp, typeMapKey(*type), &keyType, NULL);
    }
    else
    {
        const Type *indexType = comp->intType;
        parseExpr(comp, &indexType, NULL);
        typeAssertCompatible(&comp->types, comp->intType, indexType);
    }

    lexEat(&comp->lex, TOK_RBRACKET);

    const Type *itemType = NULL;
    switch ((*type)->kind)
    {
        case TYPE_ARRAY:
        {
            genGetArrayPtr(&comp->gen, typeSize(&comp->types, (*type)->base), (*type)->numItems);   // Use nominal length for range checking
            itemType = (*type)->base;
            break;
        }
        case TYPE_DYNARRAY:
        {
            genGetDynArrayPtr(&comp->gen);
            itemType = (*type)->base;
            break;
        }
        case TYPE_STR:
        {
            genGetArrayPtr(&comp->gen, typeSize(&comp->types, comp->charType), -1);                 // Use actual length for range checking
            genDeref(&comp->gen, TYPE_CHAR);
            itemType = comp->charType;
            break;
        }
        case TYPE_MAP:
        {
            genGetMapPtr(&comp->gen, *type);
            itemType = typeMapItem(*type);
            break;
        }
        default:
            break;
    }

    if ((*type)->kind == TYPE_STR)
    {
        *type = itemType;
        *isVar = false;
    }
    else
    {
        if (typeStructured(itemType))
            *type = itemType;
        else
            *type = typeAddPtrTo(&comp->types, &comp->blocks, itemType);
        *isVar = true;
    }

    *isCall = false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x15490
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpl $0x10, (%rax)
jne 0x14c76
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x14, (%rax)
jne 0x14c76
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movl $0x14, %esi
callq 0x16fa0
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpl $0x10, (%rax)
jne 0x14cd4
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x12, (%rax)
je 0x14cc2
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x13, (%rax)
je 0x14cc2
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x14, (%rax)
je 0x14cc2
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x15, (%rax)
jne 0x14cd4
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpl $0x12, (%rax)
je 0x14d25
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpl $0x13, (%rax)
je 0x14d25
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpl $0x14, (%rax)
je 0x14d25
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpl $0x15, (%rax)
je 0x14d25
movq -0x8(%rbp), %rax
movq 0x50f8(%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x5118(%rax), %rdi
leaq 0x1c41b8(%rip), %rsi    # 0x1d8ed9
movb $0x0, %al
callq *%rcx
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
callq 0x1ad90
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpl $0x15, (%rax)
jne 0x14d8c
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x13410
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x28(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0xe8c0
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x13410
movq -0x40(%rbp), %rdi
movq %rax, %rsi
leaq -0x28(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0xd6e0
jmp 0x14dcb
movq -0x8(%rbp), %rax
movq 0x5240(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0xe8c0
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x8(%rbp), %rax
movq 0x5240(%rax), %rsi
movq -0x30(%rbp), %rdx
callq 0x22870
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
movl $0x3b, %esi
callq 0x1b150
movq $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl (%rax), %eax
addl $-0x12, %eax
movl %eax, %ecx
movq %rcx, -0x48(%rbp)
subl $0x3, %eax
ja 0x14f15
movq -0x48(%rbp), %rax
leaq 0x1c3922(%rip), %rcx    # 0x1d8730
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rax
addq $0x4e70, %rax           # imm = 0x4E70
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rsi
callq 0x21fe0
movq -0x50(%rbp), %rdi
movl %eax, %esi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl 0x10(%rax), %edx
callq 0x179b0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x14f17
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
callq 0x17b00
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x14f17
movq -0x8(%rbp), %rax
addq $0x4e70, %rax           # imm = 0x4E70
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x8(%rbp), %rax
movq 0x5270(%rax), %rsi
callq 0x21fe0
movq -0x58(%rbp), %rdi
movl %eax, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x179b0
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movl $0xd, %esi
callq 0x16fa0
movq -0x8(%rbp), %rax
movq 0x5270(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x14f17
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x17b50
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x14a60
movq %rax, -0x38(%rbp)
jmp 0x14f17
jmp 0x14f17
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpl $0x14, (%rax)
jne 0x14f37
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movb $0x0, (%rax)
jmp 0x14f83
movq -0x38(%rbp), %rdi
callq 0xcfa0
testb $0x1, %al
jne 0x14f46
jmp 0x14f53
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x14f7c
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x8(%rbp), %rsi
addq $0x4220, %rsi           # imm = 0x4220
movq -0x38(%rbp), %rdx
callq 0x21fa0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movb $0x1, (%rax)
movq -0x20(%rbp), %rax
movb $0x0, (%rax)
addq $0x60, %rsp
popq %rbp
retq