// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq %rdi, %r12
movq 0x48(%rdi), %rdi
callq 0x2b774
movq %rax, 0x38(%rsp)
movq 0x48(%r12), %rdi
callq 0x2b774
movq %rax, 0x40(%rsp)
movq 0x48(%r12), %rdi
callq 0x2b774
movq %rax, %r14
movq 0x48(%r12), %rax
movq 0x18(%rax), %r13
leaq 0x1(%r13), %rcx
movq %rcx, 0x18(%rax)
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0x4a690
movq %rax, %rcx
pushq $-0x1
popq %rax
movq %rbp, (%rsp)
cmpl $0x0, (%rbp)
jne 0x4b0a0
movq %rcx, 0x18(%rsp)
movq %r13, 0x10(%rsp)
movq 0x40(%rsp), %rbp
movq %r14, 0x8(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq (%rsp), %r14
movq %r14, %rcx
callq 0x4a6a8
cmpl $0x0, (%r14)
jne 0x4b09d
movq %rbx, %r13
movq %r15, 0x28(%rsp)
movq 0x48(%r12), %rdi
pushq $0x7a
popq %rsi
movq %rax, %rdx
movq %r14, %rcx
movq %rax, %rbx
callq 0x2a981
cmpl $0x0, (%r14)
jne 0x4b09d
movq %rbx, 0x20(%rsp)
movq 0x48(%r12), %rdi
pushq $0x2d
popq %rsi
pushq $0x1
popq %rcx
movq %rax, %rdx
movq %r14, %r8
callq 0x2a819
cmpl $0x0, (%r14)
jne 0x4b09d
movq 0x48(%r12), %rdi
pushq $0x43
popq %rsi
movq %rax, %rdx
movq %rbp, %rcx
movq 0x38(%rsp), %r15
movq %r15, %r8
movq %r14, %r9
callq 0x2a8c1
cmpl $0x0, (%r14)
jne 0x4b09d
movq 0x48(%r12), %rdi
movq %r15, %rsi
movq (%rsp), %rbx
movq %rbx, %rdx
callq 0x2b781
cmpl $0x0, (%rbx)
jne 0x4b09d
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 0x4acdd
addq $0x20, %rsp
cmpl $0x0, (%rbx)
jne 0x4b09d
movq 0x48(%r12), %rdi
pushq $0x1d
popq %rsi
movq 0x10(%rsp), %r15
movq %r15, %rdx
movq %rax, %rcx
movq (%rsp), %rbx
movq %rbx, %r8
callq 0x2a9a4
cmpl $0x0, (%rbx)
jne 0x4b09d
movq 0x48(%r12), %rdi
pushq $0x45
popq %rsi
movq 0x8(%rsp), %rdx
movq (%rsp), %rbx
movq %rbx, %rcx
callq 0x2a997
cmpl $0x0, (%rbx)
jne 0x4b09d
movq 0x48(%r12), %rdi
movq %rbp, %rsi
movq (%rsp), %rbx
movq %rbx, %rdx
callq 0x2b781
cmpl $0x0, (%rbx)
je 0x4b0af
pushq $-0x1
popq %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%r12), %rdi
pushq $0x1d
popq %rsi
movq %r15, %rdx
movq 0x18(%rsp), %rcx
movq %rbx, %r8
callq 0x2a9a4
cmpl $0x0, (%rbx)
pushq $-0x1
popq %rax
jne 0x4b0a0
movq 0x48(%r12), %rdi
movq 0x8(%rsp), %rsi
movq (%rsp), %rdx
callq 0x2b781
movq 0x10(%rsp), %rax
jmp 0x4b0a0