#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*/ printf(char *, ...);
size_t strnlen(char *, int);
int eslib_file_path_check(char *path) {
  size_t len;
  size_t i;
  int adot = 0;
  int aslash = 0;
  int slashing = 0;
  if (path == NULL)
    return -1;
  len = strnlen(path, 2048);
  if (len >= 2048 || len == 0) {
    printf("bad pathlen(%d)\n", len);
    return -1;
  }
  if (path[0] != '/') {
    printf("path must begin with /\n");
    goto bad_path;
  }
  for (i = 0; i < len; ++i) {
    switch (path[i]) {
    case '.':
      if (adot) {
        printf(".. not permitted\n");
        goto bad_path;
      }
      adot = 1;
      aslash = 0;
      break;
    case '/':
      if (aslash) {
        printf("// not permitted\n");
        goto bad_path;
      }
      if (adot && slashing) {
        printf("/. not permitted\n");
        goto bad_path;
      }
      slashing = 1;
      aslash = 1;
      adot = 0;
      break;
    default:
      slashing = 0;
      adot = 0;
      aslash = 0;
      break;
    }
  }
  if (len >= 2 && path[i - 1] == '/') {
    printf("trailing slash not permitted: %s\n", path);
    return -1;
  } else if (len >= 2 && path[i - 2] == '/' && path[i - 1] == '.') {
    printf("/. not permitted\n");
    return -1;
  }
  return 0;
bad_path:
  printf("bad path: %s\n", path);
  return -1;
}


