#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_size;
  int /*<<< orphan*/ st_gid;
  int /*<<< orphan*/ st_uid;
  int /*<<< orphan*/ st_mode;
};
typedef int ssize_t;

/* Variables and functions */
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fchmod(int, int /*<<< orphan*/);
int /*<<< orphan*/ fchown(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fstat(int, struct stat *);
int open(char const *, int);
int read(int, char *, int);
int /*<<< orphan*/ unlink(char const *);
int write(int, char *, int);
int copy(const char *from_file, const char *to_file) {
  int in, out;
  char buffer[8192];
  struct stat buf;
  ssize_t size_read, total = 0;
  if (!(in = open(from_file, O_RDONLY)))
    return -1;
  if (!(out = open(to_file, O_WRONLY | O_TRUNC))) {
    close(in);
    return -1;
  }
  if (!fstat(in, &buf)) {
    close(in);
    unlink(to_file);
    close(out);
    return -1;
  }
  while ((size_read = read(in, buffer, 8192)) > -1) {
    total += size_read;
    if (write(out, buffer, size_read) != size_read) {
      unlink(to_file);
      break;
    }
    if (total == buf.st_size)
      break;
    if (size_read < 1) {
      unlink(to_file);
      break;
    }
  }
  close(in);
  fchmod(out, buf.st_mode);
  fchown(out, buf.st_uid, buf.st_gid);
  close(out);
  if (close(open(to_file, O_RDONLY)))
    return 0;
  return -1;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x2038(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    11c7 <copy+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    1346 <copy+0x1c6>
mov    -0x18(%rbp),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    0x2e1d(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jne    1202 <copy+0x82>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1346 <copy+0x1c6>
mov    -0x1c(%rbp),%edi
lea    -0x2030(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%eax
jne    123f <copy+0xbf>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1346 <copy+0x1c6>
jmp    1244 <copy+0xc4>
mov    -0x1c(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x2000,%edx
call   1060 <read@plt>
mov    %eax,-0x2034(%rbp)
cmp    $0xffffffff,%eax
jle    12df <copy+0x15f>
mov    -0x2034(%rbp),%eax
add    -0x2038(%rbp),%eax
mov    %eax,-0x2038(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    -0x2034(%rbp),%edx
call   1040 <write@plt>
cmp    -0x2034(%rbp),%eax
je     12a8 <copy+0x128>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
jmp    12df <copy+0x15f>
mov    -0x2038(%rbp),%eax
cmp    -0x2030(%rbp),%eax
jne    12bf <copy+0x13f>
jmp    12df <copy+0x15f>
cmpl   $0x1,-0x2034(%rbp)
jge    12da <copy+0x15a>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
jmp    12df <copy+0x15f>
jmp    1244 <copy+0xc4>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
mov    -0x2024(%rbp),%esi
call   1070 <fchmod@plt>
mov    -0x20(%rbp),%edi
mov    -0x2028(%rbp),%esi
mov    -0x202c(%rbp),%edx
call   1090 <fchown@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
mov    0x2cc4(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%edi
call   1050 <close@plt>
cmp    $0x0,%eax
je     133f <copy+0x1bf>
movl   $0x0,-0x4(%rbp)
jmp    1346 <copy+0x1c6>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2028,%rsp
mov    %rsi,%rbp
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
je     12a4 <copy+0x124>
mov    %eax,%ebx
mov    0x2e0f(%rip),%rax
mov    0x2e38(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
test   %eax,%eax
je     1289 <copy+0x109>
mov    %eax,%r12d
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   10a0 <fstat@plt>
test   %eax,%eax
je     128d <copy+0x10d>
mov    %rbp,0x8(%rsp)
xor    %r13d,%r13d
mov    0x10(%rsp),%r14d
lea    0x20(%rsp),%r15
nopl   (%rax)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x2000,%edx
call   1060 <read@plt>
test   %eax,%eax
js     123c <copy+0xbc>
mov    %eax,%ebp
mov    %r12d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
jne    1232 <copy+0xb2>
add    %ebp,%r13d
cmp    %r14d,%r13d
je     123c <copy+0xbc>
test   %ebp,%ebp
jne    1200 <copy+0x80>
mov    0x8(%rsp),%rdi
call   1030 <unlink@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x1c(%rsp),%esi
mov    %r12d,%edi
call   1070 <fchmod@plt>
mov    0x14(%rsp),%edx
mov    0x18(%rsp),%esi
mov    %r12d,%edi
call   1090 <fchown@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    0x8(%rsp),%rdi
call   1080 <open@plt>
mov    %eax,%edi
call   1050 <close@plt>
cmp    $0x1,%eax
sbb    %r15d,%r15d
jmp    12a4 <copy+0x124>
mov    %ebx,%edi
jmp    129f <copy+0x11f>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %rbp,%rdi
call   1030 <unlink@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %r15d,%eax
add    $0x2028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32, i32, i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
define i32 @copy(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [8192 x i8]
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %11
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %2
store i32 -1, i32* %3
br label %92
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_TRUNC
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %18, i32 %21)
store i32 %22, i32* %7
%23 = icmp ne i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %17
%25 = load i32, i32* %6
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %3
br label %92
27: ; preds = %17
%28 = load i32, i32* %6
%29 = call i32 @fstat(i32 %28, %struct.stat* %9)
%30 = icmp ne i32 %29, 0
br i1 %30, label %38, label %31
31: ; preds = %27
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
%34 = load i8*, i8** %5
%35 = call i32 @unlink(i8* %34)
%36 = load i32, i32* %7
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %3
br label %92
38: ; preds = %27
br label %39
39: ; preds = %69, %38
%40 = load i32, i32* %6
%41 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%42 = call i32 @read(i32 %40, i8* %41, i32 8192)
store i32 %42, i32* %10
%43 = icmp sgt i32 %42, -1
br i1 %43, label %44, label %70
44: ; preds = %39
%45 = load i32, i32* %10
%46 = load i32, i32* %11
%47 = add nsw i32 %46, %45
store i32 %47, i32* %11
%48 = load i32, i32* %7
%49 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%50 = load i32, i32* %10
%51 = call i32 @write(i32 %48, i8* %49, i32 %50)
%52 = load i32, i32* %10
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %57
54: ; preds = %44
%55 = load i8*, i8** %5
%56 = call i32 @unlink(i8* %55)
br label %70
57: ; preds = %44
%58 = load i32, i32* %11
%59 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%60 = load i32, i32* %59
%61 = icmp eq i32 %58, %60
br i1 %61, label %62, label %63
62: ; preds = %57
br label %70
63: ; preds = %57
%64 = load i32, i32* %10
%65 = icmp slt i32 %64, 1
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load i8*, i8** %5
%68 = call i32 @unlink(i8* %67)
br label %70
69: ; preds = %63
br label %39
70: ; preds = %66, %62, %54, %39
%71 = load i32, i32* %6
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %7
%74 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 3
%75 = load i32, i32* %74
%76 = call i32 @fchmod(i32 %73, i32 %75)
%77 = load i32, i32* %7
%78 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 2
%79 = load i32, i32* %78
%80 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%81 = load i32, i32* %80
%82 = call i32 @fchown(i32 %77, i32 %79, i32 %81)
%83 = load i32, i32* %7
%84 = call i32 @close(i32 %83)
%85 = load i8*, i8** %5
%86 = load i32, i32* @O_RDONLY
%87 = call i32 @open(i8* %85, i32 %86)
%88 = call i32 @close(i32 %87)
%89 = icmp ne i32 %88, 0
br i1 %89, label %90, label %91
90: ; preds = %70
store i32 0, i32* %3
br label %92
91: ; preds = %70
store i32 -1, i32* %3
br label %92
92: ; preds = %91, %90, %31, %24, %16
%93 = load i32, i32* %3
ret i32 %93
}

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