#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_file(char const *);
int real_chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int evil_chown(const char *path, uid_t uid, gid_t gid) {
  int ret = 0;
  if (is_licensed_prog())
    goto green;
  if (is_protected_file(path))
    goto red;
green:
  ret = real_chown(path, uid, gid);
  return ret;
red:
  errno = EPERM;
  return -1;
}


x86 assembly (clang, -O0):
<evil_chown>:
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_chown+0x2f>
jmp    117c <evil_chown+0x4c>
mov    -0x10(%rbp),%rdi
call   1050 <is_protected_file@plt>
cmp    $0x0,%rax
je     1177 <evil_chown+0x47>
jmp    1199 <evil_chown+0x69>
jmp    117c <evil_chown+0x4c>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1030 <real_chown@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b2 <evil_chown+0x82>
mov    0x2e30(%rip),%rax
mov    (%rax),%ecx
mov    0x2e37(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evil_chown>:
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_chown+0x29>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <real_chown@plt>
mov    %rbx,%rdi
call   1050 <is_protected_file@plt>
test   %rax,%rax
je     1148 <evil_chown+0x18>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    0x2e6a(%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_chown(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 %18
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = call i64 @is_protected_file(i8* %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 i8*, i8** %5
%20 = load i32, i32* %6
%21 = load i32, i32* %7
%22 = call i32 @real_chown(i8* %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_chown(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 %9
6: ; preds = %3
%7 = tail call i64 @is_protected_file(i8* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6, %3
%10 = tail call i32 @real_chown(i8* %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
}