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

/* Variables and functions */
int AT_SYMLINK_NOFOLLOW;
int /*<<< orphan*/ EPERM;
int /*<<< orphan*/ errno;
scalar_t__ is_licensed_prog();
scalar_t__ is_protected_atentry(int, char const *);
scalar_t__ is_protected_atfile(int, char const *);
int real_fchownat(int, char const *, int /*<<< orphan*/, int /*<<< orphan*/,
                  int);
int evil_fchownat(int dirfd, const char *path, uid_t uid, gid_t gid,
                  int flags) {
  int ret = 0;
  if (is_licensed_prog())
    goto green;
  if (flags & AT_SYMLINK_NOFOLLOW) {
    if (is_protected_atentry(dirfd, path))
      goto red;
  } else {
    if (is_protected_atfile(dirfd, path))
      goto red;
  }
green:
  ret = real_fchownat(dirfd, path, uid, gid, flags);
  return ret;
red:
  errno = EPERM;
  return -1;
}


x86 assembly (clang, -O0):
<evil_fchownat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1040 <is_licensed_prog@plt>
cmp    $0x0,%rax
je     1176 <evil_fchownat+0x36>
jmp    11d0 <evil_fchownat+0x90>
mov    -0x1c(%rbp),%eax
mov    0x2e58(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11ab <evil_fchownat+0x6b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <is_protected_atentry@plt>
cmp    $0x0,%rax
je     11a6 <evil_fchownat+0x66>
jmp    11f4 <evil_fchownat+0xb4>
jmp    11cb <evil_fchownat+0x8b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <is_protected_atfile@plt>
cmp    $0x0,%rax
je     11c6 <evil_fchownat+0x86>
jmp    11f4 <evil_fchownat+0xb4>
jmp    11cb <evil_fchownat+0x8b>
jmp    11d0 <evil_fchownat+0x90>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
call   1060 <real_fchownat@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120d <evil_fchownat+0xcd>
mov    0x2dcd(%rip),%rax
mov    (%rax),%ecx
mov    0x2ddc(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evil_fchownat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r12d
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %edi,%ebx
xor    %eax,%eax
call   1040 <is_licensed_prog@plt>
test   %rax,%rax
je     117d <evil_fchownat+0x3d>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
mov    %r14d,%ecx
mov    %r12d,%r8d
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <real_fchownat@plt>
mov    0x2e54(%rip),%rax
test   %r12d,(%rax)
je     119a <evil_fchownat+0x5a>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <is_protected_atentry@plt>
test   %rax,%rax
jne    11a9 <evil_fchownat+0x69>
jmp    1162 <evil_fchownat+0x22>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <is_protected_atfile@plt>
test   %rax,%rax
je     1162 <evil_fchownat+0x22>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    0x2e27(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@AT_SYMLINK_NOFOLLOW = global i32 0
@EPERM = global i32 0
@errno = global i32 0
define i32 @evil_fchownat(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 0, i32* %12
%13 = call i64 (...) @is_licensed_prog()
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %5
br label %36
16: ; preds = %5
%17 = load i32, i32* %11
%18 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%19 = and i32 %17, %18
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %16
%22 = load i32, i32* %7
%23 = load i8*, i8** %8
%24 = call i64 @is_protected_atentry(i32 %22, i8* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
br label %44
27: ; preds = %21
br label %35
28: ; preds = %16
%29 = load i32, i32* %7
%30 = load i8*, i8** %8
%31 = call i64 @is_protected_atfile(i32 %29, i8* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28
br label %44
34: ; preds = %28
br label %35
35: ; preds = %34, %27
br label %36
36: ; preds = %35, %15
%37 = load i32, i32* %7
%38 = load i8*, i8** %8
%39 = load i32, i32* %9
%40 = load i32, i32* %10
%41 = load i32, i32* %11
%42 = call i32 @real_fchownat(i32 %37, i8* %38, i32 %39, i32 %40, i32 %41)
store i32 %42, i32* %12
%43 = load i32, i32* %12
store i32 %43, i32* %6
br label %46
44: ; preds = %33, %26
%45 = load i32, i32* @EPERM
store i32 %45, i32* @errno
store i32 -1, i32* %6
br label %46
46: ; preds = %44, %36
%47 = load i32, i32* %6
ret i32 %47
}

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