#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EPERM;
scalar_t__ EXDEV;
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
scalar_t__ close(int);
scalar_t__ errno;
scalar_t__ link(char const *, char const *);
int /*<<< orphan*/ myerr(char *, char const *, ...);
int open(char const *, int, int);
int read(int, unsigned char *, int);
int /*<<< orphan*/ strerror(scalar_t__);
int write(int, unsigned char *, int);
void copy_file(char const *src, char const *dst) {
  if (link(src, dst) >= 0)
    return;
  if (errno != EXDEV && errno != EPERM)
    myerr("link(%s, %s) failed: %s", src, dst, strerror(errno));
  {
    int fdr, fdw;
    unsigned char buf[4096], *p;
    int r, w;
    if ((fdr = open(src, O_RDONLY, 0)) < 0)
      myerr("open `%s' for read failed: %s", src, strerror(errno));
    if ((fdw = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0)
      myerr("open `%s' for write failed: %s", dst, strerror(errno));
    while (1) {
      r = read(fdr, buf, sizeof(buf));
      if (r < 0)
        myerr("read failed: %s", strerror(errno));
      if (!r)
        break;
      p = buf;
      while (r) {
        w = write(fdw, p, r);
        if (r <= 0)
          myerr("write failed: %s", strerror(errno));
        p += w;
        r -= w;
      }
    }
    if (close(fdr) < 0)
      myerr("close failed: %s", strerror(errno));
    if (close(fdw) < 0)
      myerr("close failed: %s", strerror(errno));
  }
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <link@plt>
cmp    $0x0,%rax
jl     119f <copy_file+0x2f>
jmp    142a <copy_file+0x2ba>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
je     1216 <copy_file+0xa6>
mov    0x2e00(%rip),%rax
mov    (%rax),%rax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
je     1216 <copy_file+0xa6>
mov    -0x8(%rbp),%rax
mov    %rax,-0x1040(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x1038(%rbp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x1040(%rbp),%rsi
mov    -0x1038(%rbp),%rdx
mov    %eax,%ecx
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1060 <myerr@plt>
mov    -0x8(%rbp),%rdi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1267 <copy_file+0xf7>
mov    -0x8(%rbp),%rax
mov    %rax,-0x1048(%rbp)
mov    0x2d78(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x1048(%rbp),%rsi
mov    %eax,%edx
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1060 <myerr@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
mov    0x2d7d(%rip),%rax
or     (%rax),%esi
mov    0x2d6c(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12cd <copy_file+0x15d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1050(%rbp)
mov    0x2d12(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x1050(%rbp),%rsi
mov    %eax,%edx
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1060 <myerr@plt>
jmp    12d2 <copy_file+0x162>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x102c(%rbp)
cmpl   $0x0,-0x102c(%rbp)
jge    1319 <copy_file+0x1a9>
mov    0x2cc0(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xd14(%rip),%rdi
mov    $0x0,%al
call   1060 <myerr@plt>
cmpl   $0x0,-0x102c(%rbp)
jne    132b <copy_file+0x1bb>
jmp    13c6 <copy_file+0x256>
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1028(%rbp)
cmpl   $0x0,-0x102c(%rbp)
je     13c1 <copy_file+0x251>
mov    -0x18(%rbp),%edi
mov    -0x1028(%rbp),%rsi
mov    -0x102c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1030(%rbp)
cmpl   $0x0,-0x102c(%rbp)
jg     138e <copy_file+0x21e>
mov    0x2c4b(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xcbd(%rip),%rdi
mov    $0x0,%al
call   1060 <myerr@plt>
mov    -0x1030(%rbp),%ecx
mov    -0x1028(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x1028(%rbp)
mov    -0x1030(%rbp),%ecx
mov    -0x102c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x102c(%rbp)
jmp    1339 <copy_file+0x1c9>
jmp    12d2 <copy_file+0x162>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    13f8 <copy_file+0x288>
mov    0x2be1(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xc64(%rip),%rdi
mov    $0x0,%al
call   1060 <myerr@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    142a <copy_file+0x2ba>
mov    0x2baf(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xc32(%rip),%rdi
mov    $0x0,%al
call   1060 <myerr@plt>
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
call   1080 <link@plt>
test   %rax,%rax
jns    1320 <copy_file+0x1b0>
mov    0x2e24(%rip),%r14
mov    (%r14),%rdi
mov    0x2e22(%rip),%rax
cmp    (%rax),%rdi
je     11d2 <copy_file+0x62>
mov    0x2df6(%rip),%rax
cmp    (%rax),%rdi
je     11d2 <copy_file+0x62>
call   1090 <strerror@plt>
lea    0xe3d(%rip),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <myerr@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
jns    1208 <copy_file+0x98>
mov    (%r14),%rdi
call   1090 <strerror@plt>
lea    0xe1c(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <myerr@plt>
mov    0x2da1(%rip),%rax
mov    0x2de2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dcf(%rip),%rax
or     (%rax),%esi
mov    %r15,%rdi
mov    $0x180,%edx
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1251 <copy_file+0xe1>
mov    (%r14),%rdi
call   1090 <strerror@plt>
lea    0xdf1(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <myerr@plt>
lea    0xdec(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1280 <copy_file+0x110>
jne    1299 <copy_file+0x129>
jmp    12d4 <copy_file+0x164>
nopl   (%rax)
mov    (%r14),%rdi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xd94(%rip),%rdi
xor    %eax,%eax
call   1060 <myerr@plt>
lea    0x10(%rsp),%r12
jmp    12aa <copy_file+0x13a>
movslq %r15d,%rax
add    %rax,%r12
sub    %eax,%ebx
je     1260 <copy_file+0xf0>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
mov    %eax,%r15d
test   %ebx,%ebx
jg     12a0 <copy_file+0x130>
mov    (%r14),%rdi
call   1090 <strerror@plt>
movslq %eax,%rsi
mov    %r13,%rdi
xor    %eax,%eax
call   1060 <myerr@plt>
jmp    12a0 <copy_file+0x130>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
test   %rax,%rax
jns    12fb <copy_file+0x18b>
mov    (%r14),%rdi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xd61(%rip),%rdi
xor    %eax,%eax
call   1060 <myerr@plt>
mov    %ebp,%edi
call   1040 <close@plt>
test   %rax,%rax
jns    1320 <copy_file+0x1b0>
mov    (%r14),%rdi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xd3c(%rip),%rdi
xor    %eax,%eax
call   1060 <myerr@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EXDEV = global i64 0
@EPERM = global i64 0
@.str = [24 x i8] c"link(%s, %s) failed: %s\00"
@O_RDONLY = global i32 0
@.str.1 = [30 x i8] c"open `%s' for read failed: %s\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [31 x i8] c"open `%s' for write failed: %s\00"
@.str.3 = [16 x i8] c"read failed: %s\00"
@.str.4 = [17 x i8] c"write failed: %s\00"
@.str.5 = [17 x i8] c"close failed: %s\00"
define void @copy_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%11 = load i8*, i8** %3
%12 = load i8*, i8** %4
%13 = call i64 @link(i8* %11, i8* %12)
%14 = icmp sge i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
br label %118
16: ; preds = %2
%17 = load i64, i64* @errno
%18 = load i64, i64* @EXDEV
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = load i64, i64* @EPERM
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i8*, i8** %3
%26 = load i8*, i8** %4
%27 = load i64, i64* @errno
%28 = call i32 @strerror(i64 %27)
%29 = call i32 (i8*, i8*, ...) @myerr(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %25, i8* %26, i32 %28)
br label %30
30: ; preds = %24, %20, %16
%31 = load i8*, i8** %3
%32 = load i32, i32* @O_RDONLY
%33 = call i32 @open(i8* %31, i32 %32, i32 0)
store i32 %33, i32* %5
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %30
%36 = load i8*, i8** %3
%37 = load i64, i64* @errno
%38 = call i32 @strerror(i64 %37)
%39 = call i32 (i8*, i8*, ...) @myerr(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %36, i32 %38)
br label %40
40: ; preds = %35, %30
%41 = load i8*, i8** %4
%42 = load i32, i32* @O_WRONLY
%43 = load i32, i32* @O_CREAT
%44 = or i32 %42, %43
%45 = load i32, i32* @O_TRUNC
%46 = or i32 %44, %45
%47 = call i32 @open(i8* %41, i32 %46, i32 384)
store i32 %47, i32* %6
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %40
%50 = load i8*, i8** %4
%51 = load i64, i64* @errno
%52 = call i32 @strerror(i64 %51)
%53 = call i32 (i8*, i8*, ...) @myerr(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %50, i32 %52)
br label %54
54: ; preds = %49, %40
br label %55
55: ; preds = %54, %97
%56 = load i32, i32* %5
%57 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%58 = call i32 @read(i32 %56, i8* %57, i32 4096)
store i32 %58, i32* %9
%59 = load i32, i32* %9
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %67
61: ; preds = %55
%62 = load i64, i64* @errno
%63 = call i32 @strerror(i64 %62)
%64 = sext i32 %63 to i64
%65 = inttoptr i64 %64 to i8*
%66 = call i32 (i8*, i8*, ...) @myerr(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %61, %55
%68 = load i32, i32* %9
%69 = icmp ne i32 %68, 0
br i1 %69, label %71, label %70
70: ; preds = %67
br label %98
71: ; preds = %67
%72 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
store i8* %72, i8** %8
br label %73
73: ; preds = %89, %71
%74 = load i32, i32* %9
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %97
76: ; preds = %73
%77 = load i32, i32* %6
%78 = load i8*, i8** %8
%79 = load i32, i32* %9
%80 = call i32 @write(i32 %77, i8* %78, i32 %79)
store i32 %80, i32* %10
%81 = load i32, i32* %9
%82 = icmp sle i32 %81, 0
br i1 %82, label %83, label %89
83: ; preds = %76
%84 = load i64, i64* @errno
%85 = call i32 @strerror(i64 %84)
%86 = sext i32 %85 to i64
%87 = inttoptr i64 %86 to i8*
%88 = call i32 (i8*, i8*, ...) @myerr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %87)
br label %89
89: ; preds = %83, %76
%90 = load i32, i32* %10
%91 = load i8*, i8** %8
%92 = sext i32 %90 to i64
%93 = getelementptr inbounds i8, i8* %91, i64 %92
store i8* %93, i8** %8
%94 = load i32, i32* %10
%95 = load i32, i32* %9
%96 = sub nsw i32 %95, %94
store i32 %96, i32* %9
br label %73
97: ; preds = %73
br label %55
98: ; preds = %70
%99 = load i32, i32* %5
%100 = call i64 @close(i32 %99)
%101 = icmp slt i64 %100, 0
br i1 %101, label %102, label %108
102: ; preds = %98
%103 = load i64, i64* @errno
%104 = call i32 @strerror(i64 %103)
%105 = sext i32 %104 to i64
%106 = inttoptr i64 %105 to i8*
%107 = call i32 (i8*, i8*, ...) @myerr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %106)
br label %108
108: ; preds = %102, %98
%109 = load i32, i32* %6
%110 = call i64 @close(i32 %109)
%111 = icmp slt i64 %110, 0
br i1 %111, label %112, label %118
112: ; preds = %108
%113 = load i64, i64* @errno
%114 = call i32 @strerror(i64 %113)
%115 = sext i32 %114 to i64
%116 = inttoptr i64 %115 to i8*
%117 = call i32 (i8*, i8*, ...) @myerr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %116)
br label %118
118: ; preds = %15, %112, %108
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EXDEV = global i64 0
@EPERM = global i64 0
@.str = [24 x i8] c"link(%s, %s) failed: %s\00"
@O_RDONLY = global i32 0
@.str.1 = [30 x i8] c"open `%s' for read failed: %s\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [31 x i8] c"open `%s' for write failed: %s\00"
@.str.3 = [16 x i8] c"read failed: %s\00"
@.str.4 = [17 x i8] c"write failed: %s\00"
@.str.5 = [17 x i8] c"close failed: %s\00"
define void @copy_file(i8* %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = tail call i64 @link(i8* %0, i8* %1)
%5 = icmp sgt i64 %4, -1
br i1 %5, label %85, label %6
6: ; preds = %2
%7 = load i64, i64* @errno
%8 = load i64, i64* @EXDEV
%9 = icmp eq i64 %7, %8
%10 = load i64, i64* @EPERM
%11 = icmp eq i64 %7, %10
%12 = select i1 %9, i1 true, i1 %11
br i1 %12, label %16, label %13
13: ; preds = %6
%14 = tail call i32 @strerror(i64 %7)
%15 = tail call i32 (i8*, i8*, ...) @myerr(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i32 %14)
br label %16
16: ; preds = %13, %6
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* %0, i32 %18, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i64, i64* @errno
%23 = tail call i32 @strerror(i64 %22)
%24 = tail call i32 (i8*, i8*, ...) @myerr(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %23)
br label %25
25: ; preds = %21, %16
%26 = load i32, i32* @O_WRONLY
%27 = load i32, i32* @O_CREAT
%28 = or i32 %27, %26
%29 = load i32, i32* @O_TRUNC
%30 = or i32 %28, %29
%31 = tail call i32 @open(i8* %1, i32 %30, i32 384)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %25
%34 = load i64, i64* @errno
%35 = tail call i32 @strerror(i64 %34)
%36 = tail call i32 (i8*, i8*, ...) @myerr(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %1, i32 %35)
br label %37
37: ; preds = %33, %25
br label %38
38: ; preds = %61, %37
%39 = call i32 @read(i32 %19, i8* %17, i32 4096)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %38
%42 = load i64, i64* @errno
%43 = tail call i32 @strerror(i64 %42)
%44 = sext i32 %43 to i64
%45 = inttoptr i64 %44 to i8*
%46 = tail call i32 (i8*, i8*, ...) @myerr(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %45)
br label %49
47: ; preds = %38
%48 = icmp eq i32 %39, 0
br i1 %48, label %66, label %49
49: ; preds = %41, %47
br label %50
50: ; preds = %49, %61
%51 = phi i8* [ %63, %61 ], [ %17, %49 ]
%52 = phi i32 [ %64, %61 ], [ %39, %49 ]
%53 = call i32 @write(i32 %31, i8* %51, i32 %52)
%54 = icmp slt i32 %52, 1
br i1 %54, label %55, label %61
55: ; preds = %50
%56 = load i64, i64* @errno
%57 = tail call i32 @strerror(i64 %56)
%58 = sext i32 %57 to i64
%59 = inttoptr i64 %58 to i8*
%60 = tail call i32 (i8*, i8*, ...) @myerr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %55, %50
%62 = sext i32 %53 to i64
%63 = getelementptr inbounds i8, i8* %51, i64 %62
%64 = sub nsw i32 %52, %53
%65 = icmp eq i32 %64, 0
br i1 %65, label %38, label %50
66: ; preds = %47
%67 = tail call i64 @close(i32 %19)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %75
69: ; preds = %66
%70 = load i64, i64* @errno
%71 = tail call i32 @strerror(i64 %70)
%72 = sext i32 %71 to i64
%73 = inttoptr i64 %72 to i8*
%74 = tail call i32 (i8*, i8*, ...) @myerr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %73)
br label %75
75: ; preds = %69, %66
%76 = tail call i64 @close(i32 %31)
%77 = icmp slt i64 %76, 0
br i1 %77, label %78, label %84
78: ; preds = %75
%79 = load i64, i64* @errno
%80 = tail call i32 @strerror(i64 %79)
%81 = sext i32 %80 to i64
%82 = inttoptr i64 %81 to i8*
%83 = tail call i32 (i8*, i8*, ...) @myerr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %82)
br label %84
84: ; preds = %78, %75
br label %85
85: ; preds = %2, %84
ret void
}