#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*/ ErrorMessage(char *, char *);
char *dlerror();
void *dlsym(void *, char *);
int GetLibSymbol(int nHandle, char *psName, void **pvValue) {
  *pvValue = dlsym((void *)nHandle, psName);
  if (!*pvValue) {
    ErrorMessage("GetLibSymbol:  Failed to load symbol %s\n", psName);
    ErrorMessage("%s\n", dlerror());
  }
  return 1;
}


x86 assembly (clang, -O0):
<GetLibSymbol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movslq -0x4(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <dlsym@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    118e <GetLibSymbol+0x5e>
mov    -0x10(%rbp),%rsi
lea    0xe8d(%rip),%rdi
call   1040 <ErrorMessage@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe9c(%rip),%rdi
call   1040 <ErrorMessage@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetLibSymbol>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
movslq %edi,%rdi
call   1050 <dlsym@plt>
mov    %rax,(%rbx)
test   %rax,%rax
jne    116f <GetLibSymbol+0x3f>
lea    0xeaf(%rip),%rdi
mov    %r14,%rsi
call   1040 <ErrorMessage@plt>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xebe(%rip),%rdi
mov    %rax,%rsi
call   1040 <ErrorMessage@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [41 x i8] c"GetLibSymbol: Failed to load symbol %s\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @GetLibSymbol(i32 %0, i8* %1, i8** nocapture writeonly %2) {
%4 = sext i32 %0 to i64
%5 = inttoptr i64 %4 to i8*
%6 = tail call i8* @dlsym(i8* %5, i8* %1)
store i8* %6, i8** %2
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = tail call i32 @ErrorMessage(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %1)
%10 = tail call i8* (...) @dlerror()
%11 = tail call i32 @ErrorMessage(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %8, %3
ret i32 1
}