#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*/ EINVAL;
int allowed_match(char const *, char const *, char const *, int);
char *dirname(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *getenv(char const *);
char *strdup(char const *);
int allowed_env(const char *pathname, const char *envvar) {
  const char *okpath = getenv(envvar);
  if (okpath == NULL) {
    errno = EINVAL;
    return 0;
  }
  const char *denypath = getenv("DENIED");
  if (denypath == NULL)
    denypath = "";
  int debug = getenv("PRELOAD_DEBUG") ? 1 : 0;
  if (allowed_match(pathname, okpath, denypath, debug))
    return 1;
  char *dirpathbuf = strdup(pathname);
  char *dirpath = dirname(dirpathbuf);
  int dir_ok = allowed_match(dirpath, okpath, denypath, debug);
  free(dirpathbuf);
  return dir_ok;
}


x86 assembly (clang, -O0):
<allowed_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1196 <allowed_env+0x46>
mov    0x2e59(%rip),%rax
mov    (%rax),%ecx
mov    0x2e58(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1245 <allowed_env+0xf5>
lea    0xe63(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bc <allowed_env+0x6c>
lea    0xe4e(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe44(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1060 <allowed_match@plt>
cmp    $0x0,%eax
je     1205 <allowed_env+0xb5>
movl   $0x1,-0x4(%rbp)
jmp    1245 <allowed_env+0xf5>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <dirname@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1060 <allowed_match@plt>
mov    %eax,-0x44(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allowed_env>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11e5 <allowed_env+0x95>
mov    %rax,%r15
lea    0xe8f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe86(%rip),%rbx
cmovne %rax,%rbx
lea    0xe7c(%rip),%rdi
call   1030 <getenv@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1060 <allowed_match@plt>
mov    %eax,%ecx
mov    $0x1,%eax
test   %ecx,%ecx
jne    11f9 <allowed_env+0xa9>
mov    %r14,%rdi
call   1070 <strdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <dirname@plt>
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1060 <allowed_match@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <free@plt>
mov    %ebx,%eax
jmp    11f9 <allowed_env+0xa9>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    0x2deb(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [7 x i8] c"DENIED\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [14 x i8] c"PRELOAD_DEBUG\00"
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 i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i8*, i8** %5
%13 = call i8* @getenv(i8* %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32, i32* @EINVAL
store i32 %17, i32* @errno
store i32 0, i32* %3
br label %48
18: ; preds = %2
%19 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %18
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %23
23: ; preds = %22, %18
%24 = call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp ne i8* %24, null
%26 = zext i1 %25 to i64
%27 = select i1 %25, i32 1, i32 0
store i32 %27, i32* %8
%28 = load i8*, i8** %4
%29 = load i8*, i8** %6
%30 = load i8*, i8** %7
%31 = load i32, i32* %8
%32 = call i32 @allowed_match(i8* %28, i8* %29, i8* %30, i32 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %23
store i32 1, i32* %3
br label %48
35: ; preds = %23
%36 = load i8*, i8** %4
%37 = call i8* @strdup(i8* %36)
store i8* %37, i8** %9
%38 = load i8*, i8** %9
%39 = call i8* @dirname(i8* %38)
store i8* %39, i8** %10
%40 = load i8*, i8** %10
%41 = load i8*, i8** %6
%42 = load i8*, i8** %7
%43 = load i32, i32* %8
%44 = call i32 @allowed_match(i8* %40, i8* %41, i8* %42, i32 %43)
store i32 %44, i32* %11
%45 = load i8*, i8** %9
%46 = call i32 @free(i8* %45)
%47 = load i32, i32* %11
store i32 %47, i32* %3
br label %48
48: ; preds = %35, %34, %16
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [7 x i8] c"DENIED\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [14 x i8] c"PRELOAD_DEBUG\00"
define i32 @allowed_env(i8* %0, i8* nocapture %1) {
%3 = tail call i8* @getenv(i8* %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = load i32, i32* @EINVAL
store i32 %6, i32* @errno
br label %21
7: ; preds = %2
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i8* %8, null
%10 = select i1 %9, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %8
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp ne i8* %11, null
%13 = zext i1 %12 to i32
%14 = tail call i32 @allowed_match(i8* %0, i8* %3, i8* %10, i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %7
%17 = tail call i8* @strdup(i8* %0)
%18 = tail call i8* @dirname(i8* %17)
%19 = tail call i32 @allowed_match(i8* %18, i8* %3, i8* %10, i32 %13)
%20 = tail call i32 @free(i8* %17)
br label %21
21: ; preds = %16, %7, %5
%22 = phi i32 [ 0, %5 ], [ %19, %16 ], [ 1, %7 ]
ret i32 %22
}