#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*/ sendfile(int, int, 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);
  sendfile(write_fd, read_fd, &offset, stat_buf.st_size);
  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    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    0x2e5c(%rip),%rax
or     (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%ecx
lea    -0x24(%rbp),%rdx
call   1040 <sendfile@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
movl   $0x0,0x4(%rsp)
mov    0x8(%rsi),%rdi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1050 <open@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
mov    0x10(%r14),%rdi
mov    0x2e48(%rip),%rax
mov    0x2e71(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0xc(%rsp),%edx
xor    %eax,%eax
call   1050 <open@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%ecx
lea    0x4(%rsp),%rdx
mov    %eax,%edi
mov    %ebx,%esi
call   1040 <sendfile@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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
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 i32, i32* %7
%27 = load i32, i32* %6
%28 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @sendfile(i32 %26, i32 %27, i32* %9, i32 %29)
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %7
%34 = call i32 @close(i32 %33)
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
define i32 @main(i32 %0, i8** nocapture %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 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 (i8*, i32, ...) @open(i8* %8, i32 %9)
%11 = call i32 @fstat(i32 %10, %struct.stat* %3)
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %15, %14
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%18 = load i32, i32* %17
%19 = tail call i32 (i8*, i32, ...) @open(i8* %13, i32 %16, i32 %18)
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @sendfile(i32 %19, i32 %10, i32* %4, i32 %21)
%23 = call i32 @close(i32 %10)
%24 = call i32 @close(i32 %19)
ret i32 0
}