// File: /workspace/github_c_makefile_star2_O1/markdryan[P]subtilis/frontend/array_type.c
// Function: subtilis_array_append_scalar_array
void subtilis_array_append_scalar_array(subtilis_parser_t *p,
					subtilis_exp_t *a1, subtilis_exp_t *a2,
					subtilis_error_t *err)
{
	subtilis_type_t el_type;
	subtilis_ir_operand_t el_size;
	size_t dest_reg;
	subtilis_ir_operand_t a1_size;
	subtilis_ir_operand_t a2_size;
	subtilis_ir_operand_t a2_size_zero;
	subtilis_ir_operand_t a2_size_gt_zero;
	subtilis_ir_operand_t a2_data;
	subtilis_ir_operand_t op1;
	bool a2_dynamic;
	subtilis_exp_t *e;

	a2_size_zero.label = SIZE_MAX;
	el_type.type = SUBTILIS_TYPE_VOID;

	if (!subtilis_type_if_is_vector(&a1->type)) {
		subtilis_error_set_expected(err, "vector",
					    subtilis_type_name(&a1->type),
					    p->l->stream->name, p->l->line);
		goto cleanup;
	}

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

	prv_check_arrays_compat(p, &el_type, a1, a2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	el_size.integer = subtilis_type_if_size(&el_type, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

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

	a2_dynamic = subtilis_type_if_is_vector(&a2->type);
	if (a2_dynamic) {
		a2_size_zero.label = subtilis_ir_section_new_label(p->current);
		a2_size_gt_zero.label =
		    subtilis_ir_section_new_label(p->current);

		subtilis_ir_section_add_instr_reg(
		    p->current, SUBTILIS_OP_INSTR_JMPC, a2_size,
		    a2_size_gt_zero, a2_size_zero, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

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

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

	e = subtilis_builtin_ir_call_ref_grow(p, a1->exp.ir_op.reg, a1_size.reg,
					      a2_size.reg, a2_size.reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;
	dest_reg = e->exp.ir_op.reg;
	subtilis_exp_delete(e);

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

	subtilis_reference_type_memcpy_dest(p, dest_reg, a2_data.reg,
					    a2_size.reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_exp_delete(a2);
	a2 = subtilis_type_if_load_from_mem(p, &subtilis_type_integer,
					    a1->exp.ir_op.reg,
					    SUBTIILIS_ARRAY_DIMS_OFF, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	if (el_size.integer > 1) {
		op1.reg =
		    prv_div_by_const(p, a2_size.reg, el_size.integer, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
	} else {
		op1.reg = a2_size.reg;
	}

	a2->exp.ir_op.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_ADD_I32, a2->exp.ir_op, op1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_type_if_assign_to_mem(p, a1->exp.ir_op.reg,
				       SUBTIILIS_ARRAY_DIMS_OFF, a2, err);
	a2 = NULL;
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	if (a2_dynamic)
		subtilis_ir_section_add_label(p->current, a2_size_zero.label,
					      err);

cleanup:
	subtilis_type_free(&el_type);
	subtilis_exp_delete(a2);
	subtilis_exp_delete(a1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r12
movl $0x8, 0x20(%rsp)
movq %rsi, %rdi
callq 0x37815
testb %al, %al
je 0x452e5
leaq 0x20(%rsp), %rdx
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rcx
callq 0x3620e
cmpl $0x0, (%r15)
jne 0x45322
leaq 0x20(%rsp), %rsi
movq %r12, %rdi
movq %rbx, %rdx
movq %r14, %rcx
movq %r15, %r8
callq 0x4534e
cmpl $0x0, (%r15)
jne 0x45322
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x35ed8
cmpl $0x0, (%r15)
jne 0x45322
movq %rax, %r13
movq 0x148(%r14), %rsi
movq %r12, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x4d908
cmpl $0x0, (%r15)
jne 0x45322
movq %rax, 0x8(%rsp)
movq %r14, %rdi
callq 0x37815
movl %eax, %ebp
movq $-0x1, %rax
testb %bpl, %bpl
je 0x45183
movb %bpl, 0x7(%rsp)
movq 0x48(%r12), %rdi
callq 0x2d00a
movq %rax, %rbp
movq 0x48(%r12), %rdi
callq 0x2d00a
movq 0x48(%r12), %rdi
movl $0x43, %esi
movq 0x8(%rsp), %rdx
movq %rax, 0x10(%rsp)
movq %rax, %rcx
movq %rbp, 0x18(%rsp)
movq %rbp, %r8
movq %r15, %r9
callq 0x2c0c2
cmpl $0x0, (%r15)
jne 0x45322
movq 0x48(%r12), %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x2d017
cmpl $0x0, (%r15)
jne 0x45322
movb 0x7(%rsp), %bpl
movq 0x18(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x148(%rbx), %rsi
movq %r12, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x4d908
cmpl $0x0, (%r15)
jne 0x45322
movq 0x148(%rbx), %rsi
movq %r12, %rdi
movq %rax, %rdx
movq 0x8(%rsp), %rcx
movq %rcx, %r8
movq %r15, %r9
callq 0x33365
cmpl $0x0, (%r15)
jne 0x45322
movq 0x148(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq %rax, %rdi
callq 0x2b4c3
movq 0x148(%r14), %rsi
movq %r12, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x4d29c
cmpl $0x0, (%r15)
jne 0x45322
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq %rax, %rdx
movq 0x8(%rsp), %rcx
movq %r15, %r8
callq 0x4d830
cmpl $0x0, (%r15)
jne 0x45322
movq %r14, %rdi
callq 0x2b4c3
movq 0x148(%rbx), %rdx
leaq 0x2f6c1(%rip), %rsi     # 0x748f8
movl $0x10, %ecx
movq %r12, %rdi
movq %r15, %r8
callq 0x364c2
movq %rax, %r14
cmpl $0x0, (%r15)
jne 0x45322
cmpl $0x2, %r13d
jl 0x4527c
movq %r12, %rdi
movq 0x8(%rsp), %rsi
movl %r13d, %edx
movq %r15, %rcx
callq 0x4540c
movq %rax, 0x8(%rsp)
cmpl $0x0, (%r15)
jne 0x45322
movq 0x48(%r12), %rdi
movq 0x148(%r14), %rdx
xorl %esi, %esi
movq 0x8(%rsp), %rcx
movq %r15, %r8
callq 0x2c00f
movq %rax, 0x148(%r14)
cmpl $0x0, (%r15)
jne 0x45322
movq 0x148(%rbx), %rsi
movl $0x10, %edx
movq %r12, %rdi
movq %r14, %rcx
movq %r15, %r8
callq 0x36416
cmpl $0x0, (%r15)
setne %al
xorb $0x1, %bpl
orb %al, %bpl
jne 0x452e0
movq 0x48(%r12), %rdi
movq 0x18(%rsp), %rsi
movq %r15, %rdx
callq 0x2d017
xorl %r14d, %r14d
jmp 0x45322
movq %rbx, %rdi
callq 0x2f882
movq (%r12), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x306c2(%rip), %r10     # 0x759c2
leaq 0x306f2(%rip), %rdx     # 0x759f9
movq %r15, %rdi
movl $0x1d, %esi
movq %rax, %rcx
pushq $0xb62                  # imm = 0xB62
pushq %r10
callq 0x1442a
addq $0x10, %rsp
leaq 0x20(%rsp), %rdi
callq 0x2f843
movq %r14, %rdi
callq 0x2b4c3
movq %rbx, %rdi
callq 0x2b4c3
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq