// File: /workspace/github_c_makefile_star2_O3/markdryan[P]subtilis/frontend/reference_type.c
// Function: subtilis_reference_type_copy_on_write
size_t subtilis_reference_type_copy_on_write(subtilis_parser_t *p,
					     size_t store_reg, size_t loc,
					     size_t size_reg,
					     subtilis_error_t *err)
{
	size_t dest_reg;
	subtilis_ir_operand_t ptr;
	subtilis_ir_operand_t malloc_label;
	subtilis_ir_operand_t nomalloc_label;
	subtilis_ir_operand_t end_label;
	subtilis_ir_operand_t ref_count;
	subtilis_ir_operand_t op1;
	subtilis_ir_operand_t op2;
	subtilis_ir_operand_t data_reg;
	subtilis_ir_operand_t heap_reg;

	malloc_label.label = subtilis_ir_section_new_label(p->current);
	nomalloc_label.label = subtilis_ir_section_new_label(p->current);
	end_label.label = subtilis_ir_section_new_label(p->current);

	ptr.reg = p->current->reg_counter++;

	data_reg.reg = subtilis_reference_get_data(p, store_reg, loc, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	heap_reg.reg = subtilis_reference_get_heap(p, store_reg, loc, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

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

	op2.integer = 1;
	ref_count.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_EQI_I32, ref_count, op2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	subtilis_ir_section_add_instr_reg(p->current, SUBTILIS_OP_INSTR_JMPC,
					  ref_count, nomalloc_label,
					  malloc_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

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

	dest_reg = subtilis_reference_type_re_malloc(
	    p, store_reg, loc, heap_reg.reg, data_reg.reg, size_reg, size_reg,
	    size_reg, true, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	op1.reg = dest_reg;
	subtilis_ir_section_add_instr_no_reg2(p->current, SUBTILIS_OP_INSTR_MOV,
					      ptr, op1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

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

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

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

	subtilis_ir_section_add_label(p->current, end_label.label, err);

	return ptr.reg;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, %rbp
movq %rcx, 0x30(%rsp)
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r12
movq 0x48(%rdi), %rdi
callq 0x2cc99
movq %rax, 0x38(%rsp)
movq 0x48(%r12), %rdi
callq 0x2cc99
movq %rax, 0x40(%rsp)
movq 0x48(%r12), %rdi
callq 0x2cc99
movq %rax, %r15
movq 0x48(%r12), %rdi
movq 0x18(%rdi), %r13
leaq 0x1(%r13), %rax
movq %rax, 0x18(%rdi)
leal 0x4(%rbx), %ecx
movl $0x12, %esi
movq %r14, %rdx
movq %rbp, %r8
callq 0x2bc8f
movq %rax, %rcx
movq $-0x1, %rax
movq %rbp, (%rsp)
cmpl $0x0, (%rbp)
jne 0x4dd41
movq %rcx, 0x18(%rsp)
movq %r13, 0x10(%rsp)
movq 0x40(%rsp), %rbp
movq %r15, 0x8(%rsp)
leal 0x8(%rbx), %ecx
movq 0x48(%r12), %rdi
movl $0x12, %esi
movq %r14, %rdx
movq (%rsp), %r15
movq %r15, %r8
callq 0x2bc8f
cmpl $0x0, (%r15)
jne 0x4dd3a
movq %rbx, %r13
movq %r14, 0x28(%rsp)
movq 0x48(%r12), %rdi
movl $0x7a, %esi
movq %rax, %rdx
movq %r15, %rcx
movq %rax, %rbx
callq 0x2be09
cmpl $0x0, (%r15)
jne 0x4dd3a
movq %rbx, 0x20(%rsp)
movq 0x48(%r12), %rdi
movl $0x1, %ecx
movl $0x2d, %esi
movq %rax, %rdx
movq %r15, %r8
callq 0x2bc8f
cmpl $0x0, (%r15)
jne 0x4dd3a
movq 0x48(%r12), %rdi
movl $0x43, %esi
movq %rax, %rdx
movq %rbp, %rcx
movq 0x38(%rsp), %r14
movq %r14, %r8
movq %r15, %r9
callq 0x2bd42
cmpl $0x0, (%r15)
jne 0x4dd3a
movq 0x48(%r12), %rdi
movq %r14, %rsi
movq (%rsp), %rbx
movq %rbx, %rdx
callq 0x2cca6
cmpl $0x0, (%rbx)
jne 0x4dd3a
movq %r12, %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rdx
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
movq 0x30(%rsp), %r9
movq (%rsp), %rbx
pushq %rbx
pushq $0x1
pushq %r9
pushq %r9
callq 0x4d8fa
addq $0x20, %rsp
cmpl $0x0, (%rbx)
jne 0x4dd3a
movq 0x48(%r12), %rdi
movl $0x1d, %esi
movq 0x10(%rsp), %r14
movq %r14, %rdx
movq %rax, %rcx
movq (%rsp), %rbx
movq %rbx, %r8
callq 0x2be2c
cmpl $0x0, (%rbx)
jne 0x4dd3a
movq 0x48(%r12), %rdi
movl $0x45, %esi
movq 0x8(%rsp), %rdx
movq (%rsp), %rbx
movq %rbx, %rcx
callq 0x2be1f
cmpl $0x0, (%rbx)
jne 0x4dd3a
movq 0x48(%r12), %rdi
movq %rbp, %rsi
movq (%rsp), %rbx
movq %rbx, %rdx
callq 0x2cca6
cmpl $0x0, (%rbx)
je 0x4dd50
movq $-0x1, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%r12), %rdi
movl $0x1d, %esi
movq %r14, %rdx
movq 0x18(%rsp), %rcx
movq %rbx, %r8
callq 0x2be2c
cmpl $0x0, (%rbx)
movq $-0x1, %rax
jne 0x4dd41
movq 0x48(%r12), %rdi
movq 0x8(%rsp), %rsi
movq (%rsp), %rdx
callq 0x2cca6
movq 0x10(%rsp), %rax
jmp 0x4dd41