#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 *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdout;
char *strerror(int /*<<< orphan*/);
FILE *open_output(const char *fname) {
  FILE *out;
  if (!fname)
    return stdout;
  out = fopen(fname, "wb");
  if (!out) {
    fprintf(stderr, "Failed to open %s for writing: %s\n", fname,
            strerror(errno));
    return NULL;
  }
  return out;
}


x86 assembly (clang, -O0):
<open_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115a <open_output+0x2a>
mov    0x2e82(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11c6 <open_output+0x96>
mov    -0x10(%rbp),%rdi
lea    0xe9b(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11be <open_output+0x8e>
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xe57(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c6 <open_output+0x96>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_output>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     117b <open_output+0x4b>
mov    %rdi,%rbx
lea    0xebe(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
jne    1185 <open_output+0x55>
mov    0x2e8d(%rip),%rax
mov    (%rax),%ebp
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe99(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1030 <fprintf@plt>
xor    %eax,%eax
jmp    1185 <open_output+0x55>
mov    0x2e4e(%rip),%rax
mov    (%rax),%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@stdout = global i32* null
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"Failed to open %s for writing: %s\0A\00"
@errno = global i32 0
define i32* @open_output(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = load i32*, i32** @stdout
br label %13
5: ; preds = %1
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %11)
br label %13
13: ; preds = %5, %8, %3
%14 = phi i32* [ null, %8 ], [ %4, %3 ], [ %6, %5 ]
ret i32* %14
}