#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 O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ cp(int, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int input_fd;
  int output_fd;
  if (argc < 3) {
    printf("\nUSAGE: cp src dest\n\n");
  } else {
    input_fd = open(argv[1], O_RDONLY, 0);
    if (input_fd < 0) {
      printf("Error opening input file %s (%d)\n", argv[1], input_fd);
      return -1;
    }
    output_fd = open(argv[2], O_WRONLY | O_CREAT, 0660);
    if (output_fd < 0) {
      printf("Error opening output file %s (%d)\n", argv[2], output_fd);
      return -1;
    }
    cp(input_fd, output_fd);
    close(input_fd);
    close(output_fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1173 <main+0x33>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    122e <main+0xee>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11bd <main+0x7d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x14(%rbp),%edx
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <main+0xf5>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    0x2e23(%rip),%rax
or     (%rax),%esi
mov    $0x1b0,%edx
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1213 <main+0xd3>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x18(%rbp),%edx
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <main+0xf5>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <cp@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jg     1167 <main+0x17>
lea    0xee5(%rip),%rdi
call   1030 <puts@plt>
jmp    11c4 <main+0x74>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11c8 <main+0x78>
mov    0x10(%rbx),%rdi
mov    0x2e39(%rip),%rax
mov    0x2e62(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b0,%edx
call   1070 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
js     11d7 <main+0x87>
mov    %ebp,%edi
mov    %r14d,%esi
call   1040 <cp@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    11f1 <main+0xa1>
mov    0x8(%rbx),%rsi
lea    0xe2d(%rip),%rdi
mov    %ebp,%edx
jmp    11e5 <main+0x95>
mov    0x10(%rbx),%rsi
lea    0xe40(%rip),%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"\0AUSAGE: cp src dest\0A\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [34 x i8] c"Error opening input file %s (%d)\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [35 x i8] c"Error opening output file %s (%d)\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 %12
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %50
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16, i32 0)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %12
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* %6
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %23, i32 %24)
store i32 -1, i32* %3
br label %51
26: ; preds = %12
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = load i32, i32* @O_WRONLY
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = call i32 @open(i8* %29, i32 %32, i32 432)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %26
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %38
%40 = load i32, i32* %7
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %39, i32 %40)
store i32 -1, i32* %3
br label %51
42: ; preds = %26
%43 = load i32, i32* %6
%44 = load i32, i32* %7
%45 = call i32 @cp(i32 %43, i32 %44)
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %42, %10
store i32 0, i32* %3
br label %51
51: ; preds = %50, %36, %20
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [34 x i8] c"Error opening input file %s (%d)\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [35 x i8] c"Error opening output file %s (%d)\0A\00"
@str = [21 x i8] c"\0AUSAGE: cp src dest\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %30
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %6
%13 = load i8*, i8** %7
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %13, i32 %10)
br label %30
15: ; preds = %6
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %19, %18
%21 = tail call i32 @open(i8* %17, i32 %20, i32 432)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = load i8*, i8** %16
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %24, i32 %21)
br label %30
26: ; preds = %15
%27 = tail call i32 @cp(i32 %10, i32 %21)
%28 = tail call i32 @close(i32 %10)
%29 = tail call i32 @close(i32 %21)
br label %30
30: ; preds = %4, %26, %23, %12
%31 = phi i32 [ -1, %12 ], [ -1, %23 ], [ 0, %26 ], [ 0, %4 ]
ret i32 %31
}