#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_atfile(int, char const *);
int real_openat(int, char const *, int, int /*<<< orphan*/);
int evil_openat(int dirfd, const char *path, int flags, mode_t mode) {
  int ret = 0;
  if (is_licensed_prog())
    goto green;
  if (!is_protected_atfile(dirfd, path))
    goto green;
  if ((flags & O_ACCMODE) != O_RDONLY)
    goto red;
green:
  ret = real_openat(dirfd, path, flags, mode);
  return ret;
red:
  errno = EPERM;
  return -1;
}


x86 assembly (clang, -O0):
<evil_openat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <is_licensed_prog@plt>
cmp    $0x0,%rax
je     1162 <evil_openat+0x32>
jmp    11a1 <evil_openat+0x71>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <is_protected_atfile@plt>
cmp    $0x0,%eax
jne    117c <evil_openat+0x4c>
jmp    11a1 <evil_openat+0x71>
mov    -0x14(%rbp),%eax
mov    0x2e6a(%rip),%rcx
and    (%rcx),%eax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%eax
je     119c <evil_openat+0x6c>
jmp    11c1 <evil_openat+0x91>
jmp    11a1 <evil_openat+0x71>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1040 <real_openat@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11da <evil_openat+0xaa>
mov    0x2df8(%rip),%rax
mov    (%rax),%ecx
mov    0x2dff(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evil_openat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1050 <is_licensed_prog@plt>
test   %rax,%rax
je     1168 <evil_openat+0x38>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
mov    %r14d,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <real_openat@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <is_protected_atfile@plt>
test   %eax,%eax
je     114e <evil_openat+0x1e>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
and    %r15d,%eax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%eax
je     114e <evil_openat+0x1e>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    0x2e33(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
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_openat(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %10
%11 = call i64 (...) @is_licensed_prog()
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %4
br label %28
14: ; preds = %4
%15 = load i32, i32* %6
%16 = load i8*, i8** %7
%17 = call i32 @is_protected_atfile(i32 %15, i8* %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %14
br label %28
20: ; preds = %14
%21 = load i32, i32* %8
%22 = load i32, i32* @O_ACCMODE
%23 = and i32 %21, %22
%24 = load i32, i32* @O_RDONLY
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %20
br label %35
27: ; preds = %20
br label %28
28: ; preds = %27, %19, %13
%29 = load i32, i32* %6
%30 = load i8*, i8** %7
%31 = load i32, i32* %8
%32 = load i32, i32* %9
%33 = call i32 @real_openat(i32 %29, i8* %30, i32 %31, i32 %32)
store i32 %33, i32* %10
%34 = load i32, i32* %10
store i32 %34, i32* %5
br label %37
35: ; preds = %26
%36 = load i32, i32* @EPERM
store i32 %36, i32* @errno
store i32 -1, i32* %5
br label %37
37: ; preds = %35, %28
%38 = load i32, i32* %5
ret i32 %38
}

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