#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(int, int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int do_sendfile(char *filename_out, char *filename_in) {
  struct stat stat_buf;
  off_t offset = 0;
  int in_fd;
  int out_fd;
  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(out_fd, in_fd, &offset, stat_buf.st_size);
  do_delay();
  close(out_fd);
  close(in_fd);
  return 0;
}


x86 assembly (clang, -O0):
<do_sendfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11a8 <do_sendfile+0x48>
mov    -0x10(%rbp),%rsi
lea    0xe5d(%rip),%rdi
call   1070 <die@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    0x2e3c(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11e7 <do_sendfile+0x87>
mov    -0x10(%rbp),%rsi
lea    0xe1e(%rip),%rdi
call   1070 <die@plt>
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <fstat@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%ecx
lea    -0x1c(%rbp),%rdx
call   1050 <sendfile@plt>
mov    $0x0,%al
call   1030 <do_delay@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_sendfile>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
movl   $0x0,0xc(%rsp)
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    119e <do_sendfile+0x3e>
lea    0xe6a(%rip),%rdi
mov    %r14,%rsi
call   1070 <die@plt>
mov    0x2e23(%rip),%rax
mov    0x2e4c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11d4 <do_sendfile+0x74>
lea    0xe34(%rip),%rdi
mov    %r14,%rsi
call   1070 <die@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1080 <fstat@plt>
mov    0x8(%rsp),%ecx
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
mov    %ebx,%esi
call   1050 <sendfile@plt>
xor    %eax,%eax
call   1030 <do_delay@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %6
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 (i8*, i32, ...) @open(i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %14, %2
%18 = load i8*, i8** %3
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = call i32 (i8*, i32, ...) @open(i8* %18, i32 %21, i32 420)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = load i8*, i8** %4
%27 = call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %25, %17
%29 = load i32, i32* %7
%30 = call i32 @fstat(i32 %29, %struct.stat* %5)
%31 = load i32, i32* %8
%32 = load i32, i32* %7
%33 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @sendfile(i32 %31, i32 %32, i32* %6, i32 %34)
%36 = call i32 (...) @do_delay()
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %7
%40 = call i32 @close(i32 %39)
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(i8* nocapture %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca i32
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %12
12: ; preds = %10, %2
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %15, i32 420)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = tail call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %20
20: ; preds = %18, %12
%21 = call i32 @fstat(i32 %8, %struct.stat* %3)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @sendfile(i32 %16, i32 %8, i32* %4, i32 %23)
%25 = call i32 (...) @do_delay()
%26 = call i32 @close(i32 %16)
%27 = call i32 @close(i32 %8)
ret i32 0
}