// File: /workspace/github_c_makefile_star2_O0/markdryan[P]subtilis/frontend/reference_type.c
// Function: prv_resize_with_malloc
static size_t prv_resize_with_malloc(subtilis_parser_t *p, size_t loc,
				     size_t store_reg, size_t heap_reg,
				     size_t data_reg, size_t old_size_reg,
				     size_t new_size_reg, size_t delta_reg,
				     size_t alloc_size_reg,
				     subtilis_error_t *err)
{
	subtilis_ir_operand_t op2;
	subtilis_ir_operand_t store;
	subtilis_ir_operand_t free_space;
	subtilis_ir_operand_t block_size;
	subtilis_ir_operand_t old_size;
	subtilis_ir_operand_t new_size;
	subtilis_ir_operand_t alloc_needed;
	subtilis_ir_operand_t alloc_needed_label;
	subtilis_ir_operand_t alloc_end_label;
	subtilis_ir_operand_t no_alloc_needed_label;
	subtilis_ir_operand_t dest;
	subtilis_ir_operand_t data;
	subtilis_ir_operand_t heap;
	subtilis_ir_operand_t delta;
	subtilis_ir_operand_t new_block;

	alloc_needed_label.label = subtilis_ir_section_new_label(p->current);
	no_alloc_needed_label.label = subtilis_ir_section_new_label(p->current);
	alloc_end_label.label = subtilis_ir_section_new_label(p->current);

	heap.reg = heap_reg;
	data.reg = data_reg;
	dest.reg = p->current->reg_counter++;
	old_size.reg = old_size_reg;
	new_size.reg = new_size_reg;
	store.reg = store_reg;
	delta.reg = delta_reg;

	if (alloc_size_reg == SIZE_MAX)
		alloc_size_reg = new_size_reg;

	free_space.reg = subtilis_ir_section_add_instr2(
	    p->current, SUBTILIS_OP_INSTR_BLOCK_FREE, heap, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	block_size.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_ADD_I32, old_size, free_space, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	alloc_needed.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_GT_I32, new_size, block_size, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	subtilis_ir_section_add_instr_reg(p->current, SUBTILIS_OP_INSTR_JMPC,
					  alloc_needed, alloc_needed_label,
					  no_alloc_needed_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

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

	new_block.reg =
	    subtilis_reference_type_raw_alloc(p, alloc_size_reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	/*
	 * We copy from the old data_reg not the heap_reg.  This means that when
	 * this function exits, the data and heap regs will be the same even if
	 * they were not when we entered this function, defragmenting the
	 * variable.
	 */

	subtilis_reference_type_memcpy_dest(p, new_block.reg, data_reg,
					    old_size_reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	/*
	 * Here we call deref directly as we already have the pointer to the
	 * block and we don't want to call any destructors.
	 */

	reference_type_call_deref(p, heap, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	op2.integer = loc + SUBTIILIS_REFERENCE_DATA_OFF;
	subtilis_ir_section_add_instr_reg(p->current,
					  SUBTILIS_OP_INSTR_STOREO_I32,
					  new_block, store, op2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	op2.integer = loc + SUBTIILIS_REFERENCE_HEAP_OFF;
	subtilis_ir_section_add_instr_reg(p->current,
					  SUBTILIS_OP_INSTR_STOREO_I32,
					  new_block, store, op2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	subtilis_ir_section_add_instr_no_reg2(p->current, SUBTILIS_OP_INSTR_MOV,
					      dest, new_block, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	subtilis_ir_section_add_instr_no_reg(p->current, SUBTILIS_OP_INSTR_JMP,
					     alloc_end_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

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

	subtilis_ir_section_add_instr_no_reg2(
	    p->current, SUBTILIS_OP_INSTR_BLOCK_ADJUST, heap, delta, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	subtilis_ir_section_add_instr_no_reg2(p->current, SUBTILIS_OP_INSTR_MOV,
					      dest, data, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

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

	op2.integer = loc + SUBTIILIS_REFERENCE_SIZE_OFF;
	subtilis_ir_section_add_instr_reg(p->current,
					  SUBTILIS_OP_INSTR_STOREO_I32,
					  new_size, store, op2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	op2.integer = loc + SUBTIILIS_REFERENCE_ORIG_SIZE_OFF;
	subtilis_ir_section_add_instr_reg(p->current,
					  SUBTILIS_OP_INSTR_STOREO_I32,
					  new_size, store, op2, err);

	return dest.reg;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x39940
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x39940
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x39940
movq %rax, -0x80(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rcx
movq 0x18(%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x18(%rcx)
movq %rax, -0x90(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x60(%rbp)
movq 0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $-0x1, 0x20(%rbp)
jne 0x6e97f
movq 0x10(%rbp), %rax
movq %rax, 0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq 0x28(%rbp), %rcx
movq -0xa0(%rbp), %rdx
movl $0x88, %esi
callq 0x38490
movq %rax, -0x50(%rbp)
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6e9b6
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq 0x28(%rbp), %r8
movq -0x60(%rbp), %rdx
movq -0x50(%rbp), %rcx
xorl %esi, %esi
callq 0x38230
movq %rax, -0x58(%rbp)
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6e9eb
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq 0x28(%rbp), %r8
movq -0x68(%rbp), %rdx
movq -0x58(%rbp), %rcx
movl $0x33, %esi
callq 0x38230
movq %rax, -0x70(%rbp)
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6ea23
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq 0x28(%rbp), %r9
movq -0x70(%rbp), %rdx
movq -0x78(%rbp), %rcx
movq -0x88(%rbp), %r8
movl $0x43, %esi
callq 0x38360
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6ea5e
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x78(%rbp), %rsi
movq 0x28(%rbp), %rdx
callq 0x39960
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6ea89
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x10(%rbp), %rdi
movq 0x20(%rbp), %rsi
movq 0x28(%rbp), %rdx
callq 0x6de60
movq %rax, -0xb0(%rbp)
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6eab7
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x10(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq 0x28(%rbp), %r8
callq 0x6dad0
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6eae9
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x10(%rbp), %rdi
movq 0x28(%rbp), %rdx
movq -0xa0(%rbp), %rsi
callq 0x6d670
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6eb13
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x18(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq 0x28(%rbp), %r9
movq -0xb0(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %r8
movl $0x17, %esi
callq 0x38360
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6eb59
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x18(%rbp), %rax
addq $0x8, %rax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq 0x28(%rbp), %r9
movq -0xb0(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %r8
movl $0x17, %esi
callq 0x38360
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6eb9f
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq 0x28(%rbp), %r8
movq -0x90(%rbp), %rdx
movq -0xb0(%rbp), %rcx
movl $0x1d, %esi
callq 0x385a0
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6ebd9
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq 0x28(%rbp), %rcx
movq -0x80(%rbp), %rdx
movl $0x45, %esi
callq 0x38540
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6ec09
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x88(%rbp), %rsi
movq 0x28(%rbp), %rdx
callq 0x39960
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6ec37
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq 0x28(%rbp), %r8
movq -0xa0(%rbp), %rdx
movq -0xa8(%rbp), %rcx
movl $0x89, %esi
callq 0x385a0
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6ec71
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq 0x28(%rbp), %r8
movq -0x90(%rbp), %rdx
movq -0x98(%rbp), %rcx
movl $0x1d, %esi
callq 0x385a0
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6ecab
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x80(%rbp), %rsi
movq 0x28(%rbp), %rdx
callq 0x39960
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6ecd3
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x18(%rbp), %rax
addq $0x0, %rax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq 0x28(%rbp), %r9
movq -0x68(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %r8
movl $0x17, %esi
callq 0x38360
movq 0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x6ed13
movq $-0x1, -0x8(%rbp)
jmp 0x6ed4b
movq -0x18(%rbp), %rax
addq $0xc, %rax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq 0x28(%rbp), %r9
movq -0x68(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %r8
movl $0x17, %esi
callq 0x38360
movq -0x90(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax,%rax)