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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fstat(int, struct stat *);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sendfile(int, int, int /*<<< orphan*/ *, int /*<<< orphan*/);
void cp(char *file, char *tocopy) {
  int src, dest;
  struct stat stat_buf;
  off_t offset = 0;
  src = open(file, O_RDONLY);
  if (src == -1) {
    printf("Error, %s does not exists.\n", file);
    return;
  }
  fstat(src, &stat_buf);
  dest = open(tocopy, O_WRONLY | O_CREAT, stat_buf.st_mode);
  sendfile(dest, src, &offset, stat_buf.st_size);
  close(dest);
  close(src);
}


x86 assembly (clang, -O0):
<cp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    119d <cp+0x4d>
mov    -0x8(%rbp),%rsi
lea    0xe6d(%rip),%rdi
call   1030 <printf@plt>
jmp    11ee <cp+0x9e>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <fstat@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    0x2e3b(%rip),%rax
or     (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%ecx
lea    -0x24(%rbp),%rdx
call   1050 <sendfile@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movl   $0x0,0xc(%rsp)
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11d8 <cp+0x88>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
mov    0x2e36(%rip),%rax
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x14(%rsp),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebp
mov    0x10(%rsp),%ecx
lea    0xc(%rsp),%rdx
mov    %eax,%edi
mov    %ebx,%esi
call   1050 <sendfile@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe21(%rip),%rdi
mov    %r15,%rsi
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@.str = [28 x i8] c"Error, %s does not exists.\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define void @cp(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %8
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 (i8*, i32, ...) @open(i8* %9, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %36
17: ; preds = %2
%18 = load i32, i32* %5
%19 = call i32 @fstat(i32 %18, %struct.stat* %7)
%20 = load i8*, i8** %4
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 1
%25 = load i32, i32* %24
%26 = call i32 (i8*, i32, ...) @open(i8* %20, i32 %23, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = load i32, i32* %5
%29 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @sendfile(i32 %27, i32 %28, i32* %8, i32 %30)
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %17, %14
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@.str = [28 x i8] c"Error, %s does not exists.\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define void @cp(i8* %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca i32
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %25
12: ; preds = %2
%13 = call i32 @fstat(i32 %8, %struct.stat* %3)
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %15, %14
%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 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @sendfile(i32 %19, i32 %8, i32* %4, i32 %21)
%23 = call i32 @close(i32 %19)
%24 = call i32 @close(i32 %8)
br label %25
25: ; preds = %12, %10
ret void
}