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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fstat(int, struct stat *);
int open(char *, int, ...);
int sendfile(int, int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int copy_file(char *src, char *dst) {
  int in, out;
  off_t bytes = 0;
  struct stat fileinfo = {0};
  int result = 0;
  if ((in = open(src, O_RDONLY)) == -1) {
    return -1;
  }
  if ((out = open(dst, O_RDWR | O_CREAT, S_IRWXU)) == -1) {
    close(in);
    return -1;
  }
  fstat(in, &fileinfo);
  result = sendfile(out, in, &bytes, fileinfo.st_size);
  close(in);
  close(out);
  return result;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11aa <copy_file+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <copy_file+0xd7>
mov    -0x18(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11f0 <copy_file+0xa0>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <copy_file+0xd7>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1070 <fstat@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%ecx
lea    -0x24(%rbp),%rdx
call   1050 <sendfile@plt>
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
movl   $0x0,0xc(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1050 <open@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     11da <copy_file+0x9a>
mov    %eax,%ebx
mov    0x2e56(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11d3 <copy_file+0x93>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1060 <fstat@plt>
mov    0x10(%rsp),%ecx
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
mov    %ebx,%esi
call   1040 <sendfile@plt>
mov    %eax,%r15d
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%edi
jmp    11d5 <copy_file+0x95>
mov    %ebx,%edi
call   1030 <close@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
define i32 @copy_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.stat
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %8
%11 = bitcast %struct.stat* %9 to i8*
store i32 0, i32* %10
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %41
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IRWXU
%23 = call i32 (i8*, i32, ...) @open(i8* %18, i32 %21, i32 %22)
store i32 %23, i32* %7
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %3
br label %41
28: ; preds = %17
%29 = load i32, i32* %6
%30 = call i32 @fstat(i32 %29, %struct.stat* %9)
%31 = load i32, i32* %7
%32 = load i32, i32* %6
%33 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @sendfile(i32 %31, i32 %32, i32* %8, i32 %34)
store i32 %35, i32* %10
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %7
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %10
store i32 %40, i32* %3
br label %41
41: ; preds = %28, %25, %16
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
define i32 @copy_file(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca i32
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.stat*
%6 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%7 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %27, label %11
11: ; preds = %2
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = load i32, i32* @S_IRWXU
%16 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %14, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = tail call i32 @close(i32 %9)
br label %27
20: ; preds = %11
%21 = call i32 @fstat(i32 %9, %struct.stat* %5)
%22 = bitcast i64* %4 to i32*
%23 = load i32, i32* %22
%24 = call i32 @sendfile(i32 %16, i32 %9, i32* %3, i32 %23)
%25 = call i32 @close(i32 %9)
%26 = call i32 @close(i32 %16)
br label %27
27: ; preds = %2, %20, %18
%28 = phi i32 [ -1, %18 ], [ %24, %20 ], [ -1, %2 ]
ret i32 %28
}