// File: /workspace/github_c_makefile_star2_O2/MikeGray-APH[P]LibLouisAPH/source/pattern.c
// Function: pattern_check_expression
static int pattern_check_expression(
	const unichar * const input,
	int *input_crs,
	const int input_minmax,
	const int input_dir,
	const unichar * const expr_data,
	struct subpattern *subpatterns,
	int expr_crs,
	int not,
	int loop_crs,
	int *loop_cnts)
{
	struct subpattern *subpattern;
	int input_crs_prv, input_start, loop_crs_prv, loop_cnt_prv, ret;

	loop_crs_prv =
	loop_cnt_prv = 0;

	/*   save input_crs to know if loop consumed input   */
	input_start = *input_crs;

	CHECK_OUTPUT(START, 0, "check start")

	while(EXPR_TYPE(expr_crs) != PTN_END)
	{
		/*   end of input expression   */
		if(EXPR_TYPE(expr_crs) == PTN_END_OF_INPUT)
		if(*input_crs * input_dir >= input_minmax * input_dir)
		{
			if(not)
				CHECK_OUTPUT(RETURN, 0, "end of input failed:  no input and not ")
			else
				CHECK_OUTPUT(RETURN, 1, "end of input passed:  no input")
			return !not;
		}
		else
		{
			if(not)
				CHECK_OUTPUT(RETURN, 1, "end of input passed:  input and not")
			else
				CHECK_OUTPUT(RETURN, 0, "end of input failed:  input")
			return not;
		}

		/*   no more input   */
		if(*input_crs * input_dir >= input_minmax * input_dir)
		{
			switch(EXPR_TYPE(expr_crs))
			{
			case PTN_ATTRIBUTES:
			case PTN_ANY:
			case PTN_CHARS:

				CHECK_OUTPUT(RETURN, 0, "chars failed:  no input")
				return 0;
			}
		}

		switch(EXPR_TYPE(expr_crs))
		{

		case PTN_START:

			expr_crs = EXPR_NXT(expr_crs);
			break;

		case PTN_GROUP:

			CHECK_OUTPUT(SHOW, 0, "group start")
			expr_crs = EXPR_DATA_0(expr_crs);
			break;

		case PTN_NOT:

			CHECK_OUTPUT(SHOW, 0, "not start")
			not = !not;
			expr_crs = EXPR_DATA_0(expr_crs);
			break;

		case PTN_ONE_MORE:
		case PTN_ZERO_MORE:

			/*   check if loop already started   */
			if(expr_crs == loop_crs)
			{
				loop_cnts[EXPR_DATA_1(loop_crs)]++;
				CHECK_OUTPUT(SHOW, 0, "loop again")
			}
			else
			{
				/*   start loop   */
				loop_crs_prv = loop_crs;
				loop_crs = expr_crs;
				loop_cnt_prv = loop_cnts[EXPR_DATA_1(loop_crs)];
				loop_cnts[EXPR_DATA_1(loop_crs)] = 1;

#ifdef PATTERN_OUTPUT_TRACE
				if(EXPR_TYPE(expr_crs) == PTN_ONE_MORE)
					CHECK_OUTPUT(SHOW, 0, "loop+ start")
				else
					CHECK_OUTPUT(SHOW, 0, "loop start")
#endif
			}

			/*   start loop expression   */
			input_crs_prv = *input_crs;
			ret = pattern_check_expression(input, input_crs, input_minmax, input_dir, expr_data, subpatterns, EXPR_DATA_0(expr_crs), not, loop_crs, loop_cnts);
			if(ret)
			{
				CHECK_OUTPUT(RETURN, 1, "loop passed")
				return 1;
			}
			if(EXPR_TYPE(expr_crs) != PTN_ONE_MORE)
				CHECK_OUTPUT(SHOW, 0, "loop failed")
			*input_crs = input_crs_prv;

			/*   check loop count   */
			loop_cnts[EXPR_DATA_1(loop_crs)]--;
			if(EXPR_TYPE(expr_crs) == PTN_ONE_MORE)
			{
				if(loop_cnts[EXPR_DATA_1(loop_crs)] < 1)
				{
					CHECK_OUTPUT(RETURN, 0, "loop+ failed")
					return 0;
				}
				else
					CHECK_OUTPUT(SHOW, 0, "loop+ passed")
			}

			/*   continue after loop   */
			loop_cnts[EXPR_DATA_1(loop_crs)] = loop_cnt_prv;
			loop_crs = loop_crs_prv;

			expr_crs = EXPR_NXT(expr_crs);
			break;

		case PTN_OPTIONAL:

			/*   save current state   */
			input_crs_prv = *input_crs;

			/*   start optional expression   */
			CHECK_OUTPUT(CALL, 0, "option start")
			if(pattern_check_expression(input, input_crs, input_minmax, input_dir, expr_data, subpatterns, EXPR_DATA_0(expr_crs), not, loop_crs, loop_cnts))
			{
				CHECK_OUTPUT(RETURN, 1, "option passed")
				return 1;
			}
			CHECK_OUTPUT(SHOW, 0, "option failed")

			/*   continue after optional expression   */
			*input_crs = input_crs_prv;
			CHECK_OUTPUT(SHOW, 0, "no option start")
			expr_crs = EXPR_NXT(expr_crs);
			break;

		case PTN_ALTERNATE:

			/*   save current state   */
			input_crs_prv = *input_crs;

			/*   start first expression   */
			CHECK_OUTPUT(CALL, 0, "or 1 start")
			if(pattern_check_expression(input, input_crs, input_minmax, input_dir, expr_data, subpatterns, EXPR_DATA_0(expr_crs), not, loop_crs, loop_cnts))
			{
				CHECK_OUTPUT(RETURN, 1, "or 1 passed")
				return 1;
			}
			CHECK_OUTPUT(SHOW, 0, "or 1 failed")

			/*   start second expression (no need to push)   */
			*input_crs = input_crs_prv;
			CHECK_OUTPUT(SHOW, 1, "or 2 start")
			expr_crs = EXPR_DATA_1(expr_crs);
			break;

		case PTN_ANY:

			CHECK_OUTPUT(SHOW, 0, "any")
			*input_crs += input_dir;
			expr_crs = EXPR_NXT(expr_crs);
			break;

		case PTN_ATTRIBUTES:

			ret = pattern_check_attrs(input[*input_crs], EXPR_CONST_DATA(expr_crs));
			if(ret && not)
			{
				CHECK_OUTPUT(RETURN, 0, "attributes failed:  not");
				return 0;
			}
			if(!ret && !not)
			{
				CHECK_OUTPUT(RETURN, 0, "attributes failed");
				return 0;
			}
			CHECK_OUTPUT(SHOW, 0, "attributes passed")
			*input_crs += input_dir;
			expr_crs = EXPR_NXT(expr_crs);
			break;

		case PTN_CHARS:

			ret = pattern_check_chars(input[*input_crs], EXPR_CONST_DATA(expr_crs));
			if(ret && not)
			{
				CHECK_OUTPUT(RETURN, 0, "chars failed:  not");
				return 0;
			}
			if(!ret && !not)
			{
				CHECK_OUTPUT(RETURN, 0, "chars failed");
				return 0;
			}
			CHECK_OUTPUT(SHOW, 0, "chars passed")
			*input_crs += input_dir;
			expr_crs = EXPR_NXT(expr_crs);
			break;

		case PTN_SUBPATTERN:

			if(subpatterns == NULL)
			{
				CHECK_OUTPUT(RETURN, 0, "subpatterns failed:  NULL");
				return 0;
			}

			subpattern = subpattern_find(subpatterns, EXPR_CONST_DATA(expr_crs + 1), EXPR_DATA_0(expr_crs));
			if(!subpattern)
			{
				CHECK_OUTPUT(RETURN, 0, "subpattern failed:  NULL");
				return 0;
			}
			if(subpattern->processing)
			{
				CHECK_OUTPUT(RETURN, 0, "subpattern failed:  recursive");
				return 0;
			}

			/*   call subpattern   */
			subpattern->processing = 1;
			subpattern_level++;
			ret = subpattern_check(input, input_crs, input_minmax, input_dir, subpattern->expr_data, subpatterns);
			subpattern->processing = 0;
			subpattern_level--;
			if(ret && not)
			{
				CHECK_OUTPUT(RETURN, 0, "subpattern failed:  not");
				return 0;
			}
			if(!ret && !not)
			{
				CHECK_OUTPUT(RETURN, 0, "subpattern failed");
				return 0;
			}
			CHECK_OUTPUT(SHOW, 0, "subpattern passed")
			expr_crs = EXPR_NXT(expr_crs);
			break;

		case PTN_END:  break;

		default:

			CHECK_OUTPUT(RETURN, 0, "unknown opcode")
			return 0;
		}

		/*   check end expression    */
		while(EXPR_TYPE(expr_crs) == PTN_END)
		{
			/*   check for end of expressions   */
			if(EXPR_NXT(expr_crs) == PTN_END)
				break;

			expr_crs = EXPR_NXT(expr_crs);

			/*   returning loop   */
			if(EXPR_TYPE(expr_crs) == PTN_ZERO_MORE || EXPR_TYPE(expr_crs) == PTN_ONE_MORE)
			{
				CHECK_OUTPUT(SHOW, 0, "end loop")

				/*   check that loop consumed input   */
				if(*input_crs == input_start)
				{
					CHECK_OUTPUT(RETURN, 0, "loop failed:  did not consume")
					return 0;
				}

				/*   loops do not continue to the next expression   */
				break;
			}

			/*   returning not   */
			if(EXPR_TYPE(expr_crs) == PTN_NOT)
				not = !not;

			expr_crs = EXPR_NXT(expr_crs);
		}
	}

	CHECK_OUTPUT(RETURN, 1, "check passed:  end of expression");
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, 0x30(%rsp)
movq %r8, %r15
movq %rdi, 0x10(%rsp)
movq 0xa8(%rsp), %r10
movl 0xa0(%rsp), %r11d
movl 0x98(%rsp), %r13d
movl 0x90(%rsp), %ebp
movl (%rsi), %eax
movl %eax, 0x3c(%rsp)
leaq 0x4(%r8), %rbx
leaq 0x6(%r8), %rax
movq %rax, 0x40(%rsp)
movl %ecx, %eax
movl %edx, 0x4(%rsp)
imull %edx, %eax
movl %eax, 0x24(%rsp)
leaq 0x8(%r8), %rax
movq %rax, 0x48(%rsp)
movq $0x0, 0x28(%rsp)
movl $0x0, 0x20(%rsp)
movl %ecx, (%rsp)
movq %rsi, 0x50(%rsp)
movl %ebp, %r12d
movzwl (%r15,%r12,2), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x8752
cmpl $0xc, %eax
je 0x8758
movl (%rsi), %r14d
leal -0x8(%rax), %edx
xorl %r8d, %r8d
cmpw $0x2, %dx
ja 0x8396
movl %r14d, %edx
imull %ecx, %edx
cmpl 0x24(%rsp), %edx
jge 0x8775
decl %eax
cmpl $0xa, %eax
ja 0x8775
movslq %r14d, %rdx
leaq 0x96c5(%rip), %rdi      # 0x11a70
movslq (%rdi,%rax,4), %rax
addq %rdi, %rax
movslq %ebp, %r9
jmpq *%rax
cmpl %r11d, %ebp
jne 0x844f
movslq %r11d, %rax
movzwl 0x8(%r15,%rax,2), %eax
incl (%r10,%rax,4)
movl %r11d, %ebp
movq 0x28(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x846a
xorl %edx, %edx
testl %r13d, %r13d
sete %dl
movslq %ebp, %rax
movq 0x40(%rsp), %rdi
leaq (%rdi,%rax,2), %rax
movl %edx, %r13d
jmp 0x8711
movq 0x10(%rsp), %rax
movzwl (%rax,%rdx,2), %edx
leaq (%r15,%r9,2), %rax
movzwl 0x6(%rax), %r8d
movq 0x48(%rsp), %rdi
leaq (%rdi,%r9,2), %rdi
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x86d2
cmpw %dx, (%rdi,%r9,2)
je 0x86dc
incq %r9
jmp 0x8419
movslq %ebp, %rax
leaq (%rbx,%rax,2), %rax
jmp 0x8711
movslq %ebp, %rax
movq 0x40(%rsp), %rdx
leaq (%rdx,%rax,2), %rax
jmp 0x8711
movq %r11, 0x8(%rsp)
movzwl 0x8(%r15,%r9,2), %eax
movl (%r10,%rax,4), %edx
movl %edx, 0x20(%rsp)
movl $0x1, (%r10,%rax,4)
movl (%rsi), %eax
movl %eax, 0x28(%rsp)
movq %r9, 0x18(%rsp)
movzwl 0x6(%r15,%r9,2), %eax
movq 0x10(%rsp), %rdi
movl 0x4(%rsp), %edx
movq %r15, %r8
movq 0x30(%rsp), %r9
pushq %r10
pushq %rbp
pushq %r13
pushq %rax
movq %r10, %r14
callq 0x82df
addq $0x20, %rsp
testl %eax, %eax
jne 0x8752
movq 0x50(%rsp), %rsi
movl 0x28(%rsp), %eax
movl %eax, (%rsi)
movslq %ebp, %rax
movzwl 0x8(%r15,%rax,2), %ecx
movl (%r14,%rcx,4), %eax
leal -0x1(%rax), %edx
movl %edx, (%r14,%rcx,4)
cmpw $0x4, (%r15,%r12,2)
movq %r14, %r10
jne 0x84d9
cmpl $0x2, %eax
jl 0x8772
movq 0x18(%rsp), %rax
leaq (%r15,%rax,2), %rax
movl 0x20(%rsp), %edx
movl %edx, (%r10,%rcx,4)
addq $0x4, %rax
movq 0x8(%rsp), %r11
movl %r11d, %ecx
movq %rcx, 0x28(%rsp)
movl (%rsp), %ecx
jmp 0x8711
addl %ecx, %r14d
movl %r14d, (%rsi)
leaq (%rbx,%r9,2), %rax
jmp 0x8711
movzwl 0x6(%r15,%r9,2), %eax
movq 0x10(%rsp), %rdi
movl 0x4(%rsp), %edx
movq %r15, %r8
movq %rsi, %r12
movq %r9, %rbp
movq 0x30(%rsp), %r9
pushq %r10
movq %r11, 0x10(%rsp)
pushq %r11
pushq %r13
pushq %rax
callq 0x82df
addq $0x20, %rsp
testl %eax, %eax
jne 0x8752
leaq (%r15,%rbp,2), %rax
movl %r14d, (%r12)
addq $0x4, %rax
jmp 0x85a0
movzwl 0x6(%r15,%r9,2), %eax
movq 0x10(%rsp), %rdi
movl 0x4(%rsp), %edx
movq %r15, %r8
movq %rsi, %r12
movq %r9, %rbp
movq 0x30(%rsp), %r9
pushq %r10
movq %r11, 0x10(%rsp)
pushq %r11
pushq %r13
pushq %rax
callq 0x82df
addq $0x20, %rsp
testl %eax, %eax
jne 0x8752
leaq (%r15,%rbp,2), %rax
movl %r14d, (%r12)
addq $0x8, %rax
movl (%rsp), %ecx
movq %r12, %rsi
jmp 0x864a
movq 0x30(%rsp), %r12
testq %r12, %r12
je 0x8775
movq %r11, 0x8(%rsp)
movq %rsi, %rbp
movq %r9, 0x18(%rsp)
leaq (%r15,%r9,2), %rsi
addq $0x8, %rsi
movzwl -0x2(%rsi), %edx
movq %r12, %rdi
callq 0x8259
testq %rax, %rax
je 0x8772
movq %rax, %r14
cmpl $0x0, 0x2c(%rax)
jne 0x8772
movl $0x1, 0x2c(%r14)
incl 0x10c5a(%rip)           # 0x19258
movq 0x10(%r14), %r8
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movl 0x4(%rsp), %edx
movl (%rsp), %ecx
movq %r12, %r9
callq 0x7a45
andl $0x0, 0x2c(%r14)
decl 0x10c34(%rip)           # 0x19258
testl %eax, %eax
setne %al
testl %r13d, %r13d
setne %cl
cmpb %al, %cl
je 0x8772
movq 0x18(%rsp), %rax
leaq (%r15,%rax,2), %rax
addq $0x4, %rax
movl (%rsp), %ecx
movq %rbp, %rsi
movq 0xa8(%rsp), %r10
movq 0x8(%rsp), %r11
jmp 0x8711
movq %r11, %r12
movq %rsi, %r14
movq 0x10(%rsp), %rax
movzwl (%rax,%rdx,2), %esi
movq %r9, 0x18(%rsp)
movl 0x6(%r15,%r9,2), %ebp
movq 0x10bd4(%rip), %rax     # 0x19250
movq (%rax), %rcx
movslq 0x8(%rax), %rax
movq (%rcx,%rax,8), %rdi
callq 0x1e48
xorl %r8d, %r8d
movl $0x0, %ecx
testq %rax, %rax
je 0x869c
movl 0x24(%rax), %ecx
testl %ebp, %ecx
setne %al
testl %r13d, %r13d
setne %cl
cmpb %al, %cl
movl (%rsp), %ecx
movq 0xa8(%rsp), %r10
je 0x8775
movq %r14, %rsi
movq %r12, %r11
movq 0x18(%rsp), %rax
leaq (%r15,%rax,2), %rax
addl %ecx, (%r14)
addq $0x4, %rax
jmp 0x8711
testl %r13d, %r13d
jne 0x86f2
jmp 0x8772
cmpl %r9d, %r8d
setne %dil
testl %r13d, %r13d
setne %dl
cmpb %dil, %dl
je 0x8772
addl %ecx, %r14d
movl %r14d, (%rsi)
addq $0x4, %rax
jmp 0x8711
xorl %edx, %edx
testl %r13d, %r13d
sete %dl
movl %edx, %r13d
leaq (%r15,%rax,2), %rax
addq $0x4, %rax
movzwl (%rax), %edx
cmpw $-0x1, (%r15,%rdx,2)
jne 0x874a
movzwl (%rbx,%rdx,2), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
je 0x874a
movzwl (%r15,%rax,2), %edx
cmpl $0x3, %edx
je 0x86fe
addl $-0x4, %edx
cmpl $0x1, %edx
ja 0x8709
movl %eax, %edx
movl $0x0, %r8d
movl 0x3c(%rsp), %eax
cmpl %eax, (%rsi)
je 0x8775
movzwl %dx, %ebp
jmp 0x835b
pushq $0x1
popq %r8
jmp 0x8775
imull (%rsi), %ecx
cmpl 0x24(%rsp), %ecx
jge 0x8766
movl %r13d, %r8d
jmp 0x8775
xorl %r8d, %r8d
testl %r13d, %r13d
sete %r8b
jmp 0x8775
xorl %r8d, %r8d
movl %r8d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop