#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*/ errno;
int /*<<< orphan*/ *fopen(char const *, char const *);
int /*<<< orphan*/ panic(int /*<<< orphan*/);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
FILE *open_file(const char *filename, const char *mode) {
  if (0 == strcmp(filename, "-"))
    return 'r' == mode[0] ? stdin : stdout;
  {
    FILE *f = fopen(filename, mode);
    if (!f)
      panic(strerror(errno));
    return f;
  }
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1040 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11b0 <open_file+0x70>
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x72,%eax
cmp    %ecx,%eax
jne    1195 <open_file+0x55>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11a3 <open_file+0x63>
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e9 <open_file+0xa9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e1 <open_file+0xa1>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %eax,%edi
call   1030 <panic@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeaf(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1184 <open_file+0x44>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <fopen@plt>
test   %rax,%rax
jne    119d <open_file+0x5d>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %eax,%edi
call   1030 <panic@plt>
xor    %eax,%eax
jmp    119d <open_file+0x5d>
cmpb   $0x72,(%r14)
jne    1193 <open_file+0x53>
mov    0x2e57(%rip),%rax
jmp    119a <open_file+0x5a>
mov    0x2e36(%rip),%rax
mov    (%rax),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
@stdin = global i32* null
@stdout = global i32* null
@errno = global i32 0
define i32* @open_file(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %4
%8 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 0, %8
br i1 %9, label %10, label %22
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 114, %14
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = load i32*, i32** @stdin
br label %20
18: ; preds = %10
%19 = load i32*, i32** @stdout
br label %20
20: ; preds = %18, %16
%21 = phi i32* [ %17, %16 ], [ %19, %18 ]
store i32* %21, i32** %3
br label %34
22: ; preds = %2
%23 = load i8*, i8** %4
%24 = load i8*, i8** %5
%25 = call i32* @fopen(i8* %23, i8* %24)
store i32* %25, i32** %6
%26 = load i32*, i32** %6
%27 = icmp ne i32* %26, null
br i1 %27, label %32, label %28
28: ; preds = %22
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 @panic(i32 %30)
br label %32
32: ; preds = %28, %22
%33 = load i32*, i32** %6
store i32* %33, i32** %3
br label %34
34: ; preds = %32, %20
%35 = load i32*, i32** %3
ret i32* %35
}

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
@stdin = global i32* null
@stdout = global i32* null
@errno = global i32 0
define i32* @open_file(i8* %0, i8* nocapture %1) {
%3 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %11
5: ; preds = %2
%6 = load i8, i8* %1
%7 = icmp eq i8 %6, 114
%8 = load i32*, i32** @stdin
%9 = load i32*, i32** @stdout
%10 = select i1 %7, i32* %8, i32* %9
br label %18
11: ; preds = %2
%12 = tail call i32* @fopen(i8* %0, i8* %1)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 @panic(i32 %16)
br label %18
18: ; preds = %11, %14, %5
%19 = phi i32* [ %10, %5 ], [ null, %14 ], [ %12, %11 ]
ret i32* %19
}