#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 st_mode;
  scalar_t__ st_size;
};
typedef scalar_t__ ssize_t;
typedef scalar_t__ off_t;

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
size_t SSIZE_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fstat(int, struct stat *);
int open(char const *, int, ...);
scalar_t__ sendfile(int, int, int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ unlink(char const *);
int fs_copy_file(const char *dest, const char *src, int force) {
  int fdfrom, fdto, result;
  ssize_t length;
  size_t count;
  struct stat s;
  result = -1;
  fdfrom = open(src, O_RDONLY);
  if (fdfrom >= 0) {
    if (!fstat(fdfrom, &s)) {
      fdto = open(dest, O_WRONLY | O_TRUNC | O_CREAT | (force ? 0 : O_EXCL),
                  s.st_mode | 0200);
      if (fdto >= 0) {
        for (;;) {
          count = SSIZE_MAX;
          if ((off_t)count > s.st_size)
            count = (size_t)s.st_size;
          length = sendfile(fdto, fdfrom, NULL, count);
          if (length < 0)
            break;
          s.st_size -= (off_t)length;
          if (s.st_size == 0) {
            result = 0;
            break;
          }
        }
      }
      close(fdto);
      if (result)
        unlink(dest);
    }
    close(fdfrom);
  }
  return result;
}


x86 assembly (clang, -O0):
<fs_copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     12b7 <fs_copy_file+0x167>
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%eax
jne    12af <fs_copy_file+0x15f>
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    0x2e38(%rip),%rcx
or     (%rcx),%eax
mov    0x2e37(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11da <fs_copy_file+0x8a>
xor    %eax,%eax
mov    %eax,-0x54(%rbp)
jmp    11e6 <fs_copy_file+0x96>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    -0x54(%rbp),%eax
or     %eax,%esi
mov    -0x40(%rbp),%edx
or     $0x80,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     128f <fs_copy_file+0x13f>
jmp    1214 <fs_copy_file+0xc4>
mov    0x2dbd(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x38(%rbp),%rax
jle    1238 <fs_copy_file+0xe8>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x30(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sendfile@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    125f <fs_copy_file+0x10f>
jmp    128a <fs_copy_file+0x13a>
mov    -0x28(%rbp),%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1285 <fs_copy_file+0x135>
movl   $0x0,-0x20(%rbp)
jmp    128a <fs_copy_file+0x13a>
jmp    1214 <fs_copy_file+0xc4>
jmp    128f <fs_copy_file+0x13f>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x20(%rbp)
je     12aa <fs_copy_file+0x15a>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
jmp    12af <fs_copy_file+0x15f>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fs_copy_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%rax
mov    %rdi,%r14
mov    0x2e63(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     122d <fs_copy_file+0xdd>
mov    %eax,%r12d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
jne    1244 <fs_copy_file+0xf4>
mov    0x2e0b(%rip),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e41(%rip),%rax
or     (%rax),%esi
mov    $0x0,%eax
test   %ebp,%ebp
jne    11cb <fs_copy_file+0x7b>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
or     %eax,%esi
mov    $0x80,%edx
or     0x8(%rsp),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1235 <fs_copy_file+0xe5>
mov    0x10(%rsp),%rbx
mov    0x2de6(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%r13),%rcx
cmp    %rbx,%rcx
cmovg  %rbx,%rcx
mov    %ebp,%edi
mov    %r12d,%esi
xor    %edx,%edx
call   1050 <sendfile@plt>
test   %rax,%rax
js     1235 <fs_copy_file+0xe5>
sub    %rax,%rbx
jne    1200 <fs_copy_file+0xb0>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %r15d,%r15d
jmp    1244 <fs_copy_file+0xf4>
mov    $0xffffffff,%r15d
jmp    124c <fs_copy_file+0xfc>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i64 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@SSIZE_MAX = global i64 0
define i32 @fs_copy_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 -1, i32* %9
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_RDONLY
%15 = call i32 (i8*, i32, ...) @open(i8* %13, i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %84
18: ; preds = %3
%19 = load i32, i32* %7
%20 = call i32 @fstat(i32 %19, %struct.stat* %12)
%21 = icmp ne i32 %20, 0
br i1 %21, label %81, label %22
22: ; preds = %18
%23 = load i8*, i8** %4
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_TRUNC
%26 = or i32 %24, %25
%27 = load i32, i32* @O_CREAT
%28 = or i32 %26, %27
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %22
br label %34
32: ; preds = %22
%33 = load i32, i32* @O_EXCL
br label %34
34: ; preds = %32, %31
%35 = phi i32 [ 0, %31 ], [ %33, %32 ]
%36 = or i32 %28, %35
%37 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%38 = load i32, i32* %37
%39 = or i32 %38, 128
%40 = call i32 (i8*, i32, ...) @open(i8* %23, i32 %36, i32 %39)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp sge i32 %41, 0
br i1 %42, label %43, label %72
43: ; preds = %34
br label %44
44: ; preds = %70, %43
%45 = load i64, i64* @SSIZE_MAX
store i64 %45, i64* %11
%46 = load i64, i64* %11
%47 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 1
%48 = load i64, i64* %47
%49 = icmp sgt i64 %46, %48
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 1
%52 = load i64, i64* %51
store i64 %52, i64* %11
br label %53
53: ; preds = %50, %44
%54 = load i32, i32* %8
%55 = load i32, i32* %7
%56 = load i64, i64* %11
%57 = call i64 @sendfile(i32 %54, i32 %55, i32* null, i64 %56)
store i64 %57, i64* %10
%58 = load i64, i64* %10
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %53
br label %71
61: ; preds = %53
%62 = load i64, i64* %10
%63 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 1
%64 = load i64, i64* %63
%65 = sub nsw i64 %64, %62
store i64 %65, i64* %63
%66 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 1
%67 = load i64, i64* %66
%68 = icmp eq i64 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %61
store i32 0, i32* %9
br label %71
70: ; preds = %61
br label %44
71: ; preds = %69, %60
br label %72
72: ; preds = %71, %34
%73 = load i32, i32* %8
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %9
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %72
%78 = load i8*, i8** %4
%79 = call i32 @unlink(i8* %78)
br label %80
80: ; preds = %77, %72
br label %81
81: ; preds = %80, %18
%82 = load i32, i32* %7
%83 = call i32 @close(i32 %82)
br label %84
84: ; preds = %81, %3
%85 = load i32, i32* %9
ret i32 %85
}

LLVM IR (-O2):
%struct.stat = type { i32, i64 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@SSIZE_MAX = global i64 0
define i32 @fs_copy_file(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %6)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %48
9: ; preds = %3
%10 = call i32 @fstat(i32 %7, %struct.stat* %4)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %45
12: ; preds = %9
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %14, %13
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = icmp eq i32 %2, 0
%19 = load i32, i32* @O_EXCL
%20 = select i1 %18, i32 %19, i32 0
%21 = or i32 %17, %20
%22 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%23 = load i32, i32* %22
%24 = or i32 %23, 128
%25 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %21, i32 %24)
%26 = icmp sgt i32 %25, -1
br i1 %26, label %27, label %42
27: ; preds = %12
%28 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%29 = load i64, i64* %28
br label %30
30: ; preds = %27, %37
%31 = phi i64 [ %29, %27 ], [ %38, %37 ]
%32 = load i64, i64* @SSIZE_MAX
%33 = icmp sgt i64 %32, %31
%34 = select i1 %33, i64 %31, i64 %32
%35 = tail call i64 @sendfile(i32 %25, i32 %7, i32* null, i64 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %42, label %37
37: ; preds = %30
%38 = sub nsw i64 %31, %35
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %30
40: ; preds = %37
%41 = tail call i32 @close(i32 %25)
br label %45
42: ; preds = %30, %12
%43 = tail call i32 @close(i32 %25)
%44 = tail call i32 @unlink(i8* %0)
br label %45
45: ; preds = %40, %42, %9
%46 = phi i32 [ -1, %9 ], [ -1, %42 ], [ 0, %40 ]
%47 = tail call i32 @close(i32 %7)
br label %48
48: ; preds = %45, %3
%49 = phi i32 [ %46, %45 ], [ -1, %3 ]
ret i32 %49
}