#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 */
char *dlerror();
void *dlsym(void *, char const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
void *checked_dlsym(void *handle, const char *sym) {
  void *res = dlsym(handle, sym);
  if (res == NULL) {
    char *error = dlerror();
    if (error == NULL) {
      error = "checked_dlsym: sym is NULL";
    }
    fprintf(stderr, "checked_dlsym: %s\n", error);
    exit(-1);
  }
  return res;
}


x86 assembly (clang, -O0):
<checked_dlsym>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b0 <checked_dlsym+0x70>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118d <checked_dlsym+0x4d>
lea    0xe77(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe7a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checked_dlsym>:
push   %rax
call   1050 <dlsym@plt>
test   %rax,%rax
je     114d <checked_dlsym+0xd>
pop    %rcx
ret
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
lea    0xea2(%rip),%rdx
cmovne %rax,%rdx
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
lea    0xea9(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"checked_dlsym: sym is NULL\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"checked_dlsym: %s\0A\00"
define i8* @checked_dlsym(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = load i8*, i8** %4
%9 = call i8* @dlsym(i8* %7, i8* %8)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %22
12: ; preds = %2
%13 = call i8* (...) @dlerror()
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %12
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8** %6
br label %17
17: ; preds = %16, %12
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %6
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %2
%23 = load i8*, i8** %5
ret i8* %23
}

LLVM IR (-O2):
@.str = [27 x i8] c"checked_dlsym: sym is NULL\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"checked_dlsym: %s\0A\00"
define i8* @checked_dlsym(i8* %0, i8* %1) {
%3 = tail call i8* @dlsym(i8* %0, i8* %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = tail call i8* (...) @dlerror()
%7 = icmp eq i8* %6, null
%8 = select i1 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %6
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
ret i8* %3
}