#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;
};

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
scalar_t__ sendfile(int, int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int fast_copy(char *from, char *to) {
  int in = -1, out = -1;
  struct stat buf;
  in = open(from, O_RDONLY);
  if (in < 0) {
    perror(from);
    goto error;
  }
  if (0 > fstat(in, &buf)) {
    perror("fstat");
    goto error;
  }
  out = open(to, O_WRONLY | O_CREAT | O_EXCL, buf.st_mode);
  if (out < 0) {
    perror(to);
    goto error;
  }
  if (0 > sendfile(out, in, NULL, buf.st_size)) {
    perror("sendfile");
    goto error;
  }
  return 0;
error:
  if (in > -1)
    close(in);
  if (out > -1)
    close(out);
  return -1;
}


x86 assembly (clang, -O0):
<fast_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    119d <fast_copy+0x4d>
mov    -0x10(%rbp),%rdi
call   1060 <perror@plt>
jmp    1249 <fast_copy+0xf9>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <fstat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    11c8 <fast_copy+0x78>
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
jmp    1249 <fast_copy+0xf9>
mov    -0x18(%rbp),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    0x2e1c(%rip),%rax
or     (%rax),%esi
mov    0x2deb(%rip),%rax
or     (%rax),%esi
mov    -0x24(%rbp),%edx
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    120c <fast_copy+0xbc>
mov    -0x18(%rbp),%rdi
call   1060 <perror@plt>
jmp    1249 <fast_copy+0xf9>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sendfile@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    123d <fast_copy+0xed>
lea    0xdd3(%rip),%rdi
call   1060 <perror@plt>
jmp    1249 <fast_copy+0xf9>
movl   $0x0,-0x4(%rbp)
jmp    1274 <fast_copy+0x124>
cmpl   $0xffffffff,-0x1c(%rbp)
jle    125b <fast_copy+0x10b>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
cmpl   $0xffffffff,-0x20(%rbp)
jle    126d <fast_copy+0x11d>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fast_copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbp
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1050 <open@plt>
test   %eax,%eax
js     11de <fast_copy+0x8e>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
test   %rax,%rax
js     11e8 <fast_copy+0x98>
mov    0x2e37(%rip),%rax
mov    0x2e68(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    0x4(%rsp),%edx
xor    %r15d,%r15d
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <open@plt>
test   %eax,%eax
js     11ef <fast_copy+0x9f>
mov    %eax,%ebp
mov    (%rsp),%ecx
mov    %eax,%edi
mov    %ebx,%esi
xor    %edx,%edx
call   1040 <sendfile@plt>
test   %rax,%rax
jns    1204 <fast_copy+0xb4>
lea    0xe38(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%edi
jmp    11f9 <fast_copy+0xa9>
mov    %rbp,%rdi
call   1060 <perror@plt>
jmp    11fe <fast_copy+0xae>
lea    0xe11(%rip),%r14
mov    %r14,%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%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
@.str = [6 x i8] c"fstat\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [9 x i8] c"sendfile\00"
define i32 @fast_copy(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 -1, i32* %6
store i32 -1, i32* %7
%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* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @perror(i8* %15)
br label %48
17: ; preds = %2
%18 = load i32, i32* %6
%19 = call i64 @fstat(i32 %18, %struct.stat* %8)
%20 = icmp sgt i64 0, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %48
23: ; preds = %17
%24 = load i8*, i8** %5
%25 = load i32, i32* @O_WRONLY
%26 = load i32, i32* @O_CREAT
%27 = or i32 %25, %26
%28 = load i32, i32* @O_EXCL
%29 = or i32 %27, %28
%30 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%31 = load i32, i32* %30
%32 = call i32 (i8*, i32, ...) @open(i8* %24, i32 %29, i32 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %23
%36 = load i8*, i8** %5
%37 = call i32 @perror(i8* %36)
br label %48
38: ; preds = %23
%39 = load i32, i32* %7
%40 = load i32, i32* %6
%41 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i64 @sendfile(i32 %39, i32 %40, i32* null, i32 %42)
%44 = icmp sgt i64 0, %43
br i1 %44, label %45, label %47
45: ; preds = %38
%46 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %48
47: ; preds = %38
store i32 0, i32* %3
br label %61
48: ; preds = %45, %35, %21, %14
%49 = load i32, i32* %6
%50 = icmp sgt i32 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %51, %48
%55 = load i32, i32* %7
%56 = icmp sgt i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = load i32, i32* %7
%59 = call i32 @close(i32 %58)
br label %60
60: ; preds = %57, %54
store i32 -1, i32* %3
br label %61
61: ; preds = %60, %47
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@.str = [6 x i8] c"fstat\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [9 x i8] c"sendfile\00"
define i32 @fast_copy(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %26, label %8
8: ; preds = %2
%9 = call i64 @fstat(i32 %6, %struct.stat* %3)
%10 = icmp slt i64 %9, 0
br i1 %10, label %28, label %11
11: ; preds = %8
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = load i32, i32* @O_EXCL
%16 = or i32 %14, %15
%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* %1, i32 %16, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %28, label %21
21: ; preds = %11
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = tail call i64 @sendfile(i32 %19, i32 %6, i32* null, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %32, label %36
26: ; preds = %2
%27 = tail call i32 @perror(i8* %0)
br label %36
28: ; preds = %11, %8
%29 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %8 ], [ %1, %11 ]
%30 = tail call i32 @perror(i8* %29)
%31 = tail call i32 @close(i32 %6)
br label %36
32: ; preds = %21
%33 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%34 = tail call i32 @close(i32 %6)
%35 = tail call i32 @close(i32 %19)
br label %36
36: ; preds = %28, %26, %32, %21
%37 = phi i32 [ 0, %21 ], [ -1, %32 ], [ -1, %26 ], [ -1, %28 ]
ret i32 %37
}