#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*/ dv_error(char *);
scalar_t__ errno;
int /*<<< orphan*/ *fopen(char const *, char const *);
FILE *dv_fopen(const char *path, const char *mode) {
  if (errno) {
    dv_error("");
  }
  FILE *fp = fopen(path, mode);
  if (fp == NULL) {
    dv_error("Failed to open file.");
  }
  return fp;
}


x86 assembly (clang, -O0):
<dv_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2ea9(%rip),%rax
cmpq   $0x0,(%rax)
je     114d <dv_fopen+0x2d>
lea    0xecc(%rip),%rdi
call   1040 <dv_error@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1175 <dv_fopen+0x55>
lea    0xe90(%rip),%rdi
call   1040 <dv_error@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dv_fopen>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eaf(%rip),%rax
cmpq   $0x0,(%rax)
je     1143 <dv_fopen+0x23>
lea    0xed6(%rip),%rdi
call   1040 <dv_error@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1162 <dv_fopen+0x42>
lea    0xea3(%rip),%rdi
call   1040 <dv_error@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [21 x i8] c"Failed to open file.\00"
define i32* @dv_fopen(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 i64, i64* @errno
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @dv_error(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = load i8*, i8** %3
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %11, i8* %12)
store i32* %13, i32** %5
%14 = load i32*, i32** %5
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = call i32 @dv_error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %10
%19 = load i32*, i32** %5
ret i32* %19
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [21 x i8] c"Failed to open file.\00"
define noalias i32* @dv_fopen(i8* nocapture %0, i8* nocapture %1) {
%3 = load i64, i64* @errno
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @dv_error(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = tail call i32* @fopen(i8* %0, i8* %1)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @dv_error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
ret i32* %8
}