#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 */
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_quit(char *);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc != 3)
    err_quit("usage:./cpfile src dst\n");
  int n;
  char buffer[1024];
  int fd_src;
  int fd_dst;
  fd_src = open(argv[1], O_RDONLY);
  fd_dst = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0666);
  if (fd_src < 0 && fd_dst < 0)
    printf("open error\n");
  while ((n = read(fd_src, buffer, sizeof(buffer))) != 0) {
    write(fd_dst, buffer, n);
  }
  close(fd_src);
  close(fd_dst);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     118f <main+0x2f>
lea    0xe76(%rip),%rdi
call   1060 <err_quit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x424(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    0x2e33(%rip),%rax
or     (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0x0,-0x424(%rbp)
jge    1208 <main+0xa8>
cmpl   $0x0,-0x428(%rbp)
jge    1208 <main+0xa8>
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
jmp    120d <main+0xad>
mov    -0x424(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     124a <main+0xea>
mov    -0x428(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
jmp    120d <main+0xad>
mov    -0x424(%rbp),%edi
call   1050 <close@plt>
mov    -0x428(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%rbp
cmp    $0x3,%edi
je     117f <main+0x1f>
lea    0xe86(%rip),%rdi
call   1060 <err_quit@plt>
mov    0x8(%rbp),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%r14d
mov    0x2e3b(%rip),%rax
mov    0x2e54(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbp),%rdi
mov    0x2e3d(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
test   %r14d,%eax
jns    11d4 <main+0x74>
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
je     1210 <main+0xb0>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
jne    11f0 <main+0x90>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"usage:./cpfile src dst\0A\00"
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [12 x i8] c"open error\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1024 x i8]
%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 @err_quit(i8* getelementptr inbounds ([24 x i8], [24 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 (i8*, i32, ...) @open(i8* %17, i32 %18)
store i32 %19, i32* %8
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDWR
%24 = load i32, i32* @O_CREAT
%25 = or i32 %23, %24
%26 = load i32, i32* @O_TRUNC
%27 = or i32 %25, %26
%28 = call i32 (i8*, i32, ...) @open(i8* %22, i32 %27, i32 438)
store i32 %28, i32* %9
%29 = load i32, i32* %8
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %14
%32 = load i32, i32* %9
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %31, %14
br label %37
37: ; preds = %42, %36
%38 = load i32, i32* %8
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 1024)
store i32 %40, i32* %6
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = load i32, i32* %9
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%45 = load i32, i32* %6
%46 = call i32 @write(i32 %43, i8* %44, i32 %45)
br label %37
47: ; preds = %37
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %9
%51 = call i32 @close(i32 %50)
ret i32 0
}

LLVM IR (-O2):
@.str = [24 x i8] c"usage:./cpfile src dst\0A\00"
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@str = [11 x i8] c"open error\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 @err_quit(i8* getelementptr inbounds ([24 x i8], [24 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 (i8*, i32, ...) @open(i8* %10, i32 %11)
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_CREAT
%17 = or i32 %16, %15
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = tail call i32 (i8*, i32, ...) @open(i8* %14, i32 %19, i32 438)
%21 = and i32 %20, %12
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %7
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %23, %7
%26 = call i32 @read(i32 %12, i8* %8, i32 1024)
%27 = icmp eq i32 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %25, %28
%29 = phi i32 [ %31, %28 ], [ %26, %25 ]
%30 = call i32 @write(i32 %20, i8* %8, i32 %29)
%31 = call i32 @read(i32 %12, i8* %8, i32 1024)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %28
33: ; preds = %28, %25
%34 = tail call i32 @close(i32 %12)
%35 = tail call i32 @close(i32 %20)
ret i32 0
}