#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 */
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ EPERM;
int /*<<< orphan*/ errno;
scalar_t__ is_licensed_prog();
scalar_t__ is_protected_file(char const *);
int real_truncate64(char const *, int /*<<< orphan*/);
int evil_truncate64(const char *path, off_t length) {
  int ret = 0;
  if (is_licensed_prog())
    goto green;
  if (is_protected_file(path))
    goto red;
green:
  ret = real_truncate64(path, length);
  return ret;
red:
  errno = EPERM;
  return -1;
}


x86 assembly (clang, -O0):
<evil_truncate64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1030 <is_licensed_prog@plt>
cmp    $0x0,%rax
je     115c <evil_truncate64+0x2c>
jmp    1179 <evil_truncate64+0x49>
mov    -0x10(%rbp),%rdi
call   1050 <is_protected_file@plt>
cmp    $0x0,%rax
je     1174 <evil_truncate64+0x44>
jmp    1193 <evil_truncate64+0x63>
jmp    1179 <evil_truncate64+0x49>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <real_truncate64@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ac <evil_truncate64+0x7c>
mov    0x2e36(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evil_truncate64>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <is_licensed_prog@plt>
test   %rax,%rax
je     1154 <evil_truncate64+0x24>
mov    %rbx,%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <real_truncate64@plt>
mov    %rbx,%rdi
call   1050 <is_protected_file@plt>
test   %rax,%rax
je     1144 <evil_truncate64+0x14>
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    0x2e6f(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@EPERM = global i32 0
@errno = global i32 0
define i32 @evil_truncate64(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
%7 = call i64 (...) @is_licensed_prog()
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %2
br label %16
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = call i64 @is_protected_file(i8* %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
br label %21
15: ; preds = %10
br label %16
16: ; preds = %15, %9
%17 = load i8*, i8** %4
%18 = load i32, i32* %5
%19 = call i32 @real_truncate64(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
store i32 %20, i32* %3
br label %23
21: ; preds = %14
%22 = load i32, i32* @EPERM
store i32 %22, i32* @errno
store i32 -1, i32* %3
br label %23
23: ; preds = %21, %16
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@EPERM = global i32 0
@errno = global i32 0
define i32 @evil_truncate64(i8* %0, i32 %1) {
%3 = tail call i64 (...) @is_licensed_prog()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i64 @is_protected_file(i8* %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5, %2
%9 = tail call i32 @real_truncate64(i8* %0, i32 %1)
br label %12
10: ; preds = %5
%11 = load i32, i32* @EPERM
store i32 %11, i32* @errno
br label %12
12: ; preds = %10, %8
%13 = phi i32 [ %9, %8 ], [ -1, %10 ]
ret i32 %13
}