#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_rename(char const *, char const *);
int rename(const char *oldpath, const char *newpath) {
  if (fileinpath(oldpath, "SUDO_ALLOWED") ||
      fileinpath(newpath, "SUDO_ALLOWED") || getenv("SYS_RENAME")) {
    return sys_rename(oldpath, newpath);
  }
  debug("rename: ");
  errno = EPERM;
  return -1;
}


x86 assembly (clang, -O0):
<rename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1050 <fileinpath@plt>
cmp    $0x0,%rax
jne    119a <rename+0x5a>
mov    -0x18(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1050 <fileinpath@plt>
cmp    $0x0,%rax
jne    119a <rename+0x5a>
lea    0xe82(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     11af <rename+0x6f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <sys_rename@plt>
mov    %eax,-0x4(%rbp)
jmp    11d4 <rename+0x94>
lea    0xe62(%rip),%rdi
call   1040 <debug@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e15(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rename>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeaf(%rip),%rsi
call   1050 <fileinpath@plt>
test   %rax,%rax
jne    1180 <rename+0x40>
lea    0xe9e(%rip),%rsi
mov    %r14,%rdi
call   1050 <fileinpath@plt>
test   %rax,%rax
jne    1180 <rename+0x40>
lea    0xe97(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1192 <rename+0x52>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <sys_rename@plt>
lea    0xe7f(%rip),%rdi
call   1040 <debug@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    0x2e32(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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