#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 */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ stderr;
char *strdup(char *);
int check_syntax(char *code) {
  char *length = strdup(code);
  int i = 0;
  int nest_level = 0;
  while (*length) {
    if (code[i] == '[') {
      nest_level++;
    }
    if (code[i] == ']') {
      if (nest_level > 0) {
        nest_level--;
      } else {
        fprintf(stderr, "%s%i\n",
                "Compile failed:\nSYNTAX ERROR: Mismatched loop nesting at "
                "character ",
                i);
        return 0;
      }
    }
    i++;
    length++;
  }
  if (nest_level != 0) {
    fprintf(stderr, "%s\n",
            "Compiler failed:\nSYNTAX ERROR: Mismatched loop nesting");
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<check_syntax>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     11eb <check_syntax+0xcb>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x5b,%eax
jne    1172 <check_syntax+0x52>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x5d,%eax
jne    11d1 <check_syntax+0xb1>
cmpl   $0x0,-0x20(%rbp)
jle    119f <check_syntax+0x7f>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    11cc <check_syntax+0xac>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%ecx
lea    0xe4e(%rip),%rsi
lea    0xe4d(%rip),%rdx
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1226 <check_syntax+0x106>
jmp    11d1 <check_syntax+0xb1>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1147 <check_syntax+0x27>
cmpl   $0x0,-0x20(%rbp)
je     121f <check_syntax+0xff>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
lea    0xe42(%rip),%rdx
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1226 <check_syntax+0x106>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_syntax>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbp
call   1040 <strdup@plt>
mov    $0x1,%r14d
cmpb   $0x0,(%rax)
je     11b2 <check_syntax+0x92>
xor    %ecx,%ecx
xor    %edx,%edx
jmp    114b <check_syntax+0x2b>
nopl   (%rax)
cmpb   $0x0,0x1(%rax,%rcx,1)
lea    0x1(%rcx),%rcx
je     116a <check_syntax+0x4a>
mov    %edx,%esi
movzbl 0x0(%rbp,%rcx,1),%ebx
xor    %edx,%edx
cmp    $0x5b,%bl
sete   %dl
add    %esi,%edx
cmp    $0x5d,%bl
jne    1140 <check_syntax+0x20>
test   %edx,%edx
jle    1191 <check_syntax+0x71>
add    $0xffffffff,%edx
jmp    1140 <check_syntax+0x20>
test   %edx,%edx
je     11b2 <check_syntax+0x92>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
lea    0xecc(%rip),%rsi
lea    0xec9(%rip),%rdx
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11b2 <check_syntax+0x92>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
lea    0xe5e(%rip),%rdx
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [6 x i8] c"%s%i\0A\00"
@.str.1 = [68 x i8] c"Compile failed:\0ASYNTAX ERROR: Mismatched loop nesting at character \00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [55 x i8] c"Compiler failed:\0ASYNTAX ERROR: Mismatched loop nesting\00"
define i32 @check_syntax(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i8* @strdup(i8* %7)
store i8* %8, i8** %4
store i32 0, i32* %5
store i32 0, i32* %6
br label %9
9: ; preds = %43, %1
%10 = load i8*, i8** %4
%11 = load i8, i8* %10
%12 = icmp ne i8 %11, 0
br i1 %12, label %13, label %48
13: ; preds = %9
%14 = load i8*, i8** %3
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 91
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = load i32, i32* %6
%23 = add nsw i32 %22, 1
store i32 %23, i32* %6
br label %24
24: ; preds = %21, %13
%25 = load i8*, i8** %3
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 93
br i1 %31, label %32, label %43
32: ; preds = %24
%33 = load i32, i32* %6
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i32, i32* %6
%37 = add nsw i32 %36, -1
store i32 %37, i32* %6
br label %42
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = load i32, i32* %5
%41 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), i32 %40)
store i32 0, i32* %2
br label %55
42: ; preds = %35
br label %43
43: ; preds = %42, %24
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
store i32 %45, i32* %5
%46 = load i8*, i8** %4
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %4
br label %9
48: ; preds = %9
%49 = load i32, i32* %6
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %2
br label %55
54: ; preds = %48
store i32 1, i32* %2
br label %55
55: ; preds = %54, %51, %38
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [6 x i8] c"%s%i\0A\00"
@.str.1 = [68 x i8] c"Compile failed:\0ASYNTAX ERROR: Mismatched loop nesting at character \00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [55 x i8] c"Compiler failed:\0ASYNTAX ERROR: Mismatched loop nesting\00"
define i32 @check_syntax(i8* nocapture %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = load i8, i8* %2
%4 = icmp eq i8 %3, 0
br i1 %4, label %34, label %5
5: ; preds = %1, %23
%6 = phi i64 [ %25, %23 ], [ 0, %1 ]
%7 = phi i32 [ %24, %23 ], [ 0, %1 ]
%8 = phi i8* [ %26, %23 ], [ %2, %1 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %6
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 91
%12 = zext i1 %11 to i32
%13 = add nsw i32 %7, %12
%14 = icmp eq i8 %10, 93
br i1 %14, label %15, label %23
15: ; preds = %5
%16 = icmp sgt i32 %13, 0
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = add nsw i32 %13, -1
br label %23
19: ; preds = %15
%20 = trunc i64 %6 to i32
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %34
23: ; preds = %17, %5
%24 = phi i32 [ %18, %17 ], [ %13, %5 ]
%25 = add nuw i64 %6, 1
%26 = getelementptr inbounds i8, i8* %8, i64 1
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 0
br i1 %28, label %29, label %5
29: ; preds = %23
%30 = icmp eq i32 %24, 0
br i1 %30, label %34, label %31
31: ; preds = %29
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %1, %29, %31, %19
%35 = phi i32 [ 0, %19 ], [ 0, %31 ], [ 1, %29 ], [ 1, %1 ]
ret i32 %35
}