#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 /*<<< orphan*/ buf;

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getopt(int, char **, char *);
int open(char *, int);
scalar_t__ optind;
int read(int /*<<< orphan*/, char *, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  char buf[20];
  size_t nbytes;
  int fd;
  int ch;
  int n;
  int append;
  append = 0;
  nbytes = sizeof(buf);
  while ((ch = getopt(argc, argv, "ai")) != -1) {
    switch ((char)ch) {
    case 'a':
      append = 1;
      break;
    default:
      fprintf(stderr, "unrecognized option");
    }
  }
  argc -= optind;
  argv += optind;
  if (append) {
    fd = open(argv[0], O_WRONLY | O_CREAT | O_APPEND);
  } else {
    fd = open(argv[0], O_WRONLY | O_CREAT | O_TRUNC);
  }
  while ((n = read(STDIN_FILENO, buf, nbytes)) > 0) {
    write(STDOUT_FILENO, buf, n);
    write(fd, buf, n);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x48(%rbp)
movq   $0x14,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe6d(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,-0x40(%rbp)
cmp    $0xffffffff,%eax
je     11da <main+0x7a>
mov    -0x40(%rbp),%al
sub    $0x61,%al
jne    11c0 <main+0x60>
jmp    11b4 <main+0x54>
movl   $0x1,-0x48(%rbp)
jmp    11d5 <main+0x75>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1185 <main+0x25>
mov    0x2def(%rip),%rax
mov    (%rax),%rcx
movslq -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%rax
shl    $0x3,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x48(%rbp)
je     123d <main+0xdd>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
mov    0x2dd3(%rip),%rax
or     (%rax),%esi
mov    0x2d72(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x3c(%rbp)
jmp    1267 <main+0x107>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
mov    0x2da4(%rip),%rax
or     (%rax),%esi
mov    0x2d93(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x3c(%rbp)
jmp    126c <main+0x10c>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
jle    12b7 <main+0x157>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1030 <write@plt>
mov    -0x3c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1030 <write@plt>
jmp    126c <main+0x10c>
mov    -0x3c(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
mov    %edi,%ebp
lea    0xe86(%rip),%rdx
call   1080 <getopt@plt>
cmp    $0xffffffff,%eax
je     11f8 <main+0x98>
xor    %ebx,%ebx
lea    0xe73(%rip),%r14
lea    0xe6f(%rip),%r15
jmp    11b5 <main+0x55>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r13,%rsi
mov    %r14,%rdx
call   1080 <getopt@plt>
mov    %r12d,%ebx
cmp    $0xffffffff,%eax
je     11d5 <main+0x75>
mov    $0x1,%r12d
cmp    $0x61,%al
je     11a0 <main+0x40>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
call   1060 <fprintf@plt>
mov    %ebx,%r12d
jmp    11a0 <main+0x40>
mov    0x2df4(%rip),%rax
mov    (%rax),%rax
lea    0x0(,%rax,8),%rax
add    %r13,%rax
test   %r12d,%r12d
je     120d <main+0xad>
mov    0x2daa(%rip),%rcx
jmp    1214 <main+0xb4>
mov    0x2dd1(%rip),%rax
mov    (%rax),%rax
lea    0x0(,%rax,8),%rax
add    %r13,%rax
mov    0x2ddc(%rip),%rcx
mov    (%rax),%rdi
mov    0x2d8a(%rip),%rax
mov    0x2dd3(%rip),%rdx
mov    (%rdx),%esi
or     (%rax),%esi
or     (%rcx),%esi
call   1070 <open@plt>
mov    %eax,%r14d
mov    0x2d7e(%rip),%r15
mov    (%r15),%edi
mov    %rsp,%rsi
mov    $0x14,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1291 <main+0x131>
mov    %eax,%ebx
mov    0x2d89(%rip),%r12
mov    %rsp,%rbp
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
mov    (%r15),%edi
mov    $0x14,%edx
mov    %rbp,%rsi
call   1050 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     1260 <main+0x100>
mov    %r14d,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"ai\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"unrecognized option\00"
@optind = global i64 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_TRUNC = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [20 x i8]
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %11
store i64 20, i64* %7
br label %12
12: ; preds = %25, %2
%13 = load i32, i32* %4
%14 = load i8**, i8*** %5
%15 = call i32 @getopt(i32 %13, i8** %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %15, i32* %9
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %26
17: ; preds = %12
%18 = load i32, i32* %9
%19 = trunc i32 %18 to i8
%20 = sext i8 %19 to i32
switch i32 %20, label %22 [
i32 97, label %21
]
21: ; preds = %17
store i32 1, i32* %11
br label %25
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %22, %21
br label %12
26: ; preds = %12
%27 = load i64, i64* @optind
%28 = load i32, i32* %4
%29 = sext i32 %28 to i64
%30 = sub nsw i64 %29, %27
%31 = trunc i64 %30 to i32
store i32 %31, i32* %4
%32 = load i64, i64* @optind
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 %32
store i8** %34, i8*** %5
%35 = load i32, i32* %11
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %47
37: ; preds = %26
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 0
%40 = load i8*, i8** %39
%41 = load i32, i32* @O_WRONLY
%42 = load i32, i32* @O_CREAT
%43 = or i32 %41, %42
%44 = load i32, i32* @O_APPEND
%45 = or i32 %43, %44
%46 = call i32 @open(i8* %40, i32 %45)
store i32 %46, i32* %8
br label %57
47: ; preds = %26
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 0
%50 = load i8*, i8** %49
%51 = load i32, i32* @O_WRONLY
%52 = load i32, i32* @O_CREAT
%53 = or i32 %51, %52
%54 = load i32, i32* @O_TRUNC
%55 = or i32 %53, %54
%56 = call i32 @open(i8* %50, i32 %55)
store i32 %56, i32* %8
br label %57
57: ; preds = %47, %37
br label %58
58: ; preds = %64, %57
%59 = load i32, i32* @STDIN_FILENO
%60 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%61 = load i64, i64* %7
%62 = call i32 @read(i32 %59, i8* %60, i64 %61)
store i32 %62, i32* %10
%63 = icmp sgt i32 %62, 0
br i1 %63, label %64, label %73
64: ; preds = %58
%65 = load i32, i32* @STDOUT_FILENO
%66 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%67 = load i32, i32* %10
%68 = call i32 @write(i32 %65, i8* %66, i32 %67)
%69 = load i32, i32* %8
%70 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%71 = load i32, i32* %10
%72 = call i32 @write(i32 %69, i8* %70, i32 %71)
br label %58
73: ; preds = %58
%74 = load i32, i32* %8
%75 = call i32 @close(i32 %74)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"ai\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"unrecognized option\00"
@optind = global i64 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_TRUNC = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [20 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i64, i64* @optind
%9 = getelementptr inbounds i8*, i8** %1, i64 %8
br label %26
10: ; preds = %2, %18
%11 = phi i32 [ %20, %18 ], [ %5, %2 ]
%12 = phi i32 [ %19, %18 ], [ 0, %2 ]
%13 = and i32 %11, 255
%14 = icmp eq i32 %13, 97
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %10, %15
%19 = phi i32 [ %12, %15 ], [ 1, %10 ]
%20 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %10
22: ; preds = %18
%23 = load i64, i64* @optind
%24 = getelementptr inbounds i8*, i8** %1, i64 %23
%25 = icmp eq i32 %19, 0
br i1 %25, label %26, label %28
26: ; preds = %7, %22
%27 = phi i8** [ %9, %7 ], [ %24, %22 ]
br label %28
28: ; preds = %22, %26
%29 = phi i8** [ %27, %26 ], [ %24, %22 ]
%30 = phi i32* [ @O_TRUNC, %26 ], [ @O_APPEND, %22 ]
%31 = load i8*, i8** %29
%32 = load i32, i32* @O_WRONLY
%33 = load i32, i32* @O_CREAT
%34 = or i32 %33, %32
%35 = load i32, i32* %30
%36 = or i32 %34, %35
%37 = tail call i32 @open(i8* %31, i32 %36)
%38 = load i32, i32* @STDIN_FILENO
%39 = call i32 @read(i32 %38, i8* %4, i64 20)
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %49
41: ; preds = %28, %41
%42 = phi i32 [ %47, %41 ], [ %39, %28 ]
%43 = load i32, i32* @STDOUT_FILENO
%44 = call i32 @write(i32 %43, i8* %4, i32 %42)
%45 = call i32 @write(i32 %37, i8* %4, i32 %42)
%46 = load i32, i32* @STDIN_FILENO
%47 = call i32 @read(i32 %46, i8* %4, i64 20)
%48 = icmp sgt i32 %47, 0
br i1 %48, label %41, label %49
49: ; preds = %41, %28
%50 = tail call i32 @close(i32 %37)
ret i32 0
}