// File: /workspace/github_c_makefile_star2_O3/markdryan[P]subtilis/frontend/string_type.c
// Function: prv_string_str_non_const_common
static void prv_string_str_non_const_common(subtilis_parser_t *p,
					    const subtilis_symbol_t *s,
					    subtilis_exp_t *count,
					    size_t str_data,
					    subtilis_exp_t *str_len,
					    bool check_for_one,
					    subtilis_error_t *err)
/* clang-format on */
{
	subtilis_ir_operand_t end;
	subtilis_ir_operand_t memcpy_loop;
	size_t dest_reg;
	size_t val_reg;
	subtilis_ir_operand_t op1;
	subtilis_ir_operand_t op2;
	subtilis_ir_operand_t end_ptr;
	subtilis_ir_operand_t eq_one_label;
	subtilis_ir_operand_t not_eq_one_label;
	subtilis_exp_t *dup = NULL;
	subtilis_exp_t *one = NULL;

	not_eq_one_label.label = SIZE_MAX;
	eq_one_label.label = SIZE_MAX;

	if (check_for_one) {
		eq_one_label.label = subtilis_ir_section_new_label(p->current);
		not_eq_one_label.label =
		    subtilis_ir_section_new_label(p->current);
	}
	memcpy_loop.label = subtilis_ir_section_new_label(p->current);
	end.label = subtilis_ir_section_new_label(p->current);

	dup = subtilis_type_if_dup(count, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	prv_string_str_check_count(p, s, dup, end, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	dup = subtilis_type_if_dup(str_len, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	count = subtilis_type_if_mul(p, count, dup, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;
	dup = NULL;

	dest_reg = subtilis_reference_type_alloc(
	    p, &subtilis_type_string, s->loc, SUBTILIS_IR_REG_LOCAL,
	    count->exp.ir_op.reg, true, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	if (check_for_one && str_len->type.type == SUBTILIS_TYPE_INTEGER) {
		op2.integer = 1;
		op1.reg = subtilis_ir_section_add_instr(
		    p->current, SUBTILIS_OP_INSTR_EQI_I32, str_len->exp.ir_op,
		    op2, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

		subtilis_ir_section_add_instr_reg(
		    p->current, SUBTILIS_OP_INSTR_JMPC, op1, eq_one_label,
		    not_eq_one_label, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

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

		op1.reg = str_data;
		op2.integer = 0;
		val_reg = subtilis_ir_section_add_instr(
		    p->current, SUBTILIS_OP_INSTR_LOADO_I8, op1, op2, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

		subtilis_builtin_memset_i8(p, dest_reg, count->exp.ir_op.reg,
					   val_reg, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

		subtilis_ir_section_add_instr_no_reg(
		    p->current, SUBTILIS_OP_INSTR_JMP, end, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

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

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

	str_len = subtilis_type_if_exp_to_var(p, str_len, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

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

	subtilis_reference_type_memcpy_dest(p, dest_reg, str_data,
					    str_len->exp.ir_op.reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

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

	subtilis_ir_section_add_instr_no_reg2(p->current, SUBTILIS_OP_INSTR_MOV,
					      op1, op2, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	op1.integer = dest_reg;
	op2.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_EQ_I32, op1, end_ptr, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_ir_section_add_instr_reg(p->current, SUBTILIS_OP_INSTR_JMPC_NF,
					  op2, end, memcpy_loop, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

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

	subtilis_exp_delete(count);
	subtilis_exp_delete(str_len);

	return;

cleanup:

	subtilis_exp_delete(one);
	subtilis_exp_delete(dup);
	subtilis_exp_delete(count);
	subtilis_exp_delete(str_len);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, 0x10(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, %rbp
movq %rsi, %r12
movq %rdi, %r14
movq $-0x1, %rbx
movq 0x90(%rsp), %rax
movq %rax, 0x18(%rsp)
movq $-0x1, %rax
movl %r9d, 0x34(%rsp)
testl %r9d, %r9d
je 0x4ca55
movq 0x48(%r14), %rdi
callq 0x2cc99
movq %rax, %rbx
movq 0x48(%r14), %rdi
callq 0x2cc99
movq %rbx, 0x50(%rsp)
movq %rax, 0x48(%rsp)
movq 0x48(%r14), %rdi
callq 0x2cc99
movq %rax, %rbx
movq 0x48(%r14), %rdi
callq 0x2cc99
movq %rax, %r13
movq %rbp, 0x20(%rsp)
movq %rbp, %rdi
movq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x36182
movq %rax, %rbp
cmpl $0x0, (%r15)
jne 0x4cb28
movq %rbx, 0x38(%rsp)
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r13, %rcx
movq %r15, %r8
callq 0x4c890
cmpl $0x0, (%r15)
jne 0x4cb28
movq %r13, 0x28(%rsp)
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x36182
movq %rax, %rbp
cmpl $0x0, (%r15)
jne 0x4cb28
movq %r14, %rbx
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x36bc7
cmpl $0x0, (%r15)
movq %rax, 0x20(%rsp)
jne 0x4cb28
movq (%r12), %rdx
movq 0x148(%rax), %r8
movq %r15, (%rsp)
leaq 0x2809e(%rip), %rsi     # 0x74ba8
movl $0x2, %ecx
movq %rbx, %rdi
movl $0x1, %r9d
movq %rax, %rbx
callq 0x4ddaa
cmpl $0x0, (%r15)
je 0x4cb59
xorl %ebp, %ebp
xorl %edi, %edi
callq 0x2b166
movq %rbp, %rdi
callq 0x2b166
movq 0x20(%rsp), %rdi
callq 0x2b166
movq 0x10(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2b166
movq %rax, %r13
cmpb $0x0, 0x34(%rsp)
je 0x4cc7b
movq 0x10(%rsp), %rax
cmpl $0x5, (%rax)
jne 0x4cc7b
movq %r14, %r12
movq 0x48(%r14), %rdi
movq 0x10(%rsp), %rax
movq 0x148(%rax), %rdx
movl $0x1, %ecx
movl $0x2d, %esi
movq %r15, %r8
callq 0x2bc8f
cmpl $0x0, (%r15)
jne 0x4cb26
movq 0x48(%r12), %rdi
movl $0x43, %esi
movq %rax, %rdx
movq 0x50(%rsp), %rbp
movq %rbp, %rcx
movq 0x48(%rsp), %r8
movq %r15, %r9
callq 0x2bd42
cmpl $0x0, (%r15)
jne 0x4cb26
movq 0x48(%r12), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x2cca6
cmpl $0x0, (%r15)
jne 0x4cb26
movq 0x48(%r12), %rdi
xorl %ebp, %ebp
movl $0x11, %esi
movq 0x40(%rsp), %rdx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x2bc8f
cmpl $0x0, (%r15)
jne 0x4cb28
movq 0x20(%rsp), %rcx
movq 0x148(%rcx), %rdx
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rcx
movq %r15, %r8
callq 0x30791
cmpl $0x0, (%r15)
jne 0x4cb26
movq 0x48(%r14), %rdi
movl $0x45, %esi
movq 0x28(%rsp), %rdx
movq 0x18(%rsp), %rbx
movq %rbx, %rcx
callq 0x2be1f
cmpl $0x0, (%rbx)
jne 0x4cb26
movq 0x48(%r14), %rdi
movq 0x48(%rsp), %rsi
movq 0x18(%rsp), %r15
movq %r15, %rdx
callq 0x2cca6
cmpl $0x0, (%r15)
movq 0x20(%rsp), %rbx
jne 0x4cb26
movq %r14, %r12
movq 0x48(%r14), %rdi
movq 0x148(%rbx), %rcx
xorl %ebp, %ebp
xorl %esi, %esi
movq %r13, %rdx
movq %r15, %r8
callq 0x2bc8f
cmpl $0x0, (%r15)
jne 0x4cb28
movq %rax, %rbx
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x36119
movq %rax, 0x10(%rsp)
cmpl $0x0, (%r15)
jne 0x4cb26
movq 0x48(%r12), %rdi
movq 0x38(%rsp), %rsi
movq %r15, %rdx
callq 0x2cca6
cmpl $0x0, (%r15)
jne 0x4cb26
movq 0x10(%rsp), %rax
movq 0x148(%rax), %rcx
movq %r12, %rdi
movq %r13, %rsi
movq 0x40(%rsp), %rdx
movq %r15, %r8
callq 0x4d7ec
cmpl $0x0, (%r15)
jne 0x4cb26
movq 0x48(%r12), %rdi
movq 0x10(%rsp), %rax
movq 0x148(%rax), %rcx
xorl %ebp, %ebp
xorl %esi, %esi
movq %r13, %rdx
movq %r15, %r8
callq 0x2bc8f
cmpl $0x0, (%r15)
jne 0x4cb28
movq 0x48(%r12), %rdi
movl $0x1d, %esi
movq %r13, %rdx
movq %rax, %rcx
movq %r15, %r8
callq 0x2be2c
cmpl $0x0, (%r15)
jne 0x4cb26
movq 0x48(%r14), %rdi
movl $0x2b, %esi
movq %r13, %rdx
movq %rbx, %rcx
movq 0x18(%rsp), %rbx
movq %rbx, %r8
callq 0x2bc8f
cmpl $0x0, (%rbx)
jne 0x4cb26
movq 0x48(%r14), %rdi
movl $0x44, %esi
movq %rax, %rdx
movq 0x28(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x18(%rsp), %rbx
movq %rbx, %r9
callq 0x2bd42
cmpl $0x0, (%rbx)
jne 0x4cb26
movq 0x48(%r14), %rdi
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rbx
movq %rbx, %rdx
callq 0x2cca6
cmpl $0x0, (%rbx)
jne 0x4cb26
jmp 0x4cb37
nop