// File: /workspace/github_c_makefile_star2_O3/markdryan[P]subtilis/frontend/string_type.c
// Function: prv_left_right_calc_len
static size_t prv_left_right_calc_len(subtilis_parser_t *p, subtilis_exp_t *len,
				      subtilis_exp_t *value, size_t str_len_reg,
				      size_t *value_len_reg,
				      subtilis_error_t *err)
{
	subtilis_ir_operand_t op0;
	subtilis_ir_operand_t op1;
	subtilis_ir_operand_t len_op;
	subtilis_ir_operand_t str_len;
	subtilis_ir_operand_t gt_len;
	subtilis_ir_operand_t less_zero;
	subtilis_ir_operand_t condee;
	subtilis_ir_operand_t value_len;
	size_t to_comp_reg;
	size_t to_copy_reg = p->current->reg_counter++;

	str_len.reg = str_len_reg;
	if ((len->type.type == SUBTILIS_TYPE_CONST_INTEGER) &&
	    (len->exp.ir_op.integer < 0)) {
		to_comp_reg = str_len.reg;
	} else {
		to_comp_reg = p->current->reg_counter++;
		if (len->type.type == SUBTILIS_TYPE_INTEGER) {
			op1.integer = 0;
			less_zero.reg = subtilis_ir_section_add_instr(
			    p->current, SUBTILIS_OP_INSTR_LTI_I32,
			    len->exp.ir_op, op1, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return SIZE_MAX;

			gt_len.reg = subtilis_ir_section_add_instr(
			    p->current, SUBTILIS_OP_INSTR_GTE_I32,
			    len->exp.ir_op, str_len, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return SIZE_MAX;

			condee.reg = subtilis_ir_section_add_instr(
			    p->current, SUBTILIS_OP_INSTR_OR_I32, less_zero,
			    gt_len, err);
			len_op = len->exp.ir_op;
		} else {
			len_op.reg = subtilis_ir_section_add_instr2(
			    p->current, SUBTILIS_OP_INSTR_MOVI_I32,
			    len->exp.ir_op, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return SIZE_MAX;
			condee.reg = subtilis_ir_section_add_instr(
			    p->current, SUBTILIS_OP_INSTR_LT_I32, str_len,
			    len_op, err);
		}
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;

		op0.reg = to_comp_reg;
		subtilis_ir_section_add_instr4(p->current,
					       SUBTILIS_OP_INSTR_CMOV_I32, op0,
					       condee, str_len, len_op, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return SIZE_MAX;
	}

	if (value->type.type == SUBTILIS_TYPE_CONST_STRING) {
		op1.integer = subtilis_buffer_get_size(&value->exp.str) - 1;
		value_len.reg = subtilis_ir_section_add_instr2(
		    p->current, SUBTILIS_OP_INSTR_MOVI_I32, op1, err);
	} else {
		op1.integer = SUBTIILIS_STRING_SIZE_OFF;
		value_len.reg = subtilis_ir_section_add_instr(
		    p->current, SUBTILIS_OP_INSTR_LOADO_I32, value->exp.ir_op,
		    op1, err);
	}
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	op1.reg = to_comp_reg;
	condee.reg = subtilis_ir_section_add_instr(
	    p->current, SUBTILIS_OP_INSTR_GT_I32, value_len, op1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	op0.reg = to_copy_reg;
	op1.reg = to_comp_reg;

	subtilis_ir_section_add_instr4(p->current, SUBTILIS_OP_INSTR_CMOV_I32,
				       op0, condee, op1, value_len, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return SIZE_MAX;

	*value_len_reg = value_len.reg;

	return to_copy_reg;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r14
movq %r8, 0x18(%rsp)
movq %rcx, %r12
movq %rsi, %rbx
movq %rdi, %r13
movq 0x48(%rdi), %rdi
movq 0x18(%rdi), %rbp
leaq 0x1(%rbp), %rcx
movq %rcx, 0x18(%rdi)
movl (%rsi), %eax
cmpl $0x1, %eax
movq %rdx, 0x20(%rsp)
jne 0x4b0d5
cmpl $0x0, 0x148(%rbx)
js 0x4b1d6
movq %rcx, 0x8(%rsp)
leaq 0x2(%rbp), %rax
movq %rax, 0x18(%rdi)
jmp 0x4b15a
movq %rcx, 0x8(%rsp)
leaq 0x2(%rbp), %rcx
movq %rcx, 0x18(%rdi)
cmpl $0x5, %eax
jne 0x4b15a
movq 0x148(%rbx), %rdx
movl $0x3d, %esi
xorl %ecx, %ecx
movq %r14, %r8
callq 0x2bc8f
movq $-0x1, %r15
cmpl $0x0, (%r14)
jne 0x4b284
movq %rax, 0x10(%rsp)
movq 0x48(%r13), %rdi
movq 0x148(%rbx), %rdx
movl $0x3f, %esi
movq %r12, %rcx
movq %r14, %r8
callq 0x2bc8f
cmpl $0x0, (%r14)
jne 0x4b284
movq 0x48(%r13), %rdi
movl $0x26, %esi
movq 0x10(%rsp), %rdx
movq %rax, %rcx
movq %r14, %r8
callq 0x2bc8f
movq 0x148(%rbx), %rbx
jmp 0x4b199
movq 0x148(%rbx), %rdx
movl $0x1b, %esi
movq %r14, %rcx
callq 0x2be09
movq $-0x1, %r15
cmpl $0x0, (%r14)
jne 0x4b284
movq %rax, %rbx
movq 0x48(%r13), %rdi
movl $0x3b, %esi
movq %r12, %rdx
movq %rax, %rcx
movq %r14, %r8
callq 0x2bc8f
movq $-0x1, %r15
cmpl $0x0, (%r14)
jne 0x4b284
movq 0x48(%r13), %rdi
movq %r14, (%rsp)
movl $0x8a, %esi
movq 0x8(%rsp), %rdx
movq %rax, %rcx
movq %r12, %r8
movq %rbx, %r9
callq 0x2bea4
cmpl $0x0, (%r14)
jne 0x4b284
jmp 0x4b1db
movq %r12, 0x8(%rsp)
movq 0x20(%rsp), %rax
cmpl $0x3, (%rax)
jne 0x4b209
addq $0x148, %rax            # imm = 0x148
movq %rax, %rdi
callq 0x14b57
leal -0x1(%rax), %edx
movq 0x48(%r13), %rdi
movl $0x1b, %esi
movq %r14, %rcx
callq 0x2be09
jmp 0x4b223
movq 0x48(%r13), %rdi
movq 0x148(%rax), %rdx
movl $0x12, %esi
xorl %ecx, %ecx
movq %r14, %r8
callq 0x2bc8f
movq %rax, %rbx
movq $-0x1, %r15
cmpl $0x0, (%r14)
jne 0x4b284
movq 0x48(%r13), %rdi
movl $0x33, %esi
movq %rbx, %rdx
movq 0x8(%rsp), %r12
movq %r12, %rcx
movq %r14, %r8
callq 0x2bc8f
cmpl $0x0, (%r14)
jne 0x4b284
movq 0x48(%r13), %rdi
movq %r14, (%rsp)
movl $0x8a, %esi
movq %rbp, %rdx
movq %rax, %rcx
movq %r12, %r8
movq %rbx, %r9
callq 0x2bea4
cmpl $0x0, (%r14)
jne 0x4b284
movq 0x18(%rsp), %rax
movq %rbx, (%rax)
movq %rbp, %r15
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq