#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 */
struct stat {
  int st_mode;
};
typedef int /*<<< orphan*/ line;

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int O_WRONLY;
int S_IFDIR;
int S_IFMT;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int inf, outf;
  char line[10];
  int bytes;
  struct stat st;
  if (argc != 3) {
    fprintf(stderr, "USAGE: cp sourcefile destfile\n");
    return 1;
  }
  if (stat(argv[1], &st) == -1) {
    perror(argv[1]);
    return 1;
  }
  if ((st.st_mode & S_IFMT) == S_IFDIR) {
    fprintf(stderr, "%s: is a directory\n", argv[1]);
    return 1;
  }
  if ((inf = open(argv[1], O_RDONLY)) == -1) {
    perror(argv[1]);
    return 1;
  }
  if ((outf = open(argv[2], O_WRONLY | O_CREAT), 0666) == -1) {
    perror(argv[1]);
    return 1;
  }
  while ((bytes = read(inf, line, sizeof(line))) > 0)
    write(outf, line, bytes);
  close(inf);
  close(outf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11b3 <main+0x43>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x18e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11e6 <main+0x76>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x18e>
mov    -0x30(%rbp),%eax
mov    0x2de0(%rip),%rcx
and    (%rcx),%eax
mov    0x2db7(%rip),%rcx
cmp    (%rcx),%eax
jne    122c <main+0xbc>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x18e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1267 <main+0xf7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x18e>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
mov    0x2d79(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
test   $0x1,%al
jne    1298 <main+0x128>
jmp    12b1 <main+0x141>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x18e>
jmp    12b6 <main+0x146>
mov    -0x14(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0xa,%edx
call   1050 <read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jle    12e7 <main+0x177>
mov    -0x18(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
jmp    12b6 <main+0x146>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x3,%edi
jne    11d6 <main+0x66>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     128c <main+0x11c>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%eax
jne    11f7 <main+0x87>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xe5a(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    1281 <main+0x111>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    1281 <main+0x111>
mov    0x8(%r14),%rdi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     128c <main+0x11c>
mov    %eax,%r15d
mov    0x10(%r14),%rdi
mov    0x2d9c(%rip),%rax
mov    0x2dd5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebp
lea    0xe(%rsp),%rsi
mov    %r15d,%edi
mov    $0xa,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1270 <main+0x100>
lea    0xe(%rsp),%rbx
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     1250 <main+0xe0>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%r14),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
jmp    1281 <main+0x111>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [31 x i8] c"USAGE: cp sourcefile destfile\0A\00"
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
@.str.1 = [20 x i8] c"%s: is a directory\0A\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [10 x i8]
%9 = alloca i32
%10 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %81
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @stat(i8* %19, %struct.stat* %10)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @perror(i8* %25)
store i32 1, i32* %3
br label %81
27: ; preds = %16
%28 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load i32, i32* @S_IFMT
%31 = and i32 %29, %30
%32 = load i32, i32* @S_IFDIR
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %40
34: ; preds = %27
%35 = load i32, i32* @stderr
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %38)
store i32 1, i32* %3
br label %81
40: ; preds = %27
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = load i32, i32* @O_RDONLY
%45 = call i32 @open(i8* %43, i32 %44)
store i32 %45, i32* %6
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %52
47: ; preds = %40
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = call i32 @perror(i8* %50)
store i32 1, i32* %3
br label %81
52: ; preds = %40
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 2
%55 = load i8*, i8** %54
%56 = load i32, i32* @O_WRONLY
%57 = load i32, i32* @O_CREAT
%58 = or i32 %56, %57
%59 = call i32 @open(i8* %55, i32 %58)
store i32 %59, i32* %7
br i1 false, label %60, label %65
60: ; preds = %52
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 1
%63 = load i8*, i8** %62
%64 = call i32 @perror(i8* %63)
store i32 1, i32* %3
br label %81
65: ; preds = %52
br label %66
66: ; preds = %71, %65
%67 = load i32, i32* %6
%68 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%69 = call i32 @read(i32 %67, i8* %68, i32 10)
store i32 %69, i32* %9
%70 = icmp sgt i32 %69, 0
br i1 %70, label %71, label %76
71: ; preds = %66
%72 = load i32, i32* %7
%73 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%74 = load i32, i32* %9
%75 = call i32 @write(i32 %72, i8* %73, i32 %74)
br label %66
76: ; preds = %66
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
%79 = load i32, i32* %7
%80 = call i32 @close(i32 %79)
store i32 0, i32* %3
br label %81
81: ; preds = %76, %60, %47, %34, %22, %13
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [31 x i8] c"USAGE: cp sourcefile destfile\0A\00"
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
@.str.1 = [20 x i8] c"%s: is a directory\0A\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [10 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %55
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @stat(i8* %13, %struct.stat* %4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i8*, i8** %12
%18 = tail call i32 @perror(i8* %17)
br label %55
19: ; preds = %11
%20 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @S_IFMT
%23 = and i32 %22, %21
%24 = load i32, i32* @S_IFDIR
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %12
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %55
30: ; preds = %19
%31 = load i8*, i8** %12
%32 = load i32, i32* @O_RDONLY
%33 = tail call i32 @open(i8* %31, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i8*, i8** %12
%37 = tail call i32 @perror(i8* %36)
br label %55
38: ; preds = %30
%39 = getelementptr inbounds i8*, i8** %1, i64 2
%40 = load i8*, i8** %39
%41 = load i32, i32* @O_WRONLY
%42 = load i32, i32* @O_CREAT
%43 = or i32 %42, %41
%44 = tail call i32 @open(i8* %40, i32 %43)
%45 = call i32 @read(i32 %33, i8* %5, i32 10)
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %38, %47
%48 = phi i32 [ %50, %47 ], [ %45, %38 ]
%49 = call i32 @write(i32 %44, i8* %5, i32 %48)
%50 = call i32 @read(i32 %33, i8* %5, i32 10)
%51 = icmp sgt i32 %50, 0
br i1 %51, label %47, label %52
52: ; preds = %47, %38
%53 = tail call i32 @close(i32 %33)
%54 = tail call i32 @close(i32 %44)
br label %55
55: ; preds = %52, %35, %26, %16, %8
%56 = phi i32 [ 1, %8 ], [ 1, %16 ], [ 1, %26 ], [ 1, %35 ], [ 0, %52 ]
ret i32 %56
}