#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
unsigned int readNextArrayValue(char *, char **, char **, unsigned char *);
unsigned int readNextName(char *, char **, char **, unsigned int *);
unsigned int readNextValue(char *, char **, char **, unsigned char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
unsigned int validateExpressionSequence(char *);
unsigned int validateAlgebra(char *rule) {
  char *first;
  char *last;
  char *lastArrayValue;
  unsigned int hash;
  unsigned char type;
  unsigned char reenter = 0;
  unsigned int result =
      readNextArrayValue(rule, &first, &lastArrayValue, &type);
  while (result == 0) {
    result = readNextName(first, &first, &last, &hash);
    unsigned int nameLength = last - first;
    if (nameLength >= 4) {
      if (!strncmp("$all", last - 4, 4)) {
        nameLength = nameLength - 4;
        reenter = 1;
      } else if (!strncmp("$any", last - 4, 4)) {
        nameLength = nameLength - 4;
        reenter = 1;
      } else if (!strncmp("$not", last - 4, 4)) {
        nameLength = nameLength - 4;
      }
      if (nameLength == 0) {
        return 109;
      }
    }
    do {
      if (readNextValue(last, &first, &last, &type) != 0) {
        return readNextValue(last, &first, &last, &type);
      }
    } while (0);
    if (!reenter) {
      result = validateExpressionSequence(first);
    } else {
      result = validateAlgebra(first);
      reenter = 0;
    }
    if (result != 0) {
      return result;
    }
    result = readNextArrayValue(lastArrayValue, &first, &lastArrayValue, &type);
  }
  return (result == 100 ? 0 : result);
}


x86 assembly (clang, -O0):
<validateAlgebra>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movb   $0x0,-0x2e(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x2d(%rbp),%rcx
call   1040 <readNextArrayValue@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    131c <validateAlgebra+0x1bc>
mov    -0x18(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
call   1070 <readNextName@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x38(%rbp)
cmpl   $0x4,-0x38(%rbp)
jb     127a <validateAlgebra+0x11a>
mov    -0x20(%rbp),%rsi
add    $0xfffffffffffffffc,%rsi
lea    0xe2f(%rip),%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11f6 <validateAlgebra+0x96>
mov    -0x38(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x38(%rbp)
movb   $0x1,-0x2e(%rbp)
jmp    125f <validateAlgebra+0xff>
mov    -0x20(%rbp),%rsi
add    $0xfffffffffffffffc,%rsi
lea    0xe00(%rip),%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    122a <validateAlgebra+0xca>
mov    -0x38(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x38(%rbp)
movb   $0x1,-0x2e(%rbp)
jmp    125a <validateAlgebra+0xfa>
mov    -0x20(%rbp),%rsi
add    $0xfffffffffffffffc,%rsi
lea    0xdd1(%rip),%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1255 <validateAlgebra+0xf5>
mov    -0x38(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x38(%rbp)
jmp    125a <validateAlgebra+0xfa>
jmp    125f <validateAlgebra+0xff>
cmpl   $0x0,-0x38(%rbp)
jne    1275 <validateAlgebra+0x115>
movl   $0x6d,-0x4(%rbp)
jmp    133c <validateAlgebra+0x1dc>
jmp    127a <validateAlgebra+0x11a>
jmp    127f <validateAlgebra+0x11f>
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x2d(%rbp),%rcx
call   1050 <readNextValue@plt>
cmp    $0x0,%eax
je     12ba <validateAlgebra+0x15a>
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x2d(%rbp),%rcx
call   1050 <readNextValue@plt>
mov    %eax,-0x4(%rbp)
jmp    133c <validateAlgebra+0x1dc>
jmp    12bf <validateAlgebra+0x15f>
cmpb   $0x0,-0x2e(%rbp)
jne    12da <validateAlgebra+0x17a>
mov    -0x18(%rbp),%rdi
call   1060 <validateExpressionSequence@plt>
mov    %eax,-0x34(%rbp)
jmp    12ea <validateAlgebra+0x18a>
mov    -0x18(%rbp),%rdi
call   1080 <validateAlgebra@plt>
mov    %eax,-0x34(%rbp)
movb   $0x0,-0x2e(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     12ff <validateAlgebra+0x19f>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133c <validateAlgebra+0x1dc>
mov    -0x28(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x2d(%rbp),%rcx
call   1040 <readNextArrayValue@plt>
mov    %eax,-0x34(%rbp)
jmp    1188 <validateAlgebra+0x28>
cmpl   $0x64,-0x34(%rbp)
jne    1330 <validateAlgebra+0x1d0>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    1336 <validateAlgebra+0x1d6>
mov    -0x34(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validateAlgebra>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0x10(%rsp),%rsi
lea    0x18(%rsp),%rdx
lea    0x7(%rsp),%rcx
call   1040 <readNextArrayValue@plt>
mov    %eax,%ecx
test   %eax,%eax
je     119f <validateAlgebra+0x3f>
xor    %eax,%eax
cmp    $0x64,%ecx
cmovne %ecx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r14
lea    0x24(%rsp),%r15
lea    0x7(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x10(%rsp),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1070 <readNextName@plt>
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%eax
mov    %esi,%r12d
sub    %eax,%r12d
cmp    $0x4,%r12d
jb     1260 <validateAlgebra+0x100>
add    $0xfffffffffffffffc,%rsi
lea    0xe0d(%rip),%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
mov    $0x1,%r13b
test   %eax,%eax
je     1250 <validateAlgebra+0xf0>
mov    0x8(%rsp),%rsi
add    $0xfffffffffffffffc,%rsi
lea    0xdf1(%rip),%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     1250 <validateAlgebra+0xf0>
mov    0x8(%rsp),%rsi
add    $0xfffffffffffffffc,%rsi
lea    0xdd8(%rip),%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
xor    %r13d,%r13d
test   %eax,%eax
jne    1263 <validateAlgebra+0x103>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0x4,%r12d
jne    1263 <validateAlgebra+0x103>
jmp    12ef <validateAlgebra+0x18f>
nopl   0x0(%rax,%rax,1)
xor    %r13d,%r13d
mov    0x8(%rsp),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %rbp,%rcx
call   1050 <readNextValue@plt>
test   %eax,%eax
jne    12d1 <validateAlgebra+0x171>
mov    0x10(%rsp),%rdi
test   %r13b,%r13b
je     12a0 <validateAlgebra+0x140>
call   1080 <validateAlgebra@plt>
test   %eax,%eax
je     12ad <validateAlgebra+0x14d>
jmp    1190 <validateAlgebra+0x30>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
call   1060 <validateExpressionSequence@plt>
test   %eax,%eax
jne    1190 <validateAlgebra+0x30>
mov    0x18(%rsp),%rdi
mov    %rbx,%rsi
lea    0x18(%rsp),%rdx
mov    %rbp,%rcx
call   1040 <readNextArrayValue@plt>
test   %eax,%eax
je     11c0 <validateAlgebra+0x60>
mov    %eax,%ecx
jmp    1188 <validateAlgebra+0x28>
mov    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
lea    0x7(%rsp),%rcx
call   1050 <readNextValue@plt>
jmp    1190 <validateAlgebra+0x30>
mov    $0x6d,%eax
jmp    1190 <validateAlgebra+0x30>

LLVM IR (-O0):
@.str = [5 x i8] c"$all\00"
@.str.1 = [5 x i8] c"$any\00"
@.str.2 = [5 x i8] c"$not\00"
define i32 @validateAlgebra(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
store i8 0, i8* %9
%12 = load i8*, i8** %3
%13 = call i32 @readNextArrayValue(i8* %12, i8** %4, i8** %6, i8* %8)
store i32 %13, i32* %10
br label %14
14: ; preds = %82, %1
%15 = load i32, i32* %10
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %85
17: ; preds = %14
%18 = load i8*, i8** %4
%19 = call i32 @readNextName(i8* %18, i8** %4, i8** %5, i32* %7)
store i32 %19, i32* %10
%20 = load i8*, i8** %5
%21 = load i8*, i8** %4
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
%25 = trunc i64 %24 to i32
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp uge i32 %26, 4
br i1 %27, label %28, label %59
28: ; preds = %17
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i64 -4
%31 = call i32 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %30, i32 4)
%32 = icmp ne i32 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %28
%34 = load i32, i32* %11
%35 = sub i32 %34, 4
store i32 %35, i32* %11
store i8 1, i8* %9
br label %54
36: ; preds = %28
%37 = load i8*, i8** %5
%38 = getelementptr inbounds i8, i8* %37, i64 -4
%39 = call i32 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %38, i32 4)
%40 = icmp ne i32 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %36
%42 = load i32, i32* %11
%43 = sub i32 %42, 4
store i32 %43, i32* %11
store i8 1, i8* %9
br label %53
44: ; preds = %36
%45 = load i8*, i8** %5
%46 = getelementptr inbounds i8, i8* %45, i64 -4
%47 = call i32 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %46, i32 4)
%48 = icmp ne i32 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %44
%50 = load i32, i32* %11
%51 = sub i32 %50, 4
store i32 %51, i32* %11
br label %52
52: ; preds = %49, %44
br label %53
53: ; preds = %52, %41
br label %54
54: ; preds = %53, %33
%55 = load i32, i32* %11
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %54
store i32 109, i32* %2
br label %93
58: ; preds = %54
br label %59
59: ; preds = %58, %17
br label %60
60: ; preds = %59
%61 = load i8*, i8** %5
%62 = call i32 @readNextValue(i8* %61, i8** %4, i8** %5, i8* %8)
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %60
%65 = load i8*, i8** %5
%66 = call i32 @readNextValue(i8* %65, i8** %4, i8** %5, i8* %8)
store i32 %66, i32* %2
br label %93
67: ; preds = %60
br label %68
68: ; preds = %67
%69 = load i8, i8* %9
%70 = icmp ne i8 %69, 0
br i1 %70, label %74, label %71
71: ; preds = %68
%72 = load i8*, i8** %4
%73 = call i32 @validateExpressionSequence(i8* %72)
store i32 %73, i32* %10
br label %77
74: ; preds = %68
%75 = load i8*, i8** %4
%76 = call i32 @validateAlgebra(i8* %75)
store i32 %76, i32* %10
store i8 0, i8* %9
br label %77
77: ; preds = %74, %71
%78 = load i32, i32* %10
%79 = icmp ne i32 %78, 0
br i1 %79, label %80, label %82
80: ; preds = %77
%81 = load i32, i32* %10
store i32 %81, i32* %2
br label %93
82: ; preds = %77
%83 = load i8*, i8** %6
%84 = call i32 @readNextArrayValue(i8* %83, i8** %4, i8** %6, i8* %8)
store i32 %84, i32* %10
br label %14
85: ; preds = %14
%86 = load i32, i32* %10
%87 = icmp eq i32 %86, 100
br i1 %87, label %88, label %89
88: ; preds = %85
br label %91
89: ; preds = %85
%90 = load i32, i32* %10
br label %91
91: ; preds = %89, %88
%92 = phi i32 [ 0, %88 ], [ %90, %89 ]
store i32 %92, i32* %2
br label %93
93: ; preds = %91, %80, %64, %57
%94 = load i32, i32* %2
ret i32 %94
}

LLVM IR (-O2):
@.str = [5 x i8] c"$all\00"
@.str.1 = [5 x i8] c"$any\00"
@.str.2 = [5 x i8] c"$not\00"
define i32 @validateAlgebra(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
%7 = bitcast i8** %2 to i8*
%8 = bitcast i8** %3 to i8*
%9 = bitcast i8** %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = call i32 @readNextArrayValue(i8* %0, i8** %2, i8** %4, i8* %6)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %62
13: ; preds = %1, %58
%14 = load i8*, i8** %2
%15 = call i32 @readNextName(i8* %14, i8** %2, i8** %3, i32* %5)
%16 = load i8*, i8** %3
%17 = load i8*, i8** %2
%18 = ptrtoint i8* %16 to i64
%19 = ptrtoint i8* %17 to i64
%20 = sub i64 %18, %19
%21 = trunc i64 %20 to i32
%22 = icmp ugt i32 %21, 3
br i1 %22, label %23, label %40
23: ; preds = %13
%24 = getelementptr inbounds i8, i8* %16, i64 -4
%25 = call i32 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %24, i32 4)
%26 = icmp eq i32 %25, 0
br i1 %26, label %37, label %27
27: ; preds = %23
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i64 -4
%30 = call i32 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %29, i32 4)
%31 = icmp eq i32 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %27
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i64 -4
%35 = call i32 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %34, i32 4)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %27, %23, %32
%38 = phi i8 [ 0, %32 ], [ 1, %23 ], [ 1, %27 ]
%39 = icmp eq i32 %21, 4
br i1 %39, label %66, label %40
40: ; preds = %32, %13, %37
%41 = phi i8 [ %38, %37 ], [ 0, %13 ], [ 0, %32 ]
%42 = load i8*, i8** %3
%43 = call i32 @readNextValue(i8* %42, i8** %2, i8** %3, i8* %6)
%44 = icmp eq i32 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %40
%46 = load i8*, i8** %3
%47 = call i32 @readNextValue(i8* %46, i8** %2, i8** %3, i8* %6)
br label %66
48: ; preds = %40
%49 = icmp eq i8 %41, 0
%50 = load i8*, i8** %2
br i1 %49, label %51, label %53
51: ; preds = %48
%52 = call i32 @validateExpressionSequence(i8* %50)
br label %55
53: ; preds = %48
%54 = call i32 @validateAlgebra(i8* %50)
br label %55
55: ; preds = %53, %51
%56 = phi i32 [ %54, %53 ], [ %52, %51 ]
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %66
58: ; preds = %55
%59 = load i8*, i8** %4
%60 = call i32 @readNextArrayValue(i8* %59, i8** %2, i8** %4, i8* %6)
%61 = icmp eq i32 %60, 0
br i1 %61, label %13, label %62
62: ; preds = %58, %1
%63 = phi i32 [ %11, %1 ], [ %60, %58 ]
%64 = icmp eq i32 %63, 100
%65 = select i1 %64, i32 0, i32 %63
br label %66
66: ; preds = %55, %37, %45, %62
%67 = phi i32 [ %65, %62 ], [ %47, %45 ], [ 109, %37 ], [ %56, %55 ]
ret i32 %67
}