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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
scalar_t__ close(int);
char *errno;
int /*<<< orphan*/ error(char *, ...);
int fstat(int, struct stat *);
int open(char const *, int, ...);
int sendfile(int, int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(char *);
int file_copy(const char *src, const char *dst) {
  int ret = -1;
  int in_fd = -1;
  int out_fd = -1;
  struct stat sb;
  off_t offset = 0;
  if (!src || !dst) {
    error("Wrong input");
    goto out;
  }
  in_fd = open(src, O_RDONLY);
  if (in_fd < 0) {
    error("Cannot open source file (errno = %d)", errno);
    goto out;
  }
  out_fd = open(dst, O_RDWR | O_CREAT | O_TRUNC,
                S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (out_fd < 0) {
    error("Cannot create destination file (errno = %s)", strerror(errno));
    goto out;
  }
  if (fstat(in_fd, &sb) == -1) {
    error("Failed obtaining file status");
    goto out;
  }
  if (sendfile(out_fd, in_fd, &offset, sb.st_size) == -1) {
    error("Copying file failed (errno = %s)", strerror(errno));
  }
out:
  if (in_fd >= 0)
    close(in_fd);
  if (out_fd >= 0) {
    if (close(out_fd) < 0)
      error("Error while closing %s: %d", dst, errno);
    else
      ret = 0;
  }
  return ret;
}


x86 assembly (clang, -O0):
<file_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a2 <file_copy+0x42>
cmpq   $0x0,-0x10(%rbp)
jne    11b5 <file_copy+0x55>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
jmp    12d5 <file_copy+0x175>
mov    -0x8(%rbp),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f3 <file_copy+0x93>
mov    0x2ddb(%rip),%rax
mov    (%rax),%rsi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
jmp    12d5 <file_copy+0x175>
mov    -0x10(%rbp),%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    0x2df1(%rip),%rax
or     (%rax),%esi
mov    0x2de0(%rip),%rax
or     (%rax),%esi
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
mov    0x2da6(%rip),%rax
or     (%rax),%edx
mov    0x2d85(%rip),%rax
or     (%rax),%edx
mov    0x2d74(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    126e <file_copy+0x10e>
mov    0x2d67(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
jmp    12d5 <file_copy+0x175>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1296 <file_copy+0x136>
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
jmp    12d5 <file_copy+0x175>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%ecx
lea    -0x24(%rbp),%rdx
call   1040 <sendfile@plt>
cmp    $0xffffffff,%eax
jne    12d0 <file_copy+0x170>
mov    0x2d00(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
jmp    12d5 <file_copy+0x175>
cmpl   $0x0,-0x18(%rbp)
jl     12e7 <file_copy+0x187>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0x1c(%rbp)
jl     1330 <file_copy+0x1d0>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
jge    1324 <file_copy+0x1c4>
mov    -0x10(%rbp),%rsi
mov    0x2caa(%rip),%rax
mov    (%rax),%rdx
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
jmp    132b <file_copy+0x1cb>
movl   $0x0,-0x14(%rbp)
jmp    1330 <file_copy+0x1d0>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_copy>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
test   %rdi,%rdi
je     1245 <file_copy+0xe5>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1245 <file_copy+0xe5>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     1258 <file_copy+0xf8>
mov    %eax,%ebp
mov    0x2e32(%rip),%rax
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
mov    0x2ddf(%rip),%rax
mov    0x2e00(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2ddd(%rip),%rax
or     (%rax),%edx
mov    0x2dcc(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     1272 <file_copy+0x112>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
je     129a <file_copy+0x13a>
mov    0x8(%rsp),%ecx
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
mov    %ebp,%esi
call   1040 <sendfile@plt>
cmp    $0xffffffff,%eax
jne    12a8 <file_copy+0x148>
mov    0x2d8d(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
lea    0xe40(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <error@plt>
jmp    12a8 <file_copy+0x148>
lea    0xdb4(%rip),%rdi
xor    %eax,%eax
call   1050 <error@plt>
jmp    12dc <file_copy+0x17c>
mov    0x2d59(%rip),%rax
mov    (%rax),%rsi
lea    0xda3(%rip),%rdi
xor    %eax,%eax
call   1050 <error@plt>
jmp    12dc <file_copy+0x17c>
mov    0x2d3f(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
lea    0xda9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <error@plt>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    12dc <file_copy+0x17c>
lea    0xdbc(%rip),%rdi
xor    %eax,%eax
call   1050 <error@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %r14d,%edi
call   1030 <close@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    12e1 <file_copy+0x181>
mov    0x2cf0(%rip),%rax
mov    (%rax),%rdx
lea    0xdc9(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <error@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [12 x i8] c"Wrong input\00"
@O_RDONLY = global i32 0
@.str.1 = [37 x i8] c"Cannot open source file (errno = %d)\00"
@errno = global i8* null
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [44 x i8] c"Cannot create destination file (errno = %s)\00"
@.str.3 = [29 x i8] c"Failed obtaining file status\00"
@.str.4 = [33 x i8] c"Copying file failed (errno = %s)\00"
@.str.5 = [27 x i8] c"Error while closing %s: %d\00"
define i32 @file_copy(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 -1, i32* %5
store i32 -1, i32* %6
store i32 -1, i32* %7
store i32 0, i32* %9
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %12, %2
%16 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %65
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = load i32, i32* @O_RDONLY
%20 = call i32 (i8*, i32, ...) @open(i8* %18, i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i8*, i8** @errno
%25 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %65
26: ; preds = %17
%27 = load i8*, i8** %4
%28 = load i32, i32* @O_RDWR
%29 = load i32, i32* @O_CREAT
%30 = or i32 %28, %29
%31 = load i32, i32* @O_TRUNC
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IRUSR
%34 = load i32, i32* @S_IWUSR
%35 = or i32 %33, %34
%36 = load i32, i32* @S_IRGRP
%37 = or i32 %35, %36
%38 = load i32, i32* @S_IROTH
%39 = or i32 %37, %38
%40 = call i32 (i8*, i32, ...) @open(i8* %27, i32 %32, i32 %39)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %26
%44 = load i8*, i8** @errno
%45 = call i32 @strerror(i8* %44)
%46 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %45)
br label %65
47: ; preds = %26
%48 = load i32, i32* %6
%49 = call i32 @fstat(i32 %48, %struct.stat* %8)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %65
53: ; preds = %47
%54 = load i32, i32* %7
%55 = load i32, i32* %6
%56 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @sendfile(i32 %54, i32 %55, i32* %9, i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %64
60: ; preds = %53
%61 = load i8*, i8** @errno
%62 = call i32 @strerror(i8* %61)
%63 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %62)
br label %64
64: ; preds = %60, %53
br label %65
65: ; preds = %64, %51, %43, %23, %15
%66 = load i32, i32* %6
%67 = icmp sge i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i32, i32* %6
%70 = call i64 @close(i32 %69)
br label %71
71: ; preds = %68, %65
%72 = load i32, i32* %7
%73 = icmp sge i32 %72, 0
br i1 %73, label %74, label %84
74: ; preds = %71
%75 = load i32, i32* %7
%76 = call i64 @close(i32 %75)
%77 = icmp slt i64 %76, 0
br i1 %77, label %78, label %82
78: ; preds = %74
%79 = load i8*, i8** %4
%80 = load i8*, i8** @errno
%81 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %79, i8* %80)
br label %83
82: ; preds = %74
store i32 0, i32* %5
br label %83
83: ; preds = %82, %78
br label %84
84: ; preds = %83, %71
%85 = load i32, i32* %5
ret i32 %85
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [12 x i8] c"Wrong input\00"
@O_RDONLY = global i32 0
@.str.1 = [37 x i8] c"Cannot open source file (errno = %d)\00"
@errno = global i8* null
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [44 x i8] c"Cannot create destination file (errno = %s)\00"
@.str.3 = [29 x i8] c"Failed obtaining file status\00"
@.str.4 = [33 x i8] c"Copying file failed (errno = %s)\00"
@.str.5 = [27 x i8] c"Error while closing %s: %d\00"
define i32 @file_copy(i8* %0, i8* %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 = icmp ne i8* %0, null
%8 = icmp ne i8* %1, null
%9 = and i1 %7, %8
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %60
12: ; preds = %2
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i8*, i8** @errno
%18 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %60
19: ; preds = %12
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_CREAT
%22 = or i32 %21, %20
%23 = load i32, i32* @O_TRUNC
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IRUSR
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %26, %25
%28 = load i32, i32* @S_IRGRP
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IROTH
%31 = or i32 %29, %30
%32 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %24, i32 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %48, label %34
34: ; preds = %19
%35 = call i32 @fstat(i32 %14, %struct.stat* %3)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %53
39: ; preds = %34
%40 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @sendfile(i32 %32, i32 %14, i32* %4, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %53
44: ; preds = %39
%45 = load i8*, i8** @errno
%46 = call i32 @strerror(i8* %45)
%47 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %46)
br label %53
48: ; preds = %19
%49 = load i8*, i8** @errno
%50 = tail call i32 @strerror(i8* %49)
%51 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %50)
%52 = tail call i64 @close(i32 %14)
br label %60
53: ; preds = %39, %44, %37
%54 = call i64 @close(i32 %14)
%55 = call i64 @close(i32 %32)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = load i8*, i8** @errno
%59 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %1, i8* %58)
br label %60
60: ; preds = %10, %16, %48, %53, %57
%61 = phi i32 [ -1, %57 ], [ -1, %48 ], [ 0, %53 ], [ -1, %16 ], [ -1, %10 ]
ret i32 %61
}