#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 scalar_t__ ssize_t;

/* Variables and functions */
int EXIT_SUCCESS;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, char **, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ write(int, char **, scalar_t__);
int main(int argc, char *argv[]) {
  int input_fd, output_fd;
  ssize_t ret_in, ret_out;
  char buffer[8192];
  char fpath[8192];
  if (argc != 3) {
    printf("Usage: cp file1 file2");
    return 1;
  }
  input_fd = open(argv[1], O_RDONLY);
  if (input_fd == -1) {
    perror("open");
    return 2;
  }
  strcpy(fpath, argv[2]);
  strcat(fpath, ".bkp");
  output_fd = open(fpath, O_WRONLY | O_CREAT, 0644);
  if (output_fd == -1) {
    perror("open");
    return 3;
  }
  while ((ret_in = read(input_fd, &buffer, 8192)) > 0) {
    ret_out = write(output_fd, &buffer, (ssize_t)ret_in);
    if (ret_out != ret_in) {
      perror("write");
      return 4;
    }
  }
  close(input_fd);
  close(output_fd);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11bb <main+0x3b>
lea    0xe56(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ee <main+0x16e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f8 <main+0x78>
lea    0xe2f(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12ee <main+0x16e>
lea    -0x4030(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1030 <strcpy@plt>
lea    -0x4030(%rbp),%rdi
lea    0xe01(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x4030(%rbp),%rdi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
mov    0x2dc2(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1269 <main+0xe9>
lea    0xdbe(%rip),%rdi
call   1090 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    12ee <main+0x16e>
jmp    126e <main+0xee>
mov    -0x14(%rbp),%edi
lea    -0x2030(%rbp),%rsi
mov    $0x2000,%edx
call   1070 <read@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jle    12d2 <main+0x152>
mov    -0x18(%rbp),%edi
lea    -0x2030(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     12cd <main+0x14d>
lea    0xd64(%rip),%rdi
call   1090 <perror@plt>
movl   $0x4,-0x4(%rbp)
jmp    12ee <main+0x16e>
jmp    126e <main+0xee>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x4008,%rsp
cmp    $0x3,%edi
jne    124e <main+0xce>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1288 <main+0x108>
mov    %eax,%r14d
mov    0x10(%rbx),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe4c(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strcat@plt>
mov    0x2de2(%rip),%rax
mov    0x2e13(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     129b <main+0x11b>
mov    %eax,%r15d
lea    0x2000(%rsp),%rbx
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x2000,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    1261 <main+0xe1>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
cmp    %rbp,%rax
je     1210 <main+0x90>
lea    0xdde(%rip),%rdi
call   1090 <perror@plt>
mov    $0x4,%eax
jmp    127a <main+0xfa>
lea    0xdab(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    127a <main+0xfa>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%eax
add    $0x4008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd87(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%eax
jmp    127a <main+0xfa>
lea    0xd74(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3,%eax
jmp    127a <main+0xfa>

LLVM IR (-O0):
@.str = [22 x i8] c"Usage: cp file1 file2\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c".bkp\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.3 = [6 x i8] c"write\00"
@EXIT_SUCCESS = 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 i64
%9 = alloca i64
%10 = alloca [8192 x i8]
%11 = alloca [8192 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %66
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDONLY
%21 = call i32 (i8*, i32, ...) @open(i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %66
26: ; preds = %16
%27 = getelementptr inbounds [8192 x i8], [8192 x i8]* %11, i64 0, i64 0
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @strcpy(i8* %27, i8* %30)
%32 = getelementptr inbounds [8192 x i8], [8192 x i8]* %11, i64 0, i64 0
%33 = call i32 @strcat(i8* %32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%34 = getelementptr inbounds [8192 x i8], [8192 x i8]* %11, i64 0, i64 0
%35 = load i32, i32* @O_WRONLY
%36 = load i32, i32* @O_CREAT
%37 = or i32 %35, %36
%38 = call i32 (i8*, i32, ...) @open(i8* %34, i32 %37, i32 420)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %26
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 3, i32* %3
br label %66
43: ; preds = %26
br label %44
44: ; preds = %59, %43
%45 = load i32, i32* %6
%46 = bitcast [8192 x i8]* %10 to i8**
%47 = call i64 @read(i32 %45, i8** %46, i32 8192)
store i64 %47, i64* %8
%48 = icmp sgt i64 %47, 0
br i1 %48, label %49, label %60
49: ; preds = %44
%50 = load i32, i32* %7
%51 = bitcast [8192 x i8]* %10 to i8**
%52 = load i64, i64* %8
%53 = call i64 @write(i32 %50, i8** %51, i64 %52)
store i64 %53, i64* %9
%54 = load i64, i64* %9
%55 = load i64, i64* %8
%56 = icmp ne i64 %54, %55
br i1 %56, label %57, label %59
57: ; preds = %49
%58 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 4, i32* %3
br label %66
59: ; preds = %49
br label %44
60: ; preds = %44
%61 = load i32, i32* %6
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* %7
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* @EXIT_SUCCESS
store i32 %65, i32* %3
br label %66
66: ; preds = %60, %57, %41, %24, %14
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str = [22 x i8] c"Usage: cp file1 file2\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c".bkp\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.3 = [6 x i8] c"write\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8192 x i8]
%4 = alloca [8192 x i8]
%5 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 3
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %44
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %44
18: ; preds = %10
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = call i32 @strcpy(i8* %6, i8* %20)
%22 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %23
%26 = call i32 (i8*, i32, ...) @open(i8* %6, i32 %25, i32 420)
%27 = icmp eq i32 %26, -1
br i1 %27, label %30, label %28
28: ; preds = %18
%29 = bitcast [8192 x i8]* %3 to i8**
br label %32
30: ; preds = %18
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %44
32: ; preds = %28, %35
%33 = call i64 @read(i32 %14, i8** %29, i32 8192)
%34 = icmp sgt i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = call i64 @write(i32 %26, i8** %29, i64 %33)
%37 = icmp eq i64 %36, %33
br i1 %37, label %32, label %38
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %44
40: ; preds = %32
%41 = call i32 @close(i32 %14)
%42 = call i32 @close(i32 %26)
%43 = load i32, i32* @EXIT_SUCCESS
br label %44
44: ; preds = %40, %38, %30, %16, %8
%45 = phi i32 [ 1, %8 ], [ 2, %16 ], [ 3, %30 ], [ 4, %38 ], [ %43, %40 ]
ret i32 %45
}