#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 asprintf(char **, char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char const *, char const *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ log_error(char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
FILE *x_fopen(const char *path, const char *mode) {
  FILE *fp = NULL;
  char *err;
  if (path == NULL || path[0] == '\0')
    return (NULL);
  if ((fp = fopen(path, mode)) == NULL) {
    if ((asprintf(&err, "Failed to load '%s' - %s\n", path, strerror(errno))) ==
        -1) {
      exit(-1);
    }
    log_error(err);
    free(err);
    exit(-1);
  }
  return (fp);
}


x86 assembly (clang, -O0):
<x_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1193 <x_fopen+0x33>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11a0 <x_fopen+0x40>
movq   $0x0,-0x8(%rbp)
jmp    121e <x_fopen+0xbe>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1216 <x_fopen+0xb6>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
lea    -0x28(%rbp),%rdi
lea    0xe1e(%rip),%rsi
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11fa <x_fopen+0x9a>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1040 <log_error@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<x_fopen>:
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     11c6 <x_fopen+0x66>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     11c6 <x_fopen+0x66>
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
jne    11c8 <x_fopen+0x68>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe6c(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
je     11bc <x_fopen+0x5c>
mov    0x8(%rsp),%rdi
call   1040 <log_error@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Failed to load '%s' - %s\0A\00"
@errno = global i32 0
define i32* @x_fopen(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* null, i32** %6
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %16, label %10
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %10, %2
store i32* null, i32** %3
br label %38
17: ; preds = %10
%18 = load i8*, i8** %4
%19 = load i8*, i8** %5
%20 = call i32* @fopen(i8* %18, i8* %19)
store i32* %20, i32** %6
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %36
22: ; preds = %17
%23 = load i8*, i8** %4
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %23, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %22
%31 = load i8*, i8** %7
%32 = call i32 @log_error(i8* %31)
%33 = load i8*, i8** %7
%34 = call i32 @free(i8* %33)
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %17
%37 = load i32*, i32** %6
store i32* %37, i32** %3
br label %38
38: ; preds = %36, %16
%39 = load i32*, i32** %3
ret i32* %39
}

LLVM IR (-O2):
@.str = [26 x i8] c"Failed to load '%s' - %s\0A\00"
@errno = global i32 0
define noalias i32* @x_fopen(i8* %0, i8* nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %25, label %6
6: ; preds = %2
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %25, label %9
9: ; preds = %6
%10 = tail call i32* @fopen(i8* %0, i8* %1)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %25
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %12
%20 = load i8*, i8** %3
%21 = call i32 @log_error(i8* %20)
%22 = load i8*, i8** %3
%23 = call i32 @free(i8* %22)
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %9, %2, %6
%26 = phi i32* [ null, %6 ], [ null, %2 ], [ %10, %9 ]
ret i32* %26
}