// File: /workspace/github_c_makefile_star2_O0/markdryan[P]subtilis/frontend/array_type.c
// Function: subtilis_array_size_calc
subtilis_exp_t *subtilis_array_size_calc(subtilis_parser_t *p,
					 const char *var_name,
					 subtilis_exp_t **e, size_t index_count,
					 subtilis_error_t *err)
{
	size_t i;
	bool non_const_dim = false;
	subtilis_exp_t *sizee = NULL;
	subtilis_exp_t *edup = NULL;
	subtilis_exp_t *one = NULL;
	int64_t array_size = 1;

	/*
	 * TODO: This whole thing is 32 bit specific.  At least the
	 * pointer needs to be platform specific although we may still
	 * keep maximum array sizes to 32 bits.
	 */

	/* TODO:
	 * I don't see any way to detect whether the array dimensions
	 * overflow 32 bits. For constants we can use 64 bit integers to
	 * store the size. This would then limit us to using 32 bits for
	 * the array size, even on 64 bit builds (unless we resort to
	 * instrinsics) as there are no 128 bit integers in C.  For the
	 * ARM2 backend, there is no way to detect overflow of dynamic
	 * vars.  This could be done on StrongARM using UMULL.  Perhaps
	 * we could execute different code on SA here.  For now, be
	 * careful with those array bounds!
	 */

	for (i = 0; i < index_count; i++) {
		if (e[i]->type.type != SUBTILIS_TYPE_CONST_INTEGER) {
			non_const_dim = true;
			continue;
		}
		if (e[i]->exp.ir_op.integer < 0) {
			subtilis_error_bad_dim(err, var_name,
					       p->l->stream->name, p->l->line);
			return NULL;
		}
		array_size = array_size * (e[i]->exp.ir_op.integer + 1);
	}

	if (array_size > (int64_t)0x7ffffff) {
		subtilis_error_bad_dim(err, var_name, p->l->stream->name,
				       p->l->line);
		return NULL;
	}

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

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

		for (i = 0; i < index_count; i++) {
			if (e[i]->type.type != SUBTILIS_TYPE_CONST_INTEGER) {
				prv_check_dynamic_dim(p, e[i], 0, NULL, 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;
				edup = subtilis_type_if_add(p, edup, one, err);
				if (err->type != SUBTILIS_ERROR_OK)
					goto cleanup;
				sizee =
				    subtilis_type_if_mul(p, sizee, edup, err);
				if (err->type != SUBTILIS_ERROR_OK)
					goto cleanup;
			}
		}
	}

	return sizee;

cleanup:

	subtilis_exp_delete(sizee);

	return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movb $0x0, -0x39(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x1, -0x60(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x5cc69
movq -0x20(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpl $0x1, (%rax)
je 0x5cbd0
movb $0x1, -0x39(%rbp)
jmp 0x5cc58
movq -0x20(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpl $0x0, 0x148(%rax)
jge 0x5cc34
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl 0x38(%rax), %r9d
movl $0x38, %esi
leaq 0x4e667(%rip), %rcx     # 0xab276
leaq 0x4fd67(%rip), %rax     # 0xac97d
movq %rax, (%rsp)
movl $0x625, 0x8(%rsp)       # imm = 0x625
callq 0x156d0
movq $0x0, -0x8(%rbp)
jmp 0x5ce2e
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl 0x148(%rcx), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
imulq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x5cba8
cmpq $0x7ffffff, -0x60(%rbp) # imm = 0x7FFFFFF
jle 0x5ccc2
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl 0x38(%rax), %r9d
movl $0x38, %esi
leaq 0x4e5d9(%rip), %rcx     # 0xab276
leaq 0x4fcd9(%rip), %rax     # 0xac97d
movq %rax, (%rsp)
movl $0x62d, 0x8(%rsp)       # imm = 0x62D
callq 0x156d0
movq $0x0, -0x8(%rbp)
jmp 0x5ce2e
movq -0x60(%rbp), %rax
movl %eax, %edi
movq -0x30(%rbp), %rsi
callq 0x37800
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
je 0x5cceb
movq $0x0, -0x8(%rbp)
jmp 0x5ce2e
testb $0x1, -0x39(%rbp)
je 0x5ce13
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x47c10
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
je 0x5cd18
jmp 0x5ce1d
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x5ce11
movq -0x20(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpl $0x1, (%rax)
je 0x5cdfe
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x30(%rbp), %r8
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x5eb40
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
je 0x5cd70
jmp 0x5ce1d
movq -0x20(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x30(%rbp), %rsi
callq 0x47d20
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
je 0x5cd97
jmp 0x5ce1d
movq -0x30(%rbp), %rsi
movl $0x1, %edi
callq 0x37800
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
je 0x5cdb4
jmp 0x5ce1d
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x48e20
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
je 0x5cdd8
jmp 0x5ce1d
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x48fb0
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
je 0x5cdfc
jmp 0x5ce1d
jmp 0x5cdfe
jmp 0x5ce00
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x5cd20
jmp 0x5ce13
movq -0x48(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x5ce2e
movq -0x48(%rbp), %rdi
callq 0x36ee0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)