#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*/ mode_t;

/* Variables and functions */
int /*<<< orphan*/ EPERM;
int O_ACCMODE;
int O_RDONLY;
int /*<<< orphan*/ errno;
scalar_t__ is_licensed_prog();
int /*<<< orphan*/ is_protected_file(char const *);
int real_open(char const *, int, int /*<<< orphan*/);
int evil_open(const char *path, int flags, mode_t mode) {
  int ret = 0;
  if (is_licensed_prog())
    goto green;
  if (!is_protected_file(path))
    goto green;
  if ((flags & O_ACCMODE) != O_RDONLY)
    goto red;
green:
  ret = real_open(path, flags, mode);
  return ret;
red:
  errno = EPERM;
  return -1;
}


x86 assembly (clang, -O0):
<evil_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <is_licensed_prog@plt>
cmp    $0x0,%rax
je     115f <evil_open+0x2f>
jmp    119b <evil_open+0x6b>
mov    -0x10(%rbp),%rdi
call   1050 <is_protected_file@plt>
cmp    $0x0,%eax
jne    1176 <evil_open+0x46>
jmp    119b <evil_open+0x6b>
mov    -0x14(%rbp),%eax
mov    0x2e70(%rip),%rcx
and    (%rcx),%eax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%eax
je     1196 <evil_open+0x66>
jmp    11b8 <evil_open+0x88>
jmp    119b <evil_open+0x6b>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1030 <real_open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d1 <evil_open+0xa1>
mov    0x2e01(%rip),%rax
mov    (%rax),%ecx
mov    0x2e08(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evil_open>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <is_licensed_prog@plt>
test   %rax,%rax
je     1159 <evil_open+0x29>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <real_open@plt>
mov    %rbx,%rdi
call   1050 <is_protected_file@plt>
test   %eax,%eax
je     1148 <evil_open+0x18>
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
and    %ebp,%eax
mov    0x2e69(%rip),%rcx
cmp    (%rcx),%eax
je     1148 <evil_open+0x18>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    0x2e45(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_ACCMODE = global i32 0
@O_RDONLY = global i32 0
@EPERM = global i32 0
@errno = global i32 0
define i32 @evil_open(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
%9 = call i64 (...) @is_licensed_prog()
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %3
br label %25
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = call i32 @is_protected_file(i8* %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %12
br label %25
17: ; preds = %12
%18 = load i32, i32* %6
%19 = load i32, i32* @O_ACCMODE
%20 = and i32 %18, %19
%21 = load i32, i32* @O_RDONLY
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %17
br label %31
24: ; preds = %17
br label %25
25: ; preds = %24, %16, %11
%26 = load i8*, i8** %5
%27 = load i32, i32* %6
%28 = load i32, i32* %7
%29 = call i32 @real_open(i8* %26, i32 %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
store i32 %30, i32* %4
br label %33
31: ; preds = %23
%32 = load i32, i32* @EPERM
store i32 %32, i32* @errno
store i32 -1, i32* %4
br label %33
33: ; preds = %31, %25
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@O_ACCMODE = global i32 0
@O_RDONLY = global i32 0
@EPERM = global i32 0
@errno = global i32 0
define i32 @evil_open(i8* %0, i32 %1, i32 %2) {
%4 = tail call i64 (...) @is_licensed_prog()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %14
6: ; preds = %3
%7 = tail call i32 @is_protected_file(i8* %0)
%8 = icmp eq i32 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %6
%10 = load i32, i32* @O_ACCMODE
%11 = and i32 %10, %1
%12 = load i32, i32* @O_RDONLY
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %9, %6, %3
%15 = tail call i32 @real_open(i8* %0, i32 %1, i32 %2)
br label %18
16: ; preds = %9
%17 = load i32, i32* @EPERM
store i32 %17, i32* @errno
br label %18
18: ; preds = %16, %14
%19 = phi i32 [ %15, %14 ], [ -1, %16 ]
ret i32 %19
}