#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*/ RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  void *handle;
  void *funcptr;
  if (argc != 3) {
    fprintf(stderr, "usage: %s libpath  funcname\n", argv[0]);
    fprintf(stderr, "e.g. : %s /usr/lib/libc.so  system\n", argv[0]);
    exit(-1);
  }
  if (0 >= (handle = dlopen(argv[1], RTLD_LAZY))) {
    fprintf(stderr, "error detail:%s\n", dlerror());
    exit(-1);
  }
  dlerror();
  if (0 >= (funcptr = dlsym(handle, argv[2]))) {
    perror("ldsym");
    fprintf(stderr, "error detail:%s\n", dlerror());
    exit(-1);
  }
  printf("0x%08x", (long)funcptr - *(long *)handle);
  dlclose(handle);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11e2 <main+0x62>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe4a(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jb     1239 <main+0xb9>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe17(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1080 <dlsym@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jb     129e <main+0x11e>
lea    0xde8(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xdb2(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rax
sub    (%rax),%rsi
lea    0xda8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <dlclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x3,%edi
jne    11e2 <main+0x62>
mov    0x8(%r14),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
test   %rax,%rax
je     1214 <main+0x94>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    0x10(%r14),%rsi
mov    %rbx,%rdi
call   1080 <dlsym@plt>
test   %rax,%rax
je     1208 <main+0x88>
sub    (%rbx),%rax
lea    0xe90(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   10a0 <dlclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2df7(%rip),%rbx
mov    (%rbx),%edi
mov    (%r14),%rdx
lea    0xe0b(%rip),%rsi
call   1060 <fprintf@plt>
mov    (%rbx),%edi
mov    (%r14),%rdx
lea    0xe17(%rip),%rsi
jmp    1230 <main+0xb0>
lea    0xe43(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe16(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"usage: %s libpath funcname\0A\00"
@.str.1 = [36 x i8] c"e.g. : %s /usr/lib/libc.so system\0A\00"
@RTLD_LAZY = global i32 0
@.str.2 = [17 x i8] c"error detail:%s\0A\00"
@.str.3 = [6 x i8] c"ldsym\00"
@.str.4 = [7 x i8] c"0x%08x\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 3
br i1 %9, label %10, label %22
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load i32, i32* @RTLD_LAZY
%27 = call i8* @dlopen(i8* %25, i32 %26)
store i8* %27, i8** %6
%28 = icmp uge i8* null, %27
br i1 %28, label %29, label %34
29: ; preds = %22
%30 = load i32, i32* @stderr
%31 = call i8* (...) @dlerror()
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %22
%35 = call i8* (...) @dlerror()
%36 = load i8*, i8** %6
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %38
%40 = call i8* @dlsym(i8* %36, i8* %39)
store i8* %40, i8** %7
%41 = icmp uge i8* null, %40
br i1 %41, label %42, label %48
42: ; preds = %34
%43 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* @stderr
%45 = call i8* (...) @dlerror()
%46 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %34
%49 = load i8*, i8** %7
%50 = ptrtoint i8* %49 to i64
%51 = load i8*, i8** %6
%52 = bitcast i8* %51 to i64*
%53 = load i64, i64* %52
%54 = sub nsw i64 %50, %53
%55 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i64 %54)
%56 = load i8*, i8** %6
%57 = call i32 @dlclose(i8* %56)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"usage: %s libpath funcname\0A\00"
@.str.1 = [36 x i8] c"e.g. : %s /usr/lib/libc.so system\0A\00"
@RTLD_LAZY = global i32 0
@.str.2 = [17 x i8] c"error detail:%s\0A\00"
@.str.3 = [6 x i8] c"ldsym\00"
@.str.4 = [7 x i8] c"0x%08x\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %12, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @RTLD_LAZY
%16 = tail call i8* @dlopen(i8* %14, i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %23
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = tail call i8* (...) @dlerror()
%21 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %12
%24 = tail call i8* (...) @dlerror()
%25 = getelementptr inbounds i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
%27 = tail call i8* @dlsym(i8* %16, i8* %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %35
29: ; preds = %23
%30 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* @stderr
%32 = tail call i8* (...) @dlerror()
%33 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = tail call i32 @exit(i32 -1)
unreachable
35: ; preds = %23
%36 = ptrtoint i8* %27 to i64
%37 = bitcast i8* %16 to i64*
%38 = load i64, i64* %37
%39 = sub nsw i64 %36, %38
%40 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i64 %39)
%41 = tail call i32 @dlclose(i8* %16)
ret i32 0
}