x86 assembly (clang, -O0):
<eslib_file_path_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1158 <eslib_file_path_check+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <eslib_file_path_check+0x264>
mov    -0x10(%rbp),%rdi
mov    $0x800,%esi
call   1040 <strnlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x800,-0x18(%rbp)
jae    1183 <eslib_file_path_check+0x63>
cmpq   $0x0,-0x18(%rbp)
jne    11a1 <eslib_file_path_check+0x81>
mov    -0x18(%rbp),%rsi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <eslib_file_path_check+0x264>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11c4 <eslib_file_path_check+0xa4>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    136b <eslib_file_path_check+0x24b>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    12c1 <eslib_file_path_check+0x1a1>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x30(%rbp)
sub    $0x2e,%eax
je     1208 <eslib_file_path_check+0xe8>
jmp    11f7 <eslib_file_path_check+0xd7>
mov    -0x30(%rbp),%eax
sub    $0x2f,%eax
je     1238 <eslib_file_path_check+0x118>
jmp    1296 <eslib_file_path_check+0x176>
cmpl   $0x0,-0x24(%rbp)
je     1225 <eslib_file_path_check+0x105>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    136b <eslib_file_path_check+0x24b>
movl   $0x1,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
jmp    12ab <eslib_file_path_check+0x18b>
cmpl   $0x0,-0x28(%rbp)
je     1255 <eslib_file_path_check+0x135>
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    136b <eslib_file_path_check+0x24b>
cmpl   $0x0,-0x24(%rbp)
je     127c <eslib_file_path_check+0x15c>
cmpl   $0x0,-0x2c(%rbp)
je     127c <eslib_file_path_check+0x15c>
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    136b <eslib_file_path_check+0x24b>
movl   $0x1,-0x2c(%rbp)
movl   $0x1,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
jmp    12ab <eslib_file_path_check+0x18b>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
jmp    12b0 <eslib_file_path_check+0x190>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11cc <eslib_file_path_check+0xac>
cmpq   $0x2,-0x18(%rbp)
jb     1303 <eslib_file_path_check+0x1e3>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    1303 <eslib_file_path_check+0x1e3>
mov    -0x10(%rbp),%rsi
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <eslib_file_path_check+0x264>
cmpq   $0x2,-0x18(%rbp)
jb     135a <eslib_file_path_check+0x23a>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x2,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    135a <eslib_file_path_check+0x23a>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
jne    135a <eslib_file_path_check+0x23a>
lea    0xd06(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <eslib_file_path_check+0x264>
jmp    135f <eslib_file_path_check+0x23f>
movl   $0x0,-0x4(%rbp)
jmp    1384 <eslib_file_path_check+0x264>
mov    -0x10(%rbp),%rsi
lea    0xd0b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eslib_file_path_check>:
push   %rbp
push   %r14
push   %rbx
mov    $0xffffffff,%ecx
test   %rdi,%rdi
je     124b <eslib_file_path_check+0x11b>
mov    %rdi,%r14
mov    $0x800,%esi
call   1050 <strnlen@plt>
lea    -0x800(%rax),%rcx
cmp    $0xfffffffffffff800,%rcx
ja     116e <eslib_file_path_check+0x3e>
lea    0xe9a(%rip),%rdi
mov    %rax,%rsi
jmp    123f <eslib_file_path_check+0x10f>
cmpb   $0x2f,(%r14)
jne    1229 <eslib_file_path_check+0xf9>
xor    %r10d,%r10d
mov    $0x2f,%dl
mov    $0x1,%ecx
lea    0xec9(%rip),%rdi
lea    0xeb1(%rip),%r8
lea    0xecc(%rip),%r9
xor    %esi,%esi
xor    %r11d,%r11d
nopl   0x0(%rax)
movsbl %dl,%edx
cmp    $0x2f,%edx
je     11e0 <eslib_file_path_check+0xb0>
mov    $0x0,%ebx
mov    $0x0,%esi
mov    $0x0,%ebp
cmp    $0x2e,%edx
jne    1210 <eslib_file_path_check+0xe0>
mov    $0x1,%ebx
mov    $0x0,%esi
mov    %r10d,%ebp
test   %r11d,%r11d
je     1210 <eslib_file_path_check+0xe0>
jmp    126d <eslib_file_path_check+0x13d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %esi,%esi
jne    1230 <eslib_file_path_check+0x100>
mov    $0x1,%esi
test   %r11d,%r11d
je     11ff <eslib_file_path_check+0xcf>
mov    $0x0,%ebx
mov    $0x1,%ebp
test   %r10d,%r10d
je     1210 <eslib_file_path_check+0xe0>
jmp    1272 <eslib_file_path_check+0x142>
xor    %ebx,%ebx
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
cmp    %rcx,%rax
je     1252 <eslib_file_path_check+0x122>
movzbl (%r14,%rcx,1),%edx
add    $0x1,%rcx
mov    %ebp,%r10d
mov    %ebx,%r11d
jmp    11a0 <eslib_file_path_check+0x70>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
lea    0xdf7(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %ecx,%ecx
cmp    $0x2,%rax
jb     124b <eslib_file_path_check+0x11b>
mov    -0x1(%rax,%r14,1),%dl
cmp    $0x2f,%dl
jne    1277 <eslib_file_path_check+0x147>
lea    0xda6(%rip),%rdi
jmp    123c <eslib_file_path_check+0x10c>
mov    %r9,%rdi
jmp    1230 <eslib_file_path_check+0x100>
mov    %r8,%rdi
jmp    1230 <eslib_file_path_check+0x100>
cmpb   $0x2f,-0x2(%rax,%r14,1)
jne    124b <eslib_file_path_check+0x11b>
cmp    $0x2e,%dl
jne    124b <eslib_file_path_check+0x11b>
lea    0xdb6(%rip),%rdi
call   1030 <puts@plt>
jmp    1246 <eslib_file_path_check+0x116>

LLVM IR (-O0):
@.str = [17 x i8] c"bad pathlen(%d)\0A\00"
@.str.1 = [24 x i8] c"path must begin with /\0A\00"
@.str.2 = [18 x i8] c".. not permitted\0A\00"
@.str.3 = [18 x i8] c"// not permitted\0A\00"
@.str.4 = [18 x i8] c"/. not permitted\0A\00"
@.str.5 = [34 x i8] c"trailing slash not permitted: %s\0A\00"
@.str.6 = [14 x i8] c"bad path: %s\0A\00"
define i32 @eslib_file_path_check(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%9 = load i8*, i8** %3
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %107
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i64 @strnlen(i8* %13, i32 2048)
store i64 %14, i64* %4
%15 = load i64, i64* %4
%16 = icmp uge i64 %15, 2048
br i1 %16, label %20, label %17
17: ; preds = %12
%18 = load i64, i64* %4
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17, %12
%21 = load i64, i64* %4
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %21)
store i32 -1, i32* %2
br label %107
23: ; preds = %17
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 0
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 47
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %104
31: ; preds = %23
store i64 0, i64* %5
br label %32
32: ; preds = %64, %31
%33 = load i64, i64* %5
%34 = load i64, i64* %4
%35 = icmp ult i64 %33, %34
br i1 %35, label %36, label %67
36: ; preds = %32
%37 = load i8*, i8** %3
%38 = load i64, i64* %5
%39 = getelementptr inbounds i8, i8* %37, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
switch i32 %41, label %62 [
i32 46, label %42
i32 47, label %48
]
42: ; preds = %36
%43 = load i32, i32* %6
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %104
47: ; preds = %42
store i32 1, i32* %6
store i32 0, i32* %7
br label %63
48: ; preds = %36
%49 = load i32, i32* %7
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %104
53: ; preds = %48
%54 = load i32, i32* %6
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %53
%57 = load i32, i32* %8
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %104
61: ; preds = %56, %53
store i32 1, i32* %8
store i32 1, i32* %7
store i32 0, i32* %6
br label %63
62: ; preds = %36
store i32 0, i32* %8
store i32 0, i32* %6
store i32 0, i32* %7
br label %63
63: ; preds = %62, %61, %47
br label %64
64: ; preds = %63
%65 = load i64, i64* %5
%66 = add i64 %65, 1
store i64 %66, i64* %5
br label %32
67: ; preds = %32
%68 = load i64, i64* %4
%69 = icmp uge i64 %68, 2
br i1 %69, label %70, label %81
70: ; preds = %67
%71 = load i8*, i8** %3
%72 = load i64, i64* %5
%73 = sub i64 %72, 1
%74 = getelementptr inbounds i8, i8* %71, i64 %73
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = icmp eq i32 %76, 47
br i1 %77, label %78, label %81
78: ; preds = %70
%79 = load i8*, i8** %3
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %79)
store i32 -1, i32* %2
br label %107
81: ; preds = %70, %67
%82 = load i64, i64* %4
%83 = icmp uge i64 %82, 2
br i1 %83, label %84, label %102
84: ; preds = %81
%85 = load i8*, i8** %3
%86 = load i64, i64* %5
%87 = sub i64 %86, 2
%88 = getelementptr inbounds i8, i8* %85, i64 %87
%89 = load i8, i8* %88
%90 = sext i8 %89 to i32
%91 = icmp eq i32 %90, 47
br i1 %91, label %92, label %102
92: ; preds = %84
%93 = load i8*, i8** %3
%94 = load i64, i64* %5
%95 = sub i64 %94, 1
%96 = getelementptr inbounds i8, i8* %93, i64 %95
%97 = load i8, i8* %96
%98 = sext i8 %97 to i32
%99 = icmp eq i32 %98, 46
br i1 %99, label %100, label %102
100: ; preds = %92
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %107
102: ; preds = %92, %84, %81
br label %103
103: ; preds = %102
store i32 0, i32* %2
br label %107
104: ; preds = %59, %51, %45, %29
%105 = load i8*, i8** %3
%106 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i8* %105)
store i32 -1, i32* %2
br label %107
107: ; preds = %104, %103, %100, %78, %20, %11
%108 = load i32, i32* %2
ret i32 %108
}

LLVM IR (-O2):
@.str = [17 x i8] c"bad pathlen(%d)\0A\00"
@.str.5 = [34 x i8] c"trailing slash not permitted: %s\0A\00"
@.str.6 = [14 x i8] c"bad path: %s\0A\00"
@str.7 = [17 x i8] c"/. not permitted\00"
@str.8 = [17 x i8] c"// not permitted\00"
@str.9 = [17 x i8] c".. not permitted\00"
@str.10 = [23 x i8] c"path must begin with /\00"
define i32 @eslib_file_path_check(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %58, label %3
3: ; preds = %1
%4 = tail call i64 @strnlen(i8* %0, i32 2048)
%5 = add i64 %4, -2048
%6 = icmp ult i64 %5, -2047
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %4)
br label %58
9: ; preds = %3
%10 = load i8, i8* %0
%11 = icmp eq i8 %10, 47
br i1 %11, label %12, label %54
12: ; preds = %9, %33
%13 = phi i8 [ %35, %33 ], [ 47, %9 ]
%14 = phi i32 [ %30, %33 ], [ 0, %9 ]
%15 = phi i32 [ %29, %33 ], [ 0, %9 ]
%16 = phi i32 [ %28, %33 ], [ 0, %9 ]
%17 = phi i64 [ %31, %33 ], [ 0, %9 ]
%18 = sext i8 %13 to i32
switch i32 %18, label %27 [
i32 46, label %19
i32 47, label %21
]
19: ; preds = %12
%20 = icmp eq i32 %16, 0
br i1 %20, label %27, label %54
21: ; preds = %12
%22 = icmp eq i32 %15, 0
br i1 %22, label %23, label %54
23: ; preds = %21
%24 = icmp ne i32 %16, 0
%25 = icmp ne i32 %14, 0
%26 = select i1 %24, i1 %25, i1 false
br i1 %26, label %54, label %27
27: ; preds = %12, %23, %19
%28 = phi i32 [ 1, %19 ], [ 0, %23 ], [ 0, %12 ]
%29 = phi i32 [ 0, %19 ], [ 1, %23 ], [ 0, %12 ]
%30 = phi i32 [ %14, %19 ], [ 1, %23 ], [ 0, %12 ]
%31 = add nuw i64 %17, 1
%32 = icmp eq i64 %31, %4
br i1 %32, label %36, label %33
33: ; preds = %27
%34 = getelementptr inbounds i8, i8* %0, i64 %31
%35 = load i8, i8* %34
br label %12
36: ; preds = %27
%37 = icmp ugt i64 %4, 1
br i1 %37, label %38, label %58
38: ; preds = %36
%39 = add i64 %4, -1
%40 = getelementptr inbounds i8, i8* %0, i64 %39
%41 = load i8, i8* %40
%42 = icmp eq i8 %41, 47
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %58
45: ; preds = %38
%46 = add i64 %4, -2
%47 = getelementptr inbounds i8, i8* %0, i64 %46
%48 = load i8, i8* %47
%49 = icmp eq i8 %48, 47
%50 = icmp eq i8 %41, 46
%51 = select i1 %49, i1 %50, i1 false
br i1 %51, label %52, label %58
52: ; preds = %45
%53 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.7, i64 0, i64 0))
br label %58
54: ; preds = %23, %21, %19, %9
%55 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @str.10, i64 0, i64 0), %9 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @str.9, i64 0, i64 0), %19 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @str.8, i64 0, i64 0), %21 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @str.7, i64 0, i64 0), %23 ]
%56 = tail call i32 @puts(i8* dereferenceable(1) %55)
%57 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i8* %0)
br label %58
58: ; preds = %45, %36, %1, %54, %52, %43, %7
%59 = phi i32 [ -1, %7 ], [ -1, %54 ], [ -1, %43 ], [ -1, %52 ], [ -1, %1 ], [ 0, %36 ], [ 0, %45 ]
ret i32 %59
}