// File: /workspace/github_c_makefile_star2_O1/markdryan[P]subtilis/frontend/collection.c
// Function: subtilis_collection_copy_scalar
void subtilis_collection_copy_scalar(subtilis_parser_t *p, subtilis_exp_t *obj1,
				     subtilis_exp_t *obj2, bool cow,
				     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 op1;
	subtilis_ir_operand_t final_size;
	size_t ptr1;
	size_t ptr2;
	size_t buf_size;

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

	if (obj2->type.type == SUBTILIS_TYPE_CONST_STRING) {
		buf_size = subtilis_buffer_get_size(&obj2->exp.str);
		if (buf_size < 2)
			goto cleanup;
		buf_size--;
		ptr2 = subtilis_string_type_lca_const(
		    p, subtilis_buffer_get_string(&obj2->exp.str), buf_size,
		    err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		op1.integer = buf_size;
		size2.reg = subtilis_ir_section_add_instr2(
		    p->current, SUBTILIS_OP_INSTR_MOVI_I32, op1, err);
	} else if (subtilis_type_if_is_scalar_ref(&obj2->type, err)) {
		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);
	} else if ((obj2->type.type == SUBTILIS_TYPE_REC) &&
		   subtilis_type_rec_is_scalar(&obj2->type)) {
		op1.integer = subtilis_type_rec_size(&obj2->type);
		size2.reg = subtilis_ir_section_add_instr2(
		    p->current, SUBTILIS_OP_INSTR_MOVI_I32, op1, err);
		ptr2 = obj2->exp.ir_op.reg;
	} else {
		if (err->type == SUBTILIS_ERROR_OK)
			subtilis_error_set_expected(
			    err,
			    "string, constant string or array of numeric types "
			    "or scalar RECS",
			    subtilis_type_name(&obj2->type), p->l->stream->name,
			    p->l->line);
		goto cleanup;
	}
	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;

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

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

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

cleanup:

	subtilis_exp_delete(obj2);
	subtilis_exp_delete(obj1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %r15
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r12
movq 0x48(%rdi), %rdi
callq 0x2d00a
movq %rax, %r13
movq 0x48(%r12), %rdi
callq 0x2d00a
movq %rax, 0x20(%rsp)
cmpl $0x3, (%r14)
jne 0x4ea5f
movl %ebp, 0x4(%rsp)
movq %r13, 0x8(%rsp)
leaq 0x148(%r14), %r13
movq %r13, %rdi
callq 0x14ba1
cmpq $0x2, %rax
jb 0x4ec2e
movq %rax, %rbp
decq %rbp
movq %r13, %rdi
callq 0x14dfb
movq %r12, %rdi
movq %rax, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x48267
cmpl $0x0, (%r15)
jne 0x4ec2e
movq %rax, %r13
movq 0x48(%r12), %rdi
movl %ebp, %edx
movl $0x1b, %esi
movq %r15, %rcx
callq 0x2c189
movq %rax, %rbp
jmp 0x4eafa
movq %r14, %rdi
movq %r15, %rsi
callq 0x37852
testb %al, %al
je 0x4eab1
movl %ebp, 0x4(%rsp)
movq %r13, 0x8(%rsp)
movq 0x148(%r14), %rsi
movq %r12, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x4d908
cmpl $0x0, (%r15)
jne 0x4ec2e
movq %rax, %rbp
movq 0x148(%r14), %rsi
movq %r12, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x4d29c
movq %rax, %r13
jmp 0x4eafa
cmpl $0x16, (%r14)
jne 0x4ebee
movq %r14, %rdi
callq 0x301d0
testb %al, %al
je 0x4ebee
movl %ebp, 0x4(%rsp)
movq %r13, 0x8(%rsp)
movq %r14, %rdi
callq 0x30197
movq 0x48(%r12), %rdi
movl %eax, %edx
movl $0x1b, %esi
movq %r15, %rcx
callq 0x2c189
movq %rax, %rbp
movq 0x148(%r14), %r13
cmpl $0x0, (%r15)
jne 0x4ec2e
movq 0x148(%rbx), %rsi
movq %r12, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x4d908
cmpl $0x0, (%r15)
jne 0x4ec2e
movq 0x48(%r12), %rdi
movl $0x37, %esi
movq %rax, %rdx
movq %rbp, %rcx
movq %rax, 0x18(%rsp)
movq %r15, %r8
callq 0x2c00f
cmpl $0x0, (%r15)
jne 0x4ec2e
movq 0x48(%r12), %rdi
movq 0x18(%rdi), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x18(%rdi)
subq $0x8, %rsp
movl $0x8a, %esi
movq %rdx, 0x18(%rsp)
movq %rax, %rcx
movq 0x20(%rsp), %r8
movq %rbp, %r9
pushq %r15
callq 0x2c224
addq $0x10, %rsp
cmpl $0x0, (%r15)
jne 0x4ec2e
movq 0x48(%r12), %rdi
movl $0x43, %esi
movq 0x10(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq 0x8(%rsp), %r8
movq %r15, %r9
callq 0x2c0c2
cmpl $0x0, (%r15)
jne 0x4ec2e
movq 0x48(%r12), %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
callq 0x2d017
movq 0x18(%rsp), %rcx
cmpl $0x0, (%r15)
jne 0x4ec2e
movq 0x10(%rsp), %rbp
movq 0x148(%rbx), %rsi
cmpb $0x0, 0x4(%rsp)
je 0x4ec4c
movq %r12, %rdi
xorl %edx, %edx
movq %r15, %r8
callq 0x4db93
jmp 0x4ec59
cmpl $0x0, (%r15)
jne 0x4ec2e
movq %r14, %rdi
callq 0x2f882
movq (%r12), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x26f02(%rip), %r10     # 0x75b11
leaq 0x26eba(%rip), %rdx     # 0x75ad0
movq %r15, %rdi
movl $0x1d, %esi
movq %rax, %rcx
pushq $0x4b
pushq %r10
callq 0x1442a
addq $0x10, %rsp
movq %r14, %rdi
callq 0x2b4c3
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2b4c3
movq %r12, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x4d29c
cmpl $0x0, (%r15)
jne 0x4ec2e
movq %rbp, %rcx
movq %r12, %rdi
movq %rax, %rsi
movq %r13, %rdx
movq %r15, %r8
callq 0x4d830
cmpl $0x0, (%r15)
jne 0x4ec2e
movq 0x48(%r12), %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rdx
callq 0x2d017
jmp 0x4ec2e