#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 ssize_t;
typedef int mode_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int close(int);
int /*<<< orphan*/ helpAndLeave(char *, int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ pexit(char *);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int inputFd, outputFd, openFlags;
  mode_t filePerms;
  ssize_t numRead;
  char buf[1024];
  if (argc != 3) {
    helpAndLeave(argv[0], EXIT_FAILURE);
  }
  inputFd = open(argv[1], O_RDONLY);
  if (inputFd == -1) {
    pexit("open");
  }
  openFlags = O_CREAT | O_WRONLY | O_TRUNC;
  filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
  outputFd = open(argv[2], openFlags, filePerms);
  if (outputFd == -1) {
    pexit("open");
  }
  while ((numRead = read(inputFd, buf, 1024)) > 0) {
    if (write(outputFd, buf, numRead) != numRead) {
      pexit("write");
    }
  }
  if (numRead == -1) {
    pexit("read");
  }
  if (close(inputFd) == -1) {
    pexit("close");
  }
  if (close(outputFd) == -1) {
    pexit("close");
  }
  return EXIT_SUCCESS;
}


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     1198 <main+0x38>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
call   1070 <helpAndLeave@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   $0xffffffff,-0x14(%rbp)
jne    11c9 <main+0x69>
lea    0xe3c(%rip),%rdi
call   1080 <pexit@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    0x2daf(%rip),%rcx
or     (%rcx),%eax
mov    0x2e0e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    0x2dd1(%rip),%rcx
or     (%rcx),%eax
mov    0x2db8(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc7(%rip),%rcx
or     (%rcx),%eax
mov    0x2d9e(%rip),%rcx
or     (%rcx),%eax
mov    0x2d65(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    124e <main+0xee>
lea    0xdb7(%rip),%rdi
call   1080 <pexit@plt>
jmp    1253 <main+0xf3>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    129f <main+0x13f>
mov    -0x18(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    -0x24(%rbp),%eax
je     129a <main+0x13a>
lea    0xd70(%rip),%rdi
call   1080 <pexit@plt>
jmp    1253 <main+0xf3>
cmpl   $0xffffffff,-0x24(%rbp)
jne    12b5 <main+0x155>
lea    0xd5b(%rip),%rdi
call   1080 <pexit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12d2 <main+0x172>
lea    0xd43(%rip),%rdi
call   1080 <pexit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12ef <main+0x18f>
lea    0xd26(%rip),%rdi
call   1080 <pexit@plt>
mov    0x2ca2(%rip),%rax
mov    (%rax),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%rbp
cmp    $0x3,%edi
je     1189 <main+0x29>
mov    0x0(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
call   1070 <helpAndLeave@plt>
mov    0x8(%rbp),%rdi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    11b1 <main+0x51>
lea    0xe54(%rip),%rdi
call   1080 <pexit@plt>
mov    0x2e40(%rip),%rax
mov    0x2dc9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e26(%rip),%rax
or     (%rax),%esi
mov    0x2dcd(%rip),%rax
mov    0x2dee(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd3(%rip),%rax
or     (%rax),%edx
mov    0x2de2(%rip),%rax
or     (%rax),%edx
mov    0x2db9(%rip),%rax
or     (%rax),%edx
mov    0x2d80(%rip),%rax
or     (%rax),%edx
mov    0x10(%rbp),%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    1221 <main+0xc1>
lea    0xde4(%rip),%rdi
call   1080 <pexit@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1281 <main+0x121>
mov    %rsp,%rbx
lea    0xdc4(%rip),%r15
jmp    1266 <main+0x106>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1281 <main+0x121>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1250 <main+0xf0>
mov    %r15,%rdi
call   1080 <pexit@plt>
jmp    1250 <main+0xf0>
cmp    $0xffffffff,%ebp
jne    1292 <main+0x132>
lea    0xd7e(%rip),%rdi
call   1080 <pexit@plt>
mov    %r14d,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12ab <main+0x14b>
lea    0xd6a(%rip),%rdi
call   1080 <pexit@plt>
mov    %r12d,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12c4 <main+0x164>
lea    0xd51(%rip),%rdi
call   1080 <pexit@plt>
mov    0x2ccd(%rip),%rax
mov    (%rax),%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [6 x i8] c"close\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 i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1024 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 %20
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @helpAndLeave(i8* %17, i32 %18)
br label %20
20: ; preds = %14, %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDONLY
%25 = call i32 (i8*, i32, ...) @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %20
%31 = load i32, i32* @O_CREAT
%32 = load i32, i32* @O_WRONLY
%33 = or i32 %31, %32
%34 = load i32, i32* @O_TRUNC
%35 = or i32 %33, %34
store i32 %35, i32* %8
%36 = load i32, i32* @S_IRUSR
%37 = load i32, i32* @S_IWUSR
%38 = or i32 %36, %37
%39 = load i32, i32* @S_IRGRP
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IWGRP
%42 = or i32 %40, %41
%43 = load i32, i32* @S_IROTH
%44 = or i32 %42, %43
%45 = load i32, i32* @S_IWOTH
%46 = or i32 %44, %45
store i32 %46, i32* %9
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 2
%49 = load i8*, i8** %48
%50 = load i32, i32* %8
%51 = load i32, i32* %9
%52 = call i32 (i8*, i32, ...) @open(i8* %49, i32 %50, i32 %51)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %57
55: ; preds = %30
%56 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %57
57: ; preds = %55, %30
br label %58
58: ; preds = %72, %57
%59 = load i32, i32* %6
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%61 = call i32 @read(i32 %59, i8* %60, i32 1024)
store i32 %61, i32* %10
%62 = icmp sgt i32 %61, 0
br i1 %62, label %63, label %73
63: ; preds = %58
%64 = load i32, i32* %7
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%66 = load i32, i32* %10
%67 = call i32 @write(i32 %64, i8* %65, i32 %66)
%68 = load i32, i32* %10
%69 = icmp ne i32 %67, %68
br i1 %69, label %70, label %72
70: ; preds = %63
%71 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %72
72: ; preds = %70, %63
br label %58
73: ; preds = %58
%74 = load i32, i32* %10
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %78
76: ; preds = %73
%77 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %78
78: ; preds = %76, %73
%79 = load i32, i32* %6
%80 = call i32 @close(i32 %79)
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %84
82: ; preds = %78
%83 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %84
84: ; preds = %82, %78
%85 = load i32, i32* %7
%86 = call i32 @close(i32 %85)
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %90
88: ; preds = %84
%89 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %90
90: ; preds = %88, %84
%91 = load i32, i32* @EXIT_SUCCESS
ret i32 %91
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [6 x i8] c"close\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @helpAndLeave(i8* %7, i32 %8)
br label %10
10: ; preds = %6, %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 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %10
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_WRONLY
%21 = or i32 %20, %19
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRUSR
%25 = load i32, i32* @S_IWUSR
%26 = or i32 %25, %24
%27 = load i32, i32* @S_IRGRP
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IWGRP
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IROTH
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IWOTH
%34 = or i32 %32, %33
%35 = getelementptr inbounds i8*, i8** %1, i64 2
%36 = load i8*, i8** %35
%37 = tail call i32 (i8*, i32, ...) @open(i8* %36, i32 %23, i32 %34)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %18
%40 = tail call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %41
41: ; preds = %39, %18
%42 = call i32 @read(i32 %14, i8* %4, i32 1024)
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %53
44: ; preds = %41, %50
%45 = phi i32 [ %51, %50 ], [ %42, %41 ]
%46 = call i32 @write(i32 %37, i8* %4, i32 %45)
%47 = icmp eq i32 %46, %45
br i1 %47, label %50, label %48
48: ; preds = %44
%49 = tail call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %44
%51 = call i32 @read(i32 %14, i8* %4, i32 1024)
%52 = icmp sgt i32 %51, 0
br i1 %52, label %44, label %53
53: ; preds = %50, %41
%54 = phi i32 [ %42, %41 ], [ %51, %50 ]
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = tail call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %58
58: ; preds = %56, %53
%59 = tail call i32 @close(i32 %14)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %58
%62 = tail call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %63
63: ; preds = %61, %58
%64 = tail call i32 @close(i32 %37)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %68
66: ; preds = %63
%67 = tail call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %68
68: ; preds = %66, %63
%69 = load i32, i32* @EXIT_SUCCESS
ret i32 %69
}