#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 */

/* Variables and functions */
int RTLD_NOLOAD;
int RTLD_NOW;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int);
void *dlsym(void *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
void *load_symbol(char *libname, char *symbname, void **handle) {
  void *h = NULL;
  void *symbol;
  assert(handle != NULL);
  if (*handle == NULL) {
    dlerror();
    h = dlopen(libname, RTLD_NOW | RTLD_NOLOAD);
    if (h != NULL) {
      dlclose(h);
    } else {
      h = dlopen(libname, RTLD_NOW);
    }
    if (h == NULL) {
      fprintf(stderr, "Error opening library '%s'\n", libname);
      fprintf(stderr, "%s\n", dlerror());
      return NULL;
    }
    *handle = h;
  } else {
    h = *handle;
  }
  dlerror();
  symbol = dlsym(h, symbname);
  if (symbol == NULL) {
    fprintf(stderr, "Error getting symbol '%s'\n", symbname);
    fprintf(stderr, "%s\n", dlerror());
    return NULL;
  }
  return symbol;
}


x86 assembly (clang, -O0):
<load_symbol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1257 <load_symbol+0xf7>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    0x2e19(%rip),%rax
or     (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11db <load_symbol+0x7b>
mov    -0x28(%rbp),%rdi
call   1080 <dlclose@plt>
jmp    11f1 <load_symbol+0x91>
mov    -0x10(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1247 <load_symbol+0xe7>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdf0(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xde7(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <load_symbol+0x178>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    1262 <load_symbol+0x102>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <dlsym@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12d0 <load_symbol+0x170>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd87(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xd5e(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <load_symbol+0x178>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_symbol>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
xor    %edi,%edi
test   %rdx,%rdx
setne  %dil
call   1050 <assert@plt>
mov    (%r12),%rbx
test   %rbx,%rbx
jne    11d3 <load_symbol+0x73>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    0x2e52(%rip),%rbp
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
or     0x0(%rbp),%esi
mov    %r15,%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11bc <load_symbol+0x5c>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <dlclose@plt>
jmp    11cf <load_symbol+0x6f>
mov    0x0(%rbp),%esi
mov    %r15,%rdi
call   1040 <dlopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1234 <load_symbol+0xd4>
mov    %rbx,(%r12)
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <dlsym@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1228 <load_symbol+0xc8>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xdfe(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
mov    %r15,%rdx
jmp    1200 <load_symbol+0xa0>

LLVM IR (-O0):
@RTLD_NOW = global i32 0
@RTLD_NOLOAD = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Error opening library '%s'\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [27 x i8] c"Error getting symbol '%s'\0A\00"
define i8* @load_symbol(i8* %0, i8* %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
store i8* null, i8** %8
%10 = load i8**, i8*** %7
%11 = icmp ne i8** %10, null
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i8**, i8*** %7
%15 = load i8*, i8** %14
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %46
17: ; preds = %3
%18 = call i8* (...) @dlerror()
%19 = load i8*, i8** %5
%20 = load i32, i32* @RTLD_NOW
%21 = load i32, i32* @RTLD_NOLOAD
%22 = or i32 %20, %21
%23 = call i8* @dlopen(i8* %19, i32 %22)
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = load i8*, i8** %8
%28 = call i32 @dlclose(i8* %27)
br label %33
29: ; preds = %17
%30 = load i8*, i8** %5
%31 = load i32, i32* @RTLD_NOW
%32 = call i8* @dlopen(i8* %30, i32 %31)
store i8* %32, i8** %8
br label %33
33: ; preds = %29, %26
%34 = load i8*, i8** %8
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %43
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %5
%39 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @stderr
%41 = call i8* (...) @dlerror()
%42 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %41)
store i8* null, i8** %4
br label %65
43: ; preds = %33
%44 = load i8*, i8** %8
%45 = load i8**, i8*** %7
store i8* %44, i8** %45
br label %49
46: ; preds = %3
%47 = load i8**, i8*** %7
%48 = load i8*, i8** %47
store i8* %48, i8** %8
br label %49
49: ; preds = %46, %43
%50 = call i8* (...) @dlerror()
%51 = load i8*, i8** %8
%52 = load i8*, i8** %6
%53 = call i8* @dlsym(i8* %51, i8* %52)
store i8* %53, i8** %9
%54 = load i8*, i8** %9
%55 = icmp eq i8* %54, null
br i1 %55, label %56, label %63
56: ; preds = %49
%57 = load i32, i32* @stderr
%58 = load i8*, i8** %6
%59 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %58)
%60 = load i32, i32* @stderr
%61 = call i8* (...) @dlerror()
%62 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %61)
store i8* null, i8** %4
br label %65
63: ; preds = %49
%64 = load i8*, i8** %9
store i8* %64, i8** %4
br label %65
65: ; preds = %63, %56, %36
%66 = load i8*, i8** %4
ret i8* %66
}

LLVM IR (-O2):
@RTLD_NOW = global i32 0
@RTLD_NOLOAD = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Error opening library '%s'\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [27 x i8] c"Error getting symbol '%s'\0A\00"
define i8* @load_symbol(i8* %0, i8* %1, i8** %2) {
%4 = icmp ne i8** %2, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = load i8*, i8** %2
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %27
9: ; preds = %3
%10 = tail call i8* (...) @dlerror()
%11 = load i32, i32* @RTLD_NOW
%12 = load i32, i32* @RTLD_NOLOAD
%13 = or i32 %12, %11
%14 = tail call i8* @dlopen(i8* %0, i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %9
%17 = tail call i32 @dlclose(i8* %14)
br label %25
18: ; preds = %9
%19 = load i32, i32* @RTLD_NOW
%20 = tail call i8* @dlopen(i8* %0, i32 %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %35
25: ; preds = %16, %18
%26 = phi i8* [ %14, %16 ], [ %20, %18 ]
store i8* %26, i8** %2
br label %27
27: ; preds = %3, %25
%28 = phi i8* [ %26, %25 ], [ %7, %3 ]
%29 = tail call i8* (...) @dlerror()
%30 = tail call i8* @dlsym(i8* %28, i8* %1)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %39
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %35
35: ; preds = %22, %32
%36 = load i32, i32* @stderr
%37 = tail call i8* (...) @dlerror()
%38 = tail call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %35, %27
%40 = phi i8* [ %30, %27 ], [ null, %35 ]
ret i8* %40
}