#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 */

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int close(int);
int open(char const *, int, ...);
int sendfile(int, int, int /*<<< orphan*/ *, int);
int copyfile(const char *old, const char *new) {
  int oldfd, newfd, ret = -1;
  if ((oldfd = open(old, O_RDONLY)) == -1)
    goto out0;
  if ((newfd = open(new, O_WRONLY | O_TRUNC | O_CREAT, 0600)) == -1)
    goto out1;
  ret = sendfile(newfd, oldfd, NULL, 1 << 16);
  ret |= close(newfd);
out1:
  ret |= close(oldfd);
out0:
  return ret;
}


x86 assembly (clang, -O0):
<copyfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    116c <copyfile+0x3c>
jmp    11db <copyfile+0xab>
mov    -0x10(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
mov    0x2e6f(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11a8 <copyfile+0x78>
jmp    11cd <copyfile+0x9d>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x10000,%ecx
call   1040 <sendfile@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyfile>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11a0 <copyfile+0x70>
mov    %eax,%ebx
mov    0x2e6b(%rip),%rax
mov    0x2e94(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e91(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x180,%edx
xor    %eax,%eax
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11a7 <copyfile+0x77>
mov    %eax,%ebp
mov    %eax,%edi
mov    %ebx,%esi
xor    %edx,%edx
mov    $0x10000,%ecx
call   1040 <sendfile@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1030 <close@plt>
mov    %eax,%ebp
or     %r14d,%ebp
jmp    11ac <copyfile+0x7c>
mov    $0xffffffff,%eax
jmp    11b5 <copyfile+0x85>
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
or     %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define i32 @copyfile(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 -1, i32* %7
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 (i8*, i32, ...) @open(i8* %8, i32 %9)
store i32 %10, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %2
br label %36
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = load i32, i32* @O_CREAT
%19 = or i32 %17, %18
%20 = call i32 (i8*, i32, ...) @open(i8* %14, i32 %19, i32 384)
store i32 %20, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %13
br label %31
23: ; preds = %13
%24 = load i32, i32* %6
%25 = load i32, i32* %5
%26 = call i32 @sendfile(i32 %24, i32 %25, i32* null, i32 65536)
store i32 %26, i32* %7
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %7
%30 = or i32 %29, %28
store i32 %30, i32* %7
br label %31
31: ; preds = %23, %22
%32 = load i32, i32* %5
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %7
%35 = or i32 %34, %33
store i32 %35, i32* %7
br label %36
36: ; preds = %31, %12
%37 = load i32, i32* %7
ret i32 %37
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define i32 @copyfile(i8* nocapture %0, i8* nocapture %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %22, label %6
6: ; preds = %2
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %8, %7
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %11, i32 384)
%13 = icmp eq i32 %12, -1
br i1 %13, label %18, label %14
14: ; preds = %6
%15 = tail call i32 @sendfile(i32 %12, i32 %4, i32* null, i32 65536)
%16 = tail call i32 @close(i32 %12)
%17 = or i32 %16, %15
br label %18
18: ; preds = %6, %14
%19 = phi i32 [ -1, %6 ], [ %17, %14 ]
%20 = tail call i32 @close(i32 %4)
%21 = or i32 %20, %19
br label %22
22: ; preds = %2, %18
%23 = phi i32 [ -1, %2 ], [ %21, %18 ]
ret i32 %23
}