// File: /workspace/github_c_makefile_star2_O1/markdryan[P]subtilis/frontend/string_type.c
// Function: subtilis_string_type_mid
void subtilis_string_type_mid(subtilis_parser_t *p, subtilis_exp_t *str,
			      subtilis_exp_t *start, subtilis_exp_t *len,
			      subtilis_exp_t *value, subtilis_error_t *err)
{
	size_t data_reg;
	subtilis_ir_operand_t end_label;
	subtilis_ir_operand_t start_gt0_label;
	subtilis_ir_operand_t start_lt_str_len_label;
	subtilis_ir_operand_t strlen_gt0_label;
	subtilis_ir_operand_t to_copy;
	subtilis_ir_operand_t op1;
	subtilis_ir_operand_t condee;
	size_t str_len_reg;
	size_t orig_len_reg;
	size_t value_len_reg;

	end_label.label = subtilis_ir_section_new_label(p->current);
	start_gt0_label.label = subtilis_ir_section_new_label(p->current);
	start_lt_str_len_label.label =
	    subtilis_ir_section_new_label(p->current);
	strlen_gt0_label.label = subtilis_ir_section_new_label(p->current);

	len = prv_left_right_check_args(p, str, len, value, err);
	if (err->type != SUBTILIS_ERROR_OK || !len)
		goto cleanup;

	start = subtilis_type_if_to_int(p, start, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	if (start->type.type == SUBTILIS_TYPE_CONST_INTEGER) {
		if (start->exp.ir_op.integer < 0)
			goto cleanup;
	} else {
		op1.integer = 0;
		condee.reg = subtilis_ir_section_add_instr(
		    p->current, SUBTILIS_OP_INSTR_GTEI_I32, start->exp.ir_op,
		    op1, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		subtilis_ir_section_add_instr_reg(
		    p->current, SUBTILIS_OP_INSTR_JMPC, condee, start_gt0_label,
		    end_label, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

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

	op1.integer = SUBTIILIS_STRING_SIZE_OFF;
	orig_len_reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_LOADO_I32, str->exp.ir_op, op1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	op1.reg = orig_len_reg;
	condee.reg = subtilis_ir_section_add_instr(
	    p->current, (start->type.type == SUBTILIS_TYPE_CONST_INTEGER)
			    ? SUBTILIS_OP_INSTR_GTI_I32
			    : SUBTILIS_OP_INSTR_GT_I32,
	    op1, start->exp.ir_op, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_ir_section_add_instr_reg(p->current, SUBTILIS_OP_INSTR_JMPC,
					  condee, start_lt_str_len_label,
					  end_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

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

	op1.reg = orig_len_reg;
	str_len_reg = subtilis_ir_section_add_instr(
	    p->current, (start->type.type == SUBTILIS_TYPE_CONST_INTEGER)
			    ? SUBTILIS_OP_INSTR_SUBI_I32
			    : SUBTILIS_OP_INSTR_SUB_I32,
	    op1, start->exp.ir_op, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	to_copy.reg = prv_left_right_calc_len(p, len, value, str_len_reg,
					      &value_len_reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	/*
	 * If to_copy is 0 there's nothing to do.
	 */

	subtilis_ir_section_add_instr_reg(p->current, SUBTILIS_OP_INSTR_JMPC,
					  to_copy, strlen_gt0_label, end_label,
					  err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

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

	data_reg = subtilis_reference_type_copy_on_write(p, str->exp.ir_op.reg,
							 0, orig_len_reg, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	op1.reg = data_reg;
	data_reg = subtilis_ir_section_add_instr(
	    p->current, (start->type.type == SUBTILIS_TYPE_CONST_INTEGER)
			    ? SUBTILIS_OP_INSTR_ADDI_I32
			    : SUBTILIS_OP_INSTR_ADD_I32,
	    op1, start->exp.ir_op, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	prv_left_right_copy(p, data_reg, str_len_reg, value_len_reg, to_copy,
			    value, true, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_ir_section_add_label(p->current, end_label.label, err);

cleanup:

	subtilis_exp_delete(str);
	subtilis_exp_delete(start);
	subtilis_exp_delete(len);
	subtilis_exp_delete(value);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %r13
movq %r8, %r15
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %rbp
movq 0x48(%rdi), %rdi
callq 0x2d00a
movq %rax, 0x20(%rsp)
movq 0x48(%rbp), %rdi
callq 0x2d00a
movq %rax, 0x10(%rsp)
movq 0x48(%rbp), %rdi
callq 0x2d00a
movq %rax, 0x18(%rsp)
movq 0x48(%rbp), %rdi
callq 0x2d00a
movq %rax, 0x38(%rsp)
movq %rbp, %rdi
movq %r14, 0x8(%rsp)
movq %r14, %rsi
movq %r12, %rdx
movq %r15, 0x28(%rsp)
movq %r15, %rcx
movq %r13, %r8
callq 0x4b02f
movq %rax, %r12
cmpl $0x0, (%r13)
sete %al
testq %r12, %r12
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x4ad89
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x3691f
movq %rax, %r14
cmpl $0x0, (%r13)
je 0x4adc1
movq 0x8(%rsp), %r15
jmp 0x4ad91
movq 0x8(%rsp), %r15
movq %rbx, %r14
movq %r15, %rdi
callq 0x2b4c3
movq %r14, %rdi
callq 0x2b4c3
movq %r12, %rdi
callq 0x2b4c3
movq 0x28(%rsp), %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2b4c3
cmpl $0x1, (%r14)
movq 0x8(%rsp), %r15
jne 0x4afbd
cmpl $0x0, 0x148(%r14)
movq 0x20(%rsp), %rbx
js 0x4ad91
movq 0x48(%rbp), %rdi
movq 0x148(%r15), %rdx
movl $0x12, %esi
xorl %ecx, %ecx
movq %r13, %r8
callq 0x2c00f
cmpl $0x0, (%r13)
jne 0x4ad91
xorl %ecx, %ecx
cmpl $0x1, (%r14)
movq 0x48(%rbp), %rdi
sete %cl
leal 0x33(,%rcx,2), %esi
movq 0x148(%r14), %rcx
movq %rax, %rdx
movq %rax, 0x10(%rsp)
movq %r13, %r8
callq 0x2c00f
cmpl $0x0, (%r13)
jne 0x4ad91
movq 0x48(%rbp), %rdi
movl $0x43, %esi
movq %rax, %rdx
movq 0x18(%rsp), %rcx
movq %rbx, %r8
movq %r13, %r9
callq 0x2c0c2
cmpl $0x0, (%r13)
jne 0x4ad91
movq 0x48(%rbp), %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
callq 0x2d017
cmpl $0x0, (%r13)
jne 0x4ad91
xorl %eax, %eax
cmpl $0x1, (%r14)
movq 0x48(%rbp), %rdi
sete %al
leal (%rax,%rax,8), %esi
addl $0x2, %esi
movq 0x148(%r14), %rcx
movq 0x10(%rsp), %rdx
movq %r13, %r8
callq 0x2c00f
cmpl $0x0, (%r13)
jne 0x4ad91
movq %rax, %rbx
leaq 0x40(%rsp), %r8
movq %rbp, %rdi
movq %r12, %rsi
movq 0x28(%rsp), %rdx
movq %rax, %rcx
movq %r13, %r9
callq 0x4b0e2
cmpl $0x0, (%r13)
jne 0x4ad91
movq %rbx, 0x18(%rsp)
movq 0x48(%rbp), %rdi
movl $0x43, %esi
movq %rax, 0x30(%rsp)
movq %rax, %rdx
movq 0x38(%rsp), %rbx
movq %rbx, %rcx
movq 0x20(%rsp), %r8
movq %r13, %r9
callq 0x2c0c2
cmpl $0x0, (%r13)
jne 0x4ad91
movq 0x48(%rbp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x2d017
cmpl $0x0, (%r13)
jne 0x4ad91
movq 0x148(%r15), %rsi
movq %rbp, %rdi
xorl %edx, %edx
movq 0x10(%rsp), %rcx
movq %r13, %r8
callq 0x4db93
cmpl $0x0, (%r13)
jne 0x4ad82
xorl %ecx, %ecx
cmpl $0x1, (%r14)
movq 0x48(%rbp), %rdi
sete %cl
leal (%rcx,%rcx,8), %esi
movq 0x148(%r14), %rcx
movq %rax, %rdx
movq %r13, %r8
callq 0x2c00f
cmpl $0x0, (%r13)
jne 0x4ad82
movq 0x40(%rsp), %rcx
movq %rbp, %rdi
movq %rax, %rsi
movq 0x18(%rsp), %rdx
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %r9
pushq %r13
pushq $0x1
callq 0x4b2e4
addq $0x10, %rsp
cmpl $0x0, (%r13)
jne 0x4ad82
movq 0x48(%rbp), %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
callq 0x2d017
jmp 0x4ad82
movq 0x48(%rbp), %rdi
movq 0x148(%r14), %rdx
movl $0x41, %esi
xorl %ecx, %ecx
movq %r13, %r8
callq 0x2c00f
cmpl $0x0, (%r13)
movq 0x20(%rsp), %rbx
jne 0x4ad91
movq 0x48(%rbp), %rdi
movl $0x43, %esi
movq %rax, %rdx
movq 0x10(%rsp), %rcx
movq %rbx, %r8
movq %r13, %r9
callq 0x2c0c2
cmpl $0x0, (%r13)
jne 0x4ad91
movq 0x48(%rbp), %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x2d017
cmpl $0x0, (%r13)
jne 0x4ad91
jmp 0x4addf