#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*/ EINVAL;
char *dlerror();
void *dlsym(void *, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ tosLog_error(char *, char *, char *, ...);
void *tosDll_lookup(void *hDLL, char *pszSym) {
  void *h = hDLL;
  void *sym;
  if (h == ((void *)0)) {
    tosLog_error("tosDll", "lookup", "Library for symbol %s not open", pszSym);
    errno = EINVAL;
    return ((void *)0);
  }
  sym = dlsym(h, pszSym);
  ;
  if (!sym) {
    char *msg = dlerror();
    if (msg)
      tosLog_error("tosDll", "lookup", msg);
    tosLog_error("tosDll", "lookup", "%s: lookup failed", pszSym);
  }
  return sym;
}


x86 assembly (clang, -O0):
<tosDll_lookup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1192 <tosDll_lookup+0x62>
mov    -0x18(%rbp),%rcx
lea    0xea2(%rip),%rdi
lea    0xea2(%rip),%rsi
lea    0xea2(%rip),%rdx
mov    $0x0,%al
call   1040 <tosLog_error@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5d(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1205 <tosDll_lookup+0xd5>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11fd <tosDll_lookup+0xcd>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11dd <tosDll_lookup+0xad>
mov    -0x30(%rbp),%rdx
lea    0xe31(%rip),%rdi
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1040 <tosLog_error@plt>
mov    -0x18(%rbp),%rcx
lea    0xe18(%rip),%rdi
lea    0xe18(%rip),%rsi
lea    0xe37(%rip),%rdx
mov    $0x0,%al
call   1040 <tosLog_error@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tosDll_lookup>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
test   %rdi,%rdi
je     1193 <tosDll_lookup+0x63>
mov    %r14,%rsi
call   1050 <dlsym@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c6 <tosDll_lookup+0x96>
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
je     1170 <tosDll_lookup+0x40>
lea    0xea1(%rip),%rdi
lea    0xea1(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <tosLog_error@plt>
lea    0xe89(%rip),%rdi
lea    0xe89(%rip),%rsi
lea    0xea8(%rip),%rdx
xor    %ebx,%ebx
mov    %r14,%rcx
xor    %eax,%eax
call   1040 <tosLog_error@plt>
jmp    11c6 <tosDll_lookup+0x96>
lea    0xe66(%rip),%rdi
lea    0xe66(%rip),%rsi
lea    0xe66(%rip),%rdx
xor    %ebx,%ebx
mov    %r14,%rcx
xor    %eax,%eax
call   1040 <tosLog_error@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    0x2e1c(%rip),%rcx
mov    %eax,(%rcx)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [7 x i8] c"tosDll\00"
@.str.1 = [7 x i8] c"lookup\00"
@.str.2 = [31 x i8] c"Library for symbol %s not open\00"
@EINVAL = global i32 0
@errno = global i32 0
@.str.3 = [18 x i8] c"%s: lookup failed\00"
define i8* @tosDll_lookup(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = call i32 (i8*, i8*, i8*, ...) @tosLog_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EINVAL
store i32 %15, i32* @errno
store i8* null, i8** %3
br label %34
16: ; preds = %2
%17 = load i8*, i8** %6
%18 = load i8*, i8** %5
%19 = call i8* @dlsym(i8* %17, i8* %18)
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %32, label %22
22: ; preds = %16
%23 = call i8* (...) @dlerror()
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 = %22
%27 = load i8*, i8** %8
%28 = call i32 (i8*, i8*, i8*, ...) @tosLog_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %26, %22
%30 = load i8*, i8** %5
%31 = call i32 (i8*, i8*, i8*, ...) @tosLog_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %29, %16
%33 = load i8*, i8** %7
store i8* %33, i8** %3
br label %34
34: ; preds = %32, %12
%35 = load i8*, i8** %3
ret i8* %35
}

LLVM IR (-O2):
@.str = [7 x i8] c"tosDll\00"
@.str.1 = [7 x i8] c"lookup\00"
@.str.2 = [31 x i8] c"Library for symbol %s not open\00"
@EINVAL = global i32 0
@errno = global i32 0
@.str.3 = [18 x i8] c"%s: lookup failed\00"
define i8* @tosDll_lookup(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 (i8*, i8*, i8*, ...) @tosLog_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%6 = load i32, i32* @EINVAL
store i32 %6, i32* @errno
br label %17
7: ; preds = %2
%8 = tail call i8* @dlsym(i8* %0, i8* %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %17
10: ; preds = %7
%11 = tail call i8* (...) @dlerror()
%12 = icmp eq i8* %11, null
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 (i8*, i8*, i8*, ...) @tosLog_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %15
15: ; preds = %13, %10
%16 = tail call i32 (i8*, i8*, i8*, ...) @tosLog_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %17
17: ; preds = %7, %15, %4
%18 = phi i8* [ null, %4 ], [ null, %15 ], [ %8, %7 ]
ret i8* %18
}