#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 EXIT_SUCCESS;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_SYNC;
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 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_SYNC;
  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);
  }
  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);
  exit(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)
jne    11c1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe4e(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11e9 <main+0x69>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe2f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    122f <main+0xaf>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    0x2d49(%rip),%rcx
or     (%rcx),%eax
mov    0x2d98(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    0x2d6b(%rip),%rcx
or     (%rcx),%eax
mov    0x2d4a(%rip),%rcx
or     (%rcx),%eax
mov    0x2d61(%rip),%rcx
or     (%rcx),%eax
mov    0x2d30(%rip),%rcx
or     (%rcx),%eax
mov    0x2cff(%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   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12c9 <main+0x149>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
lea    0xd65(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    12ce <main+0x14e>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    1324 <main+0x1a4>
mov    -0x18(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    -0x24(%rbp),%eax
je     131f <main+0x19f>
lea    0xd27(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    12ce <main+0x14e>
cmpl   $0xffffffff,-0x24(%rbp)
jne    1344 <main+0x1c4>
lea    0xd09(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    0x2c3d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
jne    11aa <main+0x2a>
mov    0x8(%r14),%rdi
lea    0xe60(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11ce <main+0x4e>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe4a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x8(%r14),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     124b <main+0xcb>
mov    %eax,%r15d
mov    0x2e07(%rip),%rax
mov    0x2d90(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2ddd(%rip),%rax
or     (%rax),%esi
mov    0x2d94(%rip),%rax
mov    0x2db5(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d92(%rip),%rax
or     (%rax),%edx
mov    0x2da9(%rip),%rax
or     (%rax),%edx
mov    0x2d78(%rip),%rax
or     (%rax),%edx
mov    0x2d47(%rip),%rax
or     (%rax),%edx
mov    0x10(%r14),%rdi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    126c <main+0xec>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %eax,%r14d
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    12b0 <main+0x130>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1280 <main+0x100>
lea    0xd89(%rip),%rdi
jmp    12bc <main+0x13c>
cmp    $0xffffffff,%ebp
jne    12cb <main+0x14b>
lea    0xd82(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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_SYNC = 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"
@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 %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_SYNC
%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
br label %69
69: ; preds = %84, %68
%70 = load i32, i32* %6
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%72 = call i32 @read(i32 %70, i8* %71, i32 1024)
store i32 %72, i32* %10
%73 = icmp sgt i32 %72, 0
br i1 %73, label %74, label %85
74: ; preds = %69
%75 = load i32, i32* %7
%76 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%77 = load i32, i32* %10
%78 = call i32 @write(i32 %75, i8* %76, i32 %77)
%79 = load i32, i32* %10
%80 = icmp ne i32 %78, %79
br i1 %80, label %81, label %84
81: ; preds = %74
%82 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%83 = call i32 @exit(i32 1)
unreachable
84: ; preds = %74
br label %69
85: ; preds = %69
%86 = load i32, i32* %10
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %91
88: ; preds = %85
%89 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%90 = call i32 @exit(i32 1)
unreachable
91: ; preds = %85
%92 = load i32, i32* %6
%93 = call i32 @close(i32 %92)
%94 = load i32, i32* %7
%95 = call i32 @close(i32 %94)
%96 = load i32, i32* @EXIT_SUCCESS
%97 = call i32 @exit(i32 %96)
unreachable
}

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_SYNC = 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"
@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 %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_SYNC
%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, %53
%51 = call i32 @read(i32 %19, i8* %4, i32 1024)
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %59
53: ; preds = %50
%54 = call i32 @write(i32 %44, i8* %4, i32 %51)
%55 = icmp eq i32 %54, %51
br i1 %55, label %50, label %56
56: ; preds = %53
%57 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%58 = tail call i32 @exit(i32 1)
unreachable
59: ; preds = %50
%60 = icmp eq i32 %51, -1
br i1 %60, label %61, label %64
61: ; preds = %59
%62 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%63 = tail call i32 @exit(i32 1)
unreachable
64: ; preds = %59
%65 = tail call i32 @close(i32 %19)
%66 = tail call i32 @close(i32 %44)
%67 = load i32, i32* @EXIT_SUCCESS
%68 = tail call i32 @exit(i32 %67)
unreachable
}