#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();
int /*<<< orphan*/ is_permitted_renameat(int, char const *, int, char const *);
scalar_t__ is_protected_atentry(int, char const *);
int real_renameat(int, char const *, int, char const *);
int evil_renameat(int olddirfd, const char *oldpath, int newdirfd,
                  const char *newpath) {
  int ret = 0;
  if (is_licensed_prog())
    goto green;
  if (is_protected_atentry(olddirfd, oldpath))
    goto red;
  if (!is_protected_atentry(newdirfd, newpath))
    goto green;
  if (!is_permitted_renameat(olddirfd, oldpath, newdirfd, newpath))
    goto red;
green:
  ret = real_renameat(olddirfd, oldpath, newdirfd, newpath);
  return ret;
red:
  errno = EPERM;
  return -1;
}


x86 assembly (clang, -O0):
<evil_renameat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1030 <is_licensed_prog@plt>
cmp    $0x0,%rax
je     1173 <evil_renameat+0x33>
jmp    11cf <evil_renameat+0x8f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <is_protected_atentry@plt>
cmp    $0x0,%rax
je     118e <evil_renameat+0x4e>
jmp    11f0 <evil_renameat+0xb0>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1040 <is_protected_atentry@plt>
cmp    $0x0,%rax
jne    11a9 <evil_renameat+0x69>
jmp    11cf <evil_renameat+0x8f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1050 <is_permitted_renameat@plt>
cmp    $0x0,%eax
jne    11ca <evil_renameat+0x8a>
jmp    11f0 <evil_renameat+0xb0>
jmp    11cf <evil_renameat+0x8f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1060 <real_renameat@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1209 <evil_renameat+0xc9>
mov    0x2dd9(%rip),%rax
mov    (%rax),%ecx
mov    0x2de0(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evil_renameat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1030 <is_licensed_prog@plt>
test   %rax,%rax
je     1178 <evil_renameat+0x38>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
mov    %r14,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <real_renameat@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <is_protected_atentry@plt>
test   %rax,%rax
je     11a9 <evil_renameat+0x69>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    0x2e49(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%edi
mov    %r14,%rsi
call   1040 <is_protected_atentry@plt>
test   %rax,%rax
je     115e <evil_renameat+0x1e>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
mov    %r14,%rcx
call   1050 <is_permitted_renameat@plt>
test   %eax,%eax
jne    115e <evil_renameat+0x1e>
jmp    1187 <evil_renameat+0x47>

LLVM IR (-O0):
@EPERM = global i32 0
@errno = global i32 0
define i32 @evil_renameat(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %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 %35
14: ; preds = %4
%15 = load i32, i32* %6
%16 = load i8*, i8** %7
%17 = call i64 @is_protected_atentry(i32 %15, i8* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14
br label %42
20: ; preds = %14
%21 = load i32, i32* %8
%22 = load i8*, i8** %9
%23 = call i64 @is_protected_atentry(i32 %21, i8* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %20
br label %35
26: ; preds = %20
%27 = load i32, i32* %6
%28 = load i8*, i8** %7
%29 = load i32, i32* %8
%30 = load i8*, i8** %9
%31 = call i32 @is_permitted_renameat(i32 %27, i8* %28, i32 %29, i8* %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %26
br label %42
34: ; preds = %26
br label %35
35: ; preds = %34, %25, %13
%36 = load i32, i32* %6
%37 = load i8*, i8** %7
%38 = load i32, i32* %8
%39 = load i8*, i8** %9
%40 = call i32 @real_renameat(i32 %36, i8* %37, i32 %38, i8* %39)
store i32 %40, i32* %10
%41 = load i32, i32* %10
store i32 %41, i32* %5
br label %44
42: ; preds = %33, %19
%43 = load i32, i32* @EPERM
store i32 %43, i32* @errno
store i32 -1, i32* %5
br label %44
44: ; preds = %42, %35
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
@EPERM = global i32 0
@errno = global i32 0
define i32 @evil_renameat(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = tail call i64 (...) @is_licensed_prog()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %4
%8 = tail call i64 @is_protected_atentry(i32 %0, i8* %1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %7
%11 = tail call i64 @is_protected_atentry(i32 %2, i8* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i32 @is_permitted_renameat(i32 %0, i8* %1, i32 %2, i8* %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13, %10, %4
%17 = tail call i32 @real_renameat(i32 %0, i8* %1, i32 %2, i8* %3)
br label %20
18: ; preds = %13, %7
%19 = load i32, i32* @EPERM
store i32 %19, i32* @errno
br label %20
20: ; preds = %18, %16
%21 = phi i32 [ %17, %16 ], [ -1, %18 ]
ret i32 %21
}