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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ *stdin;
int main(int argc, char **argv) {
  FILE *stream_in;
  int c;
  if (argc == 1) {
    stream_in = stdin;
  } else if (argc == 2) {
    stream_in = fopen(argv[1], "r");
    if (stream_in == NULL) {
      perror("file open error: ");
      return 1;
    }
  }
  while ((c = getc(stream_in)) != EOF) {
    printf("%c", c);
  }
  if (argc == 2) {
    fclose(stream_in);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    1183 <main+0x33>
mov    0x2e71(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11d2 <main+0x82>
cmpl   $0x2,-0x8(%rbp)
jne    11cd <main+0x7d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe64(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c8 <main+0x78>
lea    0xe4b(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1220 <main+0xd0>
jmp    11cd <main+0x7d>
jmp    11d2 <main+0x82>
jmp    11d7 <main+0x87>
mov    -0x18(%rbp),%rdi
call   1070 <getc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%eax
je     1206 <main+0xb6>
mov    -0x1c(%rbp),%esi
lea    0xe18(%rip),%rdi
call   1040 <printf@plt>
jmp    11d7 <main+0x87>
cmpl   $0x2,-0x8(%rbp)
jne    1219 <main+0xc9>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
cmp    $0x1,%edi
jne    116b <main+0x1b>
mov    0x2e82(%rip),%rax
mov    (%rax),%rbx
jmp    1183 <main+0x33>
mov    0x8(%rsi),%rdi
lea    0xe8a(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11d4 <main+0x84>
mov    %rbx,%rdi
call   1070 <getc@plt>
mov    0x2e4e(%rip),%r15
cmp    (%r15),%eax
je     11b7 <main+0x67>
lea    0xe76(%rip),%rbp
xchg   %ax,%ax
mov    %rbp,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1070 <getc@plt>
cmp    (%r15),%eax
jne    11a0 <main+0x50>
xor    %ebp,%ebp
cmp    $0x2,%r14d
jne    11c7 <main+0x77>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe27(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%ebp
jmp    11c7 <main+0x77>

LLVM IR (-O0):
@stdin = global i32* null
@.str = [2 x i8] c"r\00"
@.str.1 = [18 x i8] c"file open error: \00"
@EOF = global i32 0
@.str.2 = [3 x i8] c"%c\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32*, i32** @stdin
store i32* %11, i32** %6
br label %26
12: ; preds = %2
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %25
15: ; preds = %12
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %42
24: ; preds = %15
br label %25
25: ; preds = %24, %12
br label %26
26: ; preds = %25, %10
br label %27
27: ; preds = %32, %26
%28 = load i32*, i32** %6
%29 = call i32 @getc(i32* %28)
store i32 %29, i32* %7
%30 = load i32, i32* @EOF
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i32, i32* %7
%34 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %27
35: ; preds = %27
%36 = load i32, i32* %4
%37 = icmp eq i32 %36, 2
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32*, i32** %6
%40 = call i32 @fclose(i32* %39)
br label %41
41: ; preds = %38, %35
store i32 0, i32* %3
br label %42
42: ; preds = %41, %22
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@stdin = global i32* null
@.str = [2 x i8] c"r\00"
@.str.1 = [18 x i8] c"file open error: \00"
@EOF = global i32 0
@.str.2 = [3 x i8] c"%c\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = load i32*, i32** @stdin
br label %13
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %28
13: ; preds = %6, %4
%14 = phi i32* [ %5, %4 ], [ %9, %6 ]
%15 = tail call i32 @getc(i32* %14)
%16 = load i32, i32* @EOF
%17 = icmp eq i32 %15, %16
br i1 %17, label %24, label %18
18: ; preds = %13, %18
%19 = phi i32 [ %21, %18 ], [ %15, %13 ]
%20 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = tail call i32 @getc(i32* %14)
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %18
24: ; preds = %18, %13
%25 = icmp eq i32 %0, 2
br i1 %25, label %26, label %28
26: ; preds = %24
%27 = tail call i32 @fclose(i32* %14)
br label %28
28: ; preds = %24, %26, %11
%29 = phi i32 [ 1, %11 ], [ 0, %26 ], [ 0, %24 ]
ret i32 %29
}