#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 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_fchmodat(int, char const *, int /*<<< orphan*/, int);
int evil_fchmodat(int dirfd, const char *path, mode_t mode, 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_fchmodat(dirfd, path, mode, flags);
  return ret;
red:
  errno = EPERM;
  return -1;
}


x86 assembly (clang, -O0):
<evil_fchmodat>:
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   1040 <is_licensed_prog@plt>
cmp    $0x0,%rax
je     1172 <evil_fchmodat+0x32>
jmp    11cc <evil_fchmodat+0x8c>
mov    -0x18(%rbp),%eax
mov    0x2e5c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11a7 <evil_fchmodat+0x67>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <is_protected_atentry@plt>
cmp    $0x0,%rax
je     11a2 <evil_fchmodat+0x62>
jmp    11ec <evil_fchmodat+0xac>
jmp    11c7 <evil_fchmodat+0x87>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <is_protected_atfile@plt>
cmp    $0x0,%rax
je     11c2 <evil_fchmodat+0x82>
jmp    11ec <evil_fchmodat+0xac>
jmp    11c7 <evil_fchmodat+0x87>
jmp    11cc <evil_fchmodat+0x8c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1060 <real_fchmodat@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1205 <evil_fchmodat+0xc5>
mov    0x2dd5(%rip),%rax
mov    (%rax),%ecx
mov    0x2de4(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evil_fchmodat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%ebx
xor    %eax,%eax
call   1040 <is_licensed_prog@plt>
test   %rax,%rax
je     1178 <evil_fchmodat+0x38>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
mov    %r15d,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <real_fchmodat@plt>
mov    0x2e59(%rip),%rax
test   %r15d,(%rax)
je     1195 <evil_fchmodat+0x55>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <is_protected_atentry@plt>
test   %rax,%rax
jne    11a4 <evil_fchmodat+0x64>
jmp    115e <evil_fchmodat+0x1e>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <is_protected_atfile@plt>
test   %rax,%rax
je     115e <evil_fchmodat+0x1e>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    0x2e2c(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
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_fchmodat(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 %34
14: ; preds = %4
%15 = load i32, i32* %9
%16 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%17 = and i32 %15, %16
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %14
%20 = load i32, i32* %6
%21 = load i8*, i8** %7
%22 = call i64 @is_protected_atentry(i32 %20, i8* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
br label %41
25: ; preds = %19
br label %33
26: ; preds = %14
%27 = load i32, i32* %6
%28 = load i8*, i8** %7
%29 = call i64 @is_protected_atfile(i32 %27, i8* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
br label %41
32: ; preds = %26
br label %33
33: ; preds = %32, %25
br label %34
34: ; preds = %33, %13
%35 = load i32, i32* %6
%36 = load i8*, i8** %7
%37 = load i32, i32* %8
%38 = load i32, i32* %9
%39 = call i32 @real_fchmodat(i32 %35, i8* %36, i32 %37, i32 %38)
store i32 %39, i32* %10
%40 = load i32, i32* %10
store i32 %40, i32* %5
br label %43
41: ; preds = %31, %24
%42 = load i32, i32* @EPERM
store i32 %42, i32* @errno
store i32 -1, i32* %5
br label %43
43: ; preds = %41, %34
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
@AT_SYMLINK_NOFOLLOW = global i32 0
@EPERM = global i32 0
@errno = global i32 0
define i32 @evil_fchmodat(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 %17
7: ; preds = %4
%8 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%9 = and i32 %8, %3
%10 = icmp eq i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %7
%12 = tail call i64 @is_protected_atentry(i32 %0, i8* %1)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %19
14: ; preds = %7
%15 = tail call i64 @is_protected_atfile(i32 %0, i8* %1)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11, %14, %4
%18 = tail call i32 @real_fchmodat(i32 %0, i8* %1, i32 %2, i32 %3)
br label %21
19: ; preds = %14, %11
%20 = load i32, i32* @EPERM
store i32 %20, i32* @errno
br label %21
21: ; preds = %19, %17
%22 = phi i32 [ %18, %17 ], [ -1, %19 ]
ret i32 %22
}