// File: /workspace/github_c_makefile_star2_O3/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 0x376d3
testb %al, %al
je 0x449d3
leaq 0x10(%rsp), %rdx
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x360be
cmpl $0x0, (%r12)
jne 0x44a10
cmpq $0x0, 0x160(%r15)
je 0x44a3a
leaq 0x10(%rsp), %rsi
movq %r13, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x1d3d0
cmpl $0x0, (%r12)
jne 0x44a10
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x35d88
cmpl $0x0, (%r12)
jne 0x44a10
movq %rax, %rbp
movq 0x148(%rbx), %rsi
movq %r13, %rdi
xorl %edx, %edx
movq %r12, %rcx
callq 0x4d8c4
cmpl $0x0, (%r12)
jne 0x449c7
movq %rax, 0x8(%rsp)
movq 0x48(%r13), %rdi
movl %ebp, %edx
movl $0x1b, %esi
movq %r12, %rcx
callq 0x2be09
cmpl $0x0, (%r12)
je 0x44a6d
leaq 0x10(%rsp), %rdi
callq 0x2f56e
jmp 0x44a10
movq %rbx, %rdi
callq 0x2f5ad
movq (%r13), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x31018(%rip), %r10     # 0x75a06
leaq 0x31037(%rip), %rdx     # 0x75a2c
movq %r12, %rdi
movl $0x1d, %esi
movq %rax, %rcx
pushq $0xab8                  # imm = 0xAB8
pushq %r10
callq 0x143da
addq $0x10, %rsp
movq %r14, %rdi
callq 0x2b166
movq %r15, %rdi
callq 0x2b166
movq %rbx, %rdi
callq 0x2b166
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 0x37860
movq %rax, %r15
cmpl $0x0, (%r12)
jne 0x44a10
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x36119
movq %rax, %r15
jmp 0x44963
testq %r14, %r14
je 0x44aeb
movq %rax, (%rsp)
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x44bbb
movq %rax, %r14
cmpl $0x0, (%r12)
jne 0x44a10
movl %ebp, %edi
movq %r12, %rsi
callq 0x2b7af
cmpl $0x0, (%r12)
jne 0x44a10
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0x36bc7
movq %rax, %r14
cmpl $0x0, (%r12)
jne 0x44a10
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x36119
movq %rax, %r14
cmpl $0x0, (%r12)
jne 0x44a10
movq 0x148(%r14), %r8
movq (%rsp), %rax
jmp 0x44af1
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 0x44ceb
addq $0x10, %rsp
cmpl $0x0, (%r12)
jne 0x449c7
movq %r13, %rdi
movq %rax, %rsi
xorl %edx, %edx
movq %r15, %rcx
movq %r12, %r8
callq 0x3631c
cmpl $0x0, (%r12)
je 0x44b44
xorl %r15d, %r15d
jmp 0x449c7
movq 0x148(%rbx), %rdx
leaq 0x2fdc6(%rip), %rsi     # 0x74918
movl $0x10, %ecx
movq %r13, %rdi
movq %r12, %r8
callq 0x36372
movq %rax, %r15
cmpl $0x0, (%r12)
jne 0x449c7
movq 0x48(%r13), %rdi
movq 0x148(%r15), %rdx
movl $0x1, %ecx
movl $0x9, %esi
movq %r12, %r8
callq 0x2bc8f
movq %rax, 0x148(%r15)
cmpl $0x0, (%r12)
jne 0x449c7
movq 0x148(%rbx), %rsi
movl $0x10, %edx
movq %r13, %rdi
movq %r15, %rcx
movq %r12, %r8
callq 0x362c6
jmp 0x44b3c