#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EPERM;
int /*<<< orphan*/ debug(char *);
int /*<<< orphan*/ errno;
scalar_t__ fileinpath(char const *, char *);
int /*<<< orphan*/ *getenv(char *);
scalar_t__ sys_fopen(char const *, char const *);
FILE *fopen(const char *pathname, const char *mode) {
  if (fileinpath(pathname, "SUDO_ALLOWED") || getenv("SYS_FOPEN") != NULL) {
    return (FILE *)sys_fopen(pathname, mode);
  }
  debug("fopen: ");
  errno = EPERM;
  return NULL;
}


x86 assembly (clang, -O0):
<fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1050 <fileinpath@plt>
cmp    $0x0,%rax
jne    1180 <fopen+0x40>
lea    0xe9c(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     1196 <fopen+0x56>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <sys_fopen@plt>
mov    %rax,-0x8(%rbp)
jmp    11bc <fopen+0x7c>
lea    0xe7a(%rip),%rdi
call   1040 <debug@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2e(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeaf(%rip),%rsi
call   1050 <fileinpath@plt>
test   %rax,%rax
jne    116c <fopen+0x2c>
lea    0xeab(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     117f <fopen+0x3f>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <sys_fopen@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe91(%rip),%rdi
call   1040 <debug@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    0x2e45(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
jmp    1177 <fopen+0x37>

LLVM IR (-O0):
@.str = [13 x i8] c"SUDO_ALLOWED\00"
@.str.1 = [10 x i8] c"SYS_FOPEN\00"
@.str.2 = [8 x i8] c"fopen: \00"
@EPERM = global i32 0
@errno = global i32 0
define i32* @fopen(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%6 = load i8*, i8** %4
%7 = call i64 @fileinpath(i8* %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%8 = icmp ne i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = call i32* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %17
12: ; preds = %9, %2
%13 = load i8*, i8** %4
%14 = load i8*, i8** %5
%15 = call i64 @sys_fopen(i8* %13, i8* %14)
%16 = inttoptr i64 %15 to i32*
store i32* %16, i32** %3
br label %20
17: ; preds = %9
%18 = call i32 @debug(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @EPERM
store i32 %19, i32* @errno
store i32* null, i32** %3
br label %20
20: ; preds = %17, %12
%21 = load i32*, i32** %3
ret i32* %21
}

LLVM IR (-O2):
@.str = [13 x i8] c"SUDO_ALLOWED\00"
@.str.1 = [10 x i8] c"SYS_FOPEN\00"
@.str.2 = [8 x i8] c"fopen: \00"
@EPERM = global i32 0
@errno = global i32 0
define i32* @fopen(i8* %0, i8* %1) {
%3 = tail call i64 @fileinpath(i8* %0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %5, %2
%9 = tail call i64 @sys_fopen(i8* %0, i8* %1)
%10 = inttoptr i64 %9 to i32*
br label %14
11: ; preds = %5
%12 = tail call i32 @debug(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @EPERM
store i32 %13, i32* @errno
br label %14
14: ; preds = %11, %8
%15 = phi i32* [ %10, %8 ], [ null, %11 ]
ret i32* %15
}