// File: /workspace/github_c_makefile_star2_O3/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 0x437c9
movq %r9, %rbp
movq %rcx, %r9
movq %rdx, %r12
cmpq $0x1, %r13
jne 0x43801
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 0x43d4d
addq $0x10, %rsp
cmpl $0x0, (%rbx)
jne 0x43a0a
movq %rbx, %rsi
movq (%rbp), %rdi
callq 0x36182
cmpl $0x0, (%rbx)
jne 0x43a0a
movq %rbx, %rdx
movq %r13, %rdi
movq %rax, %rsi
callq 0x36119
cmpl $0x0, (%rbx)
jne 0x43a0a
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 0x43ddf
movq (%rdi), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x3222b(%rip), %rax     # 0x75a06
leaq 0x30633(%rip), %rcx     # 0x73e15
movq %r10, %rdi
movl $0x3a, %esi
movq %r11, %rdx
pushq $0x762                  # imm = 0x762
pushq %rax
callq 0x143da
addq $0x10, %rsp
jmp 0x43a0a
leaq -0x1(%r13), %rbx
testl %ebx, %ebx
movq %r8, 0x48(%rsp)
movq %r9, 0x40(%rsp)
movq %rdi, 0x30(%rsp)
movq %r11, 0x50(%rsp)
js 0x43861
movl %r13d, %ecx
movl %ebx, %r14d
leal -0x1(%rcx), %eax
movq (%rbp,%rax,8), %rsi
cmpl $0x1, (%rsi)
jne 0x4386b
movl 0xc(%r12,%rax,4), %edx
cmpl $-0x1, %edx
je 0x43866
movl 0x148(%rsi), %ecx
testl %ecx, %ecx
js 0x438e4
cmpl %edx, %ecx
jg 0x438e4
decl %r14d
movl %eax, %ecx
testl %eax, %eax
jg 0x43823
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x4386b
movl %ebx, %r14d
jmp 0x4386b
decl %ecx
movl %ecx, %r14d
leal 0x1(%r14), %eax
cltq
movl $0x1, %r15d
cmpq %r13, %rax
jae 0x4389a
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 0x4387f
xorl %edi, %edi
cmpq %rax, %rbx
jbe 0x43925
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 0x438d8
movq %rcx, %rsi
movl 0xc(%r12,%rsi,4), %r8d
incl %r8d
imull %r8d, %edx
incq %rsi
cmpq %r13, %rsi
jb 0x438c4
addl %edx, %edi
incq %rcx
cmpq %rax, %rbx
ja 0x438ae
jmp 0x43925
movq (%rdi), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x32110(%rip), %rax     # 0x75a06
leaq 0x30518(%rip), %rcx     # 0x73e15
movq %r10, %rdi
movl $0x39, %esi
movq %r11, %rdx
pushq $0x729                  # imm = 0x729
pushq %rax
movq %r10, %r14
callq 0x143da
movq %r14, %r10
addq $0x10, %rsp
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %edi, %edi
cmpl $0x0, (%r10)
jne 0x43a0a
movq %r10, %rsi
movq %r10, 0x10(%rsp)
callq 0x2b7af
movq 0x10(%rsp), %rdx
cmpl $0x0, (%rdx)
jne 0x43a0a
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x36119
movq 0x10(%rsp), %r8
movq %rax, 0x8(%rsp)
cmpl $0x0, (%r8)
je 0x43971
xorl %edi, %edi
xorl %r13d, %r13d
xorl %r15d, %r15d
jmp 0x439e8
movq 0x48(%rsp), %rax
leal (%rax,%rbx,4), %ecx
addl $0x10, %ecx
movslq %r14d, %rax
cmpq %rax, %rbx
jne 0x439c5
movl 0x8(%r12,%r13,4), %esi
movq 0x30(%rsp), %rdi
movl %ecx, %ebx
movl %ecx, %edx
movq 0x40(%rsp), %rcx
callq 0x43ca0
movq 0x10(%rsp), %rsi
movq %rax, %r15
cmpl $0x0, (%rsi)
jne 0x439c1
movl %ebx, 0x3c(%rsp)
movl $0x1, %edi
callq 0x2b7af
movq 0x10(%rsp), %rcx
cmpl $0x0, (%rcx)
je 0x43a1b
xorl %edi, %edi
jmp 0x439e5
movl %ecx, 0x3c(%rsp)
movl %r15d, %edi
movq %r8, %rsi
callq 0x2b7af
movq 0x10(%rsp), %r9
movq %rax, %rdi
xorl %r15d, %r15d
cmpl $0x0, (%r9)
je 0x43a3c
xorl %r13d, %r13d
movq 0x8(%rsp), %rbx
callq 0x2b166
movq %r15, %rdi
callq 0x2b166
movq %r13, %rdi
callq 0x2b166
movq %rbx, %rdi
callq 0x2b166
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 0x36b28
movq 0x10(%rsp), %r9
movq %rax, %rdi
cmpl $0x0, (%r9)
jne 0x439e5
decl %r14d
testl %r14d, %r14d
js 0x43b91
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 0x43ce1
movq 0x10(%rsp), %rsi
movq %rax, %r14
cmpl $0x0, (%rsi)
jne 0x43c2b
movq -0x8(%rbp,%rbx,8), %rdi
callq 0x36182
movq 0x10(%rsp), %rsi
movq %rax, 0x28(%rsp)
cmpl $0x0, (%rsi)
jne 0x43c1c
movl $0x1, %edi
callq 0x2b7af
movq 0x10(%rsp), %rcx
cmpl $0x0, (%rcx)
jne 0x43c1c
movq 0x30(%rsp), %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x36b28
movq 0x10(%rsp), %rsi
movq %rax, 0x20(%rsp)
cmpl $0x0, (%rsi)
jne 0x43bdd
movq 0x18(%rsp), %rdi
callq 0x36182
movq 0x10(%rsp), %rcx
cmpl $0x0, (%rcx)
jne 0x43bdd
movq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
movq 0x28(%rsp), %rdx
callq 0x36bc7
movq 0x10(%rsp), %rcx
movq %rax, 0x28(%rsp)
cmpl $0x0, (%rcx)
jne 0x43bdd
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x36bc7
movq 0x10(%rsp), %rcx
movq %rax, %rdi
cmpl $0x0, (%rcx)
jne 0x43c40
movq %rdi, 0x18(%rsp)
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x36b28
movq 0x10(%rsp), %r9
movq %rax, 0x8(%rsp)
cmpl $0x0, (%r9)
jne 0x43c52
movslq %r15d, %r15
addq $-0x4, %r15
decq %rbx
jg 0x43a5e
movq $0x0, 0x20(%rsp)
movq 0x18(%rsp), %rdi
jmp 0x43b96
movq %r15, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
callq 0x2b166
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 0x43d4d
addq $0x10, %rsp
cmpl $0x0, (%rbx)
je 0x43bee
xorl %r13d, %r13d
jmp 0x43c08
movq 0x8(%rsp), %rbx
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r15
jmp 0x43c48
movq %rbx, %rsi
movq -0x8(%rbp,%r13,8), %rdi
callq 0x36182
movq 0x10(%rsp), %rcx
movq %rax, %r13
cmpl $0x0, (%rcx)
je 0x43c67
movq 0x8(%rsp), %rbx
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r15
jmp 0x439ed
movq 0x8(%rsp), %rbx
movq 0x18(%rsp), %rdi
movq %r14, %r15
jmp 0x43c48
xorl %r13d, %r13d
movq 0x8(%rsp), %rbx
movq 0x18(%rsp), %rdi
movq %r14, %r15
jmp 0x439ed
xorl %r15d, %r15d
movq 0x8(%rsp), %rbx
movq 0x28(%rsp), %r13
jmp 0x439ed
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0x8(%rsp), %rbx
movq 0x18(%rsp), %rdi
jmp 0x439ed
movq %r15, %rdi
movq %r13, %rsi
movq 0x8(%rsp), %rdx
callq 0x36b28
movq 0x10(%rsp), %r9
movq %rax, %rbx
cmpl $0x0, (%r9)
je 0x43c8a
xorl %r13d, %r13d
jmp 0x43c0d
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq 0x48(%rsp), %rcx
movq %rbx, %r8
jmp 0x437b6