#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 */
typedef scalar_t__ const off_t;

/* Variables and functions */
int EINTR;
int O_CREAT;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ POSIX_FADV_SEQUENTIAL;
int /*<<< orphan*/ POSIX_FADV_WILLNEED;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char const *, int);
int posix_fadvise(int, int /*<<< orphan*/, scalar_t__ const,
                  int /*<<< orphan*/);
int posix_fallocate(int, int /*<<< orphan*/, scalar_t__ const);
int sendfile(int, int, scalar_t__ const *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(int);
int copy(const char *src, const char *dest, const off_t filesize) {
  int err = 0;
  int in, out;
  off_t ofs = 0;
  in = open(src, O_RDONLY);
  if (in == -1) {
    fprintf(stderr, "%s\n", strerror(errno));
    return 1;
  }
  out = open(dest, O_WRONLY | O_CREAT);
  if (out == -1) {
    close(in);
    fprintf(stderr, "%s\n", strerror(errno));
    return 1;
  }
  if ((err = posix_fadvise(in, 0, filesize, POSIX_FADV_WILLNEED))) {
    fprintf(stderr, "ERROR: %s\n", strerror(err));
    goto close_fds;
  }
  if ((err = posix_fadvise(in, 0, filesize, POSIX_FADV_SEQUENTIAL))) {
    fprintf(stderr, "ERROR: %s\n", strerror(err));
    goto close_fds;
  }
  if ((err = posix_fallocate(out, 0, filesize))) {
    fprintf(stderr, "ERROR: %s\n", strerror(err));
    goto close_fds;
  }
  while (ofs < filesize) {
    if (sendfile(out, in, &ofs, (size_t)(filesize - ofs)) == -1) {
      if (errno == EINTR) {
        err = EINTR;
        goto close_fds;
      }
      continue;
    }
  }
close_fds:
  close(in);
  close(out);
  return err;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11ea <copy+0x7a>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xe2e(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b2 <copy+0x242>
mov    -0x18(%rbp),%rdi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1252 <copy+0xe2>
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xdc6(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b2 <copy+0x242>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    0x2d78(%rip),%rax
mov    (%rax),%ecx
xor    %esi,%esi
call   1040 <posix_fadvise@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     12a0 <copy+0x130>
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x24(%rbp),%edi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xd6a(%rip),%rsi
call   1050 <fprintf@plt>
jmp    139c <copy+0x22c>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    0x2d12(%rip),%rax
mov    (%rax),%ecx
xor    %esi,%esi
call   1040 <posix_fadvise@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     12ee <copy+0x17e>
mov    0x2cfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x24(%rbp),%edi
call   1090 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xd1c(%rip),%rsi
call   1050 <fprintf@plt>
jmp    139c <copy+0x22c>
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1060 <posix_fallocate@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     1333 <copy+0x1c3>
mov    0x2cb9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x24(%rbp),%edi
call   1090 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xcd7(%rip),%rsi
call   1050 <fprintf@plt>
jmp    139c <copy+0x22c>
jmp    1338 <copy+0x1c8>
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jge    1397 <copy+0x227>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x20(%rbp),%rcx
sub    $0x0,%rcx
lea    -0x38(%rbp),%rdx
call   1070 <sendfile@plt>
cmp    $0xffffffff,%eax
jne    1392 <copy+0x222>
mov    0x2c4d(%rip),%rax
mov    (%rax),%eax
mov    0x2c3c(%rip),%rcx
cmp    (%rcx),%eax
jne    138d <copy+0x21d>
mov    0x2c2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    139c <copy+0x22c>
jmp    1338 <copy+0x1c8>
jmp    1338 <copy+0x1c8>
jmp    139c <copy+0x22c>
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
movq   $0x0,(%rsp)
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1253 <copy+0xe3>
mov    %eax,%r12d
mov    0x2df6(%rip),%rax
mov    0x2e47(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     124b <copy+0xdb>
mov    %eax,%r15d
mov    0x2e08(%rip),%rax
mov    (%rax),%ecx
mov    %r12d,%edi
xor    %esi,%esi
mov    %r14,%rdx
call   1040 <posix_fadvise@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1216 <copy+0xa6>
mov    0x2dd4(%rip),%rax
mov    (%rax),%ecx
mov    %r12d,%edi
xor    %esi,%esi
mov    %r14,%rdx
call   1040 <posix_fadvise@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1216 <copy+0xa6>
xor    %ebp,%ebp
mov    %r15d,%edi
xor    %esi,%esi
mov    %r14,%rdx
call   1060 <posix_fallocate@plt>
mov    %eax,%ebx
test   %eax,%eax
je     1291 <copy+0x121>
mov    0x2dab(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1090 <strerror@plt>
lea    0xdd3(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    %ebx,%ebp
mov    %r12d,%edi
call   1030 <close@plt>
mov    %r15d,%edi
call   1030 <close@plt>
jmp    1280 <copy+0x110>
mov    %r12d,%edi
call   1030 <close@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd96(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r14,%r14
jle    1239 <copy+0xc9>
mov    %rsp,%rbx
mov    0x2d10(%rip),%r13
mov    %r15d,%edi
mov    %r12d,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1070 <sendfile@plt>
cmp    $0xffffffff,%eax
jne    12a0 <copy+0x130>
mov    0x0(%r13),%ebp
mov    0x2cf7(%rip),%rax
cmp    %ebp,(%rax)
jne    12a0 <copy+0x130>
jmp    1239 <copy+0xc9>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@errno = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@POSIX_FADV_WILLNEED = global i32 0
@.str.1 = [11 x i8] c"ERROR: %s\0A\00"
@POSIX_FADV_SEQUENTIAL = global i32 0
@EINTR = global i32 0
define i32 @copy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
store i64 0, i64* %11
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 1, i32* %4
br label %95
22: ; preds = %3
%23 = load i8*, i8** %6
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_CREAT
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %23, i32 %26)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %37
30: ; preds = %22
%31 = load i32, i32* %9
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %35)
store i32 1, i32* %4
br label %95
37: ; preds = %22
%38 = load i32, i32* %9
%39 = load i64, i64* %7
%40 = load i32, i32* @POSIX_FADV_WILLNEED
%41 = call i32 @posix_fadvise(i32 %38, i32 0, i64 %39, i32 %40)
store i32 %41, i32* %8
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %37
%44 = load i32, i32* @stderr
%45 = load i32, i32* %8
%46 = call i8* @strerror(i32 %45)
%47 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %46)
br label %89
48: ; preds = %37
%49 = load i32, i32* %9
%50 = load i64, i64* %7
%51 = load i32, i32* @POSIX_FADV_SEQUENTIAL
%52 = call i32 @posix_fadvise(i32 %49, i32 0, i64 %50, i32 %51)
store i32 %52, i32* %8
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %48
%55 = load i32, i32* @stderr
%56 = load i32, i32* %8
%57 = call i8* @strerror(i32 %56)
%58 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %57)
br label %89
59: ; preds = %48
%60 = load i32, i32* %10
%61 = load i64, i64* %7
%62 = call i32 @posix_fallocate(i32 %60, i32 0, i64 %61)
store i32 %62, i32* %8
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %59
%65 = load i32, i32* @stderr
%66 = load i32, i32* %8
%67 = call i8* @strerror(i32 %66)
%68 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %67)
br label %89
69: ; preds = %59
br label %70
70: ; preds = %87, %86, %69
%71 = load i64, i64* %7
%72 = icmp slt i64 0, %71
br i1 %72, label %73, label %88
73: ; preds = %70
%74 = load i32, i32* %10
%75 = load i32, i32* %9
%76 = load i64, i64* %7
%77 = sub nsw i64 %76, 0
%78 = call i32 @sendfile(i32 %74, i32 %75, i64* %11, i64 %77)
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %87
80: ; preds = %73
%81 = load i32, i32* @errno
%82 = load i32, i32* @EINTR
%83 = icmp eq i32 %81, %82
br i1 %83, label %84, label %86
84: ; preds = %80
%85 = load i32, i32* @EINTR
store i32 %85, i32* %8
br label %89
86: ; preds = %80
br label %70
87: ; preds = %73
br label %70
88: ; preds = %70
br label %89
89: ; preds = %88, %84, %64, %54, %43
%90 = load i32, i32* %9
%91 = call i32 @close(i32 %90)
%92 = load i32, i32* %10
%93 = call i32 @close(i32 %92)
%94 = load i32, i32* %8
store i32 %94, i32* %4
br label %95
95: ; preds = %89, %30, %17
%96 = load i32, i32* %4
ret i32 %96
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@errno = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@POSIX_FADV_WILLNEED = global i32 0
@.str.1 = [11 x i8] c"ERROR: %s\0A\00"
@POSIX_FADV_SEQUENTIAL = global i32 0
@EINTR = global i32 0
define i32 @copy(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %56
14: ; preds = %3
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %16, %15
%18 = tail call i32 @open(i8* %1, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %14
%21 = tail call i32 @close(i32 %7)
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %56
26: ; preds = %14
%27 = load i32, i32* @POSIX_FADV_WILLNEED
%28 = tail call i32 @posix_fadvise(i32 %7, i32 0, i64 %2, i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %47
30: ; preds = %26
%31 = load i32, i32* @POSIX_FADV_SEQUENTIAL
%32 = tail call i32 @posix_fadvise(i32 %7, i32 0, i64 %2, i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %47
34: ; preds = %30
%35 = tail call i32 @posix_fallocate(i32 %18, i32 0, i64 %2)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %47
37: ; preds = %34
%38 = icmp sgt i64 %2, 0
br i1 %38, label %39, label %52
39: ; preds = %37, %42
%40 = call i32 @sendfile(i32 %18, i32 %7, i64* %4, i64 %2)
%41 = icmp eq i32 %40, -1
br i1 %41, label %43, label %42
42: ; preds = %39, %43
br label %39
43: ; preds = %39
%44 = load i32, i32* @errno
%45 = load i32, i32* @EINTR
%46 = icmp eq i32 %44, %45
br i1 %46, label %52, label %42
47: ; preds = %34, %30, %26
%48 = phi i32 [ %28, %26 ], [ %32, %30 ], [ %35, %34 ]
%49 = load i32, i32* @stderr
%50 = tail call i8* @strerror(i32 %48)
%51 = tail call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %43, %47, %37
%53 = phi i32 [ 0, %37 ], [ %48, %47 ], [ %45, %43 ]
%54 = call i32 @close(i32 %7)
%55 = call i32 @close(i32 %18)
br label %56
56: ; preds = %52, %20, %9
%57 = phi i32 [ 1, %9 ], [ 1, %20 ], [ %53, %52 ]
ret i32 %57
}