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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ ftruncate(int, int);
int open(char const *, int, ...);
scalar_t__ posix_memalign(void **, int, int);
size_t read(int, void *, int);
scalar_t__ stat(char const *, struct stat *);
size_t write(int, void *, size_t);
int ef_copy(const char *from, const char *to, int mode) {
  void *buffer = NULL;
  struct stat _stat;
  int src = -1;
  int dst = -1;
  int blocksize = 4096;
  if (stat(from, &_stat) != 0)
    goto bad;
  if (_stat.st_size < blocksize) {
    blocksize = _stat.st_size + (512 - 1);
    blocksize -= blocksize % 512;
  }
  if (posix_memalign(&buffer, 512, blocksize) != 0)
    goto bad;
  if ((src = open(from, O_RDONLY)) == -1)
    goto bad;
  if ((dst = open(to, O_WRONLY | O_CREAT, mode)) == -1)
    goto bad;
  for (;;) {
    size_t rbytes = read(src, buffer, blocksize);
    size_t wbytes = 0;
    if (rbytes > 0) {
      rbytes += (512 - 1);
      rbytes -= rbytes % 512;
      wbytes = write(dst, buffer, rbytes);
      if (rbytes != wbytes)
        goto bad;
    }
    if (rbytes == 0)
      break;
    if (rbytes < 0)
      goto bad;
  }
  free(buffer);
  close(src);
  ftruncate(dst, _stat.st_size);
  close(dst);
  return 0;
bad:
  if (src != -1)
    close(src);
  if (dst != -1)
    close(dst);
  free(buffer);
  return -1;
}


