#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int creat(char *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char **argv) {
  char ch;
  int n, fd1, fd2;
  if (argc != 3) {
    printf("Usage: cp file1 file2\n");
    return 0;
  }
  if ((fd1 = open(*++argv, O_RDONLY)) < 0) {
    fprintf(stderr, "open file '%s' failed: %s\n", *argv, strerror(errno));
    return 0;
  }
  if ((fd2 = creat(*++argv, 0644)) < 0) {
    fprintf(stderr, "create file '%s' failed: %s\n", *argv, strerror(errno));
    return 0;
  }
  while ((n = read(fd1, &ch, 1)) > 0) {
    if (ch != 0) {
      if (write(fd2, &ch, n) != n) {
        fprintf(stderr, "write error\n");
        return 0;
      }
    }
  }
  close(fd1);
  close(fd2);
  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     11b8 <main+0x38>
lea    0xe59(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132b <main+0x1ab>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    122e <main+0xae>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132b <main+0x1ab>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rdi
mov    $0x1a4,%esi
call   1090 <creat@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    12a0 <main+0x120>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xda5(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132b <main+0x1ab>
jmp    12a5 <main+0x125>
mov    -0x1c(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1314 <main+0x194>
movsbl -0x11(%rbp),%eax
cmp    $0x0,%eax
je     130f <main+0x18f>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%edx
lea    -0x11(%rbp),%rsi
call   1030 <write@plt>
cmp    -0x18(%rbp),%eax
je     130a <main+0x18a>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
lea    0xd58(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132b <main+0x1ab>
jmp    130f <main+0x18f>
jmp    12a5 <main+0x125>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%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
cmp    $0x3,%edi
jne    1236 <main+0xb6>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1244 <main+0xc4>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
mov    $0x1a4,%esi
call   1090 <creat@plt>
test   %eax,%eax
js     1279 <main+0xf9>
mov    %eax,%r14d
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1268 <main+0xe8>
mov    %eax,%ebp
lea    0x7(%rsp),%r15
jmp    1205 <main+0x85>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1268 <main+0xe8>
cmpb   $0x0,0x7(%rsp)
je     11f0 <main+0x70>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     11f0 <main+0x70>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12aa <main+0x12a>
lea    0xe08(%rip),%rdi
call   1030 <puts@plt>
jmp    12aa <main+0x12a>
mov    0x2d8d(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%rbp),%rbp
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd9a(%rip),%rsi
jmp    129b <main+0x11b>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
jmp    12aa <main+0x12a>
mov    0x2d58(%rip),%rax
mov    (%rax),%ebx
mov    0x10(%rbp),%rbp
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd80(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"open file '%s' failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [29 x i8] c"create file '%s' failed: %s\0A\00"
@.str.3 = [13 x i8] c"write error\0A\00"
@str = [22 x i8] c"Usage: cp file1 file2\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = icmp eq i32 %0, 3
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %49
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 @open(i8* %9, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %7
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %8
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %15, i8* %17)
br label %49
19: ; preds = %7
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = tail call i32 @creat(i8* %21, i32 420)
%23 = icmp slt i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = call i32 @read(i32 %11, i8* %3, i32 1)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %33, label %46
27: ; preds = %19
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %20
%30 = load i32, i32* @errno
%31 = tail call i8* @strerror(i32 %30)
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %29, i8* %31)
br label %49
33: ; preds = %24, %43
%34 = phi i32 [ %44, %43 ], [ %25, %24 ]
%35 = load i8, i8* %3
%36 = icmp eq i8 %35, 0
br i1 %36, label %43, label %37
37: ; preds = %33
%38 = call i32 @write(i32 %22, i8* %3, i32 %34)
%39 = icmp eq i32 %38, %34
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %49
43: ; preds = %37, %33
%44 = call i32 @read(i32 %11, i8* %3, i32 1)
%45 = icmp sgt i32 %44, 0
br i1 %45, label %33, label %46
46: ; preds = %43, %24
%47 = tail call i32 @close(i32 %11)
%48 = tail call i32 @close(i32 %22)
br label %49
49: ; preds = %46, %40, %27, %13, %5
ret i32 0
}