#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*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
scalar_t__ strcmp(char const *, char *);
char *strerror(int /*<<< orphan*/);
FILE *open_filename(const char *filename, const char *opt, int is_input) {
  FILE *input;
  if (strcmp(filename, "-") == 0)
    input = (is_input) ? stdin : stdout;
  else {
    input = fopen(filename, opt);
    if (!input) {
      fprintf(stderr, "error: cannot open %s: %s", filename, strerror(errno));
      return NULL;
    }
  }
  return input;
}


x86 assembly (clang, -O0):
<open_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    11a5 <open_filename+0x65>
cmpl   $0x0,-0x1c(%rbp)
je     118a <open_filename+0x4a>
mov    0x2e6a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    1198 <open_filename+0x58>
mov    0x2e37(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    120b <open_filename+0xcb>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1206 <open_filename+0xc6>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe0e(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1213 <open_filename+0xd3>
jmp    120b <open_filename+0xcb>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_filename>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xead(%rip),%rsi
call   1030 <strcmp@plt>
test   %rax,%rax
je     119c <open_filename+0x5c>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <fopen@plt>
test   %rax,%rax
jne    11b3 <open_filename+0x73>
mov    0x2e64(%rip),%rax
mov    (%rax),%ebp
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe77(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
xor    %eax,%eax
jmp    11b3 <open_filename+0x73>
test   %ebp,%ebp
jne    11a9 <open_filename+0x69>
mov    0x2e21(%rip),%rax
jmp    11b0 <open_filename+0x70>
mov    0x2e38(%rip),%rax
mov    (%rax),%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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