#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*/ assert(void *);
char *calloc(int, size_t);
char *dlerror();
void *dlsym(void *, char const *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int strlen(char const *);
void *chckDlLoadSymbol(void *handle, const char *name, const char **outError) {
  assert(handle);
  void *symbol = NULL;
  const char *error = NULL;
  if (!(symbol = dlsym(handle, name))) {
    size_t len = strlen(name) + 1;
    char *nname = calloc(1, len + 1);
    if (nname) {
      nname[0] = '_';
      memcpy(nname + 1, name, len);
      symbol = dlsym(handle, nname);
      free(nname);
    }
  }
  if (!symbol)
    error = dlerror();
  if (outError)
    *outError = error;
  return symbol;
}


x86 assembly (clang, -O0):
<chckDlLoadSymbol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <assert@plt>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1226 <chckDlLoadSymbol+0xb6>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1221 <chckDlLoadSymbol+0xb1>
mov    -0x38(%rbp),%rax
movb   $0x5f,(%rax)
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1080 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1090 <dlsym@plt>
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
jmp    1226 <chckDlLoadSymbol+0xb6>
cmpq   $0x0,-0x20(%rbp)
jne    123c <chckDlLoadSymbol+0xcc>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1252 <chckDlLoadSymbol+0xe2>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chckDlLoadSymbol>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
call   1070 <assert@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1090 <dlsym@plt>
test   %rax,%rax
je     119e <chckDlLoadSymbol+0x2e>
mov    %rax,%rbx
xor    %eax,%eax
jmp    1200 <chckDlLoadSymbol+0x90>
mov    %r12,%rdi
call   1050 <strlen@plt>
movslq %eax,%rbx
lea    0x2(%rbx),%rsi
mov    $0x1,%edi
call   1060 <calloc@plt>
test   %rax,%rax
je     11f7 <chckDlLoadSymbol+0x87>
mov    %rax,%r13
add    $0x1,%rbx
movb   $0x5f,(%rax)
mov    %rax,%rdi
add    $0x1,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1080 <memcpy@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1090 <dlsym@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1040 <free@plt>
test   %rbx,%rbx
je     11f7 <chckDlLoadSymbol+0x87>
xor    %eax,%eax
jmp    1200 <chckDlLoadSymbol+0x90>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %r14,%r14
je     1208 <chckDlLoadSymbol+0x98>
mov    %rax,(%r14)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @chckDlLoadSymbol(i8* %0, i8* %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8** %2, i8*** %6
%11 = load i8*, i8** %4
%12 = call i32 @assert(i8* %11)
store i8* null, i8** %7
store i8* null, i8** %8
%13 = load i8*, i8** %4
%14 = load i8*, i8** %5
%15 = call i8* @dlsym(i8* %13, i8* %14)
store i8* %15, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %41, label %17
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = call i32 @strlen(i8* %18)
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
store i64 %21, i64* %9
%22 = load i64, i64* %9
%23 = add i64 %22, 1
%24 = call i8* @calloc(i32 1, i64 %23)
store i8* %24, i8** %10
%25 = load i8*, i8** %10
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %40
27: ; preds = %17
%28 = load i8*, i8** %10
%29 = getelementptr inbounds i8, i8* %28, i64 0
store i8 95, i8* %29
%30 = load i8*, i8** %10
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = load i8*, i8** %5
%33 = load i64, i64* %9
%34 = call i32 @memcpy(i8* %31, i8* %32, i64 %33)
%35 = load i8*, i8** %4
%36 = load i8*, i8** %10
%37 = call i8* @dlsym(i8* %35, i8* %36)
store i8* %37, i8** %7
%38 = load i8*, i8** %10
%39 = call i32 @free(i8* %38)
br label %40
40: ; preds = %27, %17
br label %41
41: ; preds = %40, %3
%42 = load i8*, i8** %7
%43 = icmp ne i8* %42, null
br i1 %43, label %46, label %44
44: ; preds = %41
%45 = call i8* (...) @dlerror()
store i8* %45, i8** %8
br label %46
46: ; preds = %44, %41
%47 = load i8**, i8*** %6
%48 = icmp ne i8** %47, null
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i8*, i8** %8
%51 = load i8**, i8*** %6
store i8* %50, i8** %51
br label %52
52: ; preds = %49, %46
%53 = load i8*, i8** %7
ret i8* %53
}

LLVM IR (-O2):
define i8* @chckDlLoadSymbol(i8* %0, i8* %1, i8** writeonly %2) {
%4 = tail call i32 @assert(i8* %0)
%5 = tail call i8* @dlsym(i8* %0, i8* %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %22
7: ; preds = %3
%8 = tail call i32 @strlen(i8* %1)
%9 = add nsw i32 %8, 1
%10 = sext i32 %9 to i64
%11 = add nsw i64 %10, 1
%12 = tail call i8* @calloc(i32 1, i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %20, label %14
14: ; preds = %7
store i8 95, i8* %12
%15 = getelementptr inbounds i8, i8* %12, i64 1
%16 = tail call i32 @memcpy(i8* %15, i8* %1, i64 %10)
%17 = tail call i8* @dlsym(i8* %0, i8* %12)
%18 = tail call i32 @free(i8* %12)
%19 = icmp eq i8* %17, null
br i1 %19, label %20, label %22
20: ; preds = %7, %14
%21 = tail call i8* (...) @dlerror()
br label %22
22: ; preds = %3, %20, %14
%23 = phi i8* [ %17, %14 ], [ null, %20 ], [ %5, %3 ]
%24 = phi i8* [ null, %14 ], [ %21, %20 ], [ null, %3 ]
%25 = icmp eq i8** %2, null
br i1 %25, label %27, label %26
26: ; preds = %22
store i8* %24, i8** %2
br label %27
27: ; preds = %26, %22
ret i8* %23
}