// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/frontend/collection.c
// Function: subtilis_collection_copy_ref
void subtilis_collection_copy_ref(subtilis_parser_t *p, subtilis_exp_t *obj1,
				  subtilis_exp_t *obj2, subtilis_error_t *err)
{
	subtilis_ir_operand_t size1;
	subtilis_ir_operand_t size2;
	subtilis_ir_operand_t skip_label;
	subtilis_ir_operand_t no_skip_label;
	subtilis_ir_operand_t condee;
	subtilis_ir_operand_t final_size;
	size_t ptr1;
	size_t ptr2;
	subtilis_type_t el_type;

	skip_label.label = subtilis_ir_section_new_label(p->current);
	no_skip_label.label = subtilis_ir_section_new_label(p->current);

	if (obj1->type.type != obj2->type.type) {
		subtilis_error_set_array_type_mismatch(err, p->l->stream->name,
						       p->l->line);
		goto free_objs;
	}

	subtilis_type_if_element_type(p, &obj1->type, &el_type, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	size2.reg =
	    subtilis_reference_type_get_size(p, obj2->exp.ir_op.reg, 0, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	ptr2 = subtilis_reference_get_data(p, obj2->exp.ir_op.reg, 0, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	size1.reg =
	    subtilis_reference_type_get_size(p, obj1->exp.ir_op.reg, 0, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	condee.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_LTE_I32, size1, size2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	final_size.reg = p->current->reg_counter++;
	subtilis_ir_section_add_instr4(p->current, SUBTILIS_OP_INSTR_CMOV_I32,
				       final_size, condee, size1, size2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_ir_section_add_instr_reg(p->current, SUBTILIS_OP_INSTR_JMPC,
					  final_size, no_skip_label, skip_label,
					  err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_ir_section_add_label(p->current, no_skip_label.label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	ptr1 = subtilis_reference_get_data(p, obj1->exp.ir_op.reg, 0, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_array_type_copy_els(p, &el_type, ptr1, final_size.reg, ptr2,
				     true, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

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

cleanup:
	subtilis_type_free(&el_type);

free_objs:
	subtilis_exp_delete(obj2);
	subtilis_exp_delete(obj1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r12
movq 0x48(%rdi), %rdi
callq 0x2b774
movq %rax, %r13
movq 0x48(%r12), %rdi
callq 0x2b774
movq %rax, %rbp
movl (%rbx), %eax
cmpl (%r14), %eax
jne 0x4c0a0
leaq 0x30(%rsp), %rdx
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rcx
callq 0x34108
cmpl $0x0, (%r15)
jne 0x4c094
movq 0x148(%r14), %rsi
movq %r12, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x4acab
movq %rax, 0x18(%rsp)
cmpl $0x0, (%r15)
jne 0x4c094
movq 0x148(%r14), %rsi
movq %r12, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x4a690
movq %rax, 0x28(%rsp)
cmpl $0x0, (%r15)
jne 0x4c094
movq 0x148(%rbx), %rsi
movq %r12, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x4acab
cmpl $0x0, (%r15)
jne 0x4c094
movq 0x48(%r12), %rdi
pushq $0x37
popq %rsi
movq %rax, %rdx
movq 0x18(%rsp), %rcx
movq %rax, 0x20(%rsp)
movq %r15, %r8
callq 0x2a819
movq 0x20(%rsp), %r8
cmpl $0x0, (%r15)
jne 0x4c094
movq 0x48(%r12), %rdi
movq 0x18(%rdi), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x18(%rdi)
movq %r15, (%rsp)
movl $0x8a, %esi
movq %rdx, 0x10(%rsp)
movq %rax, %rcx
movq 0x18(%rsp), %r9
callq 0x2aa15
cmpl $0x0, (%r15)
jne 0x4c094
movq 0x48(%r12), %rdi
pushq $0x43
popq %rsi
movq 0x10(%rsp), %rdx
movq %rbp, %rcx
movq %r13, %r8
movq %r15, %r9
callq 0x2a8c1
cmpl $0x0, (%r15)
jne 0x4c094
movq 0x48(%r12), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x2b781
cmpl $0x0, (%r15)
jne 0x4c094
movq 0x148(%rbx), %rsi
movq %r12, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x4a690
cmpl $0x0, (%r15)
jne 0x4c094
movq %r15, (%rsp)
leaq 0x30(%rsp), %rsi
pushq $0x1
popq %r9
movq %r12, %rdi
movq %rax, %rdx
movq 0x10(%rsp), %rcx
movq 0x28(%rsp), %r8
callq 0x41f9b
cmpl $0x0, (%r15)
jne 0x4c094
movq 0x48(%r12), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x2b781
leaq 0x30(%rsp), %rdi
callq 0x2dc7c
jmp 0x4c0c3
movq (%r12), %rax
movq 0x30(%rax), %rdx
movl 0x38(%rax), %ecx
leaq 0x23a2f(%rip), %r8      # 0x6fae1
pushq $0x41
popq %rsi
movq %r15, %rdi
movl $0x92, %r9d
callq 0x142c0
movq %r14, %rdi
callq 0x29d88
movq %rbx, %rdi
callq 0x29d88
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)