// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/frontend/rec_type.c
// Function: subtilis_type_rec_copy_ref
void subtilis_type_rec_copy_ref(subtilis_parser_t *p,
				const subtilis_type_t *type, size_t dest_reg,
				size_t loc, size_t src_reg, bool new_rec,
				subtilis_error_t *err)
{
	size_t i;
	uint32_t offset;
	subtilis_exp_t *e;
	subtilis_exp_t *e1;
	subtilis_ir_operand_t op1;
	subtilis_ir_operand_t op2;
	size_t ptr_reg;
	const subtilis_type_t *field_type;
	const subtilis_type_rec_t *rec = &type->params.rec;

	/*
	 * We only need to initialise fields that cannot be zero initialised.
	 */

	for (i = 0; i < rec->num_fields; i++) {
		offset = subtilis_type_rec_field_offset_id(rec, i);
		field_type = &rec->field_types[i];
		if (field_type->type == SUBTILIS_TYPE_REC) {
			op1.reg = src_reg;
			op2.integer = offset;
			ptr_reg = subtilis_ir_section_add_instr(
			    p->current, SUBTILIS_OP_INSTR_ADDI_I32, op1, op2,
			    err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;

			subtilis_rec_type_copy(p, field_type, dest_reg,
					       loc + offset, ptr_reg, new_rec,
					       err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;
		} else if (subtilis_type_if_is_reference(field_type)) {
			ptr_reg = subtilis_reference_get_pointer(p, src_reg,
								 offset, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;
			e = subtilis_exp_new_var(field_type, ptr_reg, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;
			if (new_rec) {
				if (subtilis_type_if_is_array(field_type) ||
				    subtilis_type_if_is_vector(field_type)) {
					subtilis_array_type_init_field(
					    p, field_type, dest_reg,
					    loc + offset, e, err);
				} else {
					subtilis_reference_type_init_ref(
					    p, dest_reg, loc + offset,
					    e->exp.ir_op.reg, true, true, err);
					subtilis_exp_delete(e);
				}
			} else {
				subtilis_type_if_assign_ref(
				    p, field_type, dest_reg, loc + offset, e,
				    err);
			}
			if (err->type != SUBTILIS_ERROR_OK)
				return;
		} else if (field_type->type == SUBTILIS_TYPE_FN ||
			   subtilis_type_if_is_numeric(field_type)) {
			e = subtilis_type_if_load_from_mem(
			    p, field_type, src_reg, offset, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;
			e1 = subtilis_exp_new_var(field_type, e->exp.ir_op.reg,
						  err);
			subtilis_exp_delete(e);
			if (err->type != SUBTILIS_ERROR_OK)
				return;
			subtilis_type_if_assign_to_mem(p, dest_reg,
						       loc + offset, e1, err);
		} else {
			subtilis_error_set_assertion_failed(err);
			return;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
movq 0x80(%rsp), %r13
movq %rsi, 0x38(%rsp)
leaq 0x8(%rsi), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x30(%rsp)
movl %r9d, 0x28(%rsp)
movzbl %r9b, %eax
movl %eax, 0x2c(%rsp)
xorl %r12d, %r12d
movq 0x38(%rsp), %rax
cmpq 0x28(%rax), %r12
jae 0x4373b
movq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x2e515
movq 0x38(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq 0x30(%rsp), %rdx
leaq (%rcx,%rdx), %r15
cmpl $0x16, (%rcx,%rdx)
movl %eax, %r14d
jne 0x435b8
movq 0x8(%rsp), %rax
movq 0x48(%rax), %rdi
pushq $0x9
popq %rsi
movq 0x20(%rsp), %rdx
movq %r14, %rcx
movq %r13, %r8
callq 0x2a819
cmpl $0x0, (%r13)
jne 0x4373b
addq 0x18(%rsp), %r14
movq %r13, (%rsp)
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rdx
movq %r14, %rcx
movq %rax, %r8
movl 0x2c(%rsp), %r9d
callq 0x4376f
cmpl $0x0, (%r13)
je 0x436cf
jmp 0x4373b
movq %r15, %rdi
callq 0x35808
testb %al, %al
je 0x4364f
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0x4ab28
cmpl $0x0, (%r13)
jne 0x4373b
movq %r15, %rdi
movl %eax, %esi
movq %r13, %rdx
callq 0x2a226
cmpl $0x0, (%r13)
jne 0x4373b
movq %rax, %rbx
cmpb $0x0, 0x28(%rsp)
je 0x436e0
movq %r15, %rdi
callq 0x357cc
testb %al, %al
jne 0x4362a
movq %r15, %rdi
callq 0x357ea
testb %al, %al
je 0x43705
addq 0x18(%rsp), %r14
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rdx
movq %r14, %rcx
movq %rbx, %r8
movq %r13, %r9
callq 0x4092c
jmp 0x435a8
cmpl $0x9, (%r15)
je 0x43665
movq %r15, %rdi
callq 0x35655
testb %al, %al
je 0x4374a
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq 0x20(%rsp), %rdx
movq %r14, %rcx
movq %r13, %r8
callq 0x343a4
cmpl $0x0, (%r13)
jne 0x4373b
movq %rax, %rbp
movl 0x148(%rax), %esi
movq %r15, %rdi
movq %r13, %rdx
callq 0x2a226
movq %rax, %rbx
movq %rbp, %rdi
callq 0x29d88
cmpl $0x0, (%r13)
jne 0x4373b
addq 0x18(%rsp), %r14
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq %r13, %r8
callq 0x34300
incq %r12
addq $0x148, 0x30(%rsp)      # imm = 0x148
jmp 0x43524
addq 0x18(%rsp), %r14
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rdx
movq %r14, %rcx
movq %rbx, %r8
movq %r13, %r9
callq 0x33f4e
jmp 0x435a8
addq 0x18(%rsp), %r14
movq 0x148(%rbx), %rcx
movq %r13, (%rsp)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
pushq $0x1
popq %r8
movl %r8d, %r9d
callq 0x4a250
movq %rbx, %rdi
callq 0x29d88
jmp 0x435a8
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2c2a6(%rip), %rdx     # 0x6f9f7
pushq $0xf
popq %rsi
movq %r13, %rdi
movl $0x122, %ecx            # imm = 0x122
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1429e