#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ process_file(int /*<<< orphan*/ *);
int /*<<< orphan*/ *stdin;
int main(int argc, char *argv[]) {
  FILE *fp = stdin;
  if (argc == 2) {
    fp = fopen(argv[1], "r");
    if (fp == NULL) {
      perror("Failed to open file with fopen");
      exit(EXIT_FAILURE);
    }
    process_file(fp);
    fclose(fp);
  } else {
    process_file(stdin);
  }
  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)
mov    0x2e7b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11d2 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe73(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bb <main+0x6b>
lea    0xe5a(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1040 <process_file@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11e1 <main+0x91>
mov    0x2e0f(%rip),%rax
mov    (%rax),%rdi
call   1040 <process_file@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    1180 <main+0x30>
mov    0x8(%rsi),%rdi
lea    0xe9f(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1193 <main+0x43>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <process_file@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    118f <main+0x3f>
mov    0x2e61(%rip),%rax
mov    (%rax),%rdi
call   1040 <process_file@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe68(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@stdin = global i32* null
@.str = [2 x i8] c"r\00"
@.str.1 = [31 x i8] c"Failed to open file with fopen\00"
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %16
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %13
9: ; preds = %4
%10 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %4
%14 = tail call i32 @process_file(i32* %7)
%15 = tail call i32 @fclose(i32* %7)
br label %19
16: ; preds = %2
%17 = load i32*, i32** @stdin
%18 = tail call i32 @process_file(i32* %17)
br label %19
19: ; preds = %16, %13
ret i32 0
}