// File: /workspace/github_c_makefile_star2_O1/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, %r11
movq 0x98(%rsp), %r10
movq 0x90(%rsp), %r13
movslq 0x8(%rdx), %rax
cmpq %r13, %rax
jne 0x43876
movq %r9, %rbp
movq %rcx, %r9
movq %rdx, %r12
cmpq $0x1, %r13
jne 0x438ae
movq %r8, %r14
addl $0x10, %r8d
subq $0x8, %rsp
movq %rdi, %r13
movq %r11, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %r9, %r15
pushq %r10
movq %r10, %rbx
callq 0x43dfb
addq $0x10, %rsp
cmpl $0x0, (%rbx)
jne 0x43ab8
movq %rbx, %rsi
movq (%rbp), %rdi
callq 0x362d2
cmpl $0x0, (%rbx)
jne 0x43ab8
movq %rbx, %rdx
movq %r13, %rdi
movq %rax, %rsi
callq 0x36269
cmpl $0x0, (%rbx)
jne 0x43ab8
movq %rbx, %r9
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %rax, %r8
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x43e85
movq (%rdi), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x3213a(%rip), %rax     # 0x759c2
leaq 0x30566(%rip), %rcx     # 0x73df5
movq %r10, %rdi
movl $0x3a, %esi
movq %r11, %rdx
pushq $0x762                  # imm = 0x762
pushq %rax
callq 0x1442a
addq $0x10, %rsp
jmp 0x43ab8
leaq -0x1(%r13), %rbx
testl %ebx, %ebx
movq %r8, 0x48(%rsp)
movq %r9, 0x40(%rsp)
movq %rdi, 0x30(%rsp)
movq %r11, 0x50(%rsp)
js 0x4390e
movl %r13d, %ecx
movl %ebx, %r14d
leal -0x1(%rcx), %eax
movq (%rbp,%rax,8), %rsi
cmpl $0x1, (%rsi)
jne 0x43918
movl 0xc(%r12,%rax,4), %edx
cmpl $-0x1, %edx
je 0x43913
movl 0x148(%rsi), %ecx
testl %ecx, %ecx
js 0x43992
cmpl %edx, %ecx
jg 0x43992
decl %r14d
movl %eax, %ecx
testl %eax, %eax
jg 0x438d0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x43918
movl %ebx, %r14d
jmp 0x43918
decl %ecx
movl %ecx, %r14d
leal 0x1(%r14), %eax
cltq
movl $0x1, %r15d
cmpq %r13, %rax
jae 0x43947
movslq %r14d, %rcx
movl 0x10(%r12,%rcx,4), %edx
incl %edx
imull %edx, %r15d
leaq 0x1(%rcx), %rdx
addq $0x2, %rcx
cmpq %r13, %rcx
movq %rdx, %rcx
jb 0x4392c
xorl %edi, %edi
cmpq %rax, %rbx
jbe 0x439d3
leal 0x2(%r14), %ecx
movslq %ecx, %rcx
xorl %edi, %edi
movq (%rbp,%rax,8), %rdx
movl 0x148(%rdx), %edx
incq %rax
cmpq %r13, %rax
jae 0x43985
movq %rcx, %rsi
movl 0xc(%r12,%rsi,4), %r8d
incl %r8d
imulq %r8, %rdx
incq %rsi
cmpq %r13, %rsi
jb 0x43971
addq %rdx, %rdi
incq %rcx
cmpq %rax, %rbx
ja 0x4395b
jmp 0x439d3
movq (%rdi), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x3201e(%rip), %rax     # 0x759c2
leaq 0x3044a(%rip), %rcx     # 0x73df5
movq %r10, %rdi
movl $0x39, %esi
movq %r11, %rdx
pushq $0x729                  # imm = 0x729
pushq %rax
movq %r10, %r14
callq 0x1442a
movq %r14, %r10
addq $0x10, %rsp
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %edi, %edi
cmpl $0x0, (%r10)
jne 0x43ab8
movq %r10, %rsi
movq %r10, 0x10(%rsp)
callq 0x2bb29
movq 0x10(%rsp), %rdx
cmpl $0x0, (%rdx)
jne 0x43ab8
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x36269
movq 0x10(%rsp), %r8
movq %rax, 0x8(%rsp)
cmpl $0x0, (%r8)
je 0x43a1f
xorl %edi, %edi
xorl %r13d, %r13d
xorl %r15d, %r15d
jmp 0x43a96
movq 0x48(%rsp), %rax
leal (%rax,%rbx,4), %ecx
addl $0x10, %ecx
movslq %r14d, %rax
cmpq %rax, %rbx
jne 0x43a73
movl 0x8(%r12,%r13,4), %esi
movq 0x30(%rsp), %rdi
movl %ecx, %ebx
movl %ecx, %edx
movq 0x40(%rsp), %rcx
callq 0x43d4e
movq 0x10(%rsp), %rsi
movq %rax, %r15
cmpl $0x0, (%rsi)
jne 0x43a6f
movl %ebx, 0x3c(%rsp)
movl $0x1, %edi
callq 0x2bb29
movq 0x10(%rsp), %rcx
cmpl $0x0, (%rcx)
je 0x43ac9
xorl %edi, %edi
jmp 0x43a93
movl %ecx, 0x3c(%rsp)
movl %r15d, %edi
movq %r8, %rsi
callq 0x2bb29
movq 0x10(%rsp), %r9
movq %rax, %rdi
xorl %r15d, %r15d
cmpl $0x0, (%r9)
je 0x43aea
xorl %r13d, %r13d
movq 0x8(%rsp), %rbx
callq 0x2b4c3
movq %r15, %rdi
callq 0x2b4c3
movq %r13, %rdi
callq 0x2b4c3
movq %rbx, %rdi
callq 0x2b4c3
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x30(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x36c85
movq 0x10(%rsp), %r9
movq %rax, %rdi
cmpl $0x0, (%r9)
jne 0x43a93
decl %r14d
testl %r14d, %r14d
js 0x43c3f
movq %rdi, 0x18(%rsp)
movq 0x48(%rsp), %rax
addl $0x10, %eax
cltq
movl %r14d, %ebx
leaq (%rax,%rbx,4), %r15
incq %rbx
movl 0x8(%r12,%rbx,4), %edx
movq -0x8(%rbp,%rbx,8), %rsi
movq 0x30(%rsp), %rdi
movl %r15d, %ecx
movq 0x40(%rsp), %r8
callq 0x43d8f
movq 0x10(%rsp), %rsi
movq %rax, %r14
cmpl $0x0, (%rsi)
jne 0x43cd9
movq -0x8(%rbp,%rbx,8), %rdi
callq 0x362d2
movq 0x10(%rsp), %rsi
movq %rax, 0x28(%rsp)
cmpl $0x0, (%rsi)
jne 0x43cca
movl $0x1, %edi
callq 0x2bb29
movq 0x10(%rsp), %rcx
cmpl $0x0, (%rcx)
jne 0x43cca
movq 0x30(%rsp), %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x36c85
movq 0x10(%rsp), %rsi
movq %rax, 0x20(%rsp)
cmpl $0x0, (%rsi)
jne 0x43c8b
movq 0x18(%rsp), %rdi
callq 0x362d2
movq 0x10(%rsp), %rcx
cmpl $0x0, (%rcx)
jne 0x43c8b
movq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
movq 0x28(%rsp), %rdx
callq 0x36d24
movq 0x10(%rsp), %rcx
movq %rax, 0x28(%rsp)
cmpl $0x0, (%rcx)
jne 0x43c8b
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x36d24
movq 0x10(%rsp), %rcx
movq %rax, %rdi
cmpl $0x0, (%rcx)
jne 0x43cee
movq %rdi, 0x18(%rsp)
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x36c85
movq 0x10(%rsp), %r9
movq %rax, 0x8(%rsp)
cmpl $0x0, (%r9)
jne 0x43d00
movslq %r15d, %r15
addq $-0x4, %r15
decq %rbx
jg 0x43b0c
movq $0x0, 0x20(%rsp)
movq 0x18(%rsp), %rdi
jmp 0x43c44
movq %r15, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
callq 0x2b4c3
subq $0x8, %rsp
movq 0x38(%rsp), %r15
movq %r15, %rdi
movq 0x58(%rsp), %rsi
movq %rbp, %rdx
movq %r12, %rcx
movl 0x44(%rsp), %r8d
movq 0x48(%rsp), %r14
movq %r14, %r9
movq 0x18(%rsp), %rbx
pushq %rbx
callq 0x43dfb
addq $0x10, %rsp
cmpl $0x0, (%rbx)
je 0x43c9c
xorl %r13d, %r13d
jmp 0x43cb6
movq 0x8(%rsp), %rbx
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r15
jmp 0x43cf6
movq %rbx, %rsi
movq -0x8(%rbp,%r13,8), %rdi
callq 0x362d2
movq 0x10(%rsp), %rcx
movq %rax, %r13
cmpl $0x0, (%rcx)
je 0x43d15
movq 0x8(%rsp), %rbx
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r15
jmp 0x43a9b
movq 0x8(%rsp), %rbx
movq 0x18(%rsp), %rdi
movq %r14, %r15
jmp 0x43cf6
xorl %r13d, %r13d
movq 0x8(%rsp), %rbx
movq 0x18(%rsp), %rdi
movq %r14, %r15
jmp 0x43a9b
xorl %r15d, %r15d
movq 0x8(%rsp), %rbx
movq 0x28(%rsp), %r13
jmp 0x43a9b
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0x8(%rsp), %rbx
movq 0x18(%rsp), %rdi
jmp 0x43a9b
movq %r15, %rdi
movq %r13, %rsi
movq 0x8(%rsp), %rdx
callq 0x36c85
movq 0x10(%rsp), %r9
movq %rax, %rbx
cmpl $0x0, (%r9)
je 0x43d38
xorl %r13d, %r13d
jmp 0x43cbb
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq 0x48(%rsp), %rcx
movq %rbx, %r8
jmp 0x43863