#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_mode;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ BUFSIZ;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
int open(char const *, int, ...);
int /*<<< orphan*/ perror(char *);
scalar_t__ sendfile(int, int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int copy_file_paths(char const *source_file, char const *target_file) {
  int source_fd;
  int target_fd;
  int ret = -1;
  struct stat st;
  if ((source_fd = open(source_file, O_RDONLY)) == -1) {
    return ret;
  }
  if (fstat(source_fd, &st) == -1) {
    perror("stat");
    ret = -2;
    goto cleanup_in;
  }
  if ((target_fd = open(target_file, O_WRONLY | O_CREAT, st.st_mode)) == -1) {
    ret = -3;
    goto cleanup_in;
  }
  ssize_t read;
  while ((read = sendfile(target_fd, source_fd, NULL, BUFSIZ)) > 0)
    ;
  if (read < 0) {
    perror("sendfile");
    ret = -4;
  }
  ret = 0;
cleanup_all:
  close(target_fd);
cleanup_in:
  close(source_fd);
  return ret;
}


x86 assembly (clang, -O0):
<copy_file_paths>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1192 <copy_file_paths+0x42>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <copy_file_paths+0x112>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11bf <copy_file_paths+0x6f>
lea    0xe52(%rip),%rdi
call   1060 <perror@plt>
movl   $0xfffffffe,-0x24(%rbp)
jmp    1254 <copy_file_paths+0x104>
mov    -0x18(%rbp),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%rip),%rax
or     (%rax),%esi
mov    -0x28(%rbp),%edx
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11f7 <copy_file_paths+0xa7>
movl   $0xfffffffd,-0x24(%rbp)
jmp    1254 <copy_file_paths+0x104>
jmp    11fc <copy_file_paths+0xac>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    0x2dc7(%rip),%rax
mov    (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sendfile@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jle    1227 <copy_file_paths+0xd7>
jmp    11fc <copy_file_paths+0xac>
cmpq   $0x0,-0x30(%rbp)
jge    1245 <copy_file_paths+0xf5>
lea    0xdcc(%rip),%rdi
call   1060 <perror@plt>
movl   $0xfffffffc,-0x24(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file_paths>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11d0 <copy_file_paths+0x80>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
je     11f1 <copy_file_paths+0xa1>
mov    0x2e38(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    (%rsp),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11d7 <copy_file_paths+0x87>
mov    %eax,%ebp
mov    0x2e22(%rip),%rbx
xchg   %ax,%ax
mov    (%rbx),%ecx
mov    %ebp,%edi
mov    %r15d,%esi
xor    %edx,%edx
call   1040 <sendfile@plt>
test   %rax,%rax
jg     11b0 <copy_file_paths+0x60>
js     1204 <copy_file_paths+0xb4>
mov    %ebp,%edi
call   1030 <close@plt>
xor    %ebp,%ebp
jmp    11dc <copy_file_paths+0x8c>
mov    $0xffffffff,%ebp
jmp    11e4 <copy_file_paths+0x94>
mov    $0xfffffffd,%ebp
mov    %r15d,%edi
call   1030 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe08(%rip),%rdi
call   1060 <perror@plt>
mov    $0xfffffffe,%ebp
jmp    11dc <copy_file_paths+0x8c>
lea    0xdfa(%rip),%rdi
call   1060 <perror@plt>
jmp    11c5 <copy_file_paths+0x75>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"stat\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@BUFSIZ = global i32 0
@.str.1 = [9 x i8] c"sendfile\00"
define i32 @copy_file_paths(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %34, label %8
8: ; preds = %2
%9 = call i32 @fstat(i32 %6, %struct.stat* %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %31
13: ; preds = %8
%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 0
%18 = load i32, i32* %17
%19 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %16, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %31, label %21
21: ; preds = %13, %21
%22 = load i32, i32* @BUFSIZ
%23 = tail call i64 @sendfile(i32 %19, i32 %6, i32* null, i32 %22)
%24 = icmp sgt i64 %23, 0
br i1 %24, label %21, label %25
25: ; preds = %21
%26 = icmp slt i64 %23, 0
br i1 %26, label %27, label %29
27: ; preds = %25
%28 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %25
%30 = tail call i32 @close(i32 %19)
br label %31
31: ; preds = %13, %29, %11
%32 = phi i32 [ -2, %11 ], [ 0, %29 ], [ -3, %13 ]
%33 = tail call i32 @close(i32 %6)
br label %34
34: ; preds = %2, %31
%35 = phi i32 [ %32, %31 ], [ -1, %2 ]
ret i32 %35
}