// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
movq %r9, 0x8(%rsp)
movq %r8, %rbp
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %rbx
movq 0xa8(%rsp), %r15
movq (%rdi), %rdi
movq %r15, %rdx
callq 0x12e0e
pushq $-0x1
popq %rax
cmpl $0x0, (%r15)
jne 0x15a34
movq %r12, (%rsp)
movq %r13, %rdi
callq 0x12a25
cmpl $0x2, (%r13)
jne 0x159fc
cmpb $0x28, (%rax)
jne 0x159fc
cmpb $0x0, 0x1(%rax)
je 0x15a43
movq (%rbx), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x57ada(%rip), %r10     # 0x6d4e8
leaq 0x587cd(%rip), %rdx     # 0x6e1e2
pushq $0x1d
popq %rsi
movq 0xa8(%rsp), %rdi
movq %rax, %rcx
pushq $0xb1
pushq %r10
callq 0x140cd
popq %rax
popq %rcx
pushq $-0x1
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %r15
cmpl $0x1, 0x8(%r14)
jne 0x15a6a
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
movq %r13, %rsi
movq 0xa8(%rsp), %r14
movq %r14, %rcx
callq 0x1e882
jmp 0x15a88
leaq 0x10(%rsp), %rdx
pushq $0xa
popq %rcx
movq %rbx, %rdi
movq %r13, %rsi
movq 0xa8(%rsp), %r14
movq %r14, %r8
callq 0x1e437
movq %rax, %r13
movq %rbp, %r12
cmpl $0x0, (%r14)
pushq $-0x1
popq %rax
jne 0x15a34
movq 0xa0(%rsp), %rdx
testq %r13, %r13
je 0x15ae2
cmpl $0x1, 0x8(%r15)
sete %al
cmpq $0x2, %r13
sete %cl
andb %al, %cl
cmpb $0x1, %cl
movq %rbx, %rbp
jne 0x15b04
movq (%rbp), %rax
movq 0x30(%rax), %rdx
movl 0x38(%rax), %ecx
leaq 0x57a19(%rip), %r8      # 0x6d4e8
pushq $0x67
popq %rsi
movq %r14, %rdi
movl $0xc8, %r9d
callq 0x142c0
jmp 0x15aff
movq %rdx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2de8a
cmpl $0x0, (%r14)
movq (%rsp), %rdx
movq 0x8(%rsp), %rsi
je 0x15b56
pushq $-0x1
popq %rbx
jmp 0x15b64
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rcx
callq 0x34108
pushq $-0x1
popq %rbx
cmpl $0x0, (%r14)
jne 0x15b64
leaq 0x10(%rsp), %r9
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
pushq %r14
pushq %r13
callq 0x4108f
addq $0x10, %rsp
cmpl $0x0, (%r14)
jne 0x15b64
movq 0x148(%rax), %rbx
movq %rax, %rdi
callq 0x29d88
jmp 0x15b64
movq %rbx, %rdi
movq %r14, %rcx
callq 0x4ab28
movq %rax, %rbx
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x15b7b
movq 0x10(%rsp,%r14,8), %rdi
callq 0x29d88
incq %r14
jmp 0x15b67
movq %rbx, %rax
jmp 0x15a34