// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movq %r9, 0x10(%rsp)
movq %r8, %r12
movq %rcx, %rbp
movq %rdx, 0x8(%rsp)
movq %rsi, %r13
movq %rdi, %r15
movq 0x60(%rsp), %r14
movq %rsi, %rdi
callq 0x35808
testb %al, %al
je 0x4263b
movq %rbp, (%rsp)
movq 0x48(%r15), %rdi
callq 0x2b774
movq %rax, %rbp
movq 0x48(%r15), %rdi
callq 0x2b774
movq %rax, %rbx
movq 0x48(%r15), %rdi
pushq $0x1b
popq %rsi
xorl %edx, %edx
movq %r14, %rcx
callq 0x2a981
cmpl $0x0, (%r14)
jne 0x42675
movq %rax, 0x20(%rsp)
movq 0x48(%r15), %rdi
pushq $0x43
popq %rsi
movq (%rsp), %rdx
movq %rbp, %rcx
movq %rbx, %r8
movq %r14, %r9
callq 0x2a8c1
cmpl $0x0, (%r14)
jne 0x42675
movq 0x48(%r15), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2b781
cmpl $0x0, (%r14)
jne 0x42675
movq 0x48(%r15), %rdi
pushq $0x12
popq %rsi
pushq $0x8
popq %rcx
movq 0x8(%rsp), %rdx
movq %r14, %r8
callq 0x2a819
cmpl $0x0, (%r14)
jne 0x42675
movq 0x48(%r15), %rdi
pushq $0x7a
popq %rsi
movq 0x20(%rsp), %rdx
movq %rax, %rcx
movq %r14, %r8
callq 0x2a9a4
cmpl $0x0, (%r14)
jne 0x42675
movq 0x48(%r15), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2b781
cmpl $0x0, (%r14)
jne 0x42675
movb $0x1, %bl
movq (%rsp), %rbp
jmp 0x42654
cmpl $0x16, (%r13)
jne 0x42652
movq %r13, %rdi
callq 0x2e7c9
testb %al, %al
jne 0x4256c
xorl %ebx, %ebx
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq 0x10(%rsp), %r8
movq %r14, %r9
callq 0x314cd
cmpl $0x0, (%r14)
je 0x4268a
pushq $-0x1
popq %rbp
movq %rbp, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, (%rsp)
movq %r14, %r12
movq 0x148(%rax), %r14
movq %rax, %rdi
callq 0x29d88
testb %bl, %bl
pushq $-0x1
popq %rbp
je 0x427c7
movq %r14, 0x10(%rsp)
movq 0x48(%r15), %rdi
callq 0x2b774
movq %rax, %r14
movq 0x48(%r15), %rdi
callq 0x2b774
movq %rax, %rbx
movq 0x48(%r15), %rdi
pushq $0x12
popq %rsi
pushq $0x8
popq %rcx
movq 0x8(%rsp), %rdx
movq %r12, %r8
callq 0x2a819
cmpl $0x0, (%r12)
jne 0x42678
movq %rbx, 0x18(%rsp)
movq 0x48(%r15), %rdi
pushq $0x7a
popq %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0x2a981
cmpl $0x0, (%r12)
jne 0x42678
movq 0x48(%r15), %rdi
pushq $0x3b
popq %rsi
movq %rax, %rdx
movq 0x20(%rsp), %rcx
movq %r12, %r8
callq 0x2a819
cmpl $0x0, (%r12)
jne 0x42678
movq 0x48(%r15), %rdi
pushq $0x43
popq %rsi
movq %rax, %rdx
movq %r14, %rcx
movq 0x18(%rsp), %r8
movq %r12, %r9
callq 0x2a8c1
cmpl $0x0, (%r12)
jne 0x42678
movq 0x48(%r15), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2b781
cmpl $0x0, (%r12)
jne 0x42678
movq %r15, %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
movq %r12, %rcx
callq 0x4a690
cmpl $0x0, (%r12)
jne 0x42678
movq %r15, %rdi
movq %r13, %rsi
movq %rax, %rdx
movq (%rsp), %rcx
movq %r12, %r8
callq 0x41c81
cmpl $0x0, (%r12)
jne 0x42678
movq 0x48(%r15), %rdi
movq 0x18(%rsp), %rsi
movq %r12, %rdx
callq 0x2b781
cmpl $0x0, (%r12)
movq 0x10(%rsp), %r14
jne 0x42678
movq %r14, %rbp
jmp 0x42678