#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*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *fopen(char const *, char const *);
char *strerror(int /*<<< orphan*/);
FILE *open_file_or_error(const char *filename, const char *mode, char **err) {
  assert(err != NULL);
  FILE *f = fopen(filename, mode);
  if (!f) {
    *err = strerror(errno);
    return NULL;
  }
  return f;
}


x86 assembly (clang, -O0):
<open_file_or_error>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1197 <open_file_or_error+0x67>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    119f <open_file_or_error+0x6f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file_or_error>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %edi,%edi
test   %rdx,%rdx
setne  %dil
call   1030 <assert@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1170 <open_file_or_error+0x40>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,(%r14)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i32 0
define i32* @open_file_or_error(i8* %0, i8* %1, i8** %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
%9 = load i8**, i8*** %7
%10 = icmp ne i8** %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i8*, i8** %5
%14 = load i8*, i8** %6
%15 = call i32* @fopen(i8* %13, i8* %14)
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp ne i32* %16, null
br i1 %17, label %22, label %18
18: ; preds = %3
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = load i8**, i8*** %7
store i8* %20, i8** %21
store i32* null, i32** %4
br label %24
22: ; preds = %3
%23 = load i32*, i32** %8
store i32* %23, i32** %4
br label %24
24: ; preds = %22, %18
%25 = load i32*, i32** %4
ret i32* %25
}

LLVM IR (-O2):
@errno = global i32 0
define noalias i32* @open_file_or_error(i8* nocapture %0, i8* nocapture %1, i8** writeonly %2) {
%4 = icmp ne i8** %2, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = tail call i32* @fopen(i8* %0, i8* %1)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
store i8* %11, i8** %2
br label %12
12: ; preds = %3, %9
ret i32* %7
}