#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 /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ copy(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int main(int argc, char *args[]) {
  if (argc < 3) {
    printf("useage:cp from to\n");
    exit(1);
  }
  int fd_in = open(args[1], O_RDONLY);
  if (fd_in < 0) {
    printf("open file %s error:%s\n", args[1], strerror(errno));
    exit(1);
  }
  int fd_out = open(args[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (fd_in < 0) {
    printf("open/create file %s error:%s\n", args[2], strerror(errno));
    exit(1);
  }
  copy(fd_in, fd_out);
  close(fd_in);
  close(fd_out);
  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)
jge    1198 <main+0x38>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f6 <main+0x96>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    0x2dea(%rip),%rax
or     (%rax),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    126b <main+0x10b>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1050 <copy@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jle    11e1 <main+0x71>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     11f7 <main+0x87>
mov    %eax,%ebx
mov    0x2e1b(%rip),%rax
mov    0x2e54(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%r14),%rdi
mov    0x2e3d(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %eax,%esi
call   1060 <copy@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe2f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x8(%r14),%rbx
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdf0(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"useage:cp from to\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [23 x i8] c"open file %s error:%s\0A\00"
@errno = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [30 x i8] c"open/create file %s error:%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 3
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = call i32 (i8*, i32, ...) @open(i8* %16, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %13
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %13
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = load i32, i32* @O_WRONLY
%34 = load i32, i32* @O_CREAT
%35 = or i32 %33, %34
%36 = load i32, i32* @O_TRUNC
%37 = or i32 %35, %36
%38 = call i32 (i8*, i32, ...) @open(i8* %32, i32 %37, i32 420)
store i32 %38, i32* %7
%39 = load i32, i32* %6
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %49
41: ; preds = %29
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = load i8*, i8** %43
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %44, i8* %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %29
%50 = load i32, i32* %6
%51 = load i32, i32* %7
%52 = call i32 @copy(i32 %50, i32 %51)
%53 = load i32, i32* %6
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* %7
%56 = call i32 @close(i32 %55)
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [23 x i8] c"open file %s error:%s\0A\00"
@errno = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@str = [18 x i8] c"useage:cp from to\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
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 (i8*, i32, ...) @open(i8* %9, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %7
%14 = load i8*, i8** %8
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %16)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %7
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_WRONLY
%23 = load i32, i32* @O_CREAT
%24 = or i32 %23, %22
%25 = load i32, i32* @O_TRUNC
%26 = or i32 %24, %25
%27 = tail call i32 (i8*, i32, ...) @open(i8* %21, i32 %26, i32 420)
%28 = tail call i32 @copy(i32 %11, i32 %27)
%29 = tail call i32 @close(i32 %11)
%30 = tail call i32 @close(i32 %27)
ret i32 0
}