// File: /workspace/github_c_makefile_star2_O3/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, %r15
movq %rcx, %r13
movq %rdx, 0x18(%rsp)
movq %rsi, %r12
movq %rdi, %r14
movq 0x60(%rsp), %rbp
movq %rsi, %rdi
callq 0x376f1
testb %al, %al
je 0x44e1a
movq %r13, 0x8(%rsp)
movq 0x48(%r14), %rdi
callq 0x2cc99
movq %rax, %r13
movq 0x48(%r14), %rdi
callq 0x2cc99
movq %rax, %rbx
movq 0x48(%r14), %rdi
movl $0x1b, %esi
xorl %edx, %edx
movq %rbp, %rcx
callq 0x2be09
movq $-0x1, %rcx
cmpl $0x0, (%rbp)
jne 0x44f6c
movq %rax, 0x20(%rsp)
movq 0x48(%r14), %rdi
movl $0x43, %esi
movq 0x8(%rsp), %rdx
movq %r13, %rcx
movq %rbx, %r8
movq %rbp, %r9
callq 0x2bd42
cmpl $0x0, (%rbp)
jne 0x44f65
movq 0x48(%r14), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x2cca6
cmpl $0x0, (%rbp)
jne 0x44f65
movq 0x48(%r14), %rdi
movl $0x8, %ecx
movl $0x12, %esi
movq 0x18(%rsp), %rdx
movq %rbp, %r8
callq 0x2bc8f
cmpl $0x0, (%rbp)
jne 0x44f65
movq 0x48(%r14), %rdi
movl $0x7a, %esi
movq 0x20(%rsp), %rdx
movq %rax, %rcx
movq %rbp, %r8
callq 0x2be2c
cmpl $0x0, (%rbp)
jne 0x44f65
movq 0x48(%r14), %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x2cca6
cmpl $0x0, (%rbp)
movq $-0x1, %rcx
jne 0x44f6c
movb $0x1, %bl
movq 0x8(%rsp), %r13
jmp 0x44e33
cmpl $0x16, (%r12)
jne 0x44e31
movq %r12, %rdi
callq 0x30171
testb %al, %al
jne 0x44d24
xorl %ebx, %ebx
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
movq %r15, %rcx
movq 0x10(%rsp), %r8
movq %rbp, %r9
callq 0x331a0
movq $-0x1, %rcx
cmpl $0x0, (%rbp)
jne 0x44f6c
movq %r13, 0x8(%rsp)
movq 0x148(%rax), %r13
movq %rax, %rdi
callq 0x2b166
testb %bl, %bl
je 0x44f9c
movq 0x48(%r14), %rdi
callq 0x2cc99
movq %rax, %r15
movq 0x48(%r14), %rdi
callq 0x2cc99
movq %rax, %rbx
movq 0x48(%r14), %rdi
movl $0x8, %ecx
movl $0x12, %esi
movq 0x18(%rsp), %rdx
movq %rbp, %r8
callq 0x2bc8f
cmpl $0x0, (%rbp)
jne 0x44f65
movq %rbx, 0x10(%rsp)
movq 0x48(%r14), %rdi
movl $0x7a, %esi
movq %rax, %rdx
movq %rbp, %rcx
callq 0x2be09
cmpl $0x0, (%rbp)
jne 0x44f65
movq 0x48(%r14), %rdi
movl $0x3b, %esi
movq %rax, %rdx
movq 0x20(%rsp), %rcx
movq %rbp, %r8
callq 0x2bc8f
cmpl $0x0, (%rbp)
jne 0x44f65
movq 0x48(%r14), %rdi
movl $0x43, %esi
movq %rax, %rdx
movq %r15, %rcx
movq 0x10(%rsp), %r8
movq %rbp, %r9
callq 0x2bd42
cmpl $0x0, (%rbp)
jne 0x44f65
movq 0x48(%r14), %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x2cca6
cmpl $0x0, (%rbp)
jne 0x44f65
movq %r14, %rdi
movq 0x18(%rsp), %rsi
xorl %edx, %edx
movq %rbp, %rcx
callq 0x4d258
cmpl $0x0, (%rbp)
jne 0x44f65
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
movq 0x8(%rsp), %rcx
movq %rbp, %r8
callq 0x443cb
cmpl $0x0, (%rbp)
je 0x44f7e
movq $-0x1, %rcx
movq %rcx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%r14), %rdi
movq 0x10(%rsp), %rsi
movq %rbp, %rdx
callq 0x2cca6
cmpl $0x0, (%rbp)
movq $-0x1, %rcx
jne 0x44f6c
movq %r13, %rcx
jmp 0x44f6c