#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 O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  char buf[30];
  int in, out;
  int n;
  if (argc != 3)
    exit(1);
  if ((in = open(argv[2], O_RDONLY)) == -1) {
    perror("fail to open");
    exit(1);
  }
  if ((out = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT)) == -1) {
    perror("fail to open");
    exit(1);
  }
  while ((n = read(in, buf, 30)) > 0)
    if ((write(out, buf, n)) != n) {
      perror("fail to write");
      exit(1);
    }
  if (n < 0) {
    perror("fail to read");
    exit(1);
  }
  printf("copy done\n");
  close(in);
  close(out);
  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     119a <main+0x2a>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    11d2 <main+0x62>
lea    0xe3d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    0x2e06(%rip),%rax
or     (%rax),%esi
mov    0x2e05(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x38(%rbp)
cmp    $0xffffffff,%eax
jne    121c <main+0xac>
lea    0xdf3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1221 <main+0xb1>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1e,%edx
call   1060 <read@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jle    1271 <main+0x101>
mov    -0x38(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x3c(%rbp),%eax
je     126c <main+0xfc>
lea    0xdb0(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1221 <main+0xb1>
cmpl   $0x0,-0x3c(%rbp)
jge    1291 <main+0x121>
lea    0xd99(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd90(%rip),%rdi
call   1040 <printf@plt>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    -0x38(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
cmp    $0x3,%edi
jne    1251 <main+0xe1>
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1248 <main+0xd8>
mov    %eax,%r14d
mov    0x2e15(%rip),%rax
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%rbx),%rdi
mov    0x2e37(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1248 <main+0xd8>
mov    %eax,%r15d
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1e,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    121d <main+0xad>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     11e0 <main+0x70>
lea    0xdff(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
js     125b <main+0xeb>
lea    0xe02(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r15d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb1(%rip),%rdi
jmp    120e <main+0x9e>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdb9(%rip),%rdi
jmp    120e <main+0x9e>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [13 x i8] c"fail to open\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.1 = [14 x i8] c"fail to write\00"
@.str.2 = [13 x i8] c"fail to read\00"
@.str.3 = [11 x i8] c"copy done\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [30 x i8]
%7 = alloca i32
%8 = alloca i32
%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 %14
12: ; preds = %2
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 2
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %7
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %14
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = load i32, i32* @O_WRONLY
%29 = load i32, i32* @O_TRUNC
%30 = or i32 %28, %29
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = call i32 @open(i8* %27, i32 %32)
store i32 %33, i32* %8
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %24
%36 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %24
br label %39
39: ; preds = %54, %38
%40 = load i32, i32* %7
%41 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%42 = call i32 @read(i32 %40, i8* %41, i32 30)
store i32 %42, i32* %9
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %55
44: ; preds = %39
%45 = load i32, i32* %8
%46 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%47 = load i32, i32* %9
%48 = call i32 @write(i32 %45, i8* %46, i32 %47)
%49 = load i32, i32* %9
%50 = icmp ne i32 %48, %49
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %44
br label %39
55: ; preds = %39
%56 = load i32, i32* %9
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %55
%62 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%63 = load i32, i32* %7
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* %8
%66 = call i32 @close(i32 %65)
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [13 x i8] c"fail to open\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.1 = [14 x i8] c"fail to write\00"
@.str.2 = [13 x i8] c"fail to read\00"
@str = [10 x i8] c"copy done\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [30 x i8]
%4 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 3
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %8
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_WRONLY
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %21, %20
%23 = load i32, i32* @O_CREAT
%24 = or i32 %22, %23
%25 = tail call i32 @open(i8* %19, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %17, %33
%31 = call i32 @read(i32 %12, i8* %4, i32 30)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = call i32 @write(i32 %25, i8* %4, i32 %31)
%35 = icmp eq i32 %34, %31
br i1 %35, label %30, label %36
36: ; preds = %33
%37 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %30
%40 = icmp slt i32 %31, 0
br i1 %40, label %41, label %44
41: ; preds = %39
%42 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%43 = tail call i32 @exit(i32 1)
unreachable
44: ; preds = %39
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%46 = tail call i32 @close(i32 %12)
%47 = tail call i32 @close(i32 %25)
ret i32 0
}