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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *, char const *);
int /*<<< orphan*/ errx(int, char *, char const *);
int open(char const *, int, ...);
int read(int, char *, int);
int write(int, char *, int);
void mpkg_copy(const char *src, const char *dst) {
  char buf[512];
  ssize_t nbytes, written;
  int ifd, ofd;
  if ((ifd = open(src, O_RDONLY)) == -1)
    err(1, "cannot open file: %s", src);
  if ((ofd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1)
    err(1, "cannot open file: %s", dst);
  while ((nbytes = read(ifd, buf, sizeof(buf))) > 0) {
    if ((written = write(ofd, buf, nbytes)) == -1)
      err(1, "write: %s", dst);
    if (written < nbytes)
      errx(1, "write: %s: truncated write", dst);
  }
  if (nbytes == -1)
    err(1, "read: %s", src);
  close(ifd);
  close(ofd);
}


x86 assembly (clang, -O0):
<mpkg_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x21c(%rbp)
cmp    $0xffffffff,%eax
jne    11ab <mpkg_copy+0x4b>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe5a(%rip),%rsi
call   1070 <err@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x220(%rbp)
cmp    $0xffffffff,%eax
jne    11fa <mpkg_copy+0x9a>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe0b(%rip),%rsi
call   1070 <err@plt>
jmp    11ff <mpkg_copy+0x9f>
mov    -0x21c(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x214(%rbp)
cmp    $0x0,%eax
jle    128d <mpkg_copy+0x12d>
mov    -0x220(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    -0x214(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x218(%rbp)
cmp    $0xffffffff,%eax
jne    1261 <mpkg_copy+0x101>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xdb9(%rip),%rsi
call   1070 <err@plt>
mov    -0x218(%rbp),%eax
cmp    -0x214(%rbp),%eax
jge    1288 <mpkg_copy+0x128>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xd9c(%rip),%rsi
call   1040 <errx@plt>
jmp    11ff <mpkg_copy+0x9f>
cmpl   $0xffffffff,-0x214(%rbp)
jne    12af <mpkg_copy+0x14f>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xd90(%rip),%rsi
call   1070 <err@plt>
mov    -0x21c(%rbp),%edi
call   1050 <close@plt>
mov    -0x220(%rbp),%edi
call   1050 <close@plt>
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpkg_copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    11a3 <mpkg_copy+0x43>
lea    0xe6a(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1070 <err@plt>
mov    %rbx,0x8(%rsp)
mov    0x2e11(%rip),%rax
mov    0x2e42(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2f(%rip),%rax
or     (%rax),%esi
mov    %r15,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11ed <mpkg_copy+0x8d>
lea    0xe20(%rip),%rsi
mov    $0x1,%edi
mov    %r15,%rdx
call   1070 <err@plt>
lea    0x10(%rsp),%rsi
mov    %r12d,%edi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,%r13d
test   %eax,%eax
jle    127f <mpkg_copy+0x11f>
lea    0x10(%rsp),%rbp
jmp    1227 <mpkg_copy+0xc7>
nopl   (%rax)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,%r13d
test   %eax,%eax
jle    127f <mpkg_copy+0x11f>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r13d,%edx
call   1030 <write@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     1250 <mpkg_copy+0xf0>
cmp    %r13d,%r14d
jge    1210 <mpkg_copy+0xb0>
jmp    1269 <mpkg_copy+0x109>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x1,%edi
lea    0xdb9(%rip),%rsi
mov    %r15,%rdx
call   1070 <err@plt>
cmp    %r13d,%r14d
jge    1210 <mpkg_copy+0xb0>
mov    $0x1,%edi
lea    0xdaa(%rip),%rsi
mov    %r15,%rdx
call   1040 <errx@plt>
jmp    1210 <mpkg_copy+0xb0>
cmp    $0xffffffff,%r13d
jne    129b <mpkg_copy+0x13b>
lea    0xdae(%rip),%rsi
mov    $0x1,%edi
mov    0x8(%rsp),%rdx
call   1070 <err@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %ebx,%edi
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [21 x i8] c"cannot open file: %s\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [10 x i8] c"write: %s\00"
@.str.2 = [27 x i8] c"write: %s: truncated write\00"
@.str.3 = [9 x i8] c"read: %s\00"
define void @mpkg_copy(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [512 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = load i32, i32* @O_RDONLY
%12 = call i32 (i8*, i32, ...) @open(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %14, %2
%18 = load i8*, i8** %4
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = call i32 (i8*, i32, ...) @open(i8* %18, i32 %23, i32 420)
store i32 %24, i32* %9
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = load i8*, i8** %4
%28 = call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %26, %17
br label %30
30: ; preds = %51, %29
%31 = load i32, i32* %8
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 512)
store i32 %33, i32* %6
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %52
35: ; preds = %30
%36 = load i32, i32* %9
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%38 = load i32, i32* %6
%39 = call i32 @write(i32 %36, i8* %37, i32 %38)
store i32 %39, i32* %7
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = load i8*, i8** %4
%43 = call i32 @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %41, %35
%45 = load i32, i32* %7
%46 = load i32, i32* %6
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i8*, i8** %4
%50 = call i32 @errx(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %49)
br label %51
51: ; preds = %48, %44
br label %30
52: ; preds = %30
%53 = load i32, i32* %6
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i8*, i8** %3
%57 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %56)
br label %58
58: ; preds = %55, %52
%59 = load i32, i32* %8
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %9
%62 = call i32 @close(i32 %61)
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [21 x i8] c"cannot open file: %s\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [10 x i8] c"write: %s\00"
@.str.2 = [27 x i8] c"write: %s: truncated write\00"
@.str.3 = [9 x i8] c"read: %s\00"
define void @mpkg_copy(i8* %0, i8* %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* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %10
10: ; preds = %8, %2
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %15, i32 420)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %20
20: ; preds = %18, %10
%21 = call i32 @read(i32 %6, i8* %4, i32 512)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %36
23: ; preds = %20, %33
%24 = phi i32 [ %34, %33 ], [ %21, %20 ]
%25 = call i32 @write(i32 %16, i8* %4, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %29
29: ; preds = %27, %23
%30 = icmp slt i32 %25, %24
br i1 %30, label %31, label %33
31: ; preds = %29
%32 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %33
33: ; preds = %31, %29
%34 = call i32 @read(i32 %6, i8* %4, i32 512)
%35 = icmp sgt i32 %34, 0
br i1 %35, label %23, label %36
36: ; preds = %33, %20
%37 = phi i32 [ %21, %20 ], [ %34, %33 ]
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %41
41: ; preds = %39, %36
%42 = tail call i32 @close(i32 %6)
%43 = tail call i32 @close(i32 %16)
ret void
}