#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 */
struct timespec {
  int dummy;
};

/* 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_utimensat(int, char const *, struct timespec const *, int);
int evil_utimensat(int dirfd, const char *pathname,
                   const struct timespec times[2], int flags) {
  int ret = 0;
  if (is_licensed_prog())
    goto green;
  if (flags & AT_SYMLINK_NOFOLLOW) {
    if (is_protected_atentry(dirfd, pathname))
      goto red;
  } else {
    if (is_protected_atfile(dirfd, pathname))
      goto red;
  }
green:
  ret = real_utimensat(dirfd, pathname, times, flags);
  return ret;
red:
  errno = EPERM;
  return -1;
}


x86 assembly (clang, -O0):
<evil_utimensat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1040 <is_licensed_prog@plt>
cmp    $0x0,%rax
je     1173 <evil_utimensat+0x33>
jmp    11cd <evil_utimensat+0x8d>
mov    -0x1c(%rbp),%eax
mov    0x2e5b(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11a8 <evil_utimensat+0x68>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <is_protected_atentry@plt>
cmp    $0x0,%rax
je     11a3 <evil_utimensat+0x63>
jmp    11ee <evil_utimensat+0xae>
jmp    11c8 <evil_utimensat+0x88>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <is_protected_atfile@plt>
cmp    $0x0,%rax
je     11c3 <evil_utimensat+0x83>
jmp    11ee <evil_utimensat+0xae>
jmp    11c8 <evil_utimensat+0x88>
jmp    11cd <evil_utimensat+0x8d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1060 <real_utimensat@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1207 <evil_utimensat+0xc7>
mov    0x2dd3(%rip),%rax
mov    (%rax),%ecx
mov    0x2de2(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evil_utimensat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1040 <is_licensed_prog@plt>
test   %rax,%rax
je     1178 <evil_utimensat+0x38>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r15d,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <real_utimensat@plt>
mov    0x2e59(%rip),%rax
test   %r15d,(%rax)
je     1195 <evil_utimensat+0x55>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <is_protected_atentry@plt>
test   %rax,%rax
jne    11a4 <evil_utimensat+0x64>
jmp    115e <evil_utimensat+0x1e>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <is_protected_atfile@plt>
test   %rax,%rax
je     115e <evil_utimensat+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):
%struct.timespec = type { i32 }
@AT_SYMLINK_NOFOLLOW = global i32 0
@EPERM = global i32 0
@errno = global i32 0
define i32 @evil_utimensat(i32 %0, i8* %1, %struct.timespec* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.timespec*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store %struct.timespec* %2, %struct.timespec** %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 %struct.timespec*, %struct.timespec** %8
%38 = load i32, i32* %9
%39 = call i32 @real_utimensat(i32 %35, i8* %36, %struct.timespec* %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):
%struct.timespec = type { i32 }
@AT_SYMLINK_NOFOLLOW = global i32 0
@EPERM = global i32 0
@errno = global i32 0
define i32 @evil_utimensat(i32 %0, i8* %1, %struct.timespec* %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_utimensat(i32 %0, i8* %1, %struct.timespec* %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
}