#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 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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ getchar();
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
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 || strcmp(argv[1], "--help") == 0) {
    fprintf(stderr, "%s old-file new-file\n", argv[0]);
    exit(1);
  }
  inputFd = open(argv[1], O_RDONLY);
  if (inputFd == -1) {
    fprintf(stderr, "error opening source file");
    exit(1);
  }
  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) {
    fprintf(stderr, "error opening source file");
    exit(1);
  }
  getchar();
  while ((numRead = read(inputFd, buf, 1024)) > 0)
    if (write(outputFd, buf, numRead) != numRead) {
      perror("write ");
      exit(1);
    }
  if (numRead == -1) {
    perror("read: ");
    exit(1);
  }
  close(inputFd);
  close(outputFd);
  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)
jne    11d1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe3e(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11f9 <main+0x69>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    123f <main+0xaf>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    0x2d41(%rip),%rcx
or     (%rcx),%eax
mov    0x2d98(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d3c(%rip),%rax
mov    (%rax),%eax
mov    0x2d5b(%rip),%rcx
or     (%rcx),%eax
mov    0x2d3a(%rip),%rcx
or     (%rcx),%eax
mov    0x2d51(%rip),%rcx
or     (%rcx),%eax
mov    0x2d20(%rip),%rcx
or     (%rcx),%eax
mov    0x2cf7(%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   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12d9 <main+0x149>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
lea    0xd55(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    1336 <main+0x1a6>
mov    -0x18(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    -0x24(%rbp),%eax
je     1331 <main+0x1a1>
lea    0xd15(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    12e0 <main+0x150>
cmpl   $0xffffffff,-0x24(%rbp)
jne    1356 <main+0x1c6>
lea    0xcf7(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
jne    12c2 <main+0x132>
mov    0x8(%r14),%rdi
lea    0xe4c(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     12c2 <main+0x132>
mov    0x8(%r14),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12e6 <main+0x156>
mov    %eax,%r15d
mov    0x2e0f(%rip),%rax
mov    0x2da0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df5(%rip),%rax
or     (%rax),%esi
mov    0x2d9c(%rip),%rax
mov    0x2dbd(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d9a(%rip),%rax
or     (%rax),%edx
mov    0x2db1(%rip),%rax
or     (%rax),%edx
mov    0x2d80(%rip),%rax
or     (%rax),%edx
mov    0x2d57(%rip),%rax
or     (%rax),%edx
mov    0x10(%r14),%rdi
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12e6 <main+0x156>
mov    %eax,%r14d
xor    %eax,%eax
call   1070 <getchar@plt>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    129d <main+0x10d>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1260 <main+0xd0>
lea    0xda9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmp    $0xffffffff,%ebp
je     1307 <main+0x177>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %r14d,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd32(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
lea    0xd27(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd30(%rip),%rdi
jmp    128e <main+0xfe>

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

LLVM IR (-O2):
@.str = [7 x i8] c"--help\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"%s old-file new-file\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [26 x i8] c"error opening source file\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.3 = [7 x i8] c"write \00"
@.str.4 = [7 x i8] c"read: \00"
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 %6, label %11
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %6, %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %1
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %6
%17 = load i8*, i8** %7
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 (i8*, i32, ...) @open(i8* %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = load i32, i32* @O_CREAT
%27 = load i32, i32* @O_WRONLY
%28 = or i32 %27, %26
%29 = load i32, i32* @O_TRUNC
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IRUSR
%32 = load i32, i32* @S_IWUSR
%33 = or i32 %32, %31
%34 = load i32, i32* @S_IRGRP
%35 = or i32 %33, %34
%36 = load i32, i32* @S_IWGRP
%37 = or i32 %35, %36
%38 = load i32, i32* @S_IROTH
%39 = or i32 %37, %38
%40 = load i32, i32* @S_IWOTH
%41 = or i32 %39, %40
%42 = getelementptr inbounds i8*, i8** %1, i64 2
%43 = load i8*, i8** %42
%44 = tail call i32 (i8*, i32, ...) @open(i8* %43, i32 %30, i32 %41)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %50
46: ; preds = %25
%47 = load i32, i32* @stderr
%48 = tail call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%49 = tail call i32 @exit(i32 1)
unreachable
50: ; preds = %25
%51 = tail call i32 (...) @getchar()
br label %52
52: ; preds = %55, %50
%53 = call i32 @read(i32 %19, i8* %4, i32 1024)
%54 = icmp sgt i32 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %52
%56 = call i32 @write(i32 %44, i8* %4, i32 %53)
%57 = icmp eq i32 %56, %53
br i1 %57, label %52, label %58
58: ; preds = %55
%59 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%60 = tail call i32 @exit(i32 1)
unreachable
61: ; preds = %52
%62 = icmp eq i32 %53, -1
br i1 %62, label %63, label %66
63: ; preds = %61
%64 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%65 = tail call i32 @exit(i32 1)
unreachable
66: ; preds = %61
%67 = tail call i32 @close(i32 %19)
%68 = tail call i32 @close(i32 %44)
ret i32 0
}