// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq %r8, 0x20(%rsp)
movq %rcx, %r13
movq %rsi, %r14
movq %rdi, %rbp
movq 0x48(%rdi), %rdi
movq 0x18(%rdi), %r12
leaq 0x1(%r12), %rbx
movq %rbx, 0x18(%rdi)
movl (%rsi), %eax
cmpl $0x1, %eax
jne 0x48677
cmpl $0x0, 0x148(%r14)
js 0x48759
movq %rdx, 0x10(%rsp)
leaq 0x2(%r12), %rax
movq %rax, 0x18(%rdi)
jmp 0x486f0
movq %rdx, 0x10(%rsp)
leaq 0x2(%r12), %rcx
movq %rcx, 0x18(%rdi)
cmpl $0x5, %eax
jne 0x486f0
movq 0x148(%r14), %rdx
pushq $0x3d
popq %rsi
xorl %ecx, %ecx
movq %r15, %r8
callq 0x2a819
cmpl $0x0, (%r15)
jne 0x487c0
movq %rax, 0x18(%rsp)
movq 0x48(%rbp), %rdi
movq 0x148(%r14), %rdx
pushq $0x3f
popq %rsi
movq %r13, %rcx
movq %r15, %r8
callq 0x2a819
cmpl $0x0, (%r15)
jne 0x487c0
movq 0x48(%rbp), %rdi
pushq $0x26
popq %rsi
movq 0x18(%rsp), %rdx
movq %rax, %rcx
movq %r15, %r8
callq 0x2a819
movq 0x148(%r14), %r14
jmp 0x48724
movq 0x148(%r14), %rdx
pushq $0x1b
popq %rsi
movq %r15, %rcx
callq 0x2a981
cmpl $0x0, (%r15)
jne 0x487c0
movq %rax, %r14
movq 0x48(%rbp), %rdi
pushq $0x3b
popq %rsi
movq %r13, %rdx
movq %rax, %rcx
movq %r15, %r8
callq 0x2a819
cmpl $0x0, (%r15)
jne 0x487c0
movq 0x48(%rbp), %rdi
movq %r15, (%rsp)
movl $0x8a, %esi
movq %rbx, %rdx
movq %rax, %rcx
movq %r13, %r8
movq %r14, %r9
callq 0x2aa15
cmpl $0x0, (%r15)
movq 0x10(%rsp), %rdx
jne 0x487c0
jmp 0x4875c
movq %r13, %rbx
cmpl $0x3, (%rdx)
jne 0x48784
addq $0x148, %rdx            # imm = 0x148
movq %rdx, %rdi
callq 0x14804
leal -0x1(%rax), %edx
movq 0x48(%rbp), %rdi
pushq $0x1b
popq %rsi
movq %r15, %rcx
callq 0x2a981
jmp 0x4879c
movq 0x48(%rbp), %rdi
movq 0x148(%rdx), %rdx
pushq $0x12
popq %rsi
xorl %ecx, %ecx
movq %r15, %r8
callq 0x2a819
movq %rax, %r14
cmpl $0x0, (%r15)
jne 0x487c0
movq 0x48(%rbp), %rdi
pushq $0x33
popq %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq %r15, %r8
callq 0x2a819
cmpl $0x0, (%r15)
je 0x487d2
pushq $-0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%rbp), %rdi
movq %r15, (%rsp)
movl $0x8a, %esi
movq %r12, %rdx
movq %rax, %rcx
movq %rbx, %r8
movq %r14, %r9
callq 0x2aa15
cmpl $0x0, (%r15)
pushq $-0x1
popq %rax
jne 0x487c3
movq 0x20(%rsp), %rax
movq %r14, (%rax)
movq %r12, %rax
jmp 0x487c3