// File: /workspace/github_c_makefile_star2_O1/markdryan[P]subtilis/frontend/parser_array.c
// Function: subtils_parser_array_lvalue
size_t subtils_parser_array_lvalue(subtilis_parser_t *p, subtilis_token_t *t,
				   const subtilis_type_t *stype, size_t loc,
				   const char *var_name, size_t mem_reg,
				   subtilis_type_t *type, subtilis_error_t *err)
{
	subtilis_exp_t *indices[SUBTILIS_MAX_DIMENSIONS];
	size_t i;
	subtilis_exp_t *e;
	size_t dims;
	const char *tbuf;
	size_t ret_val = SIZE_MAX;

	subtilis_lexer_get(p->l, t, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	tbuf = subtilis_token_get_text(t);
	if ((t->type != SUBTILIS_TOKEN_OPERATOR) || (strcmp(tbuf, "("))) {
		subtilis_error_set_expected(err, "(", tbuf, p->l->stream->name,
					    p->l->line);
		return SIZE_MAX;
	}

	if (stype->params.array.num_dims == 1)
		dims =
		    subtilis_round_bracketed_slice_have_b(p, t, indices, err);
	else
		dims = subtilis_var_bracketed_int_args_have_b(
		    p, t, indices, SUBTILIS_MAX_DIMENSIONS, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	if (dims == 0) {
		/* What we have here is an array reference. */

		subtilis_type_copy(type, stype, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		ret_val = subtilis_reference_get_pointer(p, mem_reg, loc, err);
		goto cleanup;
	} else if ((stype->params.array.num_dims == 1) && (dims == 2)) {
		subtilis_error_set_lvalue_expected(err, p->l->stream->name,
						   p->l->line);
		goto cleanup;
	}

	/*
	 * Otherwise we have an array element.  We need to compute its
	 * address.
	 */

	subtilis_type_if_element_type(p, stype, type, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	e = subtilis_array_index_calc(p, var_name, stype, mem_reg, loc, indices,
				      dims, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	ret_val = e->exp.ir_op.reg;
	subtilis_exp_delete(e);

cleanup:

	for (i = 0; i < dims; i++)
		subtilis_exp_delete(indices[i]);

	return ret_val;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, 0x18(%rsp)
movq %r8, %rbp
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %rbx
movq 0xb8(%rsp), %r15
movq (%rdi), %rdi
movq %r15, %rdx
callq 0x12eda
movq $-0x1, %rcx
cmpl $0x0, (%r15)
jne 0x15edd
movq %r14, 0x8(%rsp)
movq 0x18(%rsp), %r15
movq %r12, 0x10(%rsp)
movq %r13, %rdi
callq 0x12ad0
movq %rax, %r12
cmpl $0x2, (%r13)
jne 0x15e9e
leaq 0x5e380(%rip), %rsi     # 0x74212
movq %r12, %rdi
callq 0x12160
testl %eax, %eax
je 0x15eef
movq (%rbx), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x5d63c(%rip), %rax     # 0x734ec
leaq 0x5e35b(%rip), %rdx     # 0x74212
movq 0xb8(%rsp), %rdi
movl $0x1d, %esi
movq %r12, %rcx
pushq $0xb1
pushq %rax
callq 0x1442a
addq $0x10, %rsp
movq $-0x1, %rcx
movq %rcx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
cmpl $0x1, 0x8(%rax)
movq 0xb8(%rsp), %r14
leaq 0x20(%rsp), %rdx
jne 0x15f17
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rcx
callq 0x1f4fd
jmp 0x15f2a
movl $0xa, %ecx
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %r8
callq 0x1f017
movq %rax, %r12
cmpl $0x0, (%r14)
movq $-0x1, %rcx
jne 0x15edd
movq %r15, %r13
movq 0xb0(%rsp), %rdx
testq %r12, %r12
je 0x15f95
movq 0x8(%rsp), %r15
cmpl $0x1, 0x8(%r15)
sete %al
cmpq $0x2, %r12
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x15fca
movq (%rbx), %rax
movq 0x30(%rax), %rdx
movl 0x38(%rax), %ecx
leaq 0x5d576(%rip), %r8      # 0x734ec
movq %r14, %rdi
movl $0x67, %esi
movl $0xc8, %r9d
callq 0x14618
movq $-0x1, %rcx
jmp 0x16027
movq %rdx, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x2fa5f
movq $-0x1, %rcx
cmpl $0x0, (%r14)
jne 0x16027
movq %rbx, %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rdx
movq %r14, %rcx
callq 0x4d763
movq %rax, %rcx
jmp 0x16027
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rcx
callq 0x3620e
movq $-0x1, %rcx
cmpl $0x0, (%r14)
jne 0x16027
leaq 0x20(%rsp), %r9
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r13, %rcx
movq 0x10(%rsp), %r8
pushq %r14
pushq %r12
callq 0x437b3
movq $-0x1, %rcx
addq $0x10, %rsp
cmpl $0x0, (%r14)
jne 0x16027
movq 0x148(%rax), %rbx
movq %rax, %rdi
callq 0x2b4c3
movq %rbx, %rcx
testq %r12, %r12
je 0x15edd
movq %rcx, %rbx
xorl %r14d, %r14d
movq 0x20(%rsp,%r14,8), %rdi
callq 0x2b4c3
incq %r14
cmpq %r14, %r12
jne 0x16036
movq %rbx, %rcx
jmp 0x15edd