#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 */

/* Variables and functions */
scalar_t__ EINTR;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char const *, int, ...);
int read(int, unsigned char *, int);
int /*<<< orphan*/ unlink(char const *);
int write(int, unsigned char *, int);
int mpk_filehandling_copyfile(const char *dst, const char *src) {
  int fd_src, fd_dst;
  unsigned char buf[512];
  int n_r;
  if ((fd_src = open(src, O_RDONLY)) < 0)
    return -1;
  if ((fd_dst = open(dst, O_WRONLY | O_CREAT, 0644)) < 0) {
    close(fd_src);
    return -1;
  }
  while ((n_r = read(fd_src, buf, 512)) > 0) {
    int n_w;
    unsigned char *buf_ptr = buf;
    while (n_r > 0) {
      if ((n_w = write(fd_dst, buf, n_r)) < 0) {
        if (errno == EINTR)
          continue;
        close(fd_dst);
        unlink(dst);
        return -1;
      }
      n_r -= n_w;
      buf_ptr += n_w;
    }
  }
  if (n_r < 0) {
    close(fd_dst);
    unlink(dst);
    return -1;
  }
  close(fd_src);
  close(fd_dst);
  return 0;
}


x86 assembly (clang, -O0):
<mpk_filehandling_copyfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    118f <mpk_filehandling_copyfile+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <mpk_filehandling_copyfile+0x19d>
mov    -0x10(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    0x2e55(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11d1 <mpk_filehandling_copyfile+0x81>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <mpk_filehandling_copyfile+0x19d>
jmp    11d6 <mpk_filehandling_copyfile+0x86>
mov    -0x1c(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x224(%rbp)
cmp    $0x0,%eax
jle    12ac <mpk_filehandling_copyfile+0x15c>
lea    -0x220(%rbp),%rax
mov    %rax,-0x230(%rbp)
cmpl   $0x0,-0x224(%rbp)
jle    12a7 <mpk_filehandling_copyfile+0x157>
mov    -0x20(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x224(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x228(%rbp)
cmp    $0x0,%eax
jge    1274 <mpk_filehandling_copyfile+0x124>
mov    0x2d91(%rip),%rax
mov    (%rax),%rax
mov    0x2d7f(%rip),%rcx
cmp    (%rcx),%rax
jne    1257 <mpk_filehandling_copyfile+0x107>
jmp    1207 <mpk_filehandling_copyfile+0xb7>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <mpk_filehandling_copyfile+0x19d>
mov    -0x228(%rbp),%ecx
mov    -0x224(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x224(%rbp)
mov    -0x228(%rbp),%ecx
mov    -0x230(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x230(%rbp)
jmp    1207 <mpk_filehandling_copyfile+0xb7>
jmp    11d6 <mpk_filehandling_copyfile+0x86>
cmpl   $0x0,-0x224(%rbp)
jge    12d6 <mpk_filehandling_copyfile+0x186>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <mpk_filehandling_copyfile+0x19d>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpk_filehandling_copyfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%rax
mov    %rdi,%r14
mov    0x2e72(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
xor    %eax,%eax
call   1070 <open@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     125b <mpk_filehandling_copyfile+0x10b>
mov    %eax,%r12d
mov    0x2e27(%rip),%rax
mov    0x2e60(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     1253 <mpk_filehandling_copyfile+0x103>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %r12d,%edi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
mov    %r14,0x8(%rsp)
jle    1224 <mpk_filehandling_copyfile+0xd4>
lea    0x10(%rsp),%r13
mov    0x2df2(%rip),%r14
mov    0x2de3(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
test   %eax,%eax
jns    120a <mpk_filehandling_copyfile+0xba>
mov    (%r14),%rax
cmp    (%r15),%rax
je     11f0 <mpk_filehandling_copyfile+0xa0>
jmp    123b <mpk_filehandling_copyfile+0xeb>
sub    %eax,%ebp
jg     11f0 <mpk_filehandling_copyfile+0xa0>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     11f0 <mpk_filehandling_copyfile+0xa0>
test   %ebp,%ebp
js     123b <mpk_filehandling_copyfile+0xeb>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ebp,%ebp
jmp    125b <mpk_filehandling_copyfile+0x10b>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x8(%rsp),%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%ebp
jmp    125b <mpk_filehandling_copyfile+0x10b>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @mpk_filehandling_copyfile(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %48, label %8
8: ; preds = %2
%9 = load i32, i32* @O_WRONLY
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %11, i32 420)
%13 = icmp slt i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %8
%15 = call i32 @read(i32 %6, i8* %4, i32 512)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %22, label %39
17: ; preds = %8
%18 = tail call i32 @close(i32 %6)
br label %48
19: ; preds = %34
%20 = call i32 @read(i32 %6, i8* %4, i32 512)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %37, label %39
22: ; preds = %14, %37
%23 = phi i32 [ %38, %37 ], [ %15, %14 ]
br label %24
24: ; preds = %22, %27
%25 = call i32 @write(i32 %12, i8* %4, i32 %23)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br i1 %30, label %24, label %31
31: ; preds = %27
%32 = tail call i32 @close(i32 %12)
%33 = tail call i32 @unlink(i8* %0)
br label %48
34: ; preds = %24
%35 = sub nsw i32 %23, %25
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %19
37: ; preds = %34, %19
%38 = phi i32 [ %35, %34 ], [ %20, %19 ]
br label %22
39: ; preds = %19, %14
%40 = phi i32 [ %15, %14 ], [ %20, %19 ]
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = tail call i32 @close(i32 %12)
%44 = tail call i32 @unlink(i8* %0)
br label %48
45: ; preds = %39
%46 = tail call i32 @close(i32 %6)
%47 = tail call i32 @close(i32 %12)
br label %48
48: ; preds = %31, %2, %45, %42, %17
%49 = phi i32 [ -1, %17 ], [ -1, %31 ], [ -1, %42 ], [ 0, %45 ], [ -1, %2 ]
ret i32 %49
}