#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ help_and_exit();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
FILE *choose_input(int argc, char **argv) {
  FILE *input;
  if (argc > 3) {
    char *fname = argv[3];
    input = fopen(fname, "r");
    if (input == 0) {
      fprintf(stderr, "Failed to open file %s\n", fname);
      help_and_exit();
    }
  } else {
    input = stdin;
  }
  return input;
}


x86 assembly (clang, -O0):
<choose_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x4(%rbp)
jle    1199 <choose_input+0x69>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xea0(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1194 <choose_input+0x64>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe7a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1030 <help_and_exit@plt>
jmp    11a7 <choose_input+0x77>
mov    0x2e48(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<choose_input>:
push   %r14
push   %rbx
push   %rax
cmp    $0x4,%edi
jl     1177 <choose_input+0x47>
mov    0x18(%rsi),%r14
lea    0xebc(%rip),%rsi
mov    %r14,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1181 <choose_input+0x51>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
lea    0xe9e(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <help_and_exit@plt>
jmp    1181 <choose_input+0x51>
mov    0x2e6a(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Failed to open file %s\0A\00"
@stdin = global i32* null
define i32* @choose_input(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 3
br i1 %3, label %4, label %13
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 3
%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 %15
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%12 = tail call i32 (...) @help_and_exit()
br label %15
13: ; preds = %2
%14 = load i32*, i32** @stdin
br label %15
15: ; preds = %4, %9, %13
%16 = phi i32* [ %14, %13 ], [ null, %9 ], [ %7, %4 ]
ret i32* %16
}