#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;
};

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int, ...);
int read(int, char *, int);
scalar_t__ stat(char const *, struct stat *);
int write(int, char *, int);
int pjf_copyfile(const char *src, const char *dst) {
  struct stat stat_src;
  int fd_src, fd_dst, rc;
  char buf[8192];
  if (stat(src, &stat_src) != 0)
    return -1;
  fd_src = open(src, O_RDONLY);
  if (fd_src < 0)
    return -2;
  fd_dst = open(dst, O_WRONLY | O_CREAT | O_TRUNC, stat_src.st_mode);
  if (fd_dst < 0)
    return -3;
  while ((rc = read(fd_src, buf, sizeof buf)) > 0) {
    if (write(fd_dst, buf, rc) != rc)
      return -5;
  }
  if (rc < 0)
    return -4;
  close(fd_dst);
  close(fd_src);
  return 0;
}


x86 assembly (clang, -O0):
<pjf_copyfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     1186 <pjf_copyfile+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <pjf_copyfile+0x123>
mov    -0x10(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b3 <pjf_copyfile+0x63>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1273 <pjf_copyfile+0x123>
mov    -0x18(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
or     (%rax),%esi
mov    0x2e20(%rip),%rax
or     (%rax),%esi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11f5 <pjf_copyfile+0xa5>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1273 <pjf_copyfile+0x123>
jmp    11fa <pjf_copyfile+0xaa>
mov    -0x24(%rbp),%edi
lea    -0x2030(%rbp),%rsi
mov    $0x2000,%edx
call   1050 <read@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jle    1246 <pjf_copyfile+0xf6>
mov    -0x28(%rbp),%edi
lea    -0x2030(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x2c(%rbp),%eax
je     1241 <pjf_copyfile+0xf1>
movl   $0xfffffffb,-0x4(%rbp)
jmp    1273 <pjf_copyfile+0x123>
jmp    11fa <pjf_copyfile+0xaa>
cmpl   $0x0,-0x2c(%rbp)
jge    125c <pjf_copyfile+0x10c>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1273 <pjf_copyfile+0x123>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pjf_copyfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2018,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     1188 <pjf_copyfile+0x38>
add    $0x2018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     1211 <pjf_copyfile+0xc1>
mov    %eax,%r15d
mov    0x2e17(%rip),%rax
mov    0x2e48(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    0x8(%rsp),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     121b <pjf_copyfile+0xcb>
mov    %eax,%r14d
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x2000,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1225 <pjf_copyfile+0xd5>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     11e0 <pjf_copyfile+0x90>
mov    $0xfffffffb,%eax
jmp    117a <pjf_copyfile+0x2a>
mov    $0xfffffffe,%eax
jmp    117a <pjf_copyfile+0x2a>
mov    $0xfffffffd,%eax
jmp    117a <pjf_copyfile+0x2a>
js     123e <pjf_copyfile+0xee>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %r15d,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    117a <pjf_copyfile+0x2a>
mov    $0xfffffffc,%eax
jmp    117a <pjf_copyfile+0x2a>

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

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