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

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
int open(char const *, int, int);
int read(int, char *, int);
int /*<<< orphan*/ unlink(char const *);
int write(int, char *, int);
int pl_copy_file(const char *src, const char *dst) {
  char buf[512];
  int infile, outfile, size;
  struct stat st;
  infile = open(src, O_RDONLY, 0);
  if (infile < 0)
    return -1;
  if (fstat(infile, &st) < 0) {
    close(infile);
    return -1;
  }
  outfile = open(dst, O_CREAT | O_TRUNC | O_WRONLY, (int)(st.st_mode & 0777));
  if (outfile < 0) {
    close(infile);
    return -1;
  }
  while ((size = read(infile, buf, 512)) > 0) {
    int res = write(outfile, buf, size);
    if (res < size) {
      close(outfile);
      close(infile);
      unlink(dst);
      return -1;
    }
  }
  close(outfile);
  close(infile);
  return 0;
}


x86 assembly (clang, -O0):
<pl_copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x224(%rbp)
cmpl   $0x0,-0x224(%rbp)
jge    11a6 <pl_copy_file+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <pl_copy_file+0x17b>
mov    -0x224(%rbp),%edi
lea    -0x230(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
jge    11d9 <pl_copy_file+0x79>
mov    -0x224(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <pl_copy_file+0x17b>
mov    -0x18(%rbp),%rdi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    0x2e03(%rip),%rax
or     (%rax),%esi
mov    0x2dca(%rip),%rax
or     (%rax),%esi
mov    -0x230(%rbp),%edx
and    $0x1ff,%edx
call   1070 <open@plt>
mov    %eax,-0x228(%rbp)
cmpl   $0x0,-0x228(%rbp)
jge    1233 <pl_copy_file+0xd3>
mov    -0x224(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <pl_copy_file+0x17b>
jmp    1238 <pl_copy_file+0xd8>
mov    -0x224(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x22c(%rbp)
cmp    $0x0,%eax
jle    12be <pl_copy_file+0x15e>
mov    -0x228(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x22c(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x234(%rbp)
mov    -0x234(%rbp),%eax
cmp    -0x22c(%rbp),%eax
jge    12b9 <pl_copy_file+0x159>
mov    -0x228(%rbp),%edi
call   1050 <close@plt>
mov    -0x224(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <pl_copy_file+0x17b>
jmp    1238 <pl_copy_file+0xd8>
mov    -0x228(%rbp),%edi
call   1050 <close@plt>
mov    -0x224(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pl_copy_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%r14
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     123b <pl_copy_file+0xdb>
mov    %eax,%r12d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %rax,%rax
js     121f <pl_copy_file+0xbf>
mov    0x2e4b(%rip),%rax
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e01(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
and    0x8(%rsp),%edx
mov    %r14,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     121f <pl_copy_file+0xbf>
mov    %eax,%ebx
lea    0x10(%rsp),%r13
nopl   (%rax)
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x200,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1229 <pl_copy_file+0xc9>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
jge    11e0 <pl_copy_file+0x80>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
jmp    123b <pl_copy_file+0xdb>
mov    %r12d,%edi
call   1050 <close@plt>
jmp    123b <pl_copy_file+0xdb>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r12d,%edi
call   1050 <close@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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