#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 */
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ *do_generic_fopen(char const *, char const *,
                                     int /*<<< orphan*/ *(*)(char const *,
                                                             char const *));
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
FILE *flcow_fopen(char const *name, char const *mode) {
  static FILE *(*func_open)(char const *, char const *) = NULL;
  if (!func_open && !(func_open = (FILE * (*)(char const *, char const *))
                          dlsym(((void *)-1L), "fopen"))) {
    fprintf(stderr, "missing symbol: %s\n", "fopen");
    exit(1);
  }
  do {
  } while (0);
  return do_generic_fopen(name, mode, func_open);
}


x86 assembly (clang, -O0):
<flcow_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,0x2ef0(%rip)
jne    11a8 <flcow_fopen+0x68>
mov    $0xffffffffffffffff,%rdi
lea    0xe94(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,0x2ed0(%rip)
cmp    $0x0,%rax
jne    11a8 <flcow_fopen+0x68>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
lea    0xe67(%rip),%rdx
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    11ad <flcow_fopen+0x6d>
jmp    11b2 <flcow_fopen+0x72>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e87(%rip),%rdx
call   1040 <do_generic_fopen@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flcow_fopen>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ef7(%rip),%rdx
test   %rdx,%rdx
jne    1178 <flcow_fopen+0x38>
lea    0xea3(%rip),%rsi
mov    $0xffffffffffffffff,%rdi
call   1050 <dlsym@plt>
mov    %rax,%rdx
mov    %rax,0x2ed5(%rip)
test   %rax,%rax
je     118a <flcow_fopen+0x4a>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <do_generic_fopen@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
lea    0xe5f(%rip),%rdx
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@flcow_fopen.func_open = internal global i32* (i8*, i8*)* null
@.str = [6 x i8] c"fopen\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"missing symbol: %s\0A\00"
define i32* @flcow_fopen(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%5 = load i32* (i8*, i8*)*, i32* (i8*, i8*)** @flcow_fopen.func_open
%6 = icmp ne i32* (i8*, i8*)* %5, null
br i1 %6, label %15, label %7
7: ; preds = %2
%8 = call i64 @dlsym(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%9 = inttoptr i64 %8 to i32* (i8*, i8*)*
store i32* (i8*, i8*)* %9, i32* (i8*, i8*)** @flcow_fopen.func_open
%10 = icmp ne i32* (i8*, i8*)* %9, null
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %7, %2
br label %16
16: ; preds = %15
br label %17
17: ; preds = %16
%18 = load i8*, i8** %3
%19 = load i8*, i8** %4
%20 = load i32* (i8*, i8*)*, i32* (i8*, i8*)** @flcow_fopen.func_open
%21 = call i32* @do_generic_fopen(i8* %18, i8* %19, i32* (i8*, i8*)* %20)
ret i32* %21
}

LLVM IR (-O2):
@flcow_fopen.func_open = internal unnamed_addr global i32* (i8*, i8*)* null
@.str = [6 x i8] c"fopen\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"missing symbol: %s\0A\00"
define i32* @flcow_fopen(i8* %0, i8* %1) {
%3 = load i32* (i8*, i8*)*, i32* (i8*, i8*)** @flcow_fopen.func_open
%4 = icmp eq i32* (i8*, i8*)* %3, null
br i1 %4, label %5, label %13
5: ; preds = %2
%6 = tail call i64 @dlsym(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = inttoptr i64 %6 to i32* (i8*, i8*)*
store i32* (i8*, i8*)* %7, i32* (i8*, i8*)** @flcow_fopen.func_open
%8 = icmp eq i64 %6, 0
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %5, %2
%14 = phi i32* (i8*, i8*)* [ %7, %5 ], [ %3, %2 ]
%15 = tail call i32* @do_generic_fopen(i8* %0, i8* %1, i32* (i8*, i8*)* %14)
ret i32* %15
}