#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 creat(char *, int);
int /*<<< orphan*/ error(char *, ...);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc != 3)
    error("Usage: cp source destination");
  char buf[1024];
  int sourcefd, destfd, count;
  if ((sourcefd = open(argv[1], O_RDONLY)) == -1)
    error("cp: failed to open %s", argv[1]);
  if ((destfd = creat(argv[2], 0600)) == -1)
    error("cp: failed to open %s for writing", argv[2]);
  while ((count = read(sourcefd, buf, 1024)) > 0)
    if ((write(destfd, buf, count)) != count)
      error("cp: write error on file %s", argv[2]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1181 <main+0x31>
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x414(%rbp)
cmp    $0xffffffff,%eax
jne    11bc <main+0x6c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    $0x180,%esi
call   1070 <creat@plt>
mov    %eax,-0x418(%rbp)
cmp    $0xffffffff,%eax
jne    11f3 <main+0xa3>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
jmp    11f8 <main+0xa8>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x41c(%rbp)
cmp    $0x0,%eax
jle    125d <main+0x10d>
mov    -0x418(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x41c(%rbp),%eax
je     1258 <main+0x108>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
jmp    11f8 <main+0xa8>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
je     1177 <main+0x27>
lea    0xe90(%rip),%rdi
xor    %eax,%eax
call   1050 <error@plt>
mov    0x8(%r14),%rdi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    11a3 <main+0x53>
mov    0x8(%r14),%rsi
lea    0xe81(%rip),%rdi
xor    %eax,%eax
call   1050 <error@plt>
mov    0x10(%r14),%rdi
mov    $0x180,%esi
call   1070 <creat@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    11cb <main+0x7b>
mov    0x10(%r14),%rsi
lea    0xe6f(%rip),%rdi
xor    %eax,%eax
call   1050 <error@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1227 <main+0xd7>
mov    %eax,%ebx
mov    %rsp,%rbp
lea    0xe6a(%rip),%r12
jmp    1206 <main+0xb6>
nopl   (%rax)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1227 <main+0xd7>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     11f0 <main+0xa0>
mov    0x10(%r14),%rsi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <error@plt>
jmp    11f0 <main+0xa0>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Usage: cp source destination\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"cp: failed to open %s\00"
@.str.2 = [34 x i8] c"cp: failed to open %s for writing\00"
@.str.3 = [27 x i8] c"cp: write error on file %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 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 (i8*, ...) @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%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 %26
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %21, %14
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @creat(i8* %29, i32 384)
store i32 %30, i32* %8
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %37
32: ; preds = %26
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %32, %26
br label %38
38: ; preds = %55, %37
%39 = load i32, i32* %7
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%41 = call i32 @read(i32 %39, i8* %40, i32 1024)
store i32 %41, i32* %9
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %56
43: ; preds = %38
%44 = load i32, i32* %8
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%46 = load i32, i32* %9
%47 = call i32 @write(i32 %44, i8* %45, i32 %46)
%48 = load i32, i32* %9
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %55
50: ; preds = %43
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 2
%53 = load i8*, i8** %52
%54 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %50, %43
br label %38
56: ; preds = %38
ret i32 0
}

LLVM IR (-O2):
@.str = [29 x i8] c"Usage: cp source destination\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"cp: failed to open %s\00"
@.str.2 = [34 x i8] c"cp: failed to open %s for writing\00"
@.str.3 = [27 x i8] c"cp: write error on file %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = icmp eq i32 %0, 3
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%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 = %7
%15 = load i8*, i8** %9
%16 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %14, %7
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i32 @creat(i8* %19, i32 384)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i8*, i8** %18
%24 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %17
%26 = call i32 @read(i32 %12, i8* %8, i32 1024)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %38
28: ; preds = %25, %35
%29 = phi i32 [ %36, %35 ], [ %26, %25 ]
%30 = call i32 @write(i32 %20, i8* %8, i32 %29)
%31 = icmp eq i32 %30, %29
br i1 %31, label %35, label %32
32: ; preds = %28
%33 = load i8*, i8** %18
%34 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %32, %28
%36 = call i32 @read(i32 %12, i8* %8, i32 1024)
%37 = icmp sgt i32 %36, 0
br i1 %37, label %28, label %38
38: ; preds = %35, %25
ret i32 0
}