// File: /workspace/github_c_makefile_star2_O2/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 $0x188, %rsp            # imm = 0x188
movq %r8, %r12
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r13
movq %rsi, %rdi
callq 0x357ea
testb %al, %al
je 0x42250
leaq 0x40(%rsp), %rdx
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x34108
cmpl $0x0, (%r12)
jne 0x42289
cmpq $0x0, 0x160(%r15)
je 0x422b3
leaq 0x40(%rsp), %rsi
movq %r13, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x1cc3c
cmpl $0x0, (%r12)
jne 0x42289
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x33df0
cmpl $0x0, (%r12)
jne 0x42289
movq %rax, %rbp
movq 0x148(%rbx), %rsi
movq %r13, %rdi
xorl %edx, %edx
movq %r12, %rcx
callq 0x4acab
cmpl $0x0, (%r12)
jne 0x42244
movq %rax, 0x18(%rsp)
movq 0x48(%r13), %rdi
movl %ebp, %edx
pushq $0x1b
popq %rsi
movq %r12, %rcx
callq 0x2a981
cmpl $0x0, (%r12)
je 0x422e6
leaq 0x40(%rsp), %rdi
callq 0x2dc7c
jmp 0x42289
movq %rbx, %rdi
callq 0x2dcbb
movq (%r13), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x2d727(%rip), %r10     # 0x6f992
leaq 0x2d746(%rip), %rdx     # 0x6f9b8
pushq $0x1d
popq %rsi
movq %r12, %rdi
movq %rax, %rcx
pushq $0xab8                  # imm = 0xAB8
pushq %r10
callq 0x140cd
popq %rax
popq %rcx
movq %r14, %rdi
callq 0x29d88
movq %r15, %rdi
callq 0x29d88
movq %rbx, %rdi
callq 0x29d88
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x40(%rsp), %rdx
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rcx
callq 0x35977
movq %rax, %r15
cmpl $0x0, (%r12)
jne 0x42289
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3415f
movq %rax, %r15
jmp 0x421e2
testq %r14, %r14
je 0x4230d
movq %rax, 0x10(%rsp)
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x347d5
cmpl $0x0, (%r12)
je 0x4236a
xorl %r14d, %r14d
jmp 0x42289
xorl %r14d, %r14d
movq %rax, %r8
movq 0x148(%rbx), %rdx
subq $0x8, %rsp
leaq 0x48(%rsp), %rsi
movq %r13, %rdi
movq 0x20(%rsp), %rcx
movq %rax, %r9
pushq %r12
callq 0x42533
addq $0x10, %rsp
cmpl $0x0, (%r12)
jne 0x42244
movq %r13, %rdi
movq %rax, %rsi
xorl %edx, %edx
movq %r15, %rcx
movq %r12, %r8
callq 0x34352
cmpl $0x0, (%r12)
je 0x42469
xorl %r15d, %r15d
jmp 0x42244
movq %rax, %r14
cmpl $0x1, (%rax)
jne 0x423e2
movl 0x148(%r14), %ecx
testl %ecx, %ecx
jle 0x424db
movl %ebp, %edi
movq %r12, %rsi
callq 0x2a369
cmpl $0x0, (%r12)
jne 0x42289
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0x34bfb
movq %rax, %r14
cmpl $0x0, (%r12)
jne 0x42289
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x3415f
movq %rax, %r14
cmpl $0x0, (%r12)
jne 0x42289
movq 0x148(%r14), %r8
movq 0x10(%rsp), %rax
jmp 0x42313
movq 0x48(%r13), %rdi
movq 0x148(%r14), %rdx
pushq $0x35
popq %rsi
xorl %ecx, %ecx
movq %r12, %r8
callq 0x2a819
cmpl $0x0, (%r12)
jne 0x42305
movq %r13, %rdi
movq %rax, (%rsp)
callq 0x3feb4
movq %rax, 0x8(%rsp)
movq 0x48(%r13), %rdi
callq 0x2b774
movq 0x48(%r13), %rdi
pushq $0x43
popq %rsi
movq (%rsp), %rdx
movq %rax, (%rsp)
movq %rax, %rcx
movq 0x8(%rsp), %r8
movq %r12, %r9
callq 0x2a8c1
cmpl $0x0, (%r12)
jne 0x42305
movq 0x48(%r13), %rdi
movq (%rsp), %rsi
movq %r12, %rdx
callq 0x2b781
cmpl $0x0, (%r12)
jne 0x42305
jmp 0x42381
movq 0x148(%rbx), %rdx
leaq 0x2c451(%rip), %rsi     # 0x6e8c8
pushq $0x10
popq %rcx
movq %r13, %rdi
movq %r12, %r8
callq 0x343a4
movq %rax, %r15
cmpl $0x0, (%r12)
jne 0x42244
movq 0x48(%r13), %rdi
movq 0x148(%r15), %rdx
pushq $0x9
popq %rsi
pushq $0x1
popq %rcx
movq %r12, %r8
callq 0x2a819
movq %rax, 0x148(%r15)
cmpl $0x0, (%r12)
jne 0x42244
movq 0x148(%rbx), %rsi
pushq $0x10
popq %rdx
movq %r13, %rdi
movq %r15, %rcx
movq %r12, %r8
callq 0x34300
jmp 0x42362
leaq 0x2b157(%rip), %rdx     # 0x6d639
leaq 0x20(%rsp), %rdi
pushq $0x20
popq %rsi
xorl %eax, %eax
callq 0x12120
movq (%r13), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x2d48e(%rip), %rax     # 0x6f992
leaq 0x2d4c5(%rip), %rdx     # 0x6f9d0
pushq $0x1d
popq %rsi
movq %r12, %rdi
leaq 0x20(%rsp), %rcx
pushq $0xa8b                  # imm = 0xA8B
pushq %rax
callq 0x140cd
popq %rax
popq %rcx
cmpl $0x0, (%r12)
jne 0x42289
jmp 0x42381