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

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ chmod(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int open(char const *, int, ...);
size_t read(int, char *, int);
int /*<<< orphan*/ stat(char const *, struct stat *);
size_t write(int, char *, size_t);
int copyfile(const char *src, const char *dest, int overwrite) {
  int fdsrc;
  int fddest;
  char buf[4096];
  size_t rlen = 0;
  struct stat file_st;
  mode_t fmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  if (-1 != (fdsrc = open(src, O_RDONLY))) {
    if (!overwrite && !access(dest, F_OK))
      return 0;
    if (-1 != (fddest = open(dest, O_CREAT | O_WRONLY | O_TRUNC, fmode))) {
      while ((rlen = read(fdsrc, buf, sizeof(buf))))
        rlen = write(fddest, buf, rlen);
      close(fddest);
      close(fdsrc);
      stat(src, &file_st);
      chmod(dest, file_st.st_mode);
      return 0;
    }
    close(fdsrc);
  }
  return -1;
}


x86 assembly (clang, -O0):
<copyfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x1038(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    0x2e2f(%rip),%rcx
or     (%rcx),%eax
mov    0x2e16(%rip),%rcx
or     (%rcx),%eax
mov    0x2e05(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1044(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     12df <copyfile+0x16f>
cmpl   $0x0,-0x1c(%rbp)
jne    1212 <copyfile+0xa2>
mov    -0x18(%rbp),%rdi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%eax
jne    1212 <copyfile+0xa2>
movl   $0x0,-0x4(%rbp)
jmp    12e6 <copyfile+0x176>
mov    -0x18(%rbp),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    0x2d7a(%rip),%rax
or     (%rax),%esi
mov    0x2dc1(%rip),%rax
or     (%rax),%esi
mov    -0x1044(%rbp),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     12d7 <copyfile+0x167>
jmp    1255 <copyfile+0xe5>
mov    -0x20(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %rax,-0x1038(%rbp)
cmp    $0x0,%rax
je     129c <copyfile+0x12c>
mov    -0x24(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x1038(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x1038(%rbp)
jmp    1255 <copyfile+0xe5>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
lea    -0x1040(%rbp),%rsi
call   1060 <stat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1040(%rbp),%esi
call   1070 <chmod@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e6 <copyfile+0x176>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %edx,%r13d
mov    %rsi,%rbp
mov    %rdi,%r15
mov    0x2e1f(%rip),%rax
mov    0x2e38(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2e1d(%rip),%rax
or     (%rax),%ebx
mov    0x2e0c(%rip),%rax
or     (%rax),%ebx
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     1297 <copyfile+0x127>
mov    %eax,%r14d
test   %r13d,%r13d
jne    11ee <copyfile+0x7e>
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <access@plt>
test   %eax,%eax
je     128a <copyfile+0x11a>
mov    0x2e03(%rip),%rax
mov    0x2da4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2de9(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     128f <copyfile+0x11f>
mov    %eax,%ebx
mov    %rbp,%r12
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %rax,%rax
je     1262 <copyfile+0xf2>
lea    0x10(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %rax,%rax
jne    1240 <copyfile+0xd0>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r14d,%edi
call   1040 <close@plt>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1060 <stat@plt>
mov    0x8(%rsp),%esi
mov    %r12,%rdi
call   1070 <chmod@plt>
xor    %r12d,%r12d
jmp    1297 <copyfile+0x127>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %r12d,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@O_RDONLY = global i32 0
@F_OK = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
define i32 @copyfile(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = alloca [4096 x i8]
%5 = alloca %struct.stat
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = bitcast %struct.stat* %5 to i8*
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IROTH
%14 = or i32 %12, %13
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %49, label %18
18: ; preds = %3
%19 = icmp eq i32 %2, 0
br i1 %19, label %20, label %24
20: ; preds = %18
%21 = load i32, i32* @F_OK
%22 = tail call i32 @access(i8* %1, i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %49, label %24
24: ; preds = %20, %18
%25 = load i32, i32* @O_CREAT
%26 = load i32, i32* @O_WRONLY
%27 = or i32 %26, %25
%28 = load i32, i32* @O_TRUNC
%29 = or i32 %27, %28
%30 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %29, i32 %14)
%31 = icmp eq i32 %30, -1
br i1 %31, label %47, label %32
32: ; preds = %24
%33 = call i64 @read(i32 %16, i8* %6, i32 4096)
%34 = icmp eq i64 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %32, %35
%36 = phi i64 [ %38, %35 ], [ %33, %32 ]
%37 = call i64 @write(i32 %30, i8* %6, i64 %36)
%38 = call i64 @read(i32 %16, i8* %6, i32 4096)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %35
40: ; preds = %35, %32
%41 = tail call i32 @close(i32 %30)
%42 = tail call i32 @close(i32 %16)
%43 = call i32 @stat(i8* %0, %struct.stat* %5)
%44 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%45 = load i32, i32* %44
%46 = tail call i32 @chmod(i8* %1, i32 %45)
br label %49
47: ; preds = %24
%48 = tail call i32 @close(i32 %16)
br label %49
49: ; preds = %3, %47, %20, %40
%50 = phi i32 [ 0, %40 ], [ 0, %20 ], [ -1, %47 ], [ -1, %3 ]
ret i32 %50
}