#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;
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
FILE *open_output(const char *filename) {
  FILE *fp;
  errno = 0;
  if (filename == NULL)
    filename = "\0";
  fp = fopen(filename, "w+");
  if (fp == NULL)
    fprintf(stderr, "open_output(\"%s\") failed: %s\n", filename,
            strerror(errno));
  return fp;
}


x86 assembly (clang, -O0):
<open_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e95(%rip),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x8(%rbp)
jne    1160 <open_output+0x30>
lea    0xea4(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe97(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b8 <open_output+0x88>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rdx
mov    %rax,%rcx
lea    0xe52(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_output>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e9a(%rip),%rbp
movq   $0x0,0x0(%rbp)
test   %rdi,%rdi
lea    0xeb0(%rip),%rbx
cmovne %rdi,%rbx
lea    0xea7(%rip),%rsi
mov    %rbx,%rdi
call   1040 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1193 <open_output+0x63>
mov    0x2e6e(%rip),%rax
mov    (%rax),%r15d
mov    0x0(%rbp),%rdi
call   1050 <strerror@plt>
lea    0xe80(%rip),%rsi
mov    %r15d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1030 <fprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [2 x i8] zeroinitializer
@.str.1 = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"open_output(\22%s\22) failed: %s\0A\00"
define i32* @open_output(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
store i8* %0, i8** %2
store i64 0, i64* @errno
%4 = load i8*, i8** %2
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %7
6: ; preds = %1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2
br label %7
7: ; preds = %6, %1
%8 = load i8*, i8** %2
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %3
%10 = load i32*, i32** %3
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %18
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %2
%15 = load i64, i64* @errno
%16 = call i8* @strerror(i64 %15)
%17 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %14, i8* %16)
br label %18
18: ; preds = %12, %7
%19 = load i32*, i32** %3
ret i32* %19
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [2 x i8] zeroinitializer
@.str.1 = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"open_output(\22%s\22) failed: %s\0A\00"
define noalias i32* @open_output(i8* %0) {
store i64 0, i64* @errno
%2 = icmp eq i8* %0, null
%3 = select i1 %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %0
%4 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = load i64, i64* @errno
%9 = tail call i8* @strerror(i64 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %3, i8* %9)
br label %11
11: ; preds = %6, %1
ret i32* %4
}