#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*/ sendfile(int, int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int apkg_cp(char *source, char *destination) {
  int src, dest;
  struct stat st;
  off_t offset = 0;
  if ((src = open(source, O_RDONLY)) < 0)
    return -1;
  fstat(src, &st);
  if ((dest = open(destination, O_WRONLY | O_CREAT, st.st_mode)) < 0)
    return -2;
  sendfile(dest, src, &offset, st.st_size);
  close(dest);
  close(src);
  return 0;
}


x86 assembly (clang, -O0):
<apkg_cp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1183 <apkg_cp+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <apkg_cp+0xb0>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <fstat@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    0x2e55(%rip),%rax
or     (%rax),%esi
mov    -0x24(%rbp),%edx
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11c7 <apkg_cp+0x87>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11f0 <apkg_cp+0xb0>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%ecx
lea    -0x2c(%rbp),%rdx
call   1040 <sendfile@plt>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<apkg_cp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
movl   $0x0,0xc(%rsp)
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1050 <open@plt>
test   %eax,%eax
js     11c2 <apkg_cp+0x82>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
mov    0x2e4a(%rip),%rax
mov    0x2e73(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x14(%rsp),%edx
xor    %r15d,%r15d
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <open@plt>
test   %eax,%eax
js     11ca <apkg_cp+0x8a>
mov    %eax,%ebp
mov    0x10(%rsp),%ecx
lea    0xc(%rsp),%rdx
mov    %eax,%edi
mov    %ebx,%esi
call   1040 <sendfile@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%edi
call   1030 <close@plt>
jmp    11d0 <apkg_cp+0x90>
mov    $0xffffffff,%r15d
jmp    11d0 <apkg_cp+0x90>
mov    $0xfffffffe,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @apkg_cp(i8* nocapture %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 slt i32 %8, 0
br i1 %9, label %25, label %10
10: ; preds = %2
%11 = call i32 @fstat(i32 %8, %struct.stat* %3)
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%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 slt i32 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %10
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @sendfile(i32 %17, i32 %8, i32* %4, i32 %21)
%23 = call i32 @close(i32 %17)
%24 = call i32 @close(i32 %8)
br label %25
25: ; preds = %10, %2, %19
%26 = phi i32 [ 0, %19 ], [ -1, %2 ], [ -2, %10 ]
ret i32 %26
}