#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 O_APPEND;
int O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ malloc(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int write(int, char *, int);
int main(int argc, char **argv) {
  int MAX_READ = 64;
  char *buffer;
  ssize_t numRead = 0;
  ssize_t numWritten;
  int fd;
  buffer = (char *)malloc(MAX_READ * sizeof(char));
  if (argc > 1 && strcmp(argv[1], "-a") == 0) {
    numRead = read(0, buffer, MAX_READ);
    if (numRead == -1) {
      printf("read1 error!");
      return -1;
    }
    fd = open(argv[2], O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR);
    if (fd == -1) {
      printf("open error! \n");
      return -1;
    }
    buffer[numRead] = '\0';
    fprintf(stdout, "%s", buffer);
    numWritten = write(fd, buffer, numRead);
    if (numWritten == -1) {
      printf("write error! \n");
      return -1;
    }
    if ((close(fd)) == -1) {
      printf("close error! \n");
      return -1;
    }
    return 0;
  } else {
    numRead = read(0, buffer, MAX_READ);
    if (numRead == -1) {
      printf("read2 error!");
      return -1;
    }
    buffer[numRead] = '\0';
    fprintf(stdout, "%s", buffer);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x40,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    12ff <main+0x17f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe30(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12ff <main+0x17f>
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %edi,%edi
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1212 <main+0x92>
lea    0xe02(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135e <main+0x1de>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    0x2db6(%rip),%rax
or     (%rax),%esi
mov    0x2d7d(%rip),%rax
or     (%rax),%esi
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
mov    0x2d93(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1271 <main+0xf1>
lea    0xdb0(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135e <main+0x1de>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd8d(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    12ca <main+0x14a>
lea    0xd68(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135e <main+0x1de>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    12f3 <main+0x173>
lea    0xd4e(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135e <main+0x1de>
movl   $0x0,-0x4(%rbp)
jmp    135e <main+0x1de>
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %edi,%edi
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1332 <main+0x1b2>
lea    0xd1e(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135e <main+0x1de>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xccc(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebp
mov    $0x40,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
cmp    $0x2,%ebp
jl     11c3 <main+0x33>
mov    0x8(%r15),%rdi
lea    0xe47(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1217 <main+0x87>
xor    %ebp,%ebp
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     11f9 <main+0x69>
cltq
movb   $0x0,(%rbx,%rax,1)
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
mov    %rbx,%rdx
call   1090 <fprintf@plt>
jmp    120a <main+0x7a>
lea    0xe13(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     12bb <main+0x12b>
mov    %eax,%r14d
mov    0x10(%r15),%rdi
mov    0x2dbb(%rip),%rax
mov    0x2d9c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d61(%rip),%rax
or     (%rax),%esi
mov    0x2d70(%rip),%rax
mov    0x2d79(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     12c7 <main+0x137>
mov    %eax,%ebp
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
lea    0xd8a(%rip),%rsi
mov    %rbx,%rdx
call   1090 <fprintf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     12d0 <main+0x140>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
jne    120a <main+0x7a>
lea    0xd67(%rip),%rdi
jmp    12d7 <main+0x147>
lea    0xd41(%rip),%rdi
jmp    1200 <main+0x70>
lea    0xd6e(%rip),%rdi
jmp    12d7 <main+0x147>
lea    0xd57(%rip),%rdi
call   1030 <puts@plt>
jmp    1205 <main+0x75>

LLVM IR (-O0):
@.str = [3 x i8] c"-a\00"
@.str.1 = [13 x i8] c"read1 error!\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [14 x i8] c"open error! \0A\00"
@stdout = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [15 x i8] c"write error! \0A\00"
@.str.5 = [15 x i8] c"close error! \0A\00"
@.str.6 = [13 x i8] c"read2 error!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca 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 64, i32* %6
store i32 0, i32* %8
%11 = load i32, i32* %6
%12 = sext i32 %11 to i64
%13 = mul i64 %12, 1
%14 = trunc i64 %13 to i32
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %7
%17 = load i32, i32* %4
%18 = icmp sgt i32 %17, 1
br i1 %18, label %19, label %73
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %73
25: ; preds = %19
%26 = load i8*, i8** %7
%27 = load i32, i32* %6
%28 = call i32 @read(i32 0, i8* %26, i32 %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
33: ; preds = %25
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = load i32, i32* @O_CREAT
%38 = load i32, i32* @O_RDWR
%39 = or i32 %37, %38
%40 = load i32, i32* @O_APPEND
%41 = or i32 %39, %40
%42 = load i32, i32* @S_IRUSR
%43 = load i32, i32* @S_IWUSR
%44 = or i32 %42, %43
%45 = call i32 @open(i8* %36, i32 %41, i32 %44)
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %33
%49 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
50: ; preds = %33
%51 = load i8*, i8** %7
%52 = load i32, i32* %8
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
%55 = load i32, i32* @stdout
%56 = load i8*, i8** %7
%57 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %56)
%58 = load i32, i32* %10
%59 = load i8*, i8** %7
%60 = load i32, i32* %8
%61 = call i32 @write(i32 %58, i8* %59, i32 %60)
store i32 %61, i32* %9
%62 = load i32, i32* %9
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %50
%65 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
66: ; preds = %50
%67 = load i32, i32* %10
%68 = call i32 @close(i32 %67)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %72
70: ; preds = %66
%71 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
72: ; preds = %66
store i32 0, i32* %3
br label %90
73: ; preds = %19, %2
%74 = load i8*, i8** %7
%75 = load i32, i32* %6
%76 = call i32 @read(i32 0, i8* %74, i32 %75)
store i32 %76, i32* %8
%77 = load i32, i32* %8
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %81
79: ; preds = %73
%80 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
81: ; preds = %73
%82 = load i8*, i8** %7
%83 = load i32, i32* %8
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %82, i64 %84
store i8 0, i8* %85
%86 = load i32, i32* @stdout
%87 = load i8*, i8** %7
%88 = call i32 @fprintf(i32 %86, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %87)
br label %89
89: ; preds = %81
store i32 0, i32* %3
br label %90
90: ; preds = %89, %79, %72, %70, %64, %48, %31
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
@.str = [3 x i8] c"-a\00"
@.str.1 = [13 x i8] c"read1 error!\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stdout = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@.str.6 = [13 x i8] c"read2 error!\00"
@str = [14 x i8] c"close error! \00"
@str.7 = [14 x i8] c"write error! \00"
@str.8 = [13 x i8] c"open error! \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i64 @malloc(i32 64)
%4 = inttoptr i64 %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %45
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 ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %45
11: ; preds = %6
%12 = tail call i32 @read(i32 0, i8* %4, i32 64)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %55
16: ; preds = %11
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_RDWR
%21 = or i32 %20, %19
%22 = load i32, i32* @O_APPEND
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRUSR
%25 = load i32, i32* @S_IWUSR
%26 = or i32 %25, %24
%27 = tail call i32 @open(i8* %18, i32 %23, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %16
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
br label %55
31: ; preds = %16
%32 = sext i32 %12 to i64
%33 = getelementptr inbounds i8, i8* %4, i64 %32
store i8 0, i8* %33
%34 = load i32, i32* @stdout
%35 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%36 = tail call i32 @write(i32 %27, i8* %4, i32 %12)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %31
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0))
br label %55
40: ; preds = %31
%41 = tail call i32 @close(i32 %27)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %55
43: ; preds = %40
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %55
45: ; preds = %6, %2
%46 = tail call i32 @read(i32 0, i8* %4, i32 64)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
br label %55
50: ; preds = %45
%51 = sext i32 %46 to i64
%52 = getelementptr inbounds i8, i8* %4, i64 %51
store i8 0, i8* %52
%53 = load i32, i32* @stdout
%54 = tail call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
br label %55
55: ; preds = %40, %50, %48, %43, %38, %29, %14
%56 = phi i32 [ -1, %14 ], [ -1, %29 ], [ -1, %38 ], [ -1, %43 ], [ -1, %48 ], [ 0, %50 ], [ 0, %40 ]
ret i32 %56
}