x86 assembly (clang, -O0):
<ef_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x34(%rbp)
movl   $0xffffffff,-0x38(%rbp)
movl   $0x1000,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     11cc <ef_copy+0x4c>
jmp    133c <ef_copy+0x1bc>
mov    -0x30(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    11f6 <ef_copy+0x76>
mov    -0x30(%rbp),%eax
add    $0x1ff,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
mov    -0x3c(%rbp),%eax
sub    %edx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edx
lea    -0x28(%rbp),%rdi
mov    $0x200,%esi
call   10a0 <posix_memalign@plt>
cmp    $0x0,%rax
je     1216 <ef_copy+0x96>
jmp    133c <ef_copy+0x1bc>
mov    -0x10(%rbp),%rdi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    123b <ef_copy+0xbb>
jmp    133c <ef_copy+0x1bc>
mov    -0x18(%rbp),%rdi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
or     (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x38(%rbp)
cmp    $0xffffffff,%eax
jne    126c <ef_copy+0xec>
jmp    133c <ef_copy+0x1bc>
jmp    1271 <ef_copy+0xf1>
mov    -0x34(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1070 <read@plt>
mov    %rax,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
cmpq   $0x0,-0x48(%rbp)
jbe    12e7 <ef_copy+0x167>
mov    -0x48(%rbp),%rax
add    $0x1ff,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rcx
and    $0x1ff,%rcx
mov    -0x48(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x50(%rbp),%rax
je     12e2 <ef_copy+0x162>
jmp    133c <ef_copy+0x1bc>
jmp    12e7 <ef_copy+0x167>
cmpq   $0x0,-0x48(%rbp)
jne    12f7 <ef_copy+0x177>
jmp    130c <ef_copy+0x18c>
cmpq   $0x0,-0x48(%rbp)
jae    1307 <ef_copy+0x187>
jmp    133c <ef_copy+0x1bc>
jmp    1271 <ef_copy+0xf1>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1050 <ftruncate@plt>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1370 <ef_copy+0x1f0>
cmpl   $0xffffffff,-0x34(%rbp)
je     134e <ef_copy+0x1ce>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
cmpl   $0xffffffff,-0x38(%rbp)
je     1360 <ef_copy+0x1e0>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ef_copy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%rbp
movq   $0x0,0x8(%rsp)
mov    %rsp,%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    11e7 <ef_copy+0x67>
mov    (%rsp),%eax
mov    $0x1000,%ebx
cmp    $0xfff,%eax
jg     11d1 <ef_copy+0x51>
lea    0x1ff(%rax),%ecx
add    $0x3fe,%eax
test   %ecx,%ecx
cmovns %ecx,%eax
and    $0xfffffe00,%eax
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
mov    $0x200,%esi
mov    %ebx,%edx
call   10a0 <posix_memalign@plt>
test   %rax,%rax
je     1203 <ef_copy+0x83>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     11e7 <ef_copy+0x67>
mov    %eax,%r15d
mov    0x2da3(%rip),%rax
mov    0x2dcc(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r12,%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12bb <ef_copy+0x13b>
mov    %eax,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    %ebx,%edx
call   1070 <read@plt>
test   %rax,%rax
je     128f <ef_copy+0x10f>
mov    %rax,%rbp
add    $0x1ff,%rbp
and    $0xfffffffffffffe00,%rbp
mov    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    %rbp,%rdx
call   1040 <write@plt>
cmp    %rax,%rbp
jne    12c0 <ef_copy+0x140>
test   %rbp,%rbp
jne    1250 <ef_copy+0xd0>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    (%rsp),%esi
mov    %r14d,%edi
call   1050 <ftruncate@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    11f6 <ef_copy+0x76>
mov    %r15d,%r14d
jmp    12d2 <ef_copy+0x152>
mov    %r15d,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%r14d
je     11e7 <ef_copy+0x67>
mov    %r14d,%edi
call   1060 <close@plt>
jmp    11e7 <ef_copy+0x67>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @ef_copy(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64
%14 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* null, i8** %8
store i32 -1, i32* %10
store i32 -1, i32* %11
store i32 4096, i32* %12
%15 = load i8*, i8** %5
%16 = call i64 @stat(i8* %15, %struct.stat* %9)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %3
br label %96
19: ; preds = %3
%20 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* %12
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %32
24: ; preds = %19
%25 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%26 = load i32, i32* %25
%27 = add nsw i32 %26, 511
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = srem i32 %28, 512
%30 = load i32, i32* %12
%31 = sub nsw i32 %30, %29
store i32 %31, i32* %12
br label %32
32: ; preds = %24, %19
%33 = load i32, i32* %12
%34 = call i64 @posix_memalign(i8** %8, i32 512, i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %32
br label %96
37: ; preds = %32
%38 = load i8*, i8** %5
%39 = load i32, i32* @O_RDONLY
%40 = call i32 (i8*, i32, ...) @open(i8* %38, i32 %39)
store i32 %40, i32* %10
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %37
br label %96
43: ; preds = %37
%44 = load i8*, i8** %6
%45 = load i32, i32* @O_WRONLY
%46 = load i32, i32* @O_CREAT
%47 = or i32 %45, %46
%48 = load i32, i32* %7
%49 = call i32 (i8*, i32, ...) @open(i8* %44, i32 %47, i32 %48)
store i32 %49, i32* %11
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %52
51: ; preds = %43
br label %96
52: ; preds = %43
br label %53
53: ; preds = %84, %52
%54 = load i32, i32* %10
%55 = load i8*, i8** %8
%56 = load i32, i32* %12
%57 = call i64 @read(i32 %54, i8* %55, i32 %56)
store i64 %57, i64* %13
store i64 0, i64* %14
%58 = load i64, i64* %13
%59 = icmp ugt i64 %58, 0
br i1 %59, label %60, label %76
60: ; preds = %53
%61 = load i64, i64* %13
%62 = add i64 %61, 511
store i64 %62, i64* %13
%63 = load i64, i64* %13
%64 = urem i64 %63, 512
%65 = load i64, i64* %13
%66 = sub i64 %65, %64
store i64 %66, i64* %13
%67 = load i32, i32* %11
%68 = load i8*, i8** %8
%69 = load i64, i64* %13
%70 = call i64 @write(i32 %67, i8* %68, i64 %69)
store i64 %70, i64* %14
%71 = load i64, i64* %13
%72 = load i64, i64* %14
%73 = icmp ne i64 %71, %72
br i1 %73, label %74, label %75
74: ; preds = %60
br label %96
75: ; preds = %60
br label %76
76: ; preds = %75, %53
%77 = load i64, i64* %13
%78 = icmp eq i64 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %76
br label %85
80: ; preds = %76
%81 = load i64, i64* %13
%82 = icmp ult i64 %81, 0
br i1 %82, label %83, label %84
83: ; preds = %80
br label %96
84: ; preds = %80
br label %53
85: ; preds = %79
%86 = load i8*, i8** %8
%87 = call i32 @free(i8* %86)
%88 = load i32, i32* %10
%89 = call i32 @close(i32 %88)
%90 = load i32, i32* %11
%91 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%92 = load i32, i32* %91
%93 = call i32 @ftruncate(i32 %90, i32 %92)
%94 = load i32, i32* %11
%95 = call i32 @close(i32 %94)
store i32 0, i32* %4
br label %111
96: ; preds = %83, %74, %51, %42, %36, %18
%97 = load i32, i32* %10
%98 = icmp ne i32 %97, -1
br i1 %98, label %99, label %102
99: ; preds = %96
%100 = load i32, i32* %10
%101 = call i32 @close(i32 %100)
br label %102
102: ; preds = %99, %96
%103 = load i32, i32* %11
%104 = icmp ne i32 %103, -1
br i1 %104, label %105, label %108
105: ; preds = %102
%106 = load i32, i32* %11
%107 = call i32 @close(i32 %106)
br label %108
108: ; preds = %105, %102
%109 = load i8*, i8** %8
%110 = call i32 @free(i8* %109)
store i32 -1, i32* %4
br label %111
111: ; preds = %108, %85
%112 = load i32, i32* %4
ret i32 %112
}

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