#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 int /*<<< orphan*/ buf;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int, ...);
scalar_t__ read(int, char **, int);
scalar_t__ stat(char const *, struct stat *);
scalar_t__ write(int, char **, scalar_t__);
bool fs_cp_file(const char *src, const char *dst) {
  int fh_source;
  int fh_destination;
  struct stat stat_source;
  ssize_t filesize;
  ssize_t readbytes;
  ssize_t writtenbytes;
  char buf[65536];
  bool retval = false;
  if (stat(src, &stat_source))
    return false;
  fh_source = open(src, O_RDONLY);
  if (fh_source == -1)
    return false;
  fh_destination = open(dst, O_CREAT | O_WRONLY, stat_source.st_mode & 07777);
  if (fh_destination == -1) {
    goto bail_out_close_source;
  }
  filesize = stat_source.st_size;
  while (filesize > 0) {
    readbytes = read(fh_source, &buf, sizeof(buf));
    writtenbytes = write(fh_destination, &buf, readbytes);
    if (writtenbytes != readbytes) {
      goto bail_out_close_dest;
    }
    filesize -= writtenbytes;
  }
  retval = true;
bail_out_close_dest:
  (void)close(fh_destination);
bail_out_close_source:
  (void)close(fh_source);
  return retval;
}


x86 assembly (clang, -O0):
<fs_cp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10060,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x10054(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     1190 <fs_cp_file+0x40>
movl   $0x0,-0x4(%rbp)
jmp    1281 <fs_cp_file+0x131>
mov    -0x10(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11bd <fs_cp_file+0x6d>
movl   $0x0,-0x4(%rbp)
jmp    1281 <fs_cp_file+0x131>
mov    -0x18(%rbp),%rdi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    0x2df7(%rip),%rax
or     (%rax),%esi
mov    -0x30(%rbp),%edx
and    $0xfff,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11f5 <fs_cp_file+0xa5>
jmp    1270 <fs_cp_file+0x120>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jle    125e <fs_cp_file+0x10e>
mov    -0x1c(%rbp),%edi
lea    -0x10050(%rbp),%rsi
mov    $0x10000,%edx
call   1050 <read@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x10050(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x40(%rbp),%rax
je     124a <fs_cp_file+0xfa>
jmp    1268 <fs_cp_file+0x118>
mov    -0x48(%rbp),%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    11fd <fs_cp_file+0xad>
movl   $0x1,-0x10054(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x10054(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fs_cp_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10018,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1060 <stat@plt>
xor    %r15d,%r15d
test   %rax,%rax
jne    1233 <fs_cp_file+0xe3>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1233 <fs_cp_file+0xe3>
mov    %eax,%r14d
mov    0x2e54(%rip),%rax
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0xfff,%edx
and    (%rsp),%edx
xor    %r15d,%r15d
mov    %rbp,%rdi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     122b <fs_cp_file+0xdb>
mov    %eax,%r12d
mov    0x8(%rsp),%rbx
mov    $0x1,%r15d
test   %rbx,%rbx
jle    1223 <fs_cp_file+0xd3>
lea    0x10(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r13,%rsi
mov    $0x10000,%edx
call   1050 <read@plt>
mov    %rax,%rbp
mov    %r12d,%edi
mov    %r13,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    %rbp,%rax
jne    1220 <fs_cp_file+0xd0>
sub    %rax,%rbx
test   %rbx,%rbx
jg     11f0 <fs_cp_file+0xa0>
jmp    1223 <fs_cp_file+0xd3>
xor    %r15d,%r15d
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %r15d,%eax
add    $0x10018,%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_CREAT = global i32 0
@O_WRONLY = global i32 0
define i32 @fs_cp_file(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 i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca [65536 x i8]
%13 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %13
%14 = load i8*, i8** %4
%15 = call i64 @stat(i8* %14, %struct.stat* %8)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 0, i32* %3
br label %67
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = load i32, i32* @O_RDONLY
%21 = call i32 (i8*, i32, ...) @open(i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %18
store i32 0, i32* %3
br label %67
25: ; preds = %18
%26 = load i8*, i8** %5
%27 = load i32, i32* @O_CREAT
%28 = load i32, i32* @O_WRONLY
%29 = or i32 %27, %28
%30 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%31 = load i32, i32* %30
%32 = and i32 %31, 4095
%33 = call i32 (i8*, i32, ...) @open(i8* %26, i32 %29, i32 %32)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %25
br label %63
37: ; preds = %25
%38 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%39 = load i64, i64* %38
store i64 %39, i64* %9
br label %40
40: ; preds = %55, %37
%41 = load i64, i64* %9
%42 = icmp sgt i64 %41, 0
br i1 %42, label %43, label %59
43: ; preds = %40
%44 = load i32, i32* %6
%45 = bitcast [65536 x i8]* %12 to i8**
%46 = call i64 @read(i32 %44, i8** %45, i32 65536)
store i64 %46, i64* %10
%47 = load i32, i32* %7
%48 = bitcast [65536 x i8]* %12 to i8**
%49 = load i64, i64* %10
%50 = call i64 @write(i32 %47, i8** %48, i64 %49)
store i64 %50, i64* %11
%51 = load i64, i64* %11
%52 = load i64, i64* %10
%53 = icmp ne i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %43
br label %60
55: ; preds = %43
%56 = load i64, i64* %11
%57 = load i64, i64* %9
%58 = sub nsw i64 %57, %56
store i64 %58, i64* %9
br label %40
59: ; preds = %40
store i32 1, i32* %13
br label %60
60: ; preds = %59, %54
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
br label %63
63: ; preds = %60, %36
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %13
store i32 %66, i32* %3
br label %67
67: ; preds = %63, %24, %17
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
%struct.stat = type { i32, i64 }
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
define i32 @fs_cp_file(i8* %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [65536 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [65536 x i8], [65536 x i8]* %4, i64 0, i64 0
%7 = call i64 @stat(i8* %0, %struct.stat* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %41
9: ; preds = %2
%10 = load i32, i32* @O_RDONLY
%11 = call i32 (i8*, i32, ...) @open(i8* %0, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %41, label %13
13: ; preds = %9
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_WRONLY
%16 = or i32 %15, %14
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%18 = load i32, i32* %17
%19 = and i32 %18, 4095
%20 = call i32 (i8*, i32, ...) @open(i8* %1, i32 %16, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %38, label %22
22: ; preds = %13
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%24 = load i64, i64* %23
%25 = bitcast [65536 x i8]* %4 to i8**
%26 = icmp sgt i64 %24, 0
br i1 %26, label %29, label %35
27: ; preds = %29
%28 = icmp sgt i64 %34, 0
br i1 %28, label %29, label %35
29: ; preds = %22, %27
%30 = phi i64 [ %34, %27 ], [ %24, %22 ]
%31 = call i64 @read(i32 %11, i8** %25, i32 65536)
%32 = call i64 @write(i32 %20, i8** %25, i64 %31)
%33 = icmp eq i64 %32, %31
%34 = sub nsw i64 %30, %32
br i1 %33, label %27, label %35
35: ; preds = %29, %27, %22
%36 = phi i32 [ 1, %22 ], [ 1, %27 ], [ 0, %29 ]
%37 = call i32 @close(i32 %20)
br label %38
38: ; preds = %13, %35
%39 = phi i32 [ 0, %13 ], [ %36, %35 ]
%40 = call i32 @close(i32 %11)
br label %41
41: ; preds = %9, %2, %38
%42 = phi i32 [ %39, %38 ], [ 0, %2 ], [ 0, %9 ]
ret i32 %42
}