#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 *realpath(char const *, char *);
int /*<<< orphan*/ stderr;
char *strchrnul(char const *, char);
scalar_t__ strncmp(char const *, char const *, int);
int allowed_match(const char *path, const char *okpath) {
  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 (*okpath) {
    const char *end = strchrnul(okpath, ':');
    if (strncmp(okpath, resolved, end - okpath) == 0)
      return 1;
    okpath = end;
    while (*okpath == ':')
      ++okpath;
  }
  fprintf(stderr, "Access to \"%s\" denied by gcc-explorer policy\n", path);
  errno = EACCES;
  return 0;
}


x86 assembly (clang, -O0):
<allowed_match>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e99(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe71(%rip),%rsi
mov    $0xa,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11a8 <allowed_match+0x68>
jmp    11dc <allowed_match+0x9c>
mov    -0x48(%rbp),%rsi
mov    -0x10(%rbp),%rdi
call   1060 <realpath@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d7 <allowed_match+0x97>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    129e <allowed_match+0x15e>
jmp    11dc <allowed_match+0x9c>
jmp    11e1 <allowed_match+0xa1>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1265 <allowed_match+0x125>
mov    -0x18(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchrnul@plt>
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1237 <allowed_match+0xf7>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    129e <allowed_match+0x15e>
mov    -0x40(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    1260 <allowed_match+0x120>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    123f <allowed_match+0xff>
jmp    11e1 <allowed_match+0xa1>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd92(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%ecx
mov    0x2d3a(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%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   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e98(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    0xe8e(%rip),%rsi
mov    $0xa,%edx
call   1030 <strncmp@plt>
mov    %r14,%r15
test   %rax,%rax
je     1197 <allowed_match+0x57>
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <realpath@plt>
mov    %rax,%r15
test   %rax,%rax
je     120a <allowed_match+0xca>
cmpb   $0x0,(%rbx)
je     11e0 <allowed_match+0xa0>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1040 <strchrnul@plt>
mov    %rax,%r12
mov    %r12d,%edx
sub    %ebx,%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strncmp@plt>
test   %rax,%rax
je     1219 <allowed_match+0xd9>
add    $0xffffffffffffffff,%r12
mov    %r12,%rbx
nopl   0x0(%rax,%rax,1)
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
cmp    $0x3a,%al
je     11d0 <allowed_match+0x90>
test   %al,%al
jne    119c <allowed_match+0x5c>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    0x2dc0(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%eax
jmp    120c <allowed_match+0xcc>

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

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [11 x i8] c"/proc/self\00"
@stderr = global i32 0
@.str.1 = [46 x i8] c"Access to \22%s\22 denied by gcc-explorer policy\0A\00"
@EACCES = global i32 0
@errno = global i32 0
define i32 @allowed_match(i8* %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 10)
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = call i8* @realpath(i8* %0, i8* %5)
%10 = icmp eq i8* %9, null
br i1 %10, label %32, label %11
11: ; preds = %8, %2
%12 = phi i8* [ %9, %8 ], [ %0, %2 ]
%13 = load i8, i8* %1
%14 = icmp eq i8 %13, 0
br i1 %14, label %28, label %15
15: ; preds = %24, %11
%16 = phi i8* [ %1, %11 ], [ %25, %24 ]
%17 = call i8* @strchrnul(i8* %16, i8 signext 58)
%18 = ptrtoint i8* %17 to i64
%19 = ptrtoint i8* %16 to i64
%20 = sub i64 %18, %19
%21 = trunc i64 %20 to i32
%22 = call i64 @strncmp(i8* %16, i8* %12, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %32, label %24
24: ; preds = %15, %24
%25 = phi i8* [ %27, %24 ], [ %17, %15 ]
%26 = load i8, i8* %25
%27 = getelementptr inbounds i8, i8* %25, i64 1
switch i8 %26, label %15 [
i8 58, label %24
i8 0, label %28
]
28: ; preds = %24, %11
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%31 = load i32, i32* @EACCES
store i32 %31, i32* @errno
br label %32
32: ; preds = %15, %8, %28
%33 = phi i32 [ 0, %28 ], [ 0, %8 ], [ 1, %15 ]
ret i32 %33
}