#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*/ EXIT_FAILURE;
int /*<<< orphan*/ RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
void *get_func_addr(char *func) {
  void *addr;
  void *handle;
  char *error;
  dlerror();
  handle = dlopen("libc.so", RTLD_LAZY);
  if (handle == NULL) {
    fprintf(stderr, "%s\n", dlerror());
    exit(EXIT_FAILURE);
  }
  addr = dlsym(handle, func);
  error = dlerror();
  if (error != NULL) {
    fprintf(stderr, "%s\n", error);
    exit(EXIT_FAILURE);
  }
  dlclose(handle);
  return addr;
}


x86 assembly (clang, -O0):
<get_func_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ca <get_func_addr+0x6a>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe51(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1218 <get_func_addr+0xb8>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe03(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1080 <dlclose@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_func_addr>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe82(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11b8 <get_func_addr+0x58>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <dlsym@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
jne    11d3 <get_func_addr+0x73>
mov    %rbx,%rdi
call   1080 <dlclose@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e21(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe39(%rip),%rsi
mov    %ebx,%edi
jmp    11e3 <get_func_addr+0x83>
mov    0x2e06(%rip),%rcx
mov    (%rcx),%edi
lea    0xe25(%rip),%rsi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"libc.so\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @get_func_addr(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = call i8* (...) @dlerror()
%7 = load i32, i32* @RTLD_LAZY
%8 = call i8* @dlopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %7)
store i8* %8, i8** %4
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %17
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i8* (...) @dlerror()
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
%18 = load i8*, i8** %4
%19 = load i8*, i8** %2
%20 = call i8* @dlsym(i8* %18, i8* %19)
store i8* %20, i8** %3
%21 = call i8* (...) @dlerror()
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %30
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %5
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %17
%31 = load i8*, i8** %4
%32 = call i32 @dlclose(i8* %31)
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
@.str = [8 x i8] c"libc.so\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @get_func_addr(i8* %0) {
%2 = tail call i8* (...) @dlerror()
%3 = load i32, i32* @RTLD_LAZY
%4 = tail call i8* @dlopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i8* (...) @dlerror()
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = tail call i8* @dlsym(i8* %4, i8* %0)
%14 = tail call i8* (...) @dlerror()
%15 = icmp eq i8* %14, null
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %12
%22 = tail call i32 @dlclose(i8* %4)
ret i8* %13
}