#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 */

/* Variables and functions */
int /*<<< orphan*/ EPERM;
int /*<<< orphan*/ errno;
scalar_t__ is_licensed_prog();
scalar_t__ is_protected_atentry(int, char const *);
int real_unlinkat(int, char const *, int);
int evil_unlinkat(int dirfd, const const char *pathname, int flags) {
  int ret = 0;
  if (is_licensed_prog())
    goto green;
  if (is_protected_atentry(dirfd, pathname))
    goto red;
green:
  ret = real_unlinkat(dirfd, pathname, flags);
  return ret;
red:
  errno = EPERM;
  return -1;
}


x86 assembly (clang, -O0):
<evil_unlinkat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1030 <is_licensed_prog@plt>
cmp    $0x0,%rax
je     115f <evil_unlinkat+0x2f>
jmp    117f <evil_unlinkat+0x4f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <is_protected_atentry@plt>
cmp    $0x0,%rax
je     117a <evil_unlinkat+0x4a>
jmp    119c <evil_unlinkat+0x6c>
jmp    117f <evil_unlinkat+0x4f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <real_unlinkat@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b5 <evil_unlinkat+0x85>
mov    0x2e2d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e34(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evil_unlinkat>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1030 <is_licensed_prog@plt>
test   %rax,%rax
je     1159 <evil_unlinkat+0x29>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <real_unlinkat@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <is_protected_atentry@plt>
test   %rax,%rax
je     1148 <evil_unlinkat+0x18>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    0x2e68(%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_unlinkat(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %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 %19
12: ; preds = %3
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = call i64 @is_protected_atentry(i32 %13, i8* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
br label %25
18: ; preds = %12
br label %19
19: ; preds = %18, %11
%20 = load i32, i32* %5
%21 = load i8*, i8** %6
%22 = load i32, i32* %7
%23 = call i32 @real_unlinkat(i32 %20, i8* %21, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %8
store i32 %24, i32* %4
br label %27
25: ; preds = %17
%26 = load i32, i32* @EPERM
store i32 %26, i32* @errno
store i32 -1, i32* %4
br label %27
27: ; preds = %25, %19
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@EPERM = global i32 0
@errno = global i32 0
define i32 @evil_unlinkat(i32 %0, i8* %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_atentry(i32 %0, i8* %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6, %3
%10 = tail call i32 @real_unlinkat(i32 %0, i8* %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
}