// File: /workspace/github_c_makefile_star2_O1/markdryan[P]subtilis/frontend/array_type.c
// Function: subtilis_array_append_scalar
void subtilis_array_append_scalar(subtilis_parser_t *p, subtilis_exp_t *a1,
				  subtilis_exp_t *a2, subtilis_exp_t *gran,
				  subtilis_error_t *err)
{
	subtilis_type_t el_type;
	size_t dest_reg;
	size_t gran_reg;
	subtilis_ir_operand_t a1_size;
	subtilis_ir_operand_t a2_size;
	subtilis_ir_operand_t op1;
	subtilis_exp_t *el_size_e;
	size_t el_size;

	if (!subtilis_type_if_is_vector(&a1->type)) {
		subtilis_error_set_expected(err, "dynamic 1d array",
					    subtilis_type_name(&a1->type),
					    p->l->stream->name, p->l->line);
		goto free_as;
	}

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

	if (a2->partial_name) {
		subtilis_parser_call_add_addr(p, &el_type, a2, err);
	} else {
		a2 = subtilis_type_if_coerce_type(p, a2, &el_type, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto free_as;
		a2 = subtilis_type_if_exp_to_var(p, a2, err);
	}
	if (err->type != SUBTILIS_ERROR_OK)
		goto free_as;

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

	op1.integer = el_size;
	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;

	a2_size.reg = subtilis_ir_section_add_instr2(
	    p->current, SUBTILIS_OP_INSTR_MOVI_I32, op1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	if (gran) {
		gran = prv_check_gran(p, gran, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto free_as;
		el_size_e = subtilis_exp_new_int32(el_size, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto free_as;
		gran = subtilis_type_if_mul(p, gran, el_size_e, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto free_as;
		gran = subtilis_type_if_exp_to_var(p, gran, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto free_as;
		gran_reg = gran->exp.ir_op.reg;
	} else {
		gran_reg = a2_size.reg;
	}

	dest_reg = prv_grow_wrapper(p, &el_type, a1->exp.ir_op.reg, a1_size.reg,
				    gran_reg, a2_size.reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_type_if_assign_to_new_mem(p, dest_reg, 0, a2, err);
	a2 = NULL;
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	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;

	op1.integer = 1;
	a2->exp.ir_op.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_ADDI_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;

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %r8, %r12
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r13
movq %rsi, %rdi
callq 0x37815
testb %al, %al
je 0x44a79
leaq 0x10(%rsp), %rdx
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x3620e
cmpl $0x0, (%r12)
jne 0x44ab6
cmpq $0x0, 0x160(%r15)
je 0x44ae0
leaq 0x10(%rsp), %rsi
movq %r13, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x1d2a6
cmpl $0x0, (%r12)
jne 0x44ab6
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x35ed8
cmpl $0x0, (%r12)
jne 0x44ab6
movq %rax, %rbp
movq 0x148(%rbx), %rsi
movq %r13, %rdi
xorl %edx, %edx
movq %r12, %rcx
callq 0x4d908
cmpl $0x0, (%r12)
jne 0x44a6d
movq %rax, 0x8(%rsp)
movq 0x48(%r13), %rdi
movl %ebp, %edx
movl $0x1b, %esi
movq %r12, %rcx
callq 0x2c189
cmpl $0x0, (%r12)
je 0x44b13
leaq 0x10(%rsp), %rdi
callq 0x2f843
jmp 0x44ab6
movq %rbx, %rdi
callq 0x2f882
movq (%r13), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x30f2e(%rip), %r10     # 0x759c2
leaq 0x30f4d(%rip), %rdx     # 0x759e8
movq %r12, %rdi
movl $0x1d, %esi
movq %rax, %rcx
pushq $0xab8                  # imm = 0xAB8
pushq %r10
callq 0x1442a
addq $0x10, %rsp
movq %r14, %rdi
callq 0x2b4c3
movq %r15, %rdi
callq 0x2b4c3
movq %rbx, %rdi
callq 0x2b4c3
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rsp), %rdx
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rcx
callq 0x379c3
movq %rax, %r15
cmpl $0x0, (%r12)
jne 0x44ab6
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x36269
movq %rax, %r15
jmp 0x44a09
testq %r14, %r14
je 0x44b91
movq %rax, (%rsp)
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x44c61
movq %rax, %r14
cmpl $0x0, (%r12)
jne 0x44ab6
movl %ebp, %edi
movq %r12, %rsi
callq 0x2bb29
cmpl $0x0, (%r12)
jne 0x44ab6
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0x36d24
movq %rax, %r14
cmpl $0x0, (%r12)
jne 0x44ab6
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x36269
movq %rax, %r14
cmpl $0x0, (%r12)
jne 0x44ab6
movq 0x148(%r14), %r8
movq (%rsp), %rax
jmp 0x44b97
xorl %r14d, %r14d
movq %rax, %r8
movq 0x148(%rbx), %rdx
subq $0x8, %rsp
leaq 0x18(%rsp), %rsi
movq %r13, %rdi
movq 0x10(%rsp), %rcx
movq %rax, %r9
pushq %r12
callq 0x44d95
addq $0x10, %rsp
cmpl $0x0, (%r12)
jne 0x44a6d
movq %r13, %rdi
movq %rax, %rsi
xorl %edx, %edx
movq %r15, %rcx
movq %r12, %r8
callq 0x3646c
cmpl $0x0, (%r12)
je 0x44bea
xorl %r15d, %r15d
jmp 0x44a6d
movq 0x148(%rbx), %rdx
leaq 0x2fd00(%rip), %rsi     # 0x748f8
movl $0x10, %ecx
movq %r13, %rdi
movq %r12, %r8
callq 0x364c2
movq %rax, %r15
cmpl $0x0, (%r12)
jne 0x44a6d
movq 0x48(%r13), %rdi
movq 0x148(%r15), %rdx
movl $0x1, %ecx
movl $0x9, %esi
movq %r12, %r8
callq 0x2c00f
movq %rax, 0x148(%r15)
cmpl $0x0, (%r12)
jne 0x44a6d
movq 0x148(%rbx), %rsi
movl $0x10, %edx
movq %r13, %rdi
movq %r15, %rcx
movq %r12, %r8
callq 0x36416
jmp 0x44be2