#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 const *, char *);
int /*<<< orphan*/ *stdin;
size_t strlen(char const *);
scalar_t__ strncmp(char *, char const *, size_t);
FILE *open_file(const char *fname) {
  FILE *f = NULL;
  size_t len;
  if (fname) {
    len = strlen(fname);
    if (strncmp("-", fname, len) == 0) {
      f = stdin;
    } else {
      f = fopen(fname, "r");
    }
  }
  return f;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a6 <open_file+0x76>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe95(%rip),%rdi
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    118d <open_file+0x5d>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
jmp    11a1 <open_file+0x71>
mov    -0x8(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
jmp    11a6 <open_file+0x76>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %rbx
test   %rdi,%rdi
je     1165 <open_file+0x35>
mov    %rdi,%rbx
call   1040 <strlen@plt>
lea    0xebb(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1169 <open_file+0x39>
lea    0xea6(%rip),%rsi
mov    %rbx,%rdi
pop    %rbx
jmp    1050 <fopen@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e78(%rip),%rax
mov    (%rax),%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
@stdin = global i32* null
@.str.1 = [2 x i8] c"r\00"
define i32* @open_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
store i8* %0, i8** %2
store i32* null, i32** %3
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %20
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %4
%10 = load i8*, i8** %2
%11 = load i64, i64* %4
%12 = call i64 @strncmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %10, i64 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = load i32*, i32** @stdin
store i32* %15, i32** %3
br label %19
16: ; preds = %7
%17 = load i8*, i8** %2
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %3
br label %19
19: ; preds = %16, %14
br label %20
20: ; preds = %19, %1
%21 = load i32*, i32** %3
ret i32* %21
}

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
@stdin = global i32* null
@.str.1 = [2 x i8] c"r\00"
define i32* @open_file(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %11, label %3
3: ; preds = %1
%4 = tail call i64 @strlen(i8* %0)
%5 = tail call i64 @strncmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = load i32*, i32** @stdin
br label %11
9: ; preds = %3
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %7, %9, %1
%12 = phi i32* [ %8, %7 ], [ %10, %9 ], [ null, %1 ]
ret i32* %12
}