#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 /*<<< orphan*/ ELIBACC;
int RTLD_LOCAL;
int RTLD_NOW;
int /*<<< orphan*/ dlclose(void *);
int /*<<< orphan*/ *dlerror();
void *dlopen(int /*<<< orphan*/ *, int);
void *dlsym(void *, char const *);
int /*<<< orphan*/ errno;
void **dlsym_libc(const char *symbol) {
  void *libc = dlopen(NULL, RTLD_LOCAL | RTLD_NOW);
  if (libc != NULL) {
    void *sym = dlsym(libc, symbol);
    if (dlerror() == NULL)
      return sym;
    dlclose(libc);
  };
  ;
  errno = ELIBACC;
  return NULL;
}


x86 assembly (clang, -O0):
<dlsym_libc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    0x2e84(%rip),%rax
or     (%rax),%esi
xor    %eax,%eax
mov    %eax,%edi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ae <dlsym_libc+0x6e>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
cmp    $0x0,%rax
jne    11a5 <dlsym_libc+0x65>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c8 <dlsym_libc+0x88>
mov    -0x18(%rbp),%rdi
call   1060 <dlclose@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%ecx
mov    0x2e12(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dlsym_libc>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e72(%rip),%rax
mov    0x2e8b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edi,%edi
call   1040 <dlopen@plt>
test   %rax,%rax
je     118a <dlsym_libc+0x4a>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <dlsym@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
je     119f <dlsym_libc+0x5f>
mov    %rbx,%rdi
call   1060 <dlclose@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    0x2e36(%rip),%rcx
mov    %eax,(%rcx)
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@RTLD_LOCAL = global i32 0
@RTLD_NOW = global i32 0
@ELIBACC = global i32 0
@errno = global i32 0
define i8** @dlsym_libc(i8* %0) {
%2 = alloca i8**
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i32, i32* @RTLD_LOCAL
%7 = load i32, i32* @RTLD_NOW
%8 = or i32 %6, %7
%9 = call i8* @dlopen(i32* null, i32 %8)
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %24
12: ; preds = %1
%13 = load i8*, i8** %4
%14 = load i8*, i8** %3
%15 = call i8* @dlsym(i8* %13, i8* %14)
store i8* %15, i8** %5
%16 = call i32* (...) @dlerror()
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = load i8*, i8** %5
%20 = bitcast i8* %19 to i8**
store i8** %20, i8*** %2
br label %26
21: ; preds = %12
%22 = load i8*, i8** %4
%23 = call i32 @dlclose(i8* %22)
br label %24
24: ; preds = %21, %1
%25 = load i32, i32* @ELIBACC
store i32 %25, i32* @errno
store i8** null, i8*** %2
br label %26
26: ; preds = %24, %18
%27 = load i8**, i8*** %2
ret i8** %27
}

LLVM IR (-O2):
@RTLD_LOCAL = global i32 0
@RTLD_NOW = global i32 0
@ELIBACC = global i32 0
@errno = global i32 0
define i8** @dlsym_libc(i8* %0) {
%2 = load i32, i32* @RTLD_LOCAL
%3 = load i32, i32* @RTLD_NOW
%4 = or i32 %3, %2
%5 = tail call i8* @dlopen(i32* null, i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %15, label %7
7: ; preds = %1
%8 = tail call i8* @dlsym(i8* %5, i8* %0)
%9 = tail call i32* (...) @dlerror()
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = bitcast i8* %8 to i8**
br label %17
13: ; preds = %7
%14 = tail call i32 @dlclose(i8* %5)
br label %15
15: ; preds = %13, %1
%16 = load i32, i32* @ELIBACC
store i32 %16, i32* @errno
br label %17
17: ; preds = %11, %15
%18 = phi i8** [ null, %15 ], [ %12, %11 ]
ret i8** %18
}