// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/frontend/reference_type.c
// Function: subtilis_reference_type_re_malloc
size_t subtilis_reference_type_re_malloc(subtilis_parser_t *p, size_t store_reg,
					 size_t loc, size_t heap_reg,
					 size_t data_reg, size_t size_reg,
					 size_t new_size_reg,
					 size_t alloc_size_reg,
					 bool heap_known_valid,
					 subtilis_error_t *err)

/* clang-format on */
{
	size_t dest_reg;
	subtilis_ir_operand_t data;

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

	/*
	 * There's no leak potential here as mempcy and deref cannot fail.
	 */

	subtilis_reference_type_memcpy_dest(p, dest_reg, data_reg, size_reg,
					    err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	if (heap_known_valid) {
		data.reg = heap_reg;
		reference_type_call_deref(p, data, err);
	} else {
		subtilis_reference_type_deref(p, store_reg, loc, err);
	}
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	subtilis_reference_set_data(p, dest_reg, store_reg, loc, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	subtilis_reference_set_heap(p, dest_reg, store_reg, loc, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	if (size_reg != new_size_reg) {
		subtilis_reference_type_set_size(p, store_reg, loc,
						 new_size_reg, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;
		subtilis_reference_type_set_orig_size(p, store_reg, loc,
						      new_size_reg, 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 $0x18, %rsp
movq %r9, %r14
movq %r8, 0x8(%rsp)
movq %rcx, %r12
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %rbp
movq 0x68(%rsp), %rbx
movq 0x58(%rsp), %rsi
movq %rbx, %rdx
callq 0x4ae20
pushq $-0x1
popq %rcx
cmpl $0x0, (%rbx)
jne 0x4adbd
movq %r12, 0x10(%rsp)
movq %r13, (%rsp)
movq %rbp, %rdi
movq %rax, %rsi
movq 0x8(%rsp), %rdx
movq %r14, %rcx
movq %rbx, %r8
movq %rax, %r12
callq 0x4abdd
cmpl $0x0, (%rbx)
jne 0x4adba
movq %rbp, %rdi
cmpb $0x0, 0x60(%rsp)
je 0x4ad60
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x4a982
movq %r15, %r13
jmp 0x4ad72
movq (%rsp), %rsi
movq %r15, %r13
movq %r15, %rdx
movq %rbx, %rcx
callq 0x4aeaa
cmpl $0x0, (%rbx)
jne 0x4adba
movq %r12, %rsi
movq %r14, 0x8(%rsp)
movq %rbp, %rdi
movq %rbx, %r15
movq (%rsp), %rbx
movq %rbx, %rdx
movq %r13, %rcx
movq %r15, %r8
movq %r12, %r14
callq 0x4ab59
cmpl $0x0, (%r15)
jne 0x4adba
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r13, %rcx
movq %r15, %r8
callq 0x4ab75
cmpl $0x0, (%r15)
je 0x4adcf
pushq $-0x1
popq %rcx
movq %rcx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %r14
movq 0x50(%rsp), %r15
cmpq %r15, 0x8(%rsp)
jne 0x4ade3
movq %r12, %rcx
jmp 0x4adbd
movq %rbp, %rdi
movq (%rsp), %rbx
movq %rbx, %rsi
movq %r13, %rdx
movq %r15, %rcx
movq %r14, %r8
callq 0x4acc2
cmpl $0x0, (%r14)
jne 0x4adba
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r15, %rcx
movq %r14, %r8
callq 0x4aeaf
cmpl $0x0, (%r14)
pushq $-0x1
popq %rcx
jne 0x4adbd
jmp 0x4adde