#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 */
typedef int /*<<< orphan*/ uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ RTLD_LAZY;
void *dlopen(int /*<<< orphan*/ *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int fscanf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/ *, char **);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ printvmt(void *, void *, int /*<<< orphan*/);
__attribute__((constructor)) void _test(void) {
  FILE *symlist = fopen("/tmp/symlist", "r");
  void *dlHandle = dlopen(NULL, RTLD_LAZY);
  uint64_t size = 0;
  char *current = NULL;
  int ret = 0;
  while ((ret = fscanf(symlist, "%lu %ms\n", &size, &current)) != EOF) {
    if (current) {
      void *sym = dlsym(dlHandle, current);
      printf("sym %s = %p (size = %lu)\n", current, sym, size);
      if (sym) {
        printvmt(dlHandle, sym, size);
        printf("\n");
      }
      free(current);
    }
  }
  exit(1);
}


x86 assembly (clang, -O0):
<_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe71(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edi
call   1070 <dlopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe39(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1060 <fscanf@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dfb(%rip),%rcx
cmp    (%rcx),%eax
je     1261 <_test+0xe1>
cmpq   $0x0,-0x20(%rbp)
je     125c <_test+0xdc>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <dlsym@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x30(%rbp)
je     1253 <_test+0xd3>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <printvmt@plt>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    11cb <_test+0x4b>
mov    $0x1,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<_test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe5b(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r14
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1080 <dlopen@plt>
mov    %rax,%rbx
movl   $0x0,0xc(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xe30(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x10(%rsp),%rcx
mov    %r14,%rdi
call   1070 <fscanf@plt>
mov    0x2df0(%rip),%rcx
cmp    (%rcx),%eax
jne    1206 <_test+0x76>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe02(%rip),%r15
lea    0xc(%rsp),%r12
lea    0x10(%rsp),%r13
jmp    1246 <_test+0xb6>
nopl   0x0(%rax)
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1070 <fscanf@plt>
mov    0x2da6(%rip),%rcx
cmp    (%rcx),%eax
je     11fc <_test+0x6c>
mov    0x10(%rsp),%rsi
test   %rsi,%rsi
je     122a <_test+0x9a>
mov    %rbx,%rdi
call   10a0 <dlsym@plt>
mov    %rax,%rbp
mov    0x10(%rsp),%rsi
mov    0xc(%rsp),%ecx
lea    0xdad(%rip),%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
test   %rbp,%rbp
je     1220 <_test+0x90>
mov    0xc(%rsp),%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <printvmt@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
jmp    1220 <_test+0x90>

LLVM IR (-O0):
@.str = [13 x i8] c"/tmp/symlist\00"
@.str.1 = [2 x i8] c"r\00"
@RTLD_LAZY = global i32 0
@.str.2 = [9 x i8] c"%lu %ms\0A\00"
@EOF = global i32 0
@.str.3 = [26 x i8] c"sym %s = %p (size = %lu)\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @_test, i8* null }]
define void @_test() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %1
%8 = load i32, i32* @RTLD_LAZY
%9 = call i8* @dlopen(i32* null, i32 %8)
store i8* %9, i8** %2
store i32 0, i32* %3
store i8* null, i8** %4
store i32 0, i32* %5
br label %10
10: ; preds = %37, %0
%11 = load i32*, i32** %1
%12 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32* %3, i8** %4)
store i32 %12, i32* %5
%13 = load i32, i32* @EOF
%14 = icmp ne i32 %12, %13
br i1 %14, label %15, label %38
15: ; preds = %10
%16 = load i8*, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %37
18: ; preds = %15
%19 = load i8*, i8** %2
%20 = load i8*, i8** %4
%21 = call i8* @dlsym(i8* %19, i8* %20)
store i8* %21, i8** %6
%22 = load i8*, i8** %4
%23 = load i8*, i8** %6
%24 = load i32, i32* %3
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %22, i8* %23, i32 %24)
%26 = load i8*, i8** %6
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %34
28: ; preds = %18
%29 = load i8*, i8** %2
%30 = load i8*, i8** %6
%31 = load i32, i32* %3
%32 = call i32 @printvmt(i8* %29, i8* %30, i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %28, %18
%35 = load i8*, i8** %4
%36 = call i32 @free(i8* %35)
br label %37
37: ; preds = %34, %15
br label %10
38: ; preds = %10
%39 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [13 x i8] c"/tmp/symlist\00"
@.str.1 = [2 x i8] c"r\00"
@RTLD_LAZY = global i32 0
@.str.2 = [9 x i8] c"%lu %ms\0A\00"
@EOF = global i32 0
@.str.3 = [26 x i8] c"sym %s = %p (size = %lu)\0A\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @_test, i8* null }]
define void @_test() {
%1 = alloca i32
%2 = alloca i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = load i32, i32* @RTLD_LAZY
%5 = tail call i8* @dlopen(i32* null, i32 %4)
%6 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%7 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%8 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32* %1, i8** %2)
%9 = load i32, i32* @EOF
%10 = icmp eq i32 %8, %9
br i1 %10, label %31, label %11
11: ; preds = %0, %27
%12 = load i8*, i8** %2
%13 = icmp eq i8* %12, null
br i1 %13, label %27, label %14
14: ; preds = %11
%15 = call i8* @dlsym(i8* %5, i8* %12)
%16 = load i8*, i8** %2
%17 = load i32, i32* %1
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %16, i8* %15, i32 %17)
%19 = icmp eq i8* %15, null
br i1 %19, label %24, label %20
20: ; preds = %14
%21 = load i32, i32* %1
%22 = call i32 @printvmt(i8* %5, i8* %15, i32 %21)
%23 = call i32 @putchar(i32 10)
br label %24
24: ; preds = %20, %14
%25 = load i8*, i8** %2
%26 = call i32 @free(i8* %25)
br label %27
27: ; preds = %24, %11
%28 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32* %1, i8** %2)
%29 = load i32, i32* @EOF
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %11
31: ; preds = %27, %0
%32 = call i32 @exit(i32 1)
unreachable
}