// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movq %r8, %r15
movl %ecx, %r10d
movq %rdi, 0x10(%rsp)
movq 0xb8(%rsp), %r11
movl 0xb0(%rsp), %eax
movq %rax, 0x30(%rsp)
movl 0xa8(%rsp), %r13d
movl 0xa0(%rsp), %ebx
movl (%rsi), %eax
movl %eax, 0x4c(%rsp)
leaq 0x4(%r8), %rdi
leaq 0x6(%r8), %rax
movq %rax, 0x50(%rsp)
movl %ecx, %eax
movl %edx, 0xc(%rsp)
imull %edx, %eax
movl %eax, 0x3c(%rsp)
leaq 0x8(%r8), %rax
movq %rax, 0x58(%rsp)
movq $0x0, 0x40(%rsp)
movl $0x0, 0x38(%rsp)
movl %ecx, 0x8(%rsp)
movq %r9, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movl %ebx, %r14d
movzwl (%r15,%r14,2), %eax
movl $0x1, %ebp
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x93ad
cmpl $0xc, %eax
je 0x9394
movl (%rsi), %r12d
leal -0x8(%rax), %ecx
cmpw $0x2, %cx
ja 0x8f99
movl %r12d, %ecx
imull %r10d, %ecx
cmpl 0x3c(%rsp), %ecx
jge 0x9390
decl %eax
cmpl $0xa, %eax
ja 0x9390
movslq %r12d, %rdx
leaq 0xab52(%rip), %rcx      # 0x13b00
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x30(%rsp), %rcx
cmpl %ecx, %ebx
movq %r13, 0x60(%rsp)
jne 0x905b
movslq %ecx, %rax
movzwl 0x8(%r15,%rax,2), %eax
incl (%r11,%rax,4)
movslq %ebx, %rdx
movl %ecx, %ebx
movq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0x9074
xorl %eax, %eax
testl %r13d, %r13d
sete %al
movslq %ebx, %rcx
movq 0x50(%rsp), %rdx
leaq (%rdx,%rcx,2), %rbx
movl %eax, %r13d
jmp 0x9337
movslq %ebx, %rax
movzwl 0x6(%r15,%rax,2), %ecx
testq %rcx, %rcx
je 0x903c
movq 0x10(%rsp), %rdi
movzwl (%rdi,%rdx,2), %r8d
movq 0x58(%rsp), %rdx
leaq (%rdx,%rax,2), %rdi
xorl %edx, %edx
cmpw %r8w, (%rdi,%rdx,2)
je 0x9315
incq %rdx
cmpq %rdx, %rcx
jne 0x9029
testl %r13d, %r13d
jne 0x9324
jmp 0x9390
movslq %ebx, %rax
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax,2), %rbx
jmp 0x9337
movslq %ebx, %rdx
movzwl 0x8(%r15,%rdx,2), %eax
movl (%r11,%rax,4), %ecx
movl %ecx, 0x38(%rsp)
movl $0x1, (%r11,%rax,4)
movl (%rsi), %r12d
movq %rdx, 0x40(%rsp)
movzwl 0x6(%r15,%rdx,2), %eax
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %edx
movl %r10d, %ecx
movq %r15, %r8
pushq %r11
pushq %rbx
movq 0x70(%rsp), %r13
pushq %r13
pushq %rax
callq 0x8ed2
movq 0xd8(%rsp), %r11
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %r9
movl 0x28(%rsp), %r10d
addq $0x20, %rsp
testl %eax, %eax
movq 0x18(%rsp), %rdi
jne 0x93ad
movl %r12d, (%rsi)
movslq %ebx, %rax
movzwl 0x8(%r15,%rax,2), %eax
movl (%r11,%rax,4), %ecx
leal -0x1(%rcx), %edx
movl %edx, (%r11,%rax,4)
cmpw $0x4, (%r15,%r14,2)
jne 0x90f1
cmpl $0x2, %ecx
jl 0x9390
movq 0x40(%rsp), %rcx
leaq (%r15,%rcx,2), %rbx
movl 0x38(%rsp), %ecx
movl %ecx, (%r11,%rax,4)
addq $0x4, %rbx
movq 0x30(%rsp), %rax
movq %rax, 0x40(%rsp)
jmp 0x9337
addl %r10d, %r12d
movl %r12d, (%rsi)
movslq %ebx, %rax
leaq (%rdi,%rax,2), %rbx
jmp 0x9337
movslq %ebx, %rbx
movzwl 0x6(%r15,%rbx,2), %eax
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %edx
movl %r10d, %ecx
movq %r15, %r8
pushq %r11
pushq 0x38(%rsp)
pushq %r13
pushq %rax
callq 0x8ed2
movq 0x38(%rsp), %rdi
movq 0xd8(%rsp), %r11
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %r9
movl 0x28(%rsp), %r10d
addq $0x20, %rsp
testl %eax, %eax
jne 0x93ad
leaq (%r15,%rbx,2), %rbx
movl %r12d, (%rsi)
jmp 0x930f
movslq %ebx, %rbx
movzwl 0x6(%r15,%rbx,2), %eax
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %edx
movl %r10d, %ecx
movq %r15, %r8
pushq %r11
pushq 0x38(%rsp)
pushq %r13
pushq %rax
callq 0x8ed2
movq 0x38(%rsp), %rdi
movq 0xd8(%rsp), %r11
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %r9
movl 0x28(%rsp), %r10d
addq $0x20, %rsp
testl %eax, %eax
jne 0x93ad
leaq (%r15,%rbx,2), %rbx
movl %r12d, (%rsi)
addq $0x8, %rbx
jmp 0x9337
testq %r9, %r9
je 0x9390
movslq %ebx, %rax
leaq (%r15,%rax,2), %rbx
movzwl 0x6(%rbx), %eax
movq %r9, %r14
cmpl %eax, 0x20(%r14)
jne 0x9225
testq %rax, %rax
je 0x921f
movq 0x8(%r14), %rdx
xorl %ecx, %ecx
movzwl (%rdx,%rcx,2), %edi
cmpw 0x8(%rbx,%rcx,2), %di
jne 0x9221
incq %rcx
cmpq %rcx, %rax
jne 0x9207
jmp 0x92a2
xorl %ecx, %ecx
cmpl %eax, %ecx
je 0x92a2
movq (%r14), %r14
testq %r14, %r14
jne 0x91f6
jmp 0x9390
movq 0x10(%rsp), %rax
movzwl (%rax,%rdx,2), %esi
movslq %ebx, %rbx
movl 0x6(%r15,%rbx,2), %r14d
movq 0x10ff6(%rip), %rax     # 0x1a240
movq (%rax), %rcx
movslq 0x8(%rax), %rax
movq (%rcx,%rax,8), %rdi
callq 0x1ef5
xorl %ebp, %ebp
movl $0x0, %ecx
testq %rax, %rax
je 0x9269
movl 0x24(%rax), %ecx
testl %r14d, %ecx
setne %al
testl %r13d, %r13d
setne %cl
cmpb %al, %cl
movl 0x8(%rsp), %r10d
movq 0x28(%rsp), %r9
movq 0x20(%rsp), %rsi
movq 0xb8(%rsp), %r11
movq 0x18(%rsp), %rdi
je 0x93ad
leaq (%r15,%rbx,2), %rbx
addl %r10d, (%rsi)
jmp 0x930f
xorl %ebp, %ebp
cmpl $0x0, 0x2c(%r14)
jne 0x93ad
movl $0x1, 0x2c(%r14)
incl 0x10f8b(%rip)           # 0x1a248
movq 0x10(%r14), %r8
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %edx
movl %r10d, %ecx
callq 0x8664
movq 0x18(%rsp), %rdi
movq 0xb8(%rsp), %r11
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %r9
movl 0x8(%rsp), %r10d
movl $0x0, 0x2c(%r14)
decl 0x10f4c(%rip)           # 0x1a248
testl %eax, %eax
setne %al
testl %r13d, %r13d
setne %cl
cmpb %al, %cl
je 0x93ad
addq $0x4, %rbx
jmp 0x9337
cmpl %edx, %ecx
setne %cl
testl %r13d, %r13d
setne %dl
cmpb %cl, %dl
je 0x9390
leaq (%r15,%rax,2), %rbx
addl %r10d, %r12d
movl %r12d, (%rsi)
addq $0x4, %rbx
movq 0x18(%rsp), %rdi
movzwl (%rbx), %eax
cmpw $-0x1, (%r15,%rax,2)
jne 0x9388
movzwl %ax, %ecx
movzwl (%rdi,%rcx,2), %ecx
cmpq $0xffff, %rcx           # imm = 0xFFFF
je 0x9388
movzwl (%r15,%rcx,2), %eax
cmpl $0x3, %eax
je 0x9366
addl $-0x4, %eax
cmpl $0x1, %eax
ja 0x9371
jmp 0x9379
xorl %eax, %eax
testl %r13d, %r13d
sete %al
movl %eax, %r13d
movzwl 0x4(%r15,%rcx,2), %eax
jmp 0x933a
movl %ecx, %eax
movl $0x0, %ebp
movl 0x4c(%rsp), %ecx
cmpl %ecx, (%rsi)
je 0x93ad
movzwl %ax, %ebx
jmp 0x8f5b
xorl %ebp, %ebp
jmp 0x93ad
imull (%rsi), %r10d
cmpl 0x3c(%rsp), %r10d
jge 0x93a4
movl %r13d, %ebp
jmp 0x93ad
xorl %ebp, %ebp
testl %r13d, %r13d
sete %bpl
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop