#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 close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
scalar_t__ strcmp(char *, char *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int input_fd, output_fd, open_flags;
  mode_t file_perms;
  ssize_t num_read;
  char buf[1024];
  if (argc != 3 || strcmp(argv[1], "--help") == 0) {
    printf("%s old-file new-file\n", argv[0]);
    exit(1);
  }
  input_fd = open(argv[1], O_RDONLY);
  if (input_fd == -1) {
    printf("opening file %s\n", argv[1]);
    exit(1);
  }
  open_flags = O_CREAT | O_WRONLY | O_TRUNC;
  file_perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
  output_fd = open(argv[2], open_flags, file_perms);
  if (output_fd == -1) {
    printf("opening file %s\n", argv[2]);
    exit(1);
  }
  while ((num_read = read(input_fd, buf, 1024)) > 0) {
    if (write(output_fd, buf, num_read) != num_read) {
      perror("couldn't write whole buffer");
      exit(2);
    }
  }
  if (num_read == -1) {
    perror("read error");
    exit(1);
  }
  if (close(input_fd) == -1) {
    perror("close error");
    exit(1);
  }
  if (close(output_fd) == -1) {
    perror("close error");
    exit(1);
  }
  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    11c1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe4e(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    11de <main+0x5e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe38(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1221 <main+0xa1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe0b(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    0x2d67(%rip),%rcx
or     (%rcx),%eax
mov    0x2db6(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    0x2d79(%rip),%rcx
or     (%rcx),%eax
mov    0x2d60(%rip),%rcx
or     (%rcx),%eax
mov    0x2d6f(%rip),%rcx
or     (%rcx),%eax
mov    0x2d46(%rip),%rcx
or     (%rcx),%eax
mov    0x2d1d(%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    12b8 <main+0x138>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xd74(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    12bd <main+0x13d>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    1313 <main+0x193>
mov    -0x18(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    -0x24(%rbp),%eax
je     130e <main+0x18e>
lea    0xd2f(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
jmp    12bd <main+0x13d>
cmpl   $0xffffffff,-0x24(%rbp)
jne    1333 <main+0x1b3>
lea    0xd26(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    135a <main+0x1da>
lea    0xd0a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1381 <main+0x201>
lea    0xce3(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %eax,%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,%r14
cmp    $0x3,%edi
jne    12ae <main+0x12e>
mov    0x8(%r14),%rdi
lea    0xe5a(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     12ae <main+0x12e>
mov    0x8(%r14),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12c3 <main+0x143>
mov    %eax,%r15d
mov    0x2e1d(%rip),%rax
mov    0x2db6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e03(%rip),%rax
or     (%rax),%esi
mov    0x2db2(%rip),%rax
mov    0x2dcb(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2db0(%rip),%rax
or     (%rax),%edx
mov    0x2dbf(%rip),%rax
or     (%rax),%edx
mov    0x2d96(%rip),%rax
or     (%rax),%edx
mov    0x2d6d(%rip),%rax
or     (%rax),%edx
mov    0x10(%r14),%rdi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12c9 <main+0x149>
mov    %eax,%r12d
mov    %rsp,%rbx
nop
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    127d <main+0xfd>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1240 <main+0xc0>
lea    0xdc0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
cmp    $0xffffffff,%ebp
je     12e3 <main+0x163>
mov    %r15d,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     12ba <main+0x13a>
mov    %r12d,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     12ba <main+0x13a>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rsi
lea    0xd4f(%rip),%rdi
jmp    12d4 <main+0x154>
lea    0xd94(%rip),%rdi
jmp    12ea <main+0x16a>
mov    0x8(%r14),%rsi
jmp    12cd <main+0x14d>
mov    0x10(%r14),%rsi
lea    0xd49(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd60(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

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

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