#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 /*<<< orphan*/ EPERM;
int /*<<< orphan*/ errno;
scalar_t__ is_licensed_prog();
scalar_t__ is_protected_fd(int);
int real_fchown(int, int /*<<< orphan*/, int /*<<< orphan*/);
int evil_fchown(int fd, uid_t uid, gid_t gid) {
  int ret = 0;
  if (is_licensed_prog())
    goto green;
  if (is_protected_fd(fd))
    goto red;
green:
  ret = real_fchown(fd, uid, gid);
  return ret;
red:
  errno = EPERM;
  return -1;
}


x86 assembly (clang, -O0):
<evil_fchown>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1050 <is_licensed_prog@plt>
cmp    $0x0,%rax
je     115e <evil_fchown+0x2e>
jmp    117a <evil_fchown+0x4a>
mov    -0x8(%rbp),%edi
call   1030 <is_protected_fd@plt>
cmp    $0x0,%rax
je     1175 <evil_fchown+0x45>
jmp    1196 <evil_fchown+0x66>
jmp    117a <evil_fchown+0x4a>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1040 <real_fchown@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11af <evil_fchown+0x7f>
mov    0x2e33(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evil_fchown>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%ebx
xor    %eax,%eax
call   1050 <is_licensed_prog@plt>
test   %rax,%rax
je     1157 <evil_fchown+0x27>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %r14d,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <real_fchown@plt>
mov    %ebx,%edi
call   1030 <is_protected_fd@plt>
test   %rax,%rax
je     1147 <evil_fchown+0x17>
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    0x2e6d(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@EPERM = global i32 0
@errno = global i32 0
define i32 @evil_fchown(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %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 %18
12: ; preds = %3
%13 = load i32, i32* %5
%14 = call i64 @is_protected_fd(i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
br label %24
17: ; preds = %12
br label %18
18: ; preds = %17, %11
%19 = load i32, i32* %5
%20 = load i32, i32* %6
%21 = load i32, i32* %7
%22 = call i32 @real_fchown(i32 %19, i32 %20, i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
store i32 %23, i32* %4
br label %26
24: ; preds = %16
%25 = load i32, i32* @EPERM
store i32 %25, i32* @errno
store i32 -1, i32* %4
br label %26
26: ; preds = %24, %18
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
@EPERM = global i32 0
@errno = global i32 0
define i32 @evil_fchown(i32 %0, i32 %1, i32 %2) {
%4 = tail call i64 (...) @is_licensed_prog()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i64 @is_protected_fd(i32 %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6, %3
%10 = tail call i32 @real_fchown(i32 %0, i32 %1, i32 %2)
br label %13
11: ; preds = %6
%12 = load i32, i32* @EPERM
store i32 %12, i32* @errno
br label %13
13: ; preds = %11, %9
%14 = phi i32 [ %10, %9 ], [ -1, %11 ]
ret i32 %14
}