#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 *);
int /*<<< orphan*/ print_error(char *, char *);
void *resolve_symbol(void *handle, char *name) {
  char *error = NULL;
  void *symbol = NULL;
  if (NULL != handle) {
    dlerror();
    symbol = dlsym(handle, name);
    error = dlerror();
    if (NULL != error) {
      print_error("dlsym", error);
    }
  }
  return symbol;
}


x86 assembly (clang, -O0):
<resolve_symbol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
je     11a0 <resolve_symbol+0x70>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
je     119b <resolve_symbol+0x6b>
mov    -0x18(%rbp),%rsi
lea    0xe6a(%rip),%rdi
call   1040 <print_error@plt>
jmp    11a0 <resolve_symbol+0x70>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_symbol>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1171 <resolve_symbol+0x41>
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <dlsym@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
je     1173 <resolve_symbol+0x43>
lea    0xe99(%rip),%rdi
mov    %rax,%rsi
call   1040 <print_error@plt>
jmp    1173 <resolve_symbol+0x43>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [6 x i8] c"dlsym\00"
define i8* @resolve_symbol(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
store i8* null, i8** %5
store i8* null, i8** %6
%7 = load i8*, i8** %3
%8 = icmp ne i8* null, %7
br i1 %8, label %9, label %21
9: ; preds = %2
%10 = call i8* (...) @dlerror()
%11 = load i8*, i8** %3
%12 = load i8*, i8** %4
%13 = call i8* @dlsym(i8* %11, i8* %12)
store i8* %13, i8** %6
%14 = call i8* (...) @dlerror()
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = icmp ne i8* null, %15
br i1 %16, label %17, label %20
17: ; preds = %9
%18 = load i8*, i8** %5
%19 = call i32 @print_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %9
br label %21
21: ; preds = %20, %2
%22 = load i8*, i8** %6
ret i8* %22
}

LLVM IR (-O2):
@.str = [6 x i8] c"dlsym\00"
define i8* @resolve_symbol(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %11, label %4
4: ; preds = %2
%5 = tail call i8* (...) @dlerror()
%6 = tail call i8* @dlsym(i8* %0, i8* %1)
%7 = tail call i8* (...) @dlerror()
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = tail call i32 @print_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %11
11: ; preds = %4, %9, %2
%12 = phi i8* [ %6, %9 ], [ %6, %4 ], [ null, %2 ]
ret i8* %12
}