// File: /workspace/github_c_makefile_star2_O0/markdryan[P]subtilis/frontend/array_type.c
// Function: subtilis_array_get_dim
subtilis_exp_t *subtilis_array_get_dim(subtilis_parser_t *p, subtilis_exp_t *ar,
				       subtilis_exp_t *dim,
				       subtilis_error_t *err)
{
	int32_t requested_dim;
	int32_t dim_index;
	size_t num_dims;
	subtilis_ir_operand_t op;
	subtilis_exp_t *e = NULL;

	/*
	 * This is unfortunate as DIM(a()) may generate one add
	 * instruction for the array reference even though we don't
	 * actually need it. Hopefully, the optimiser will be able to
	 * get rid of it.
	 */

	num_dims = ar->type.params.array.num_dims;
	if (!dim) {
		e = subtilis_exp_new_int32(num_dims, err);
	} else {
		if (dim->type.type == SUBTILIS_TYPE_CONST_INTEGER) {
			if ((dim->exp.ir_op.integer <= 0) ||
			    (dim->exp.ir_op.integer > num_dims)) {
				subtilis_error_bad_index(
				    err, "dim", p->l->stream->name, p->l->line);
				goto cleanup;
			}
			dim_index = dim->exp.ir_op.integer - 1;
			requested_dim = ar->type.params.array.dims[dim_index];
			if (requested_dim != SUBTILIS_DYNAMIC_DIMENSION) {
				e = subtilis_exp_new_int32(requested_dim, err);
			} else {
				e = subtilis_type_if_load_from_mem(
				    p, &subtilis_type_integer,
				    ar->exp.ir_op.reg,
				    SUBTIILIS_ARRAY_DIMS_OFF +
					(sizeof(int32_t) * dim_index),
				    err);
			}
		} else {
			prv_check_dimension_num(p, dim, num_dims, err);
			if (err->type != SUBTILIS_ERROR_OK)
				goto cleanup;

			op.integer = 1;
			dim->exp.ir_op.reg = subtilis_ir_section_add_instr(
			    p->current, SUBTILIS_OP_INSTR_SUBI_I32,
			    dim->exp.ir_op, op, err);
			if (err->type != SUBTILIS_ERROR_OK)
				goto cleanup;
			/* TODO This is int32 specific */
			op.integer = 2;
			dim->exp.ir_op.reg = subtilis_ir_section_add_instr(
			    p->current, SUBTILIS_OP_INSTR_LSLI_I32,
			    dim->exp.ir_op, op, err);
			if (err->type != SUBTILIS_ERROR_OK)
				goto cleanup;
			op.reg = subtilis_ir_section_add_instr(
			    p->current, SUBTILIS_OP_INSTR_ADD_I32,
			    ar->exp.ir_op, dim->exp.ir_op, err);
			if (err->type != SUBTILIS_ERROR_OK)
				goto cleanup;
			e = subtilis_type_if_load_from_mem(
			    p, &subtilis_type_integer, op.reg,
			    SUBTIILIS_ARRAY_DIMS_OFF, err);
		}
	}

cleanup:

	subtilis_exp_delete(ar);
	subtilis_exp_delete(dim);

	return e;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movslq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x5fd5b
movq -0x30(%rbp), %rax
movl %eax, %edi
movq -0x20(%rbp), %rsi
callq 0x37800
movq %rax, -0x40(%rbp)
jmp 0x5ff48
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x5fe3b
movq -0x18(%rbp), %rax
cmpl $0x0, 0x148(%rax)
jle 0x5fd86
movq -0x18(%rbp), %rax
movslq 0x148(%rax), %rax
cmpq -0x30(%rbp), %rax
jbe 0x5fdd0
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x38(%rax), %r9d
movl $0x39, %esi
leaq 0x4a7f4(%rip), %rdx     # 0xaa5a0
leaq 0x4b4c3(%rip), %rcx     # 0xab276
leaq 0x4cbc3(%rip), %rax     # 0xac97d
movq %rax, (%rsp)
movl $0x88c, 0x8(%rsp)       # imm = 0x88C
callq 0x156d0
jmp 0x5ff4a
movq -0x18(%rbp), %rax
movl 0x148(%rax), %eax
subl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl 0xc(%rax,%rcx,4), %eax
movl %eax, -0x24(%rbp)
cmpl $-0x1, -0x24(%rbp)
je 0x5fe07
movl -0x24(%rbp), %edi
movq -0x20(%rbp), %rsi
callq 0x37800
movq %rax, -0x40(%rbp)
jmp 0x5fe36
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x148(%rax), %rdx
movslq -0x28(%rbp), %rcx
shlq $0x2, %rcx
addq $0x10, %rcx
movq -0x20(%rbp), %r8
leaq 0x4baa3(%rip), %rsi     # 0xab8d0
callq 0x48080
movq %rax, -0x40(%rbp)
jmp 0x5ff46
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x20(%rbp), %rcx
callq 0x5ff70
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x5fe60
jmp 0x5ff4a
movl $0x1, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %r8
movq 0x148(%rax), %rdx
movq -0x38(%rbp), %rcx
movl $0xb, %esi
callq 0x38230
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x148(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x5fea8
jmp 0x5ff4a
movl $0x2, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %r8
movq 0x148(%rax), %rdx
movq -0x38(%rbp), %rcx
movl $0x4c, %esi
callq 0x38230
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x148(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x5feed
jmp 0x5ff4a
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %r8
movq 0x148(%rcx), %rdx
movq 0x148(%rax), %rcx
xorl %esi, %esi
callq 0x38230
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x5ff25
jmp 0x5ff4a
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
movq -0x20(%rbp), %r8
leaq 0x4b998(%rip), %rsi     # 0xab8d0
movl $0x10, %ecx
callq 0x48080
movq %rax, -0x40(%rbp)
jmp 0x5ff48
jmp 0x5ff4a
movq -0x10(%rbp), %rdi
callq 0x36ee0
movq -0x18(%rbp), %rdi
callq 0x36ee0
movq -0x40(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)