#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 */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ malloc(int);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, char *, ...);
int read(int, char *, int);
char *strerror(int /*<<< orphan*/);
scalar_t__ write(int, char *, int);
int main(int argc, char **argv) {
  int src, dst;
  char *copy_byte;
  int read_count;
  if (argc != 3) {
    printf("Usage:%s <fromfile> <tofile>\n", argv[0]);
    return -1;
  }
  if ((src = open(argv[1], O_RDONLY)) < 0) {
    printf("open %s:%s\n", argv[1], strerror(errno));
    return -1;
  }
  if ((dst = open(
           argv[2], O_RDWR | O_CREAT | O_TRUNC,
           (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH))) < 0) {
    printf("open %s:%s\n", argv[1], strerror(errno));
    return -1;
  }
  copy_byte = (char *)malloc(512);
  while (1) {
    read_count = read(src, copy_byte, 512 - 1);
    if (read_count == 0)
      break;
    if (read_count < 0) {
      printf("read:%s\n", strerror(errno));
      return -1;
    }
    if (write(dst, copy_byte, read_count) < 0) {
      printf("write:%s\n", strerror(errno));
      return -1;
    }
  }
  close(src);
  close(dst);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11b1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <main+0x20d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1210 <main+0xa0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <main+0x20d>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
mov    0x2dbf(%rip),%rax
or     (%rax),%esi
mov    0x2d5e(%rip),%rax
mov    (%rax),%edx
mov    0x2d7d(%rip),%rax
or     (%rax),%edx
mov    0x2d5c(%rip),%rax
or     (%rax),%edx
mov    0x2d73(%rip),%rax
or     (%rax),%edx
mov    0x2d42(%rip),%rax
or     (%rax),%edx
mov    0x2d21(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12b7 <main+0x147>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <main+0x20d>
mov    $0x200,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1ff,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    12e8 <main+0x178>
jmp    1366 <main+0x1f6>
cmpl   $0x0,-0x24(%rbp)
jge    131d <main+0x1ad>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xd20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <main+0x20d>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1361 <main+0x1f1>
mov    0x2c73(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xce5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <main+0x20d>
jmp    12c5 <main+0x155>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x3,%edi
jne    125e <main+0xee>
mov    0x8(%r14),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     126a <main+0xfa>
mov    %eax,%r15d
mov    0x2e2f(%rip),%rax
mov    0x2e48(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    0x2dd4(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd2(%rip),%rax
or     (%rax),%edx
mov    0x2de9(%rip),%rax
or     (%rax),%edx
mov    0x2db8(%rip),%rax
or     (%rax),%edx
mov    0x10(%r14),%rdi
mov    0x2d93(%rip),%rax
or     (%rax),%edx
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     126a <main+0xfa>
mov    %eax,%ebp
mov    $0x200,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1ff,%edx
call   1060 <read@plt>
test   %eax,%eax
je     1292 <main+0x122>
js     12a5 <main+0x135>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    1220 <main+0xb0>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdd7(%rip),%rdi
jmp    12ba <main+0x14a>
mov    (%r14),%rsi
lea    0xd98(%rip),%rdi
jmp    12bd <main+0x14d>
mov    0x8(%r14),%rbx
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd9b(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12c4 <main+0x154>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    12c9 <main+0x159>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd70(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Usage:%s <fromfile> <tofile>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"open %s:%s\0A\00"
@errno = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str.2 = [9 x i8] c"read:%s\0A\00"
@.str.3 = [10 x i8] c"write:%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %93
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = call i32 (i8*, i32, ...) @open(i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %17
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %29)
store i32 -1, i32* %3
br label %93
31: ; preds = %17
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = load i32, i32* @O_RDWR
%36 = load i32, i32* @O_CREAT
%37 = or i32 %35, %36
%38 = load i32, i32* @O_TRUNC
%39 = or i32 %37, %38
%40 = load i32, i32* @S_IRUSR
%41 = load i32, i32* @S_IWUSR
%42 = or i32 %40, %41
%43 = load i32, i32* @S_IRGRP
%44 = or i32 %42, %43
%45 = load i32, i32* @S_IWGRP
%46 = or i32 %44, %45
%47 = load i32, i32* @S_IROTH
%48 = or i32 %46, %47
%49 = load i32, i32* @S_IWOTH
%50 = or i32 %48, %49
%51 = call i32 (i8*, i32, ...) @open(i8* %34, i32 %39, i32 %50)
store i32 %51, i32* %7
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %60
53: ; preds = %31
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %56, i8* %58)
store i32 -1, i32* %3
br label %93
60: ; preds = %31
%61 = call i64 @malloc(i32 512)
%62 = inttoptr i64 %61 to i8*
store i8* %62, i8** %8
br label %63
63: ; preds = %60, %87
%64 = load i32, i32* %6
%65 = load i8*, i8** %8
%66 = call i32 @read(i32 %64, i8* %65, i32 511)
store i32 %66, i32* %9
%67 = load i32, i32* %9
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %63
br label %88
70: ; preds = %63
%71 = load i32, i32* %9
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %77
73: ; preds = %70
%74 = load i32, i32* @errno
%75 = call i8* @strerror(i32 %74)
%76 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %75)
store i32 -1, i32* %3
br label %93
77: ; preds = %70
%78 = load i32, i32* %7
%79 = load i8*, i8** %8
%80 = load i32, i32* %9
%81 = call i64 @write(i32 %78, i8* %79, i32 %80)
%82 = icmp slt i64 %81, 0
br i1 %82, label %83, label %87
83: ; preds = %77
%84 = load i32, i32* @errno
%85 = call i8* @strerror(i32 %84)
%86 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %85)
store i32 -1, i32* %3
br label %93
87: ; preds = %77
br label %63
88: ; preds = %69
%89 = load i32, i32* %6
%90 = call i32 @close(i32 %89)
%91 = load i32, i32* %7
%92 = call i32 @close(i32 %91)
store i32 0, i32* %3
br label %93
93: ; preds = %88, %83, %73, %53, %24, %12
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
@.str = [30 x i8] c"Usage:%s <fromfile> <tofile>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"open %s:%s\0A\00"
@errno = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str.2 = [9 x i8] c"read:%s\0A\00"
@.str.3 = [10 x i8] c"write:%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %66
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 (i8*, i32, ...) @open(i8* %9, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %7
%14 = load i8*, i8** %8
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %16)
br label %66
18: ; preds = %7
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_CREAT
%23 = or i32 %22, %21
%24 = load i32, i32* @O_TRUNC
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IRUSR
%27 = load i32, i32* @S_IWUSR
%28 = or i32 %27, %26
%29 = load i32, i32* @S_IRGRP
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IWGRP
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IROTH
%34 = or i32 %32, %33
%35 = load i32, i32* @S_IWOTH
%36 = or i32 %34, %35
%37 = tail call i32 (i8*, i32, ...) @open(i8* %20, i32 %25, i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %18
%40 = load i8*, i8** %8
%41 = load i32, i32* @errno
%42 = tail call i8* @strerror(i32 %41)
%43 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %40, i8* %42)
br label %66
44: ; preds = %18
%45 = tail call i64 @malloc(i32 512)
%46 = inttoptr i64 %45 to i8*
br label %47
47: ; preds = %56, %44
%48 = tail call i32 @read(i32 %11, i8* %46, i32 511)
%49 = icmp eq i32 %48, 0
br i1 %49, label %63, label %50
50: ; preds = %47
%51 = icmp slt i32 %48, 0
br i1 %51, label %52, label %56
52: ; preds = %50
%53 = load i32, i32* @errno
%54 = tail call i8* @strerror(i32 %53)
%55 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %54)
br label %66
56: ; preds = %50
%57 = tail call i64 @write(i32 %37, i8* %46, i32 %48)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %47
59: ; preds = %56
%60 = load i32, i32* @errno
%61 = tail call i8* @strerror(i32 %60)
%62 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %61)
br label %66
63: ; preds = %47
%64 = tail call i32 @close(i32 %11)
%65 = tail call i32 @close(i32 %37)
br label %66
66: ; preds = %63, %59, %52, %39, %13, %4
%67 = phi i32 [ -1, %4 ], [ -1, %13 ], [ -1, %39 ], [ 0, %63 ], [ -1, %52 ], [ -1, %59 ]
ret i32 %67
}