// File: /workspace/github_c_makefile_star2_O2/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, 0x48(%rsp)
movq %rdx, %rbp
movq %rsi, %r12
movq %rdi, %r13
movq 0x90(%rsp), %rax
movq %rax, 0x18(%rsp)
movl %r9d, 0x38(%rsp)
testl %r9d, %r9d
je 0x49ed2
movq 0x48(%r13), %rdi
callq 0x2b774
movq %rax, 0x50(%rsp)
movq 0x48(%r13), %rdi
callq 0x2b774
jmp 0x49eda
pushq $-0x1
popq %rax
movq %rax, 0x50(%rsp)
movq %rax, 0x30(%rsp)
movq 0x48(%r13), %rdi
callq 0x2b774
movq %rax, %r14
movq 0x48(%r13), %rdi
callq 0x2b774
movq %rax, %rbx
movq %rbp, 0x20(%rsp)
movq %rbp, %rdi
movq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x341c4
movq %rax, %rbp
cmpl $0x0, (%r15)
jne 0x49fa6
movq %r14, 0x40(%rsp)
movq %r13, %rdi
movq %r12, %r14
movq %r12, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
movq %r15, %r8
callq 0x49d2b
cmpl $0x0, (%r15)
jne 0x49fa6
movq %rbx, 0x28(%rsp)
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x341c4
movq %rax, %rbp
cmpl $0x0, (%r15)
jne 0x49fa6
movq %r13, %rbx
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x34bfb
cmpl $0x0, (%r15)
movq %rax, 0x20(%rsp)
jne 0x49fa6
movq %rax, %r12
movq (%r14), %rdx
movq 0x148(%rax), %r8
movq %r15, (%rsp)
leaq 0x24bc9(%rip), %rsi     # 0x6eb58
pushq $0x2
popq %rcx
pushq $0x1
popq %r9
movq %rbx, %rdi
callq 0x4b101
cmpl $0x0, (%r15)
je 0x49fd7
xorl %ebp, %ebp
xorl %edi, %edi
callq 0x29d88
movq %rbp, %rdi
callq 0x29d88
movq 0x20(%rsp), %rdi
callq 0x29d88
movq 0x10(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x29d88
movq %rax, %rbx
movq %r12, %r14
cmpb $0x0, 0x38(%rsp)
je 0x4a0f8
movq 0x10(%rsp), %rax
cmpl $0x5, (%rax)
jne 0x4a0f8
movq %r13, %r12
movq 0x48(%r13), %rdi
movq 0x10(%rsp), %rax
movq 0x148(%rax), %rdx
pushq $0x2d
popq %rsi
pushq $0x1
popq %rcx
movq %r15, %r8
callq 0x2a819
cmpl $0x0, (%r15)
jne 0x49fa4
movq 0x48(%r12), %rdi
pushq $0x43
popq %rsi
movq %rax, %rdx
movq 0x50(%rsp), %rbp
movq %rbp, %rcx
movq 0x30(%rsp), %r8
movq %r15, %r9
callq 0x2a8c1
cmpl $0x0, (%r15)
jne 0x49fa4
movq 0x48(%r12), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x2b781
cmpl $0x0, (%r15)
jne 0x49fa4
movq 0x48(%r12), %rdi
xorl %ebp, %ebp
pushq $0x11
popq %rsi
movq 0x48(%rsp), %rdx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x2a819
cmpl $0x0, (%r15)
jne 0x49fa6
movq 0x20(%rsp), %rcx
movq 0x148(%rcx), %rdx
movq %r12, %rdi
movq %rbx, %r12
movq %rbx, %rsi
movq %rax, %rcx
movq %r15, %r8
callq 0x2ec82
cmpl $0x0, (%r15)
jne 0x49fa4
movq 0x48(%r13), %rdi
pushq $0x45
popq %rsi
movq 0x28(%rsp), %rdx
movq 0x18(%rsp), %rbx
movq %rbx, %rcx
callq 0x2a997
cmpl $0x0, (%rbx)
jne 0x49fa4
movq 0x48(%r13), %rdi
movq 0x30(%rsp), %rsi
movq 0x18(%rsp), %r15
movq %r15, %rdx
callq 0x2b781
cmpl $0x0, (%r15)
movq 0x20(%rsp), %r14
movq %r12, %rbx
jne 0x49fa4
movq %r13, %r12
movq 0x48(%r13), %rdi
movq 0x148(%r14), %rcx
xorl %ebp, %ebp
xorl %esi, %esi
movq %rbx, %rdx
movq %r15, %r8
callq 0x2a819
cmpl $0x0, (%r15)
jne 0x49fa6
movq %rbx, %r14
movq %rax, %rbx
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x3415f
movq %rax, 0x10(%rsp)
cmpl $0x0, (%r15)
jne 0x49fa4
movq 0x48(%r12), %rdi
movq 0x40(%rsp), %rsi
movq %r15, %rdx
callq 0x2b781
cmpl $0x0, (%r15)
jne 0x49fa4
movq 0x10(%rsp), %rax
movq 0x148(%rax), %rcx
movq %r12, %rdi
movq %r14, %rbp
movq %r14, %rsi
movq 0x48(%rsp), %rdx
movq %r15, %r8
callq 0x4abdd
cmpl $0x0, (%r15)
jne 0x49fa4
movq 0x48(%r12), %rdi
movq 0x10(%rsp), %rax
movq 0x148(%rax), %rcx
movq %rbp, %rdx
xorl %ebp, %ebp
xorl %esi, %esi
movq %rdx, 0x38(%rsp)
movq %r15, %r8
callq 0x2a819
cmpl $0x0, (%r15)
jne 0x49fa6
movq 0x48(%r12), %rdi
pushq $0x1d
popq %rsi
movq 0x38(%rsp), %rdx
movq %rax, %rcx
movq %r15, %r8
callq 0x2a9a4
cmpl $0x0, (%r15)
jne 0x49fa4
movq 0x48(%r13), %rdi
pushq $0x2b
popq %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq 0x18(%rsp), %rbx
movq %rbx, %r8
callq 0x2a819
cmpl $0x0, (%rbx)
jne 0x49fa4
movq 0x48(%r13), %rdi
pushq $0x44
popq %rsi
movq %rax, %rdx
movq 0x28(%rsp), %rcx
movq 0x40(%rsp), %r8
movq 0x18(%rsp), %rbx
movq %rbx, %r9
callq 0x2a8c1
cmpl $0x0, (%rbx)
jne 0x49fa4
movq 0x48(%r13), %rdi
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rbx
movq %rbx, %rdx
callq 0x2b781
cmpl $0x0, (%rbx)
jne 0x49fa4
jmp 0x49fb5
nop