#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 unlink(char *);
int apkg_mv(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 unlink(source);
}


x86 assembly (clang, -O0):
<apkg_mv>:
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    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1193 <apkg_mv+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <apkg_mv+0xb5>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <fstat@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    0x2e45(%rip),%rax
or     (%rax),%esi
mov    -0x24(%rbp),%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11d7 <apkg_mv+0x87>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1205 <apkg_mv+0xb5>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%ecx
lea    -0x2c(%rbp),%rdx
call   1050 <sendfile@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<apkg_mv>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
movl   $0x0,0x4(%rsp)
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     11d8 <apkg_mv+0x88>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
mov    0x2e39(%rip),%rax
mov    0x2e62(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0xc(%rsp),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     11df <apkg_mv+0x8f>
mov    %eax,%ebx
mov    0x8(%rsp),%ecx
lea    0x4(%rsp),%rdx
mov    %eax,%edi
mov    %ebp,%esi
call   1050 <sendfile@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
jmp    11e4 <apkg_mv+0x94>
mov    $0xffffffff,%eax
jmp    11e4 <apkg_mv+0x94>
mov    $0xfffffffe,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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_mv(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 %39
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 %39
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)
%37 = load i8*, i8** %4
%38 = call i32 @unlink(i8* %37)
store i32 %38, i32* %3
br label %39
39: ; preds = %27, %26, %14
%40 = load i32, i32* %3
ret i32 %40
}

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_mv(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 %26, 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 %26, 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)
%25 = call i32 @unlink(i8* %0)
br label %26
26: ; preds = %10, %2, %19
%27 = phi i32 [ %25, %19 ], [ -1, %2 ], [ -2, %10 ]
ret i32 %27
}