#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 utimbuf {
  int /*<<< orphan*/ modtime;
  int /*<<< orphan*/ actime;
};
struct stat {
  int /*<<< orphan*/ st_mtime;
  int /*<<< orphan*/ st_atime;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ ENOENT;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char const *, int, ...);
int read(int, char *, int);
scalar_t__ stat(char const *, struct stat *);
scalar_t__ utime(char const *, struct utimbuf *);
scalar_t__ write(int, char *, int);
int apkg_copyfile(const char *src, const char *dest) {
  char buf[8192];
  int infd, outfd;
  int r;
  struct stat srcStat;
  struct utimbuf times;
  if (stat(src, &srcStat) < 0) {
    if (errno == ENOENT)
      return 0;
    else
      return -1;
  }
  if ((infd = open(src, O_RDONLY)) < 0)
    return -1;
  if ((outfd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, srcStat.st_mode)) < 0)
    return -1;
  while ((r = read(infd, buf, sizeof(buf))) > 0) {
    if (write(outfd, buf, r) < 0)
      return -1;
  }
  close(outfd);
  close(infd);
  if (r < 0)
    return -1;
  times.actime = srcStat.st_atime;
  times.modtime = srcStat.st_mtime;
  if (utime(dest, &times) < 0)
    return -1;
  return 1;
}


x86 assembly (clang, -O0):
<apkg_copyfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x2038(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    11bf <apkg_copyfile+0x5f>
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%rax
jne    11b3 <apkg_copyfile+0x53>
movl   $0x0,-0x4(%rbp)
jmp    1307 <apkg_copyfile+0x1a7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <apkg_copyfile+0x1a7>
mov    -0x10(%rbp),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x2024(%rbp)
cmp    $0x0,%eax
jge    11ee <apkg_copyfile+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <apkg_copyfile+0x1a7>
mov    -0x18(%rbp),%rdi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
or     (%rax),%esi
mov    0x2de5(%rip),%rax
or     (%rax),%esi
mov    -0x2030(%rbp),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x2028(%rbp)
cmp    $0x0,%eax
jge    1235 <apkg_copyfile+0xd5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <apkg_copyfile+0x1a7>
jmp    123a <apkg_copyfile+0xda>
mov    -0x2024(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x2000,%edx
call   1060 <read@plt>
mov    %eax,-0x202c(%rbp)
cmp    $0x0,%eax
jle    1293 <apkg_copyfile+0x133>
mov    -0x2028(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    -0x202c(%rbp),%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    128e <apkg_copyfile+0x12e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <apkg_copyfile+0x1a7>
jmp    123a <apkg_copyfile+0xda>
mov    -0x2028(%rbp),%edi
call   1050 <close@plt>
mov    -0x2024(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x202c(%rbp)
jge    12c2 <apkg_copyfile+0x162>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <apkg_copyfile+0x1a7>
mov    -0x2034(%rbp),%eax
mov    %eax,-0x203c(%rbp)
mov    -0x2038(%rbp),%eax
mov    %eax,-0x2040(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x2040(%rbp),%rsi
call   1030 <utime@plt>
cmp    $0x0,%rax
jge    1300 <apkg_copyfile+0x1a0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <apkg_copyfile+0x1a7>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<apkg_copyfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2028,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x10(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
js     1219 <apkg_copyfile+0xb9>
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     1237 <apkg_copyfile+0xd7>
mov    %eax,%r12d
mov    0x2dfb(%rip),%rax
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
mov    0x18(%rsp),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     1237 <apkg_copyfile+0xd7>
mov    %eax,%ebp
lea    0x20(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x2000,%edx
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    124c <apkg_copyfile+0xec>
mov    %ebp,%edi
mov    %r13,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
test   %rax,%rax
jns    11f0 <apkg_copyfile+0x90>
jmp    1237 <apkg_copyfile+0xd7>
mov    0x2da8(%rip),%rax
mov    (%rax),%rax
mov    0x2d8e(%rip),%rcx
xor    %r15d,%r15d
cmp    (%rcx),%rax
setne  %r15b
neg    %r15d
mov    %r15d,%eax
add    $0x2028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r12d,%edi
call   1050 <close@plt>
test   %ebx,%ebx
js     1237 <apkg_copyfile+0xd7>
mov    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1030 <utime@plt>
xor    %ecx,%ecx
test   %rax,%rax
setns  %cl
lea    (%rcx,%rcx,1),%r15d
add    $0xffffffff,%r15d
jmp    1237 <apkg_copyfile+0xd7>

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

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