#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*/ EACCES;
int PATH_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
char *my_strchrnul(char const *, char);
char *realpath(char const *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strncmp(char const *, char const *, int);
int allowed_match(const char *path, const char *okpath, const char *denypath,
                  int debug) {
  char resolvedBuf[PATH_MAX];
  const char *resolved = path;
  if (!strncmp(resolved, "/proc/self", 10)) {
  } else {
    resolved = realpath(path, resolvedBuf);
    if (resolved == NULL) {
      return 0;
    }
  }
  while (*denypath) {
    const char *end = my_strchrnul(denypath, ':');
    if (strncmp(denypath, resolved, end - denypath) == 0)
      goto deny;
    denypath = end;
    while (*denypath == ':')
      ++denypath;
  }
  while (*okpath) {
    const char *end = my_strchrnul(okpath, ':');
    if (strncmp(okpath, resolved, end - okpath) == 0)
      return 1;
    okpath = end;
    while (*okpath == ':')
      ++okpath;
  }
deny:
  if (debug) {
    fprintf(stderr, "Access to \"%s\" denied\n", path);
  }
  errno = EACCES;
  return 0;
}


x86 assembly (clang, -O0):
<allowed_match>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    0xe6a(%rip),%rsi
mov    $0xa,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11af <allowed_match+0x6f>
jmp    11e3 <allowed_match+0xa3>
mov    -0x60(%rbp),%rsi
mov    -0x10(%rbp),%rdi
call   1060 <realpath@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11de <allowed_match+0x9e>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    132f <allowed_match+0x1ef>
jmp    11e3 <allowed_match+0xa3>
jmp    11e8 <allowed_match+0xa8>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     125e <allowed_match+0x11e>
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <my_strchrnul@plt>
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x50(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1230 <allowed_match+0xf0>
jmp    12ec <allowed_match+0x1ac>
mov    -0x50(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    1259 <allowed_match+0x119>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1238 <allowed_match+0xf8>
jmp    11e8 <allowed_match+0xa8>
jmp    1263 <allowed_match+0x123>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     12e7 <allowed_match+0x1a7>
mov    -0x18(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <my_strchrnul@plt>
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x58(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12b9 <allowed_match+0x179>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    132f <allowed_match+0x1ef>
mov    -0x58(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    12e2 <allowed_match+0x1a2>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    12c1 <allowed_match+0x181>
jmp    1263 <allowed_match+0x123>
jmp    12ec <allowed_match+0x1ac>
cmpl   $0x0,-0x24(%rbp)
je     130f <allowed_match+0x1cf>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd01(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2cca(%rip),%rax
mov    (%rax),%ecx
mov    0x2ca9(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allowed_match>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,-0x2c(%rbp)
mov    %rdx,%rbx
mov    %rsi,%r13
mov    %rdi,%r15
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    0xe85(%rip),%rsi
mov    $0xa,%edx
call   1030 <strncmp@plt>
mov    %r15,%r12
test   %rax,%rax
je     11a4 <allowed_match+0x64>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <realpath@plt>
mov    %rax,%r12
test   %rax,%rax
je     1271 <allowed_match+0x131>
cmpb   $0x0,(%rbx)
je     11f0 <allowed_match+0xb0>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1040 <my_strchrnul@plt>
mov    %rax,%r14
mov    %r14d,%edx
sub    %ebx,%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strncmp@plt>
test   %rax,%rax
je     1241 <allowed_match+0x101>
add    $0xffffffffffffffff,%r14
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
cmp    $0x3a,%al
je     11e0 <allowed_match+0xa0>
test   %al,%al
jne    11a9 <allowed_match+0x69>
cmpb   $0x0,0x0(%r13)
je     1241 <allowed_match+0x101>
mov    %r13,%rdi
mov    $0x3a,%esi
call   1040 <my_strchrnul@plt>
mov    %rax,%rbx
mov    %ebx,%edx
sub    %r13d,%edx
mov    %r13,%rdi
mov    %r12,%rsi
call   1030 <strncmp@plt>
test   %rax,%rax
je     1282 <allowed_match+0x142>
add    $0xffffffffffffffff,%rbx
mov    %rbx,%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl 0x1(%r13),%eax
add    $0x1,%r13
cmp    $0x3a,%al
je     1230 <allowed_match+0xf0>
test   %al,%al
jne    11f7 <allowed_match+0xb7>
cmpl   $0x0,-0x2c(%rbp)
je     125f <allowed_match+0x11f>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xdb4(%rip),%rsi
mov    %r15,%rdx
call   1050 <fprintf@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    0x2d59(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%eax
jmp    1273 <allowed_match+0x133>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [11 x i8] c"/proc/self\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Access to \22%s\22 denied\0A\00"
@EACCES = global i32 0
@errno = global i32 0
define i32 @allowed_match(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%16 = load i32, i32* @PATH_MAX
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %10
%19 = alloca i8, i64 %17
store i64 %17, i64* %11
%20 = load i8*, i8** %6
store i8* %20, i8** %12
%21 = load i8*, i8** %12
%22 = call i64 @strncmp(i8* %21, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 10)
%23 = icmp ne i64 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %4
br label %32
25: ; preds = %4
%26 = load i8*, i8** %6
%27 = call i8* @realpath(i8* %26, i8* %19)
store i8* %27, i8** %12
%28 = load i8*, i8** %12
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %31
30: ; preds = %25
store i32 0, i32* %5
store i32 1, i32* %13
br label %102
31: ; preds = %25
br label %32
32: ; preds = %31, %24
br label %33
33: ; preds = %61, %32
%34 = load i8*, i8** %8
%35 = load i8, i8* %34
%36 = icmp ne i8 %35, 0
br i1 %36, label %37, label %62
37: ; preds = %33
%38 = load i8*, i8** %8
%39 = call i8* @my_strchrnul(i8* %38, i8 signext 58)
store i8* %39, i8** %14
%40 = load i8*, i8** %8
%41 = load i8*, i8** %12
%42 = load i8*, i8** %14
%43 = load i8*, i8** %8
%44 = ptrtoint i8* %42 to i64
%45 = ptrtoint i8* %43 to i64
%46 = sub i64 %44, %45
%47 = trunc i64 %46 to i32
%48 = call i64 @strncmp(i8* %40, i8* %41, i32 %47)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %37
br label %93
51: ; preds = %37
%52 = load i8*, i8** %14
store i8* %52, i8** %8
br label %53
53: ; preds = %58, %51
%54 = load i8*, i8** %8
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 58
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = load i8*, i8** %8
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %8
br label %53
61: ; preds = %53
br label %33
62: ; preds = %33
br label %63
63: ; preds = %91, %62
%64 = load i8*, i8** %7
%65 = load i8, i8* %64
%66 = icmp ne i8 %65, 0
br i1 %66, label %67, label %92
67: ; preds = %63
%68 = load i8*, i8** %7
%69 = call i8* @my_strchrnul(i8* %68, i8 signext 58)
store i8* %69, i8** %15
%70 = load i8*, i8** %7
%71 = load i8*, i8** %12
%72 = load i8*, i8** %15
%73 = load i8*, i8** %7
%74 = ptrtoint i8* %72 to i64
%75 = ptrtoint i8* %73 to i64
%76 = sub i64 %74, %75
%77 = trunc i64 %76 to i32
%78 = call i64 @strncmp(i8* %70, i8* %71, i32 %77)
%79 = icmp eq i64 %78, 0
br i1 %79, label %80, label %81
80: ; preds = %67
store i32 1, i32* %5
store i32 1, i32* %13
br label %102
81: ; preds = %67
%82 = load i8*, i8** %15
store i8* %82, i8** %7
br label %83
83: ; preds = %88, %81
%84 = load i8*, i8** %7
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
%87 = icmp eq i32 %86, 58
br i1 %87, label %88, label %91
88: ; preds = %83
%89 = load i8*, i8** %7
%90 = getelementptr inbounds i8, i8* %89, i32 1
store i8* %90, i8** %7
br label %83
91: ; preds = %83
br label %63
92: ; preds = %63
br label %93
93: ; preds = %92, %50
%94 = load i32, i32* %9
%95 = icmp ne i32 %94, 0
br i1 %95, label %96, label %100
96: ; preds = %93
%97 = load i32, i32* @stderr
%98 = load i8*, i8** %6
%99 = call i32 @fprintf(i32 %97, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %98)
br label %100
100: ; preds = %96, %93
%101 = load i32, i32* @EACCES
store i32 %101, i32* @errno
store i32 0, i32* %5
store i32 1, i32* %13
br label %102
102: ; preds = %100, %80, %30
%103 = load i8*, i8** %10
%104 = load i32, i32* %5
ret i32 %104
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [11 x i8] c"/proc/self\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Access to \22%s\22 denied\0A\00"
@EACCES = global i32 0
@errno = global i32 0
define i32 @allowed_match(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 10)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %4
%11 = call i8* @realpath(i8* %0, i8* %7)
%12 = icmp eq i8* %11, null
br i1 %12, label %53, label %13
13: ; preds = %10, %4
%14 = phi i8* [ %11, %10 ], [ %0, %4 ]
%15 = load i8, i8* %2
%16 = icmp eq i8 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %29, %13
%18 = load i8, i8* %1
%19 = icmp eq i8 %18, 0
br i1 %19, label %46, label %33
20: ; preds = %29, %13
%21 = phi i8* [ %2, %13 ], [ %30, %29 ]
%22 = call i8* @my_strchrnul(i8* %21, i8 signext 58)
%23 = ptrtoint i8* %22 to i64
%24 = ptrtoint i8* %21 to i64
%25 = sub i64 %23, %24
%26 = trunc i64 %25 to i32
%27 = call i64 @strncmp(i8* %21, i8* %14, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %46, label %29
29: ; preds = %20, %29
%30 = phi i8* [ %32, %29 ], [ %22, %20 ]
%31 = load i8, i8* %30
%32 = getelementptr inbounds i8, i8* %30, i64 1
switch i8 %31, label %20 [
i8 58, label %29
i8 0, label %17
]
33: ; preds = %42, %17
%34 = phi i8* [ %1, %17 ], [ %43, %42 ]
%35 = call i8* @my_strchrnul(i8* %34, i8 signext 58)
%36 = ptrtoint i8* %35 to i64
%37 = ptrtoint i8* %34 to i64
%38 = sub i64 %36, %37
%39 = trunc i64 %38 to i32
%40 = call i64 @strncmp(i8* %34, i8* %14, i32 %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %53, label %42
42: ; preds = %33, %42
%43 = phi i8* [ %45, %42 ], [ %35, %33 ]
%44 = load i8, i8* %43
%45 = getelementptr inbounds i8, i8* %43, i64 1
switch i8 %44, label %33 [
i8 58, label %42
i8 0, label %46
]
46: ; preds = %20, %42, %17
%47 = icmp eq i32 %3, 0
br i1 %47, label %51, label %48
48: ; preds = %46
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %51
51: ; preds = %48, %46
%52 = load i32, i32* @EACCES
store i32 %52, i32* @errno
br label %53
53: ; preds = %33, %10, %51
%54 = phi i32 [ 0, %51 ], [ 0, %10 ], [ 1, %33 ]
ret i32 %54
}