#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*/ debug(char *);
int /*<<< orphan*/ errno;
scalar_t__ fileinpath(char const *, char *);
scalar_t__ getenv(char *);
int sys_unlinkat(int, char const *, int);
int unlinkat(int dirfd, const char *pathname, int flags) {
  if (fileinpath(pathname, "SUDO_ALLOWED") || getenv("SYS_UNLINK")) {
    return sys_unlinkat(dirfd, pathname, flags);
  }
  debug("unlinkat: ");
  errno = EPERM;
  return -1;
}


x86 assembly (clang, -O0):
<unlinkat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea3(%rip),%rsi
call   1050 <fileinpath@plt>
cmp    $0x0,%rax
jne    1182 <unlinkat+0x42>
lea    0xe9a(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     1199 <unlinkat+0x59>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <sys_unlinkat@plt>
mov    %eax,-0x4(%rbp)
jmp    11be <unlinkat+0x7e>
lea    0xe78(%rip),%rdi
call   1040 <debug@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unlinkat>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xead(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fileinpath@plt>
test   %rax,%rax
jne    1171 <unlinkat+0x31>
lea    0xea6(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1182 <unlinkat+0x42>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <sys_unlinkat@plt>
lea    0xe8f(%rip),%rdi
call   1040 <debug@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    0x2e42(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"SUDO_ALLOWED\00"
@.str.1 = [11 x i8] c"SYS_UNLINK\00"
@.str.2 = [11 x i8] c"unlinkat: \00"
@EPERM = global i32 0
@errno = global i32 0
define i32 @unlinkat(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%8 = load i8*, i8** %6
%9 = call i64 @fileinpath(i8* %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%10 = icmp ne i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %3
%12 = call i64 @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %11, %3
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %7
%18 = call i32 @sys_unlinkat(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %4
br label %22
19: ; preds = %11
%20 = call i32 @debug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @EPERM
store i32 %21, i32* @errno
store i32 -1, i32* %4
br label %22
22: ; preds = %19, %14
%23 = load i32, i32* %4
ret i32 %23
}

LLVM IR (-O2):
@.str = [13 x i8] c"SUDO_ALLOWED\00"
@.str.1 = [11 x i8] c"SYS_UNLINK\00"
@.str.2 = [11 x i8] c"unlinkat: \00"
@EPERM = global i32 0
@errno = global i32 0
define i32 @unlinkat(i32 %0, i8* %1, i32 %2) {
%4 = tail call i64 @fileinpath(i8* %1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i64 @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6, %3
%10 = tail call i32 @sys_unlinkat(i32 %0, i8* %1, i32 %2)
br label %14
11: ; preds = %6
%12 = tail call i32 @debug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @EPERM
store i32 %13, i32* @errno
br label %14
14: ; preds = %11, %9
%15 = phi i32 [ %10, %9 ], [ -1, %11 ]
ret i32 %15
}