// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/frontend/reference_type.c
// Function: subtilis_reference_type_init_ref
void subtilis_reference_type_init_ref(subtilis_parser_t *p, size_t dest_mem_reg,
				      size_t dest_loc, size_t source_reg,
				      bool check_size, bool ref,
				      subtilis_error_t *err)
{
	subtilis_ir_operand_t op0;
	subtilis_ir_operand_t op1;
	subtilis_ir_operand_t op2;
	subtilis_ir_operand_t copy;
	subtilis_ir_operand_t zero;
	subtilis_ir_operand_t gtzero;

	zero.label = SIZE_MAX;
	op0.reg = source_reg;

	op1.integer = SUBTIILIS_REFERENCE_SIZE_OFF;
	copy.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_LOADO_I32, op0, op1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	op1.reg = dest_mem_reg;
	op2.integer = dest_loc + SUBTIILIS_REFERENCE_SIZE_OFF;
	subtilis_ir_section_add_instr_reg(
	    p->current, SUBTILIS_OP_INSTR_STOREO_I32, copy, op1, op2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	if (check_size) {
		zero.label = subtilis_ir_section_new_label(p->current);
		gtzero.label = subtilis_ir_section_new_label(p->current);
		subtilis_ir_section_add_instr_reg(p->current,
						  SUBTILIS_OP_INSTR_JMPC, copy,
						  gtzero, zero, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;
		subtilis_ir_section_add_label(p->current, gtzero.label, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;
	}

	op1.integer = SUBTIILIS_REFERENCE_DATA_OFF;
	copy.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_LOADO_I32, op0, op1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	op1.reg = dest_mem_reg;
	op2.integer = dest_loc + SUBTIILIS_REFERENCE_DATA_OFF;
	subtilis_ir_section_add_instr_reg(
	    p->current, SUBTILIS_OP_INSTR_STOREO_I32, copy, op1, op2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	op1.integer = SUBTIILIS_REFERENCE_HEAP_OFF;
	copy.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_LOADO_I32, op0, op1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	if (ref) {
		subtilis_ir_section_add_instr_no_reg(
		    p->current, SUBTILIS_OP_INSTR_REF, copy, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;
	}

	op1.reg = dest_mem_reg;
	op2.integer = dest_loc + SUBTIILIS_REFERENCE_HEAP_OFF;
	subtilis_ir_section_add_instr_reg(
	    p->current, SUBTILIS_OP_INSTR_STOREO_I32, copy, op1, op2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	op1.integer = SUBTIILIS_REFERENCE_ORIG_SIZE_OFF;
	copy.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_LOADO_I32, op0, op1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	op1.reg = dest_mem_reg;
	op2.integer = dest_loc + SUBTIILIS_REFERENCE_ORIG_SIZE_OFF;
	subtilis_ir_section_add_instr_reg(
	    p->current, SUBTILIS_OP_INSTR_STOREO_I32, copy, op1, op2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	if (check_size)
		subtilis_ir_section_add_label(p->current, zero.label, err);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r9d, 0x10(%rsp)
movl %r8d, 0x4(%rsp)
movq %rcx, %r13
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
movq 0x50(%rsp), %rbx
movq 0x48(%rdi), %rdi
pushq $0x12
popq %rsi
movq %rcx, %rdx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x2a819
cmpl $0x0, (%rbx)
jne 0x4a2b2
movq %rax, %rbp
movq 0x48(%r14), %rdi
movl %r15d, %r8d
pushq $0x17
popq %rsi
movq %rax, %rdx
movq %r12, %rcx
movq %rbx, %r9
callq 0x2a8c1
cmpl $0x0, (%rbx)
je 0x4a2c1
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0x4(%rsp)
je 0x4a317
movq 0x48(%r14), %rdi
callq 0x2b774
movq %rax, 0x8(%rsp)
movq 0x48(%r14), %rdi
callq 0x2b774
movq 0x48(%r14), %rdi
pushq $0x43
popq %rsi
movq %rbp, %rdx
movq %rax, %rbp
movq %rax, %rcx
movq 0x8(%rsp), %r8
movq %rbx, %r9
callq 0x2a8c1
cmpl $0x0, (%rbx)
jne 0x4a2b2
movq 0x48(%r14), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x2b781
cmpl $0x0, (%rbx)
jne 0x4a2b2
jmp 0x4a31f
pushq $-0x1
popq %rax
movq %rax, 0x8(%rsp)
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %r12, %rcx
movq 0x48(%r14), %rdi
movq %rcx, %rbp
orq $0x4, %rcx
pushq $0x12
popq %rsi
movq %r13, %rdx
movq %rbx, %r8
callq 0x2a819
cmpl $0x0, (%rbx)
jne 0x4a2b2
leal 0x4(%r15), %r8d
movq 0x48(%r14), %rdi
pushq $0x17
popq %rsi
movq %rax, %rdx
movq %r12, %rcx
movq %rbx, %r9
callq 0x2a8c1
cmpl $0x0, (%rbx)
jne 0x4a2b2
movq 0x48(%r14), %rdi
movq %rbp, %rcx
orq $0x8, %rcx
pushq $0x12
popq %rsi
movq %r13, %rdx
movq %rbx, %r8
callq 0x2a819
cmpl $0x0, (%rbx)
jne 0x4a2b2
cmpb $0x0, 0x10(%rsp)
je 0x4a3be
movq 0x48(%r14), %rdi
pushq $0x78
popq %rsi
movq %rax, %rdx
movq %rbx, %rcx
movq %rax, 0x10(%rsp)
callq 0x2a997
movq 0x10(%rsp), %rax
cmpl $0x0, (%rbx)
jne 0x4a2b2
leal 0x8(%r15), %r8d
movq 0x48(%r14), %rdi
pushq $0x17
popq %rsi
movq %rax, %rdx
movq %r12, %rcx
movq %rbx, %r9
callq 0x2a8c1
cmpl $0x0, (%rbx)
jne 0x4a2b2
movq 0x48(%r14), %rdi
orq $0xc, %rbp
pushq $0x12
popq %rsi
movq %r13, %rdx
movq %rbp, %rcx
movq %rbx, %r8
callq 0x2a819
cmpl $0x0, (%rbx)
jne 0x4a2b2
addl $0xc, %r15d
movq 0x48(%r14), %rdi
pushq $0x17
popq %rsi
movq %rax, %rdx
movq %r12, %rcx
movq %r15, %r8
movq %rbx, %r9
callq 0x2a8c1
cmpl $0x0, (%rbx)
setne %al
movl 0x4(%rsp), %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0x4a2b2
movq 0x48(%r14), %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2b781