#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 /*<<< orphan*/ 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;
scalar_t__ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
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)) {
    printf("%s old-file new-file\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  inputFd = open(argv[1], O_RDONLY);
  if (inputFd == -1) {
    printf("error opening file %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  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) {
    printf("error opening file %s\n", argv[2]);
    exit(EXIT_FAILURE);
  }
  while ((numRead = read(inputFd, buf, 1024)) > 0) {
    if (write(outputFd, buf, numRead) != numRead) {
      printf("could not write the whole buffer\n");
      exit(EXIT_FAILURE);
    }
  }
  if (numRead == -1) {
    printf("error while reading\n");
  }
  if (close(inputFd)) {
    printf("could not close input file descriptor\n");
    exit(EXIT_FAILURE);
  }
  if (close(outputFd)) {
    printf("could not close output file descriptor\n");
    exit(EXIT_FAILURE);
  }
  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    11b1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe5e(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    11d4 <main+0x64>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    121d <main+0xad>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    0x2d5b(%rip),%rcx
or     (%rcx),%eax
mov    0x2dba(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    0x2d7d(%rip),%rcx
or     (%rcx),%eax
mov    0x2d64(%rip),%rcx
or     (%rcx),%eax
mov    0x2d73(%rip),%rcx
or     (%rcx),%eax
mov    0x2d4a(%rip),%rcx
or     (%rcx),%eax
mov    0x2d11(%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    12ba <main+0x14a>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    12bf <main+0x14f>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    131b <main+0x1ab>
mov    -0x18(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    -0x24(%rbp),%eax
je     1316 <main+0x1a6>
lea    0xd33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    12bf <main+0x14f>
cmpl   $0xffffffff,-0x24(%rbp)
jne    1333 <main+0x1c3>
lea    0xd2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     1361 <main+0x1f1>
lea    0xd1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c4e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     138f <main+0x21f>
lea    0xd18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c20(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2c02(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
jne    11ac <main+0x2c>
mov    0x8(%r14),%rdi
lea    0xe5e(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    11cb <main+0x4b>
mov    (%r14),%rsi
lea    0xe51(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x8(%r14),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
jne    11ea <main+0x6a>
mov    0x8(%r14),%rsi
jmp    1252 <main+0xd2>
mov    %eax,%r15d
mov    0x2e04(%rip),%rax
mov    0x2d8d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dea(%rip),%rax
or     (%rax),%esi
mov    0x2d91(%rip),%rax
mov    0x2db2(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d97(%rip),%rax
or     (%rax),%edx
mov    0x2da6(%rip),%rax
or     (%rax),%edx
mov    0x2d7d(%rip),%rax
or     (%rax),%edx
mov    0x2d44(%rip),%rax
or     (%rax),%edx
mov    0x10(%r14),%rdi
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
jne    125e <main+0xde>
mov    0x10(%r14),%rsi
lea    0xdc4(%rip),%rdi
jmp    11b6 <main+0x36>
mov    %eax,%r12d
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    12a0 <main+0x120>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     1270 <main+0xf0>
lea    0xdf7(%rip),%rdi
jmp    12e9 <main+0x169>
cmp    $0xffffffff,%ebp
jne    12b1 <main+0x131>
lea    0xdd5(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
call   1060 <close@plt>
test   %rax,%rax
jne    12d9 <main+0x159>
mov    %r12d,%edi
call   1060 <close@plt>
test   %rax,%rax
jne    12e2 <main+0x162>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd7b(%rip),%rdi
jmp    12e9 <main+0x169>
lea    0xd4b(%rip),%rdi
call   1030 <puts@plt>
jmp    11bd <main+0x3d>

LLVM IR (-O0):
@.str = [7 x i8] c"--help\00"
@.str.1 = [22 x i8] c"%s old-file new-file\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [23 x i8] c"error opening file %s\0A\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 = [34 x i8] c"could not write the whole buffer\0A\00"
@.str.4 = [21 x i8] c"error while reading\0A\00"
@.str.5 = [39 x i8] c"could not close input file descriptor\0A\00"
@.str.6 = [40 x i8] c"could not close output file descriptor\0A\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 i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
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 %42
35: ; preds = %27
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %27
%43 = load i32, i32* @O_CREAT
%44 = load i32, i32* @O_WRONLY
%45 = or i32 %43, %44
%46 = load i32, i32* @O_TRUNC
%47 = or i32 %45, %46
store i32 %47, i32* %8
%48 = load i32, i32* @S_IRUSR
%49 = load i32, i32* @S_IWUSR
%50 = or i32 %48, %49
%51 = load i32, i32* @S_IRGRP
%52 = or i32 %50, %51
%53 = load i32, i32* @S_IWGRP
%54 = or i32 %52, %53
%55 = load i32, i32* @S_IROTH
%56 = or i32 %54, %55
%57 = load i32, i32* @S_IWOTH
%58 = or i32 %56, %57
store i32 %58, i32* %9
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 2
%61 = load i8*, i8** %60
%62 = load i32, i32* %8
%63 = load i32, i32* %9
%64 = call i32 (i8*, i32, ...) @open(i8* %61, i32 %62, i32 %63)
store i32 %64, i32* %7
%65 = load i32, i32* %7
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %74
67: ; preds = %42
%68 = load i8**, i8*** %5
%69 = getelementptr inbounds i8*, i8** %68, i64 2
%70 = load i8*, i8** %69
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %70)
%72 = load i32, i32* @EXIT_FAILURE
%73 = call i32 @exit(i32 %72)
unreachable
74: ; preds = %42
br label %75
75: ; preds = %91, %74
%76 = load i32, i32* %6
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%78 = call i32 @read(i32 %76, i8* %77, i32 1024)
store i32 %78, i32* %10
%79 = icmp sgt i32 %78, 0
br i1 %79, label %80, label %92
80: ; preds = %75
%81 = load i32, i32* %7
%82 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%83 = load i32, i32* %10
%84 = call i32 @write(i32 %81, i8* %82, i32 %83)
%85 = load i32, i32* %10
%86 = icmp ne i32 %84, %85
br i1 %86, label %87, label %91
87: ; preds = %80
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%89 = load i32, i32* @EXIT_FAILURE
%90 = call i32 @exit(i32 %89)
unreachable
91: ; preds = %80
br label %75
92: ; preds = %75
%93 = load i32, i32* %10
%94 = icmp eq i32 %93, -1
br i1 %94, label %95, label %97
95: ; preds = %92
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %97
97: ; preds = %95, %92
%98 = load i32, i32* %6
%99 = call i64 @close(i32 %98)
%100 = icmp ne i64 %99, 0
br i1 %100, label %101, label %105
101: ; preds = %97
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
%103 = load i32, i32* @EXIT_FAILURE
%104 = call i32 @exit(i32 %103)
unreachable
105: ; preds = %97
%106 = load i32, i32* %7
%107 = call i64 @close(i32 %106)
%108 = icmp ne i64 %107, 0
br i1 %108, label %109, label %113
109: ; preds = %105
%110 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
%111 = load i32, i32* @EXIT_FAILURE
%112 = call i32 @exit(i32 %111)
unreachable
113: ; preds = %105
%114 = load i32, i32* @EXIT_SUCCESS
%115 = call i32 @exit(i32 %114)
unreachable
}

LLVM IR (-O2):
@.str = [7 x i8] c"--help\00"
@.str.1 = [22 x i8] c"%s old-file new-file\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [23 x i8] c"error opening file %s\0A\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
@EXIT_SUCCESS = global i32 0
@str = [39 x i8] c"could not close output file descriptor\00"
@str.7 = [38 x i8] c"could not close input file descriptor\00"
@str.8 = [20 x i8] c"error while reading\00"
@str.9 = [33 x i8] c"could not write the whole buffer\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 i8*, i8** %1
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
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 %26
21: ; preds = %16
%22 = load i8*, i8** %7
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %16
%27 = load i32, i32* @O_CREAT
%28 = load i32, i32* @O_WRONLY
%29 = or i32 %28, %27
%30 = load i32, i32* @O_TRUNC
%31 = or i32 %29, %30
%32 = load i32, i32* @S_IRUSR
%33 = load i32, i32* @S_IWUSR
%34 = or i32 %33, %32
%35 = load i32, i32* @S_IRGRP
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IWGRP
%38 = or i32 %36, %37
%39 = load i32, i32* @S_IROTH
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IWOTH
%42 = or i32 %40, %41
%43 = getelementptr inbounds i8*, i8** %1, i64 2
%44 = load i8*, i8** %43
%45 = tail call i32 (i8*, i32, ...) @open(i8* %44, i32 %31, i32 %42)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %52
47: ; preds = %26
%48 = load i8*, i8** %43
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = load i32, i32* @EXIT_FAILURE
%51 = tail call i32 @exit(i32 %50)
unreachable
52: ; preds = %26, %55
%53 = call i32 @read(i32 %19, i8* %4, i32 1024)
%54 = icmp sgt i32 %53, 0
br i1 %54, label %55, label %62
55: ; preds = %52
%56 = call i32 @write(i32 %45, i8* %4, i32 %53)
%57 = icmp eq i32 %56, %53
br i1 %57, label %52, label %58
58: ; preds = %55
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.9, i64 0, i64 0))
%60 = load i32, i32* @EXIT_FAILURE
%61 = tail call i32 @exit(i32 %60)
unreachable
62: ; preds = %52
%63 = icmp eq i32 %53, -1
br i1 %63, label %64, label %66
64: ; preds = %62
%65 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.8, i64 0, i64 0))
br label %66
66: ; preds = %64, %62
%67 = tail call i64 @close(i32 %19)
%68 = icmp eq i64 %67, 0
br i1 %68, label %73, label %69
69: ; preds = %66
%70 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.7, i64 0, i64 0))
%71 = load i32, i32* @EXIT_FAILURE
%72 = tail call i32 @exit(i32 %71)
unreachable
73: ; preds = %66
%74 = tail call i64 @close(i32 %45)
%75 = icmp eq i64 %74, 0
br i1 %75, label %80, label %76
76: ; preds = %73
%77 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%78 = load i32, i32* @EXIT_FAILURE
%79 = tail call i32 @exit(i32 %78)
unreachable
80: ; preds = %73
%81 = load i32, i32* @EXIT_SUCCESS
%82 = tail call i32 @exit(i32 %81)
unreachable
}