#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 /*<<< orphan*/ errno;
scalar_t__ is_licensed_prog();
scalar_t__ is_protected_fd(int);
int real_fchmod(int, int /*<<< orphan*/);
int evil_fchmod(int fd, mode_t mode) {
  int ret = 0;
  if (is_licensed_prog())
    goto green;
  if (is_protected_fd(fd))
    goto red;
green:
  ret = real_fchmod(fd, mode);
  return ret;
red:
  errno = EPERM;
  return -1;
}


x86 assembly (clang, -O0):
<evil_fchmod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1050 <is_licensed_prog@plt>
cmp    $0x0,%rax
je     115b <evil_fchmod+0x2b>
jmp    1177 <evil_fchmod+0x47>
mov    -0x8(%rbp),%edi
call   1040 <is_protected_fd@plt>
cmp    $0x0,%rax
je     1172 <evil_fchmod+0x42>
jmp    1190 <evil_fchmod+0x60>
jmp    1177 <evil_fchmod+0x47>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <real_fchmod@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a9 <evil_fchmod+0x79>
mov    0x2e39(%rip),%rax
mov    (%rax),%ecx
mov    0x2e40(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evil_fchmod>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
xor    %eax,%eax
call   1050 <is_licensed_prog@plt>
test   %rax,%rax
je     1152 <evil_fchmod+0x22>
mov    %ebp,%edi
mov    %ebx,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <real_fchmod@plt>
mov    %ebp,%edi
call   1040 <is_protected_fd@plt>
test   %rax,%rax
je     1143 <evil_fchmod+0x13>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    0x2e72(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@EPERM = global i32 0
@errno = global i32 0
define i32 @evil_fchmod(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %6
%7 = call i64 (...) @is_licensed_prog()
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %2
br label %16
10: ; preds = %2
%11 = load i32, i32* %4
%12 = call i64 @is_protected_fd(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
br label %21
15: ; preds = %10
br label %16
16: ; preds = %15, %9
%17 = load i32, i32* %4
%18 = load i32, i32* %5
%19 = call i32 @real_fchmod(i32 %17, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
store i32 %20, i32* %3
br label %23
21: ; preds = %14
%22 = load i32, i32* @EPERM
store i32 %22, i32* @errno
store i32 -1, i32* %3
br label %23
23: ; preds = %21, %16
%24 = load i32, i32* %3
ret i32 %24
}

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