#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 /*<<< orphan*/ EINVAL;
int PATH_MAX;
char *dirname(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *getenv(char const *);
char *realpath(char *, char *);
char *strchrnul(char const *, char);
char *strdup(char const *);
scalar_t__ strncmp(char const *, char *, int);
int allowed_env(const char *pathname, const char *envvar) {
  char *dirpathbuf = strdup(pathname);
  char *dirpath = dirname(dirpathbuf);
  char resolvedBuf[PATH_MAX];
  char *resolved = realpath(dirpath, resolvedBuf);
  free(dirpathbuf);
  if (resolved == NULL) {
    return 0;
  }
  const char *okpath = getenv(envvar);
  if (okpath == NULL) {
    errno = EINVAL;
    return 0;
  }
  while (*okpath) {
    const char *end = strchrnul(okpath, ':');
    if (strncmp(okpath, resolved, end - okpath) == 0)
      return 1;
    okpath = end;
    while (*okpath == ':')
      ++okpath;
  }
  errno = EACCES;
  return 0;
}


x86 assembly (clang, -O0):
<allowed_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <dirname@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <realpath@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x40(%rbp)
jne    11f6 <allowed_env+0x86>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12dc <allowed_env+0x16c>
mov    -0x18(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1233 <allowed_env+0xc3>
mov    0x2db3(%rip),%rax
mov    (%rax),%ecx
mov    0x2db2(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12dc <allowed_env+0x16c>
jmp    1238 <allowed_env+0xc8>
mov    -0x50(%rbp),%rax
cmpb   $0x0,(%rax)
je     12bc <allowed_env+0x14c>
mov    -0x50(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchrnul@plt>
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x58(%rbp),%rax
mov    -0x50(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    128e <allowed_env+0x11e>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12dc <allowed_env+0x16c>
mov    -0x58(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    12b7 <allowed_env+0x147>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    1296 <allowed_env+0x126>
jmp    1238 <allowed_env+0xc8>
mov    0x2d1d(%rip),%rax
mov    (%rax),%ecx
mov    0x2d04(%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_env>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
call   1090 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <dirname@plt>
mov    0x2e58(%rip),%rcx
mov    (%rcx),%ecx
mov    %rsp,%rsi
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rsi
mov    %rsi,%rsp
mov    %rax,%rdi
call   1080 <realpath@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <free@plt>
xor    %r14d,%r14d
test   %r15,%r15
je     1236 <allowed_env+0xc6>
mov    %r12,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1222 <allowed_env+0xb2>
mov    %rax,%rbx
mov    0x2e03(%rip),%r13
cmpb   $0x0,(%rax)
je     1229 <allowed_env+0xb9>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1060 <strchrnul@plt>
mov    %rax,%r12
mov    %r12d,%edx
sub    %ebx,%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strncmp@plt>
test   %rax,%rax
je     1248 <allowed_env+0xd8>
add    $0xffffffffffffffff,%r12
mov    %r12,%rbx
xchg   %ax,%ax
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
cmp    $0x3a,%al
je     1210 <allowed_env+0xa0>
test   %al,%al
jne    11e2 <allowed_env+0x72>
jmp    1229 <allowed_env+0xb9>
mov    0x2d9f(%rip),%r13
mov    0x0(%r13),%eax
mov    0x2d9c(%rip),%rcx
mov    %eax,(%rcx)
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%r14d
jmp    1236 <allowed_env+0xc6>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@EACCES = global i32 0
define i32 @allowed_env(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%14 = load i8*, i8** %4
%15 = call i8* @strdup(i8* %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = call i8* @dirname(i8* %16)
store i8* %17, i8** %7
%18 = load i32, i32* @PATH_MAX
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %8
%21 = alloca i8, i64 %19
store i64 %19, i64* %9
%22 = load i8*, i8** %7
%23 = call i8* @realpath(i8* %22, i8* %21)
store i8* %23, i8** %10
%24 = load i8*, i8** %6
%25 = call i32 @free(i8* %24)
%26 = load i8*, i8** %10
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %29
28: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %11
br label %68
29: ; preds = %2
%30 = load i8*, i8** %5
%31 = call i8* @getenv(i8* %30)
store i8* %31, i8** %12
%32 = load i8*, i8** %12
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = load i32, i32* @EINVAL
store i32 %35, i32* @errno
store i32 0, i32* %3
store i32 1, i32* %11
br label %68
36: ; preds = %29
br label %37
37: ; preds = %65, %36
%38 = load i8*, i8** %12
%39 = load i8, i8* %38
%40 = icmp ne i8 %39, 0
br i1 %40, label %41, label %66
41: ; preds = %37
%42 = load i8*, i8** %12
%43 = call i8* @strchrnul(i8* %42, i8 signext 58)
store i8* %43, i8** %13
%44 = load i8*, i8** %12
%45 = load i8*, i8** %10
%46 = load i8*, i8** %13
%47 = load i8*, i8** %12
%48 = ptrtoint i8* %46 to i64
%49 = ptrtoint i8* %47 to i64
%50 = sub i64 %48, %49
%51 = trunc i64 %50 to i32
%52 = call i64 @strncmp(i8* %44, i8* %45, i32 %51)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %41
store i32 1, i32* %3
store i32 1, i32* %11
br label %68
55: ; preds = %41
%56 = load i8*, i8** %13
store i8* %56, i8** %12
br label %57
57: ; preds = %62, %55
%58 = load i8*, i8** %12
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 58
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = load i8*, i8** %12
%64 = getelementptr inbounds i8, i8* %63, i32 1
store i8* %64, i8** %12
br label %57
65: ; preds = %57
br label %37
66: ; preds = %37
%67 = load i32, i32* @EACCES
store i32 %67, i32* @errno
store i32 0, i32* %3
store i32 1, i32* %11
br label %68
68: ; preds = %66, %54, %34, %28
%69 = load i8*, i8** %8
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@EACCES = global i32 0
define i32 @allowed_env(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i8* @strdup(i8* %0)
%4 = tail call i8* @dirname(i8* %3)
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = call i8* @realpath(i8* %4, i8* %7)
%9 = call i32 @free(i8* %3)
%10 = icmp eq i8* %8, null
br i1 %10, label %33, label %11
11: ; preds = %2
%12 = call i8* @getenv(i8* %1)
%13 = icmp eq i8* %12, null
br i1 %13, label %30, label %14
14: ; preds = %11
%15 = load i8, i8* %12
%16 = icmp eq i8 %15, 0
br i1 %16, label %30, label %17
17: ; preds = %26, %14
%18 = phi i8* [ %12, %14 ], [ %27, %26 ]
%19 = call i8* @strchrnul(i8* %18, i8 signext 58)
%20 = ptrtoint i8* %19 to i64
%21 = ptrtoint i8* %18 to i64
%22 = sub i64 %20, %21
%23 = trunc i64 %22 to i32
%24 = call i64 @strncmp(i8* %18, i8* %8, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %33, label %26
26: ; preds = %17, %26
%27 = phi i8* [ %29, %26 ], [ %19, %17 ]
%28 = load i8, i8* %27
%29 = getelementptr inbounds i8, i8* %27, i64 1
switch i8 %28, label %17 [
i8 58, label %26
i8 0, label %30
]
30: ; preds = %26, %14, %11
%31 = phi i32* [ @EINVAL, %11 ], [ @EACCES, %14 ], [ @EACCES, %26 ]
%32 = load i32, i32* %31
store i32 %32, i32* @errno
br label %33
33: ; preds = %17, %30, %2
%34 = phi i32 [ 0, %2 ], [ 0, %30 ], [ 1, %17 ]
ret i32 %34
}