#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;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int O_APPEND;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ STDIN_FILENO;
int STDOUT_FILENO;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ exitErr(char *);
int getopt(int, char **, char *);
int open(char *, int, int);
size_t optind;
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ usage(char *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  ssize_t numRead;
  char buf[1024];
  int outFd, append = 0;
  switch (getopt(argc, argv, "ha:")) {
  case 'h':
    usage(argv[0]);
    break;
  case 'a':
    append = 1;
    break;
  }
  int flags = O_WRONLY | O_CREAT;
  if (append) {
    flags |= O_APPEND;
  } else {
    flags |= O_TRUNC;
  }
  if (argc > 1) {
    outFd = open(argv[optind], flags, S_IRUSR | S_IWUSR);
    if (outFd == -1)
      exitErr("open");
  }
  while ((numRead = read(STDIN_FILENO, buf, 1024)) > 0) {
    if (write(STDOUT_FILENO, buf, numRead) != numRead) {
      exitErr("write");
    }
    if (outFd) {
      if (write(outFd, buf, numRead) != numRead) {
        exitErr("write");
      }
    }
  }
  if (numRead == -1)
    exitErr("read");
  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)
movl   $0x0,-0x428(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe5f(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,%ecx
mov    %ecx,-0x430(%rbp)
sub    $0x61,%eax
je     11e1 <main+0x71>
jmp    11bc <main+0x4c>
mov    -0x430(%rbp),%eax
sub    $0x68,%eax
jne    11eb <main+0x7b>
jmp    11d0 <main+0x60>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <usage@plt>
jmp    11eb <main+0x7b>
movl   $0x1,-0x428(%rbp)
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    0x2dfd(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x42c(%rbp)
cmpl   $0x0,-0x428(%rbp)
je     122a <main+0xba>
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
or     -0x42c(%rbp),%eax
mov    %eax,-0x42c(%rbp)
jmp    123f <main+0xcf>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
or     -0x42c(%rbp),%eax
mov    %eax,-0x42c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    129c <main+0x12c>
mov    -0x10(%rbp),%rax
mov    0x2d74(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x42c(%rbp),%esi
mov    0x2d50(%rip),%rax
mov    (%rax),%edx
mov    0x2d5f(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0xffffffff,-0x424(%rbp)
jne    1297 <main+0x127>
lea    0xd72(%rip),%rdi
call   1030 <exitErr@plt>
jmp    129c <main+0x12c>
jmp    12a1 <main+0x131>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    1335 <main+0x1c5>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
cmp    -0x14(%rbp),%eax
je     12f4 <main+0x184>
lea    0xd1a(%rip),%rdi
call   1030 <exitErr@plt>
cmpl   $0x0,-0x424(%rbp)
je     1330 <main+0x1c0>
mov    -0x424(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
cmp    -0x14(%rbp),%eax
je     132b <main+0x1bb>
lea    0xce3(%rip),%rdi
call   1030 <exitErr@plt>
jmp    1330 <main+0x1c0>
jmp    12a1 <main+0x131>
cmpl   $0xffffffff,-0x14(%rbp)
jne    134b <main+0x1db>
lea    0xcc9(%rip),%rdi
call   1030 <exitErr@plt>
mov    0x2c5e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe73(%rip),%rdx
call   1080 <getopt@plt>
cmp    $0x61,%eax
je     11b2 <main+0x42>
cmp    $0x68,%eax
jne    11a4 <main+0x34>
mov    (%rbx),%rdi
call   1060 <usage@plt>
mov    0x2e45(%rip),%rax
cmp    $0x2,%ebp
jl     1211 <main+0xa1>
jmp    11be <main+0x4e>
mov    0x2dd7(%rip),%rax
cmp    $0x2,%ebp
jl     1211 <main+0xa1>
mov    0x2e33(%rip),%rcx
mov    (%rcx),%esi
mov    0x2dca(%rip),%rcx
or     (%rcx),%esi
or     (%rax),%esi
mov    0x2def(%rip),%rax
mov    (%rax),%rax
mov    (%rbx,%rax,8),%rdi
mov    0x2dd1(%rip),%rax
mov    0x2de2(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    1211 <main+0xa1>
lea    0xdfe(%rip),%rdi
call   1030 <exitErr@plt>
mov    $0xffffffff,%r15d
mov    0x2d90(%rip),%r12
mov    (%r12),%edi
mov    %rsp,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    12a1 <main+0x131>
mov    0x2daa(%rip),%r13
mov    %rsp,%rbx
lea    0xdc9(%rip),%r14
jmp    1267 <main+0xf7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r12),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    12a1 <main+0x131>
mov    0x0(%r13),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     1281 <main+0x111>
mov    %r14,%rdi
call   1030 <exitErr@plt>
test   %r15d,%r15d
je     1250 <main+0xe0>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     1250 <main+0xe0>
mov    %r14,%rdi
call   1030 <exitErr@plt>
jmp    1250 <main+0xe0>
cmp    $0xffffffff,%ebp
jne    12b2 <main+0x142>
lea    0xd62(%rip),%rdi
call   1030 <exitErr@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@.str = [4 x i8] c"ha:\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_TRUNC = global i32 0
@optind = global i64 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [5 x i8] c"read\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
switch i32 %5, label %9 [
i32 104, label %6
i32 97, label %10
]
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @usage(i8* %7)
br label %9
9: ; preds = %2, %6
br label %10
10: ; preds = %2, %9
%11 = phi i32* [ @O_TRUNC, %9 ], [ @O_APPEND, %2 ]
%12 = icmp sgt i32 %0, 1
br i1 %12, label %13, label %29
13: ; preds = %10
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_WRONLY
%16 = or i32 %14, %15
%17 = load i32, i32* %11
%18 = or i32 %17, %16
%19 = load i64, i64* @optind
%20 = getelementptr inbounds i8*, i8** %1, i64 %19
%21 = load i8*, i8** %20
%22 = load i32, i32* @S_IRUSR
%23 = load i32, i32* @S_IWUSR
%24 = or i32 %23, %22
%25 = tail call i32 @open(i8* %21, i32 %18, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %13
%28 = tail call i32 @exitErr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %13, %27, %10
%30 = phi i32 [ -1, %27 ], [ %25, %13 ], [ undef, %10 ]
%31 = load i32, i32* @STDIN_FILENO
%32 = call i32 @read(i32 %31, i8* %4, i32 1024)
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %53
34: ; preds = %29
%35 = icmp eq i32 %30, 0
br label %36
36: ; preds = %34, %49
%37 = phi i32 [ %32, %34 ], [ %51, %49 ]
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i32 @write(i32 %38, i8* %4, i32 %37)
%40 = icmp eq i32 %39, %37
br i1 %40, label %43, label %41
41: ; preds = %36
%42 = tail call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %36
br i1 %35, label %49, label %44
44: ; preds = %43
%45 = call i32 @write(i32 %30, i8* %4, i32 %37)
%46 = icmp eq i32 %45, %37
br i1 %46, label %49, label %47
47: ; preds = %44
%48 = tail call i32 @exitErr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %44, %47, %43
%50 = load i32, i32* @STDIN_FILENO
%51 = call i32 @read(i32 %50, i8* %4, i32 1024)
%52 = icmp sgt i32 %51, 0
br i1 %52, label %36, label %53
53: ; preds = %49, %29
%54 = phi i32 [ %32, %29 ], [ %51, %49 ]
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = tail call i32 @exitErr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %58
58: ; preds = %56, %53
%59 = load i32, i32* @EXIT_SUCCESS
%60 = tail call i32 @exit(i32 %59)
unreachable
}