#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 *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;
  }
  if (allowed_match(pathname, okpath))
    return 1;
  char *dirpathbuf = strdup(pathname);
  char *dirpath = dirname(dirpathbuf);
  int dir_ok = allowed_match(dirpath, okpath);
  free(dirpathbuf);
  return dir_ok;
}


x86 assembly (clang, -O0):
<allowed_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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    11f1 <allowed_env+0xa1>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <allowed_match@plt>
cmp    $0x0,%eax
je     11b8 <allowed_env+0x68>
movl   $0x1,-0x4(%rbp)
jmp    11f1 <allowed_env+0xa1>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <dirname@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <allowed_match@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allowed_env>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11a9 <allowed_env+0x59>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1060 <allowed_match@plt>
mov    %eax,%ecx
mov    $0x1,%eax
test   %ecx,%ecx
jne    11bd <allowed_env+0x6d>
mov    %r14,%rdi
call   1070 <strdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <dirname@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <allowed_match@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <free@plt>
mov    %ebx,%eax
jmp    11bd <allowed_env+0x6d>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    0x2e27(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = 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 i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = call i8* @getenv(i8* %10)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* @EINVAL
store i32 %15, i32* @errno
store i32 0, i32* %3
br label %33
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = load i8*, i8** %6
%19 = call i32 @allowed_match(i8* %17, i8* %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 1, i32* %3
br label %33
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = call i8* @strdup(i8* %23)
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = call i8* @dirname(i8* %25)
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = load i8*, i8** %6
%29 = call i32 @allowed_match(i8* %27, i8* %28)
store i32 %29, i32* %9
%30 = load i8*, i8** %7
%31 = call i32 @free(i8* %30)
%32 = load i32, i32* %9
store i32 %32, i32* %3
br label %33
33: ; preds = %22, %21, %14
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
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 %15
7: ; preds = %2
%8 = tail call i32 @allowed_match(i8* %0, i8* %3)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = tail call i8* @strdup(i8* %0)
%12 = tail call i8* @dirname(i8* %11)
%13 = tail call i32 @allowed_match(i8* %12, i8* %3)
%14 = tail call i32 @free(i8* %11)
br label %15
15: ; preds = %7, %10, %5
%16 = phi i32 [ 0, %5 ], [ %13, %10 ], [ 1, %7 ]
ret i32 %16
}