// File: /workspace/github_c_makefile_star2_O1/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
movl %ecx, %r10d
movq %rdi, 0x8(%rsp)
movq 0xa8(%rsp), %r11
movl 0xa0(%rsp), %eax
movq %rax, 0x20(%rsp)
movl 0x98(%rsp), %r13d
movl 0x90(%rsp), %eax
movl (%rsi), %ecx
movl %ecx, 0x3c(%rsp)
leaq 0x4(%r8), %rcx
movq %rcx, 0x48(%rsp)
leaq 0x6(%r8), %rcx
movq %rcx, 0x40(%rsp)
movl %r10d, %ecx
movl %edx, 0x4(%rsp)
imull %edx, %ecx
movl %ecx, 0x2c(%rsp)
leaq 0x8(%r8), %rcx
movq %rcx, 0x50(%rsp)
xorl %r12d, %r12d
movl $0x0, 0x28(%rsp)
movl %r10d, (%rsp)
movq %rsi, 0x18(%rsp)
movl %eax, %ebx
movzwl (%r15,%rbx,2), %ecx
movl $0x1, %ebp
cmpl $0xffff, %ecx           # imm = 0xFFFF
je 0x94e1
cmpl $0xc, %ecx
je 0x94c8
movl (%rsi), %r14d
leal -0x8(%rcx), %edx
cmpw $0x2, %dx
ja 0x90ac
movl %r14d, %edx
imull %r10d, %edx
cmpl 0x2c(%rsp), %edx
jge 0x94c4
leal -0x1(%rcx), %edi
cmpl $0xa, %edi
ja 0x90f6
movslq %r14d, %rdx
leaq 0xa972(%rip), %r8       # 0x13a30
movslq (%r8,%rdi,4), %rcx
addq %r8, %rcx
jmpq *%rcx
movq 0x20(%rsp), %rcx
cmpl %ecx, %eax
jne 0x9171
movq %rcx, %rdx
movslq %edx, %rcx
movzwl 0x8(%r15,%rcx,2), %ecx
incl (%r11,%rcx,4)
movq %rdx, 0x10(%rsp)
movl %r12d, %ecx
movq %rcx, 0x20(%rsp)
jmp 0x9191
cmpl $0xffff, %ecx           # imm = 0xFFFF
je 0x9459
jmp 0x94c4
xorl %ecx, %ecx
testl %r13d, %r13d
sete %cl
cltq
movq 0x40(%rsp), %rdx
leaq (%rdx,%rax,2), %rbp
movl %ecx, %r13d
jmp 0x9455
cltq
movzwl 0x6(%r15,%rax,2), %ecx
testl %ecx, %ecx
je 0x9423
movq 0x8(%rsp), %rdi
movzwl (%rdi,%rdx,2), %r11d
leal 0x1(%rcx), %edx
movl %ecx, %r8d
movq 0x50(%rsp), %rdi
leaq (%rdi,%rax,2), %r9
xorl %edi, %edi
cmpw %r11w, (%r9,%rdi,2)
je 0x942a
incq %rdi
cmpq %rdi, %r8
jne 0x914d
jmp 0x942e
cltq
movq 0x40(%rsp), %rcx
jmp 0x9231
movslq %eax, %rcx
movzwl 0x8(%r15,%rcx,2), %ecx
movl (%r11,%rcx,4), %edx
movl %edx, 0x28(%rsp)
movl $0x1, (%r11,%rcx,4)
movl %eax, %ecx
movq %rcx, 0x10(%rsp)
movl (%rsi), %r12d
movslq %eax, %r14
movzwl 0x6(%r15,%r14,2), %eax
movq 0x8(%rsp), %rdi
movl 0x4(%rsp), %edx
movl %r10d, %ecx
movq %r15, %r8
movq 0x30(%rsp), %r9
pushq %r11
pushq 0x18(%rsp)
pushq %r13
pushq %rax
callq 0x8fea
movq 0xc8(%rsp), %r11
movq 0x38(%rsp), %rsi
movl 0x20(%rsp), %r10d
addq $0x20, %rsp
testl %eax, %eax
jne 0x94e1
movl %r12d, (%rsi)
movslq 0x10(%rsp), %rax
movzwl 0x8(%r15,%rax,2), %eax
movl (%r11,%rax,4), %ecx
leal -0x1(%rcx), %edx
movl %edx, (%r11,%rax,4)
cmpw $0x4, (%r15,%rbx,2)
jne 0x9207
cmpl $0x2, %ecx
jl 0x94c4
leaq (%r15,%r14,2), %rbp
movl 0x28(%rsp), %ecx
movl %ecx, (%r11,%rax,4)
addq $0x4, %rbp
movq 0x20(%rsp), %rax
movl %eax, %r12d
jmp 0x9455
addl %r10d, %r14d
movl %r14d, (%rsi)
cltq
movq 0x48(%rsp), %rcx
leaq (%rcx,%rax,2), %rbp
jmp 0x9455
movslq %eax, %rbx
movzwl 0x6(%r15,%rbx,2), %eax
movq 0x8(%rsp), %rdi
movl 0x4(%rsp), %edx
movl %r10d, %ecx
movq %r15, %r8
movq 0x30(%rsp), %r9
pushq %r11
pushq 0x28(%rsp)
pushq %r13
pushq %rax
callq 0x8fea
movq 0xc8(%rsp), %r11
movq 0x38(%rsp), %rsi
movl 0x20(%rsp), %r10d
addq $0x20, %rsp
testl %eax, %eax
jne 0x94e1
leaq (%r15,%rbx,2), %rbp
jmp 0x944e
movslq %eax, %rbx
movzwl 0x6(%r15,%rbx,2), %eax
movq 0x8(%rsp), %rdi
movl 0x4(%rsp), %edx
movl %r10d, %ecx
movq %r15, %r8
movq 0x30(%rsp), %r9
pushq %r11
pushq 0x28(%rsp)
pushq %r13
pushq %rax
callq 0x8fea
movq 0xc8(%rsp), %r11
movq 0x38(%rsp), %rsi
movl 0x20(%rsp), %r10d
addq $0x20, %rsp
testl %eax, %eax
jne 0x94e1
leaq (%r15,%rbx,2), %rbp
movl %r14d, (%rsi)
addq $0x8, %rbp
jmp 0x9455
movq 0x30(%rsp), %r9
testq %r9, %r9
je 0x94c4
cltq
leaq (%r15,%rax,2), %rbp
movzwl 0x6(%rbp), %eax
movq %r9, %rbx
cmpl %eax, 0x20(%rbx)
jne 0x932d
testq %rax, %rax
je 0x9327
movq 0x8(%rbx), %rdx
xorl %ecx, %ecx
movzwl (%rdx,%rcx,2), %edi
cmpw 0x8(%rbp,%rcx,2), %di
jne 0x9329
incq %rcx
cmpq %rcx, %rax
jne 0x9310
movl %eax, %ecx
jmp 0x9329
xorl %ecx, %ecx
cmpl %eax, %ecx
je 0x9337
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x9300
xorl %ebx, %ebx
testq %rbx, %rbx
je 0x94c4
cmpl $0x0, 0x2c(%rbx)
jne 0x94c4
movl $0x1, 0x2c(%rbx)
incl 0x10ef1(%rip)           # 0x1a248
movq 0x10(%rbx), %r8
movq 0x8(%rsp), %rdi
movl 0x4(%rsp), %edx
movl %r10d, %ecx
callq 0x8725
movl $0x0, 0x2c(%rbx)
decl 0x10ecf(%rip)           # 0x1a248
testl %eax, %eax
setne %al
testl %r13d, %r13d
setne %cl
cmpb %al, %cl
je 0x94c4
addq $0x4, %rbp
movl (%rsp), %r10d
movq 0x18(%rsp), %rsi
movq 0xa8(%rsp), %r11
jmp 0x9455
movq %r12, 0x10(%rsp)
movq 0x8(%rsp), %rcx
movzwl (%rcx,%rdx,2), %esi
movslq %eax, %rbx
movzwl 0x8(%r15,%rbx,2), %r14d
movzwl 0x6(%r15,%rbx,2), %r12d
movq 0x10e76(%rip), %rax     # 0x1a240
movq (%rax), %rcx
movslq 0x8(%rax), %rax
movq (%rcx,%rax,8), %rdi
callq 0x1e99
xorl %ebp, %ebp
movl $0x0, %ecx
testq %rax, %rax
je 0x93e9
movl 0x24(%rax), %ecx
shll $0x10, %r14d
orl %r12d, %r14d
testl %r14d, %ecx
setne %al
testl %r13d, %r13d
setne %cl
cmpb %al, %cl
movl (%rsp), %r10d
movq 0x18(%rsp), %rsi
movq 0xa8(%rsp), %r11
movq 0x10(%rsp), %r12
je 0x94e1
leaq (%r15,%rbx,2), %rbp
addl %r10d, (%rsi)
jmp 0x9451
movl $0x1, %edx
jmp 0x9436
incl %edi
movl %edi, %edx
movq 0xa8(%rsp), %r11
incl %ecx
cmpl %ecx, %edx
setne %cl
testl %r13d, %r13d
setne %dl
cmpb %cl, %dl
je 0x94c4
leaq (%r15,%rax,2), %rbp
addl %r10d, %r14d
movl %r14d, (%rsi)
addq $0x4, %rbp
movzwl (%rbp), %eax
movl %eax, %ecx
cmpw $-0x1, (%r15,%rcx,2)
jne 0x906e
movslq %eax, %rcx
movzwl 0x4(%r15,%rcx,2), %ecx
cmpq $0xffff, %rcx           # imm = 0xFFFF
je 0x906e
movzwl (%r15,%rcx,2), %eax
cmpl $0x3, %eax
je 0x9491
addl $-0x4, %eax
cmpl $0x1, %eax
ja 0x949c
jmp 0x94af
xorl %eax, %eax
testl %r13d, %r13d
sete %al
movl %eax, %r13d
movzwl 0x4(%r15,%rcx,2), %eax
cmpw $-0x1, (%r15,%rax,2)
je 0x9467
jmp 0x906e
movl %ecx, %eax
movl $0x0, %ebp
movl 0x3c(%rsp), %ecx
cmpl %ecx, (%rsi)
jne 0x906e
jmp 0x94e1
xorl %ebp, %ebp
jmp 0x94e1
imull (%rsi), %r10d
cmpl 0x2c(%rsp), %r10d
jge 0x94d8
movl %r13d, %ebp
jmp 0x94e1
xorl %ebp, %ebp
testl %r13d, %r13d
sete %bpl
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop