// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/frontend/array_type.c
// Function: subtilis_array_index_calc
subtilis_exp_t *
subtilis_array_index_calc(subtilis_parser_t *p, const char *var_name,
			  const subtilis_type_t *type, size_t mem_reg,
			  size_t loc, subtilis_exp_t **e, size_t index_count,
			  subtilis_error_t *err)
{
	int i;
	int32_t dim_size;
	size_t last_index;
	subtilis_exp_t *one;
	int32_t offset;
	int32_t last_offset;
	int32_t block_size = 0;
	int dyn_start = 0;
	subtilis_exp_t *blocke = NULL;
	subtilis_exp_t *sizee = NULL;
	subtilis_exp_t *edup = NULL;
	subtilis_exp_t *maxe = NULL;
	subtilis_exp_t *blocke_dup = NULL;
	int64_t array_size;

	if (index_count != type->params.array.num_dims) {
		subtilis_error_bad_index_count(err, var_name,
					       p->l->stream->name, p->l->line);
		return NULL;
	}

	if (index_count == 1)
		return prv_1d_index_calc(p, var_name, type, mem_reg, loc, e,
					 index_count, err);

	offset = loc + SUBTIILIS_ARRAY_DIMS_OFF;

	/*
	 * First we compute the constant part of the array indexing.
	 * This can all be done at compile time if both the indexes and
	 * the dimensions are known to the compiler.  We start with the
	 * final index and work our way toward the first index.  Two
	 * sizes are returned here.
	 *
	 * array_size - the offset in elements from the beginning of the
	 * array we know we need to skip.  This does not include the
	 * offset of the last index, which is added later.
	 *
	 * block_size - the cummulative product elements of all the
	 * trailing dimensions that we've processed. dyn_start -
	 * contains the first index that could not be processed at
	 * compile time.  If dyn_start == indxex_count -1 none of the
	 * indices could be processed at compile time.
	 */

	array_size = prv_check_constant_dims(p, e, index_count, var_name, type,
					     &dyn_start, &block_size, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return NULL;

	sizee = subtilis_exp_new_int32((int32_t)array_size, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return NULL;

	sizee = subtilis_type_if_exp_to_var(p, sizee, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	last_index = index_count - 1;
	last_offset = offset + (sizeof(int32_t) * last_index);
	if (dyn_start == index_count - 1) {
		/*
		 * none of the indices could be processed at compile
		 * time, which means we don't know the block_size for
		 * the last element which the loop below needs.  So we
		 * need to compute it.
		 */

		dim_size = type->params.array.dims[last_index];
		maxe = prv_get_dynamic_dim_size(p, dim_size, last_offset,
						mem_reg, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

		one = subtilis_exp_new_int32(1, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

		blocke = subtilis_type_if_add(p, maxe, one, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

		dyn_start--;
	} else {
		blocke = subtilis_exp_new_int32(block_size, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
	}

	offset += (sizeof(int32_t) * dyn_start);

	/* Need to compute  the dynamic portion of the offset array */

	for (i = dyn_start; i >= 0; i--) {
		dim_size = type->params.array.dims[i];

		maxe = prv_check_dynamic_index(p, e[i], dim_size, offset,
					       mem_reg, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		edup = subtilis_type_if_dup(e[i], err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		one = subtilis_exp_new_int32(1, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		maxe = subtilis_type_if_add(p, maxe, one, err);
		one = NULL;
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		blocke_dup = subtilis_type_if_dup(blocke, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		edup = subtilis_type_if_mul(p, blocke_dup, edup, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		blocke = subtilis_type_if_mul(p, blocke, maxe, err);
		maxe = NULL;
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		sizee = subtilis_type_if_add(p, sizee, edup, err);
		edup = NULL;
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

		offset -= sizeof(int32_t);
	}

	subtilis_exp_delete(blocke);

	/*
	 * Check and add the offset for the final index.  If it's
	 * constant and the last dimension is constant we'll have
	 * already checked it but hey hum.
	 */

	prv_check_last_index(p, var_name, e, type, last_offset, mem_reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	edup = subtilis_type_if_dup(e[index_count - 1], err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	sizee = subtilis_type_if_add(p, edup, sizee, err);
	edup = NULL;
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	return prv_compute_element_address(p, type, mem_reg, loc, sizee, err);

cleanup:

	subtilis_exp_delete(blocke);
	subtilis_exp_delete(maxe);
	subtilis_exp_delete(edup);
	subtilis_exp_delete(sizee);

	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r10
movq 0x98(%rsp), %rbx
movq 0x90(%rsp), %r13
movslq 0x8(%rdx), %rax
cmpq %r13, %rax
jne 0x4114c
movq %r9, %rbp
movq %rcx, %r9
movq %rdx, %r12
cmpq $0x1, %r13
jne 0x41180
movq %r8, %r14
addl $0x10, %r8d
subq $0x8, %rsp
movq %rdi, %r13
movq %r10, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %r9, %r15
pushq %rbx
callq 0x4162a
popq %rax
popq %rcx
cmpl $0x0, (%rbx)
jne 0x4133c
movq (%rbp), %rdi
movq %rbx, %rsi
callq 0x341c4
cmpl $0x0, (%rbx)
jne 0x4133c
movq %r13, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x3415f
cmpl $0x0, (%rbx)
jne 0x4133c
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %rax, %r8
movq %rbx, %r9
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x416b3
movq (%rdi), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x2e834(%rip), %rax     # 0x6f992
leaq 0x2cc60(%rip), %rcx     # 0x6ddc5
pushq $0x3a
popq %rsi
movq %rbx, %rdi
movq %r10, %rdx
pushq $0x762                  # imm = 0x762
pushq %rax
callq 0x140cd
popq %rax
popq %rcx
jmp 0x4133c
leaq -0x1(%r13), %r14
cmpl $-0x1, %r14d
pushq $-0x1
popq %r11
cmovll %r14d, %r11d
movl %r14d, %eax
movq %r8, 0x40(%rsp)
movq %r9, 0x38(%rsp)
movq %rdi, 0x10(%rsp)
movq %r10, 0x50(%rsp)
testl %eax, %eax
js 0x411d6
movl %eax, %ecx
movq (%rbp,%rcx,8), %rdx
cmpl $0x1, (%rdx)
jne 0x411d3
movl 0xc(%r12,%rcx,4), %ecx
cmpl $-0x1, %ecx
je 0x411d3
movl 0x148(%rdx), %edx
testl %edx, %edx
js 0x41230
cmpl %ecx, %edx
jg 0x41230
decl %eax
jmp 0x411a7
movl %eax, %r11d
pushq $0x1
popq %r15
leal 0x1(%r11), %eax
cltq
movq %rax, %rcx
cmpq %r13, %rcx
jae 0x411f8
movl 0xc(%r12,%rcx,4), %edx
incl %edx
imull %edx, %r15d
incq %rcx
jmp 0x411e3
leal 0x2(%r11), %ecx
xorl %edi, %edi
cmpq %rax, %r14
jbe 0x41267
movslq %ecx, %rsi
movq (%rbp,%rax,8), %rdx
movl 0x148(%rdx), %edx
cmpq %r13, %rsi
jae 0x41227
movl 0xc(%r12,%rsi,4), %r8d
incl %r8d
imull %r8d, %edx
incq %rsi
jmp 0x41211
incq %rax
addl %edx, %edi
incl %ecx
jmp 0x411fe
movq (%rdi), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x2e750(%rip), %rax     # 0x6f992
leaq 0x2cb7c(%rip), %rcx     # 0x6ddc5
pushq $0x39
popq %rsi
movq %rbx, %rdi
movq %r10, %rdx
pushq $0x729                  # imm = 0x729
pushq %rax
callq 0x140cd
popq %rax
popq %rcx
xorl %r15d, %r15d
xorl %r11d, %r11d
xorl %edi, %edi
cmpl $0x0, (%rbx)
jne 0x4133c
movq %rbx, %rsi
movq %r11, 0x20(%rsp)
callq 0x2a369
cmpl $0x0, (%rbx)
jne 0x4133c
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x3415f
movq %rax, 0x18(%rsp)
cmpl $0x0, (%rbx)
je 0x412aa
xorl %edi, %edi
xorl %r14d, %r14d
xorl %r15d, %r15d
jmp 0x4131d
movq 0x40(%rsp), %rax
leal (%rax,%r14,4), %eax
addl $0x10, %eax
movslq 0x20(%rsp), %rcx
cmpq %rcx, %r14
jne 0x412fd
movl 0x8(%r12,%r13,4), %esi
movq 0x10(%rsp), %rdi
movl %eax, %r14d
movl %eax, %edx
movq 0x38(%rsp), %rcx
movq %rbx, %r8
callq 0x4157f
movq %rax, %r15
cmpl $0x0, (%rbx)
jne 0x412f9
movl %r14d, 0x2c(%rsp)
pushq $0x1
popq %rdi
movq %rbx, %rsi
callq 0x2a369
cmpl $0x0, (%rbx)
je 0x4134d
xorl %edi, %edi
jmp 0x4131a
movq %rcx, %r14
movl %eax, 0x2c(%rsp)
movl %r15d, %edi
movq %rbx, %rsi
callq 0x2a369
movq %rax, %rdi
xorl %r15d, %r15d
cmpl $0x0, (%rbx)
je 0x41377
xorl %r14d, %r14d
callq 0x29d88
movq %r15, %rdi
callq 0x29d88
movq %r14, %rdi
callq 0x29d88
movq 0x18(%rsp), %rdi
callq 0x29d88
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
movq %rbx, %rcx
callq 0x34b18
movq %rax, %rdi
cmpl $0x0, (%rbx)
jne 0x4131a
movq 0x20(%rsp), %rax
decl %eax
movq %rax, 0x20(%rsp)
movslq %eax, %r14
movq %rdi, 0x8(%rsp)
movq 0x40(%rsp), %rax
addl $0x10, %eax
cltq
leaq (%rax,%r14,4), %rcx
movq 0x20(%rsp), %rax
testl %eax, %eax
js 0x414c3
movq %rax, 0x20(%rsp)
movl %eax, %r14d
movl 0xc(%r12,%r14,4), %edx
movq (%rbp,%r14,8), %rsi
movq 0x10(%rsp), %rdi
movq %rcx, 0x48(%rsp)
movq 0x38(%rsp), %r8
movq %rbx, %r9
callq 0x415be
movq %rax, %r15
cmpl $0x0, (%rbx)
jne 0x414f8
movq (%rbp,%r14,8), %rdi
movq %rbx, %rsi
callq 0x341c4
movq %rax, %r14
cmpl $0x0, (%rbx)
jne 0x41537
pushq $0x1
popq %rdi
movq %rbx, %rsi
callq 0x2a369
cmpl $0x0, (%rbx)
jne 0x41537
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
movq %rbx, %rcx
callq 0x34b18
movq %rax, 0x30(%rsp)
cmpl $0x0, (%rbx)
jne 0x414b4
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x341c4
cmpl $0x0, (%rbx)
jne 0x414b4
movq %r14, %rdx
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
movq %rbx, %rcx
callq 0x34bfb
movq %rax, %r15
cmpl $0x0, (%rbx)
jne 0x414fd
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq %rbx, %rcx
callq 0x34bfb
movq %rax, %rdi
cmpl $0x0, (%rbx)
jne 0x41512
movq %rdi, 0x8(%rsp)
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x34b18
movq %rax, 0x18(%rsp)
cmpl $0x0, (%rbx)
jne 0x4151a
movq 0x48(%rsp), %rcx
movslq %ecx, %rcx
addq $-0x4, %rcx
movq 0x20(%rsp), %rax
decl %eax
xorl %r15d, %r15d
jmp 0x4138f
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %r15
jmp 0x4131d
movq 0x8(%rsp), %rdi
callq 0x29d88
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq %rbp, %rdx
movq %r12, %rcx
movl 0x34(%rsp), %r8d
movq 0x40(%rsp), %r9
pushq %rbx
callq 0x4162a
popq %rax
popq %rcx
cmpl $0x0, (%rbx)
je 0x41522
xorl %r14d, %r14d
jmp 0x41537
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rax
movq %r15, %r14
movq %rax, %r15
jmp 0x4131d
movq %r15, %r14
jmp 0x412a5
xorl %r14d, %r14d
xorl %r15d, %r15d
jmp 0x41537
movq -0x8(%rbp,%r13,8), %rdi
movq %rbx, %rsi
callq 0x341c4
movq %rax, %r14
cmpl $0x0, (%rbx)
je 0x41541
movq 0x8(%rsp), %rdi
jmp 0x4131d
movq %r14, %rsi
movq 0x40(%rsp), %r14
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq %rbx, %rcx
callq 0x34b18
movq %rax, 0x18(%rsp)
cmpl $0x0, (%rbx)
jne 0x414f8
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq 0x38(%rsp), %rdx
movq %r14, %rcx
movq 0x18(%rsp), %r8
jmp 0x41136