#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 /*<<< orphan*/ EACCES;
int /*<<< orphan*/ EBADF;
int /*<<< orphan*/ EINVAL;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char const *, int, ...);
int sendfile(int, int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int stat(char const *, struct stat *);
int copy(const char *old_name, const char *new_name) {
  struct stat stat_buf;
  if (stat(old_name, &stat_buf) == -1) {
    errno = EINVAL;
    return -1;
  }
  int source = open(old_name, O_RDONLY);
  int target = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, stat_buf.st_mode);
  if (source == -1) {
    errno = EBADF;
    return -1;
  }
  if (target == -1) {
    close(source);
    errno = EBADF;
    return -1;
  }
  if (sendfile(target, source, NULL, stat_buf.st_size) == -1) {
    close(source);
    close(target);
    errno = EACCES;
    return -1;
  }
  close(source);
  close(target);
  return 0;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    1184 <copy+0x44>
mov    0x2e4b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <copy+0x13f>
mov    -0x10(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    0x2e49(%rip),%rax
or     (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ef <copy+0xaf>
mov    0x2dd8(%rip),%rax
mov    (%rax),%ecx
mov    0x2ddf(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <copy+0x13f>
cmpl   $0xffffffff,-0x28(%rbp)
jne    121f <copy+0xdf>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%ecx
mov    0x2daf(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <copy+0x13f>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sendfile@plt>
cmp    $0xffffffff,%eax
jne    1268 <copy+0x128>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%ecx
mov    0x2d66(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <copy+0x13f>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %rsp,%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     11e3 <copy+0xa3>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%r14d
mov    0x2e25(%rip),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e63(%rip),%rax
or     (%rax),%esi
mov    0x4(%rsp),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    0x2e0c(%rip),%rbp
cmp    $0xffffffff,%r14d
je     11f4 <copy+0xb4>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11ec <copy+0xac>
mov    (%rsp),%ecx
xor    %ebp,%ebp
mov    %ebx,%edi
mov    %r14d,%esi
xor    %edx,%edx
call   1050 <sendfile@plt>
mov    %eax,%r15d
mov    %r14d,%edi
call   1030 <close@plt>
mov    %ebx,%edi
call   1030 <close@plt>
cmp    $0xffffffff,%r15d
jne    1205 <copy+0xc5>
mov    0x2def(%rip),%rbp
jmp    11f4 <copy+0xb4>
mov    0x2dce(%rip),%rbp
jmp    11f4 <copy+0xb4>
mov    %r14d,%edi
call   1030 <close@plt>
mov    0x0(%rbp),%eax
mov    0x2dc2(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@EINVAL = global i32 0
@errno = global i32 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@EBADF = global i32 0
@EACCES = global i32 0
define i32 @copy(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @stat(i8* %9, %struct.stat* %6)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* @EINVAL
store i32 %13, i32* @errno
store i32 -1, i32* %3
br label %56
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i32, i32* @O_RDONLY
%17 = call i32 (i8*, i32, ...) @open(i8* %15, i32 %16)
store i32 %17, i32* %7
%18 = load i8*, i8** %5
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%25 = load i32, i32* %24
%26 = call i32 (i8*, i32, ...) @open(i8* %18, i32 %23, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %14
%30 = load i32, i32* @EBADF
store i32 %30, i32* @errno
store i32 -1, i32* %3
br label %56
31: ; preds = %14
%32 = load i32, i32* %8
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i32, i32* %7
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* @EBADF
store i32 %37, i32* @errno
store i32 -1, i32* %3
br label %56
38: ; preds = %31
%39 = load i32, i32* %8
%40 = load i32, i32* %7
%41 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @sendfile(i32 %39, i32 %40, i32* null, i32 %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %51
45: ; preds = %38
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* @EACCES
store i32 %50, i32* @errno
store i32 -1, i32* %3
br label %56
51: ; preds = %38
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %8
%55 = call i32 @close(i32 %54)
store i32 0, i32* %3
br label %56
56: ; preds = %51, %45, %34, %29, %12
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@EINVAL = global i32 0
@errno = global i32 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@EBADF = global i32 0
@EACCES = global i32 0
define i32 @copy(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %30, label %7
7: ; preds = %2
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %8)
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %12, %13
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%16 = load i32, i32* %15
%17 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %14, i32 %16)
%18 = icmp eq i32 %9, -1
br i1 %18, label %30, label %19
19: ; preds = %7
%20 = icmp eq i32 %17, -1
br i1 %20, label %21, label %23
21: ; preds = %19
%22 = tail call i32 @close(i32 %9)
br label %30
23: ; preds = %19
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = tail call i32 @sendfile(i32 %17, i32 %9, i32* null, i32 %25)
%27 = icmp eq i32 %26, -1
%28 = tail call i32 @close(i32 %9)
%29 = tail call i32 @close(i32 %17)
br i1 %27, label %30, label %33
30: ; preds = %23, %7, %2, %21
%31 = phi i32* [ @EBADF, %21 ], [ @EINVAL, %2 ], [ @EBADF, %7 ], [ @EACCES, %23 ]
%32 = load i32, i32* %31
store i32 %32, i32* @errno
br label %33
33: ; preds = %30, %23
%34 = phi i32 [ 0, %23 ], [ -1, %30 ]
ret i32 %34
}