// File: /workspace/github_c_makefile_star2_O1/markdryan[P]subtilis/frontend/array_type.c
// Function: prv_grow_wrapper
static size_t prv_grow_wrapper(subtilis_parser_t *p,
			       const subtilis_type_t *el_type,
			       size_t a1_mem_reg, size_t a1_size_reg,
			       size_t gran_reg, size_t a2_size_reg,
			       subtilis_error_t *err)
{
	subtilis_ir_operand_t get_ref_label;
	subtilis_ir_operand_t empty_label;
	subtilis_ir_operand_t ref_label;
	subtilis_ir_operand_t skip_ref_label;
	subtilis_ir_operand_t a1_heap;
	subtilis_ir_operand_t op1;
	subtilis_ir_operand_t op2;
	subtilis_ir_operand_t get_ref;
	subtilis_ir_operand_t get_new_ref;
	subtilis_ir_operand_t condee;
	subtilis_exp_t *e;
	size_t dest_reg;
	size_t data_reg;
	bool check_ref = subtilis_type_if_is_reference(el_type) ||
			 ((el_type->type == SUBTILIS_TYPE_REC) &&
			  subtilis_type_rec_need_deref(el_type));

	/*
	 * If we're dealing with a collection of references or structures that
	 * contain references and the reference count of the collection is > 1
	 * before the call to ref_grow and has fallen by 1 after the call to
	 * grow, we know a copy has taken place.  In this case we need to ref
	 * all the copied elements.  We can't do this in ref_grow as this
	 * function is type agnostic.
	 */

	/*
	 * TODO: It may be worth providing a specific builtin for growing
	 * collection of references.  RECs are trickier as they have custom
	 * reffers and dereffers.  But do we really want to duplicate all this
	 * code each time we append a string.
	 */

	if (check_ref) {
		get_ref_label.label = subtilis_ir_section_new_label(p->current);
		empty_label.label = subtilis_ir_section_new_label(p->current);

		op1.integer = 0;
		get_ref.reg = subtilis_ir_section_add_instr2(
		    p->current, SUBTILIS_OP_INSTR_MOVI_I32, op1, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;

		condee.reg = a1_size_reg;
		subtilis_ir_section_add_instr_reg(
		    p->current, SUBTILIS_OP_INSTR_JMPC, condee, get_ref_label,
		    empty_label, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;

		subtilis_ir_section_add_label(p->current, get_ref_label.label,
					      err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;

		op1.reg = a1_mem_reg;
		op2.integer = SUBTIILIS_REFERENCE_HEAP_OFF;
		a1_heap.reg = subtilis_ir_section_add_instr(
		    p->current, SUBTILIS_OP_INSTR_LOADO_I32, op1, op2, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;

		subtilis_ir_section_add_instr_no_reg2(p->current,
						      SUBTILIS_OP_INSTR_GETREF,
						      get_ref, a1_heap, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;
		subtilis_ir_section_add_label(p->current, empty_label.label,
					      err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;
	}

	e = subtilis_builtin_ir_call_ref_grow(p, a1_mem_reg, a1_size_reg,
					      gran_reg, a2_size_reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;
	dest_reg = e->exp.ir_op.reg;
	subtilis_exp_delete(e);

	if (check_ref) {
		ref_label.label = subtilis_ir_section_new_label(p->current);
		skip_ref_label.label =
		    subtilis_ir_section_new_label(p->current);

		op1.reg = a1_mem_reg;
		op2.integer = SUBTIILIS_REFERENCE_HEAP_OFF;
		a1_heap.reg = subtilis_ir_section_add_instr(
		    p->current, SUBTILIS_OP_INSTR_LOADO_I32, op1, op2, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;

		get_new_ref.reg = subtilis_ir_section_add_instr2(
		    p->current, SUBTILIS_OP_INSTR_GETREF, a1_heap, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;

		condee.reg = subtilis_ir_section_add_instr(
		    p->current, SUBTILIS_OP_INSTR_LT_I32, get_new_ref, get_ref,
		    err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;

		subtilis_ir_section_add_instr_reg(
		    p->current, SUBTILIS_OP_INSTR_JMPC, condee, ref_label,
		    skip_ref_label, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;

		subtilis_ir_section_add_label(p->current, ref_label.label, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;

		data_reg = subtilis_reference_get_data(p, a1_mem_reg, 0, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;
		subtilis_array_type_ref_els(p, el_type, data_reg, a1_size_reg,
					    err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;

		subtilis_ir_section_add_label(p->current, skip_ref_label.label,
					      err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;
	}

	return dest_reg;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x30(%rsp)
movq %r8, %r13
movq %rcx, %r12
movq %rdx, 0x8(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x37833
movb $0x1, %bpl
testb %al, %al
jne 0x44ddc
cmpl $0x16, (%r15)
jne 0x44dda
movq %r15, %rdi
callq 0x3041c
movl %eax, %ebp
jmp 0x44ddc
xorl %ebp, %ebp
movq %r12, 0x10(%rsp)
movq 0x70(%rsp), %r14
testb %bpl, %bpl
je 0x44ee8
movq %r13, 0x28(%rsp)
movq 0x48(%rbx), %rdi
callq 0x2d00a
movq %rax, %r12
movq 0x48(%rbx), %rdi
callq 0x2d00a
movq %rax, %r13
movq 0x48(%rbx), %rdi
movl $0x1b, %esi
xorl %edx, %edx
movq %r14, %rcx
callq 0x2c189
movq $-0x1, %rcx
cmpl $0x0, (%r14)
jne 0x45039
movq %rax, 0x18(%rsp)
movq 0x48(%rbx), %rdi
movl $0x43, %esi
movq 0x10(%rsp), %rdx
movq %r12, %rcx
movq %r13, %r8
movq %r14, %r9
callq 0x2c0c2
cmpl $0x0, (%r14)
jne 0x45011
movq %r13, 0x20(%rsp)
movq 0x48(%rbx), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2d017
cmpl $0x0, (%r14)
jne 0x45011
movq 0x48(%rbx), %rdi
movl $0x8, %ecx
movl $0x12, %esi
movq 0x8(%rsp), %rdx
movq %r14, %r8
callq 0x2c00f
cmpl $0x0, (%r14)
jne 0x45011
movq 0x48(%rbx), %rdi
movl $0x7a, %esi
movq 0x18(%rsp), %rdx
movq %rax, %rcx
movq %r14, %r8
callq 0x2c1ac
cmpl $0x0, (%r14)
jne 0x45011
movq 0x48(%rbx), %rdi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
callq 0x2d017
cmpl $0x0, (%r14)
movq $-0x1, %rcx
movq 0x28(%rsp), %r13
jne 0x45039
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r13, %rcx
movq 0x30(%rsp), %r8
movq %r14, %r9
callq 0x33365
movq $-0x1, %rcx
cmpl $0x0, (%r14)
jne 0x45039
movq 0x148(%rax), %r12
movq %rax, %rdi
callq 0x2b4c3
testb %bpl, %bpl
je 0x45036
movq 0x48(%rbx), %rdi
callq 0x2d00a
movq %rax, %r13
movq 0x48(%rbx), %rdi
callq 0x2d00a
movq %rax, %rbp
movq 0x48(%rbx), %rdi
movl $0x8, %ecx
movl $0x12, %esi
movq 0x8(%rsp), %rdx
movq %r14, %r8
callq 0x2c00f
cmpl $0x0, (%r14)
jne 0x45011
movq 0x48(%rbx), %rdi
movl $0x7a, %esi
movq %rax, %rdx
movq %r14, %rcx
callq 0x2c189
cmpl $0x0, (%r14)
jne 0x45011
movq 0x48(%rbx), %rdi
movl $0x3b, %esi
movq %rax, %rdx
movq 0x18(%rsp), %rcx
movq %r14, %r8
callq 0x2c00f
cmpl $0x0, (%r14)
jne 0x45011
movq 0x48(%rbx), %rdi
movl $0x43, %esi
movq %rax, %rdx
movq %r13, %rcx
movq %rbp, %r8
movq %r14, %r9
callq 0x2c0c2
cmpl $0x0, (%r14)
jne 0x45011
movq 0x48(%rbx), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x2d017
cmpl $0x0, (%r14)
jne 0x45011
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
movq %r14, %rcx
callq 0x4d29c
cmpl $0x0, (%r14)
jne 0x45011
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
movq 0x10(%rsp), %rcx
movq %r14, %r8
callq 0x44471
cmpl $0x0, (%r14)
je 0x4501a
movq $-0x1, %rcx
jmp 0x45039
movq 0x48(%rbx), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2d017
cmpl $0x0, (%r14)
movq $-0x1, %rcx
jne 0x45039
movq %r12, %rcx
movq %rcx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq