#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 RTLD_GLOBAL;
int RTLD_NOW;
scalar_t__ dlerror();
void *dlopen(char *, int);
void *dlsym(void *, char const *);
void *get_vdso_sym(const char *name) {
  void *handle;
  void *sym;
  handle = dlopen("linux-vdso.so.1", RTLD_NOW | RTLD_GLOBAL);
  if (!handle)
    handle = dlopen("linux-gate.so.1", RTLD_NOW | RTLD_GLOBAL);
  if (!handle)
    handle = dlopen("linux-vdso32.so.1", RTLD_NOW | RTLD_GLOBAL);
  if (!handle)
    handle = dlopen("linux-vdso64.so.1", RTLD_NOW | RTLD_GLOBAL);
  if (handle) {
    (void)dlerror();
    sym = dlsym(handle, name);
    if (dlerror())
      sym = NULL;
  } else {
    sym = NULL;
  }
  return sym;
}


x86 assembly (clang, -O0):
<get_vdso_sym>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
mov    0x2e9c(%rip),%rax
or     (%rax),%esi
lea    0xeab(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118b <get_vdso_sym+0x5b>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    0x2e6f(%rip),%rax
or     (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b8 <get_vdso_sym+0x88>
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    0x2e42(%rip),%rax
or     (%rax),%esi
lea    0xe71(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e5 <get_vdso_sym+0xb5>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
or     (%rax),%esi
lea    0xe56(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1226 <get_vdso_sym+0xf6>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
cmp    $0x0,%rax
je     1221 <get_vdso_sym+0xf1>
movq   $0x0,-0x18(%rbp)
jmp    122e <get_vdso_sym+0xfe>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_vdso_sym>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e9e(%rip),%r12
mov    0x2e9f(%rip),%rbx
mov    (%rbx),%esi
or     (%r12),%esi
lea    0xeaa(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11b1 <get_vdso_sym+0x81>
mov    (%rbx),%esi
or     (%r12),%esi
lea    0xea0(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11b1 <get_vdso_sym+0x81>
mov    (%rbx),%esi
or     (%r12),%esi
lea    0xe96(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11b1 <get_vdso_sym+0x81>
mov    (%rbx),%esi
or     (%r12),%esi
lea    0xe8e(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,%r15
test   %rax,%rax
je     11e5 <get_vdso_sym+0xb5>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <dlsym@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
cmove  %r14,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %ebx,%ebx
jmp    11d6 <get_vdso_sym+0xa6>

LLVM IR (-O0):
@.str = [16 x i8] c"linux-vdso.so.1\00"
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@.str.1 = [16 x i8] c"linux-gate.so.1\00"
@.str.2 = [18 x i8] c"linux-vdso32.so.1\00"
@.str.3 = [18 x i8] c"linux-vdso64.so.1\00"
define i8* @get_vdso_sym(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i32, i32* @RTLD_NOW
%6 = load i32, i32* @RTLD_GLOBAL
%7 = or i32 %5, %6
%8 = call i8* @dlopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %7)
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %16, label %11
11: ; preds = %1
%12 = load i32, i32* @RTLD_NOW
%13 = load i32, i32* @RTLD_GLOBAL
%14 = or i32 %12, %13
%15 = call i8* @dlopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %14)
store i8* %15, i8** %3
br label %16
16: ; preds = %11, %1
%17 = load i8*, i8** %3
%18 = icmp ne i8* %17, null
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = load i32, i32* @RTLD_NOW
%21 = load i32, i32* @RTLD_GLOBAL
%22 = or i32 %20, %21
%23 = call i8* @dlopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %22)
store i8* %23, i8** %3
br label %24
24: ; preds = %19, %16
%25 = load i8*, i8** %3
%26 = icmp ne i8* %25, null
br i1 %26, label %32, label %27
27: ; preds = %24
%28 = load i32, i32* @RTLD_NOW
%29 = load i32, i32* @RTLD_GLOBAL
%30 = or i32 %28, %29
%31 = call i8* @dlopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %30)
store i8* %31, i8** %3
br label %32
32: ; preds = %27, %24
%33 = load i8*, i8** %3
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %44
35: ; preds = %32
%36 = call i64 (...) @dlerror()
%37 = load i8*, i8** %3
%38 = load i8*, i8** %2
%39 = call i8* @dlsym(i8* %37, i8* %38)
store i8* %39, i8** %4
%40 = call i64 (...) @dlerror()
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %35
store i8* null, i8** %4
br label %43
43: ; preds = %42, %35
br label %45
44: ; preds = %32
store i8* null, i8** %4
br label %45
45: ; preds = %44, %43
%46 = load i8*, i8** %4
ret i8* %46
}

LLVM IR (-O2):
@.str = [16 x i8] c"linux-vdso.so.1\00"
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@.str.1 = [16 x i8] c"linux-gate.so.1\00"
@.str.2 = [18 x i8] c"linux-vdso32.so.1\00"
@.str.3 = [18 x i8] c"linux-vdso64.so.1\00"
define i8* @get_vdso_sym(i8* %0) {
%2 = load i32, i32* @RTLD_NOW
%3 = load i32, i32* @RTLD_GLOBAL
%4 = or i32 %3, %2
%5 = tail call i8* @dlopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %25
7: ; preds = %1
%8 = load i32, i32* @RTLD_NOW
%9 = load i32, i32* @RTLD_GLOBAL
%10 = or i32 %9, %8
%11 = tail call i8* @dlopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %25
13: ; preds = %7
%14 = load i32, i32* @RTLD_NOW
%15 = load i32, i32* @RTLD_GLOBAL
%16 = or i32 %15, %14
%17 = tail call i8* @dlopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %25
19: ; preds = %13
%20 = load i32, i32* @RTLD_NOW
%21 = load i32, i32* @RTLD_GLOBAL
%22 = or i32 %21, %20
%23 = tail call i8* @dlopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %32, label %25
25: ; preds = %1, %7, %13, %19
%26 = phi i8* [ %23, %19 ], [ %17, %13 ], [ %11, %7 ], [ %5, %1 ]
%27 = tail call i64 (...) @dlerror()
%28 = tail call i8* @dlsym(i8* %26, i8* %0)
%29 = tail call i64 (...) @dlerror()
%30 = icmp eq i64 %29, 0
%31 = select i1 %30, i8* %28, i8* null
br label %32
32: ; preds = %25, %19
%33 = phi i8* [ %31, %25 ], [ null, %19 ]
ret i8* %33
}