#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*/ EPERM;
int /*<<< orphan*/ debug(char *);
int /*<<< orphan*/ errno;
scalar_t__ fileinpath(char const *, char *);
scalar_t__ getenv(char *);
int sys_open64(char const *, int, unsigned short);
int open64(const char *pathname, int flags, unsigned short mode) {
  if (fileinpath(pathname, "SUDO_ALLOWED") || getenv("SYS_OPEN")) {
    return sys_open64(pathname, flags, mode);
  }
  debug("open64: ");
  errno = EPERM;
  return -1;
}


x86 assembly (clang, -O0):
<open64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dx,%ax
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %ax,-0x16(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1050 <fileinpath@plt>
cmp    $0x0,%rax
jne    1186 <open64+0x46>
lea    0xe96(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     119e <open64+0x5e>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
movzwl -0x16(%rbp),%edx
call   1060 <sys_open64@plt>
mov    %eax,-0x4(%rbp)
jmp    11c3 <open64+0x83>
lea    0xe71(%rip),%rdi
call   1040 <debug@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e26(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open64>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xead(%rip),%rsi
call   1050 <fileinpath@plt>
test   %rax,%rax
jne    116e <open64+0x2e>
lea    0xea9(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1180 <open64+0x40>
movzwl %bp,%edx
mov    %rbx,%rdi
mov    %r14d,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <sys_open64@plt>
lea    0xe8f(%rip),%rdi
call   1040 <debug@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    0x2e44(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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