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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fstat(int, struct stat *);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sendfile(int, int, int /*<<< orphan*/, int /*<<< orphan*/ *,
                            int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int read_fd;
  int write_fd;
  struct stat stat_buf;
  off_t offset = 0;
  read_fd = open(argv[1], O_RDONLY);
  fstat(read_fd, &stat_buf);
  write_fd = open(argv[2], O_WRONLY | O_CREAT, stat_buf.st_mode);
  printf("Now copying from %s to %s...\n", argv[1], argv[2]);
  sendfile(read_fd, write_fd, offset, &stat_buf.st_size, NULL, 0);
  printf("Done!\n");
  close(read_fd);
  close(write_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <fstat@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    0x2e4c(%rip),%rax
or     (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
call   1050 <sendfile@plt>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1080 <fstat@plt>
mov    0x10(%rbx),%rdi
mov    0x2e30(%rip),%rax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x4(%rsp),%edx
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%r15d
mov    0x8(%rbx),%rsi
mov    0x10(%rbx),%rdx
lea    0xe40(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %r15d,%esi
xor    %edx,%edx
mov    %r14,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <sendfile@plt>
lea    0xe3b(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r15d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str = [30 x i8] c"Now copying from %s to %s...\0A\00"
@.str.1 = [7 x i8] c"Done!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = call i32 @fstat(i32 %15, %struct.stat* %8)
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 2
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_WRONLY
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%24 = load i32, i32* %23
%25 = call i32 (i8*, i32, ...) @open(i8* %19, i32 %22, i32 %24)
store i32 %25, i32* %7
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %28, i8* %31)
%33 = load i32, i32* %6
%34 = load i32, i32* %7
%35 = load i32, i32* %9
%36 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%37 = call i32 @sendfile(i32 %33, i32 %34, i32 %35, i32* %36, i32* null, i32 0)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* %6
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str = [30 x i8] c"Now copying from %s to %s...\0A\00"
@str = [6 x i8] c"Done!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 (i8*, i32, ...) @open(i8* %6, i32 %7)
%9 = call i32 @fstat(i32 %8, %struct.stat* %3)
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%16 = load i32, i32* %15
%17 = tail call i32 (i8*, i32, ...) @open(i8* %11, i32 %14, i32 %16)
%18 = load i8*, i8** %5
%19 = load i8*, i8** %10
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %19)
%21 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%22 = call i32 @sendfile(i32 %8, i32 %17, i32 0, i32* %21, i32* null, i32 0)
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%24 = call i32 @close(i32 %8)
%25 = call i32 @close(i32 %17)
ret i32 0
}