#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*/ exit(int);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
FILE *open_write(const char *fname, const char *func) {
  FILE *f;
  if (!(f = fopen(fname, "w"))) {
    fprintf(stderr,
            "%s(): "
            "unable to open %s for writing: ",
            func, fname);
    perror("");
    exit(1);
  }
  return f;
}


x86 assembly (clang, -O0):
<open_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11a1 <open_write+0x61>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    0xe7c(%rip),%rsi
call   1030 <fprintf@plt>
lea    0xe95(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_write>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeaf(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     1163 <open_write+0x23>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
lea    0xe8f(%rip),%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1030 <fprintf@plt>
lea    0xea2(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [38 x i8] c"%s(): unable to open %s for writing: \00"
@.str.2 = [1 x i8] zeroinitializer
define i32* @open_write(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %5
%8 = icmp ne i32* %7, null
br i1 %8, label %16, label %9
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %4
%12 = load i8*, i8** %3
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %11, i8* %12)
%14 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32*, i32** %5
ret i32* %17
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [38 x i8] c"%s(): unable to open %s for writing: \00"
@.str.2 = [1 x i8] zeroinitializer
define noalias i32* @open_write(i8* %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %0)
%8 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
ret i32* %3
}