#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 */
scalar_t__ errno;
scalar_t__ file_is_stdin(char const *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
char *strerror(scalar_t__);
FILE *open_file(const char *file) {
  if (file_is_stdin(file))
    return stdin;
  else {
    errno = 0;
    FILE *fp = fopen(file, "r");
    if (!fp) {
      fprintf(stderr, "Can't open file: %s\n", strerror(errno));
      return NULL;
    } else
      return fp;
  }
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <file_is_stdin@plt>
cmp    $0x0,%rax
je     1172 <open_file+0x32>
mov    0x2e82(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11e1 <open_file+0xa1>
mov    0x2e57(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d9 <open_file+0x99>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe3b(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e1 <open_file+0xa1>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <file_is_stdin@plt>
test   %rax,%rax
je     115d <open_file+0x1d>
mov    0x2e90(%rip),%rax
mov    (%rax),%rax
jmp    11a3 <open_file+0x63>
mov    0x2e6c(%rip),%r14
movq   $0x0,(%r14)
lea    0xe8e(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
jne    11a3 <open_file+0x63>
mov    0x2e52(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%rdi
call   1060 <strerror@plt>
lea    0xe6b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@stdin = global i32* null
@errno = global i64 0
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Can't open file: %s\0A\00"
define i32* @open_file(i8* %0) {
%2 = tail call i64 @file_is_stdin(i8* %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = load i32*, i32** @stdin
br label %14
6: ; preds = %1
store i64 0, i64* @errno
%7 = tail call i32* @fopen(i8* %0, 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 %14
9: ; preds = %6
%10 = load i32, i32* @stderr
%11 = load i64, i64* @errno
%12 = tail call i8* @strerror(i64 %11)
%13 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %9, %6, %4
%15 = phi i32* [ %5, %4 ], [ null, %9 ], [ %7, %6 ]
ret i32* %15
}