#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 */
struct stat {
  int /*<<< orphan*/ st_size;
};
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, char *);
int /*<<< orphan*/ do_delay();
int /*<<< orphan*/ fstat(int, struct stat *);
int open(char *, int, ...);
int /*<<< orphan*/ sendfile_aes_close(int);
int sendfile_aes_open(char *, int, char *, int, int);
int /*<<< orphan*/ sendfile_aes_send(int, int, int, int /*<<< orphan*/ *,
                                     int /*<<< orphan*/);
int do_sendfile_aes(char *filename_out, char *filename_in, char *key,
                    int key_length, char *iv, int iv_length, int encrypt) {
  struct stat stat_buf;
  off_t offset = 0;
  int in_fd;
  int out_fd;
  int handle;
  handle = sendfile_aes_open(key, key_length, iv, iv_length, encrypt);
  do_delay();
  in_fd = open(filename_in, O_RDONLY);
  if (in_fd < 0)
    die("Can't open file: %s", filename_in);
  out_fd = open(filename_out, O_WRONLY | O_CREAT, 0644);
  if (out_fd < 0)
    die("Can't open file: %s", filename_in);
  fstat(in_fd, &stat_buf);
  sendfile_aes_send(handle, out_fd, in_fd, &offset, stat_buf.st_size);
  do_delay();
  sendfile_aes_close(handle);
  close(out_fd);
  close(in_fd);
  return 0;
}


x86 assembly (clang, -O0):
<do_sendfile_aes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    0x10(%rbp),%r8d
call   1040 <sendfile_aes_open@plt>
mov    %eax,-0x40(%rbp)
mov    $0x0,%al
call   1030 <do_delay@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    11fb <do_sendfile_aes+0x7b>
mov    -0x10(%rbp),%rsi
lea    0xe0a(%rip),%rdi
call   1080 <die@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
mov    0x2de9(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    123a <do_sendfile_aes+0xba>
mov    -0x10(%rbp),%rsi
lea    0xdcb(%rip),%rdi
call   1080 <die@plt>
mov    -0x38(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10a0 <fstat@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%edx
mov    -0x30(%rbp),%r8d
lea    -0x34(%rbp),%rcx
call   1060 <sendfile_aes_send@plt>
mov    $0x0,%al
call   1030 <do_delay@plt>
mov    -0x40(%rbp),%edi
call   1090 <sendfile_aes_close@plt>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
mov    -0x38(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_sendfile_aes>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8,%rax
mov    %rsi,%r12
mov    %rdi,%r15
mov    0x40(%rsp),%r8d
movl   $0x0,0xc(%rsp)
mov    %rdx,%rdi
mov    %ecx,%esi
mov    %rax,%rdx
mov    %r9d,%ecx
call   1040 <sendfile_aes_open@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1030 <do_delay@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11e4 <do_sendfile_aes+0x64>
lea    0xe24(%rip),%rdi
mov    %r12,%rsi
call   1080 <die@plt>
mov    0x2ddd(%rip),%rax
mov    0x2e06(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r15,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    121a <do_sendfile_aes+0x9a>
lea    0xdee(%rip),%rdi
mov    %r12,%rsi
call   1080 <die@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   10a0 <fstat@plt>
mov    0x8(%rsp),%r8d
lea    0xc(%rsp),%rcx
mov    %r14d,%edi
mov    %ebx,%esi
mov    %ebp,%edx
call   1060 <sendfile_aes_send@plt>
xor    %eax,%eax
call   1030 <do_delay@plt>
mov    %r14d,%edi
call   1090 <sendfile_aes_close@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [20 x i8] c"Can't open file: %s\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @do_sendfile_aes(i8* %0, i8* %1, i8* %2, i32 %3, i8* %4, i32 %5, i32 %6) {
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.stat
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i8* %0, i8** %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i32 %5, i32* %13
store i32 %6, i32* %14
store i32 0, i32* %16
%20 = load i8*, i8** %10
%21 = load i32, i32* %11
%22 = load i8*, i8** %12
%23 = load i32, i32* %13
%24 = load i32, i32* %14
%25 = call i32 @sendfile_aes_open(i8* %20, i32 %21, i8* %22, i32 %23, i32 %24)
store i32 %25, i32* %19
%26 = call i32 (...) @do_delay()
%27 = load i8*, i8** %9
%28 = load i32, i32* @O_RDONLY
%29 = call i32 (i8*, i32, ...) @open(i8* %27, i32 %28)
store i32 %29, i32* %17
%30 = load i32, i32* %17
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %7
%33 = load i8*, i8** %9
%34 = call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %32, %7
%36 = load i8*, i8** %8
%37 = load i32, i32* @O_WRONLY
%38 = load i32, i32* @O_CREAT
%39 = or i32 %37, %38
%40 = call i32 (i8*, i32, ...) @open(i8* %36, i32 %39, i32 420)
store i32 %40, i32* %18
%41 = load i32, i32* %18
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %35
%44 = load i8*, i8** %9
%45 = call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %43, %35
%47 = load i32, i32* %17
%48 = call i32 @fstat(i32 %47, %struct.stat* %15)
%49 = load i32, i32* %19
%50 = load i32, i32* %18
%51 = load i32, i32* %17
%52 = getelementptr inbounds %struct.stat, %struct.stat* %15, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @sendfile_aes_send(i32 %49, i32 %50, i32 %51, i32* %16, i32 %53)
%55 = call i32 (...) @do_delay()
%56 = load i32, i32* %19
%57 = call i32 @sendfile_aes_close(i32 %56)
%58 = load i32, i32* %18
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %17
%61 = call i32 @close(i32 %60)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [20 x i8] c"Can't open file: %s\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @do_sendfile_aes(i8* nocapture %0, i8* %1, i8* %2, i32 %3, i8* %4, i32 %5, i32 %6) {
%8 = alloca %struct.stat
%9 = alloca i32
%10 = bitcast %struct.stat* %8 to i8*
%11 = bitcast i32* %9 to i8*
store i32 0, i32* %9
%12 = tail call i32 @sendfile_aes_open(i8* %2, i32 %3, i8* %4, i32 %5, i32 %6)
%13 = tail call i32 (...) @do_delay()
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %7
%18 = tail call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %19
19: ; preds = %17, %7
%20 = load i32, i32* @O_WRONLY
%21 = load i32, i32* @O_CREAT
%22 = or i32 %21, %20
%23 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %22, i32 420)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = tail call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %27
27: ; preds = %25, %19
%28 = call i32 @fstat(i32 %15, %struct.stat* %8)
%29 = getelementptr inbounds %struct.stat, %struct.stat* %8, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @sendfile_aes_send(i32 %12, i32 %23, i32 %15, i32* %9, i32 %30)
%32 = call i32 (...) @do_delay()
%33 = call i32 @sendfile_aes_close(i32 %12)
%34 = call i32 @close(i32 %23)
%35 = call i32 @close(i32 %15)
ret i32 0
}