#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 = stdout;
  if (fname) {
    out = fopen(fname, "w");
    if (!out) {
      fprintf(stderr, "Can't 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)
mov    0x2e8d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11be <open_output+0x8e>
mov    -0x10(%rbp),%rdi
lea    0xea0(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b9 <open_output+0x89>
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xe5b(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c6 <open_output+0x96>
jmp    11be <open_output+0x8e>
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    0xe98(%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 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Can't 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 i32*, i32** @stdout
store i32* %5, i32** %4
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %20
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %19, label %13
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %3
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %15, i8* %17)
store i32* null, i32** %2
br label %22
19: ; preds = %8
br label %20
20: ; preds = %19, %1
%21 = load i32*, i32** %4
store i32* %21, i32** %2
br label %22
22: ; preds = %20, %13
%23 = load i32*, i32** %2
ret i32* %23
}

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