#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 (*parse_symbol_func)(void *, char *, int);
typedef int /*<<< orphan*/ default_path;

/* Variables and functions */
int /*<<< orphan*/ RTLD_LAZY;
char *dlerror();
void *dlopen(char const *, int /*<<< orphan*/);
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *getenv(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, void *);
int /*<<< orphan*/ stderr;
int mem_parse_symbol(void *addr, char *buffer, int buflen) {
  typedef int (*parse_symbol_func)(void *addr, char *buffer, int buflen);
  static void *handle;
  static parse_symbol_func parse_func;
  int ret;
  if (parse_func == NULL || handle == NULL) {
    const char *libpath = NULL;
    libpath = getenv("LIBSYMBOL_PATH");
    if (libpath == NULL) {
      size_t i;
      char *default_path[] = {
          "./libsymbol.so",          "/lib/libsymbol.so",
          "/usr/lib/libsymbol.so",   "/lib64/libsymbol.so",
          "/usr/lib64/libsymbol.so",
      };
      for (i = 0; i < (sizeof(default_path) / sizeof(default_path[0])); i++) {
        handle = dlopen(default_path[i], RTLD_LAZY);
        if (handle != NULL)
          break;
      }
    } else {
      handle = dlopen(libpath, RTLD_LAZY);
    }
    if (handle == NULL) {
      fprintf(stderr, "%s\n", dlerror());
      buffer[0] = '\0';
      return 0;
    }
    parse_func = (parse_symbol_func)dlsym(handle, "parse_symbol_by_addr");
    if (parse_func == NULL) {
      fprintf(stderr, "%s\n", dlerror());
      buffer[0] = '\0';
      return 0;
    }
  }
  ret = parse_func(addr, buffer, buflen);
  if (ret == 0) {
    snprintf(buffer, buflen, "%p #library #", addr);
    buffer[buflen - 1] = '\0';
  }
  return 1;
}


x86 assembly (clang, -O0):
<mem_parse_symbol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,0x2edd(%rip)
je     119f <mem_parse_symbol+0x2f>
cmpq   $0x0,0x2ec7(%rip)
jne    132c <mem_parse_symbol+0x1bc>
movq   $0x0,-0x28(%rbp)
lea    0xe52(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1268 <mem_parse_symbol+0xf8>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
lea    0xe36(%rip),%rax
mov    %rax,-0x60(%rbp)
lea    0xe3e(%rip),%rax
mov    %rax,-0x58(%rbp)
lea    0xe2f(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xe3e(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xe2f(%rip),%rax
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x5,-0x30(%rbp)
jae    1263 <mem_parse_symbol+0xf3>
mov    -0x30(%rbp),%rax
mov    -0x60(%rbp,%rax,8),%rdi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
call   1070 <dlopen@plt>
mov    %rax,0x2e26(%rip)
cmpq   $0x0,0x2e1e(%rip)
je     124d <mem_parse_symbol+0xdd>
jmp    1263 <mem_parse_symbol+0xf3>
jmp    1252 <mem_parse_symbol+0xe2>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1211 <mem_parse_symbol+0xa1>
jmp    1281 <mem_parse_symbol+0x111>
mov    -0x28(%rbp),%rdi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
call   1070 <dlopen@plt>
mov    %rax,0x2ddf(%rip)
cmpq   $0x0,0x2dd7(%rip)
jne    12c7 <mem_parse_symbol+0x157>
mov    0x2d4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    $0x0,%al
call   1040 <dlerror@plt>
mov    -0x64(%rbp),%edi
mov    %rax,%rdx
lea    0xd9d(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    137c <mem_parse_symbol+0x20c>
mov    0x2d92(%rip),%rdi
lea    0xd7b(%rip),%rsi
call   1090 <dlsym@plt>
mov    %rax,0x2d87(%rip)
cmpq   $0x0,0x2d7f(%rip)
jne    1327 <mem_parse_symbol+0x1b7>
mov    0x2cea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    $0x0,%al
call   1040 <dlerror@plt>
mov    -0x68(%rbp),%edi
mov    %rax,%rdx
lea    0xd3d(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    137c <mem_parse_symbol+0x20c>
jmp    132c <mem_parse_symbol+0x1bc>
mov    0x2d35(%rip),%rax
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   *%rax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1375 <mem_parse_symbol+0x205>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rcx
lea    0xd06(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_parse_symbol>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2ee9(%rip),%rax
test   %rax,%rax
je     118a <mem_parse_symbol+0x2a>
cmpq   $0x0,0x2ed4(%rip)
jne    1246 <mem_parse_symbol+0xe6>
lea    0xe6f(%rip),%rdi
call   1030 <getenv@plt>
mov    0x2e3b(%rip),%rbp
mov    0x0(%rbp),%esi
test   %rax,%rax
je     11be <mem_parse_symbol+0x5e>
mov    %rax,%rdi
call   1060 <dlopen@plt>
mov    %rax,0x2ea4(%rip)
test   %rax,%rax
jne    122b <mem_parse_symbol+0xcb>
jmp    1280 <mem_parse_symbol+0x120>
lea    0xe4a(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,0x2e87(%rip)
test   %rax,%rax
jne    122b <mem_parse_symbol+0xcb>
mov    0x0(%rbp),%esi
lea    0xe42(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,0x2e6c(%rip)
test   %rax,%rax
jne    122b <mem_parse_symbol+0xcb>
mov    0x0(%rbp),%esi
lea    0xe23(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,0x2e51(%rip)
test   %rax,%rax
jne    122b <mem_parse_symbol+0xcb>
mov    0x0(%rbp),%esi
lea    0xe22(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,0x2e36(%rip)
test   %rax,%rax
je     12b5 <mem_parse_symbol+0x155>
lea    0xe1e(%rip),%rsi
mov    %rax,%rdi
call   1080 <dlsym@plt>
mov    %rax,0x2e1f(%rip)
test   %rax,%rax
je     1280 <mem_parse_symbol+0x120>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   *%rax
mov    $0x1,%ebp
test   %eax,%eax
jne    12a8 <mem_parse_symbol+0x148>
lea    0xe04(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
call   1050 <snprintf@plt>
movslq %r15d,%rax
add    %rax,%rbx
add    $0xffffffffffffffff,%rbx
mov    $0x1,%ebp
jmp    12a5 <mem_parse_symbol+0x145>
mov    0x2d59(%rip),%rax
mov    (%rax),%r14d
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <dlerror@plt>
lea    0xdb2(%rip),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
movb   $0x0,(%rbx)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x0(%rbp),%esi
lea    0xd75(%rip),%rdi
jmp    11a8 <mem_parse_symbol+0x48>

LLVM IR (-O0):
@mem_parse_symbol.handle = internal global i8* null
@mem_parse_symbol.parse_func = internal global i32 (i8*, i8*, i32)* null
@.str = [15 x i8] c"LIBSYMBOL_PATH\00"
@.str.1 = [15 x i8] c"./libsymbol.so\00"
@.str.2 = [18 x i8] c"/lib/libsymbol.so\00"
@.str.3 = [22 x i8] c"/usr/lib/libsymbol.so\00"
@.str.4 = [20 x i8] c"/lib64/libsymbol.so\00"
@.str.5 = [24 x i8] c"/usr/lib64/libsymbol.so\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.6 = [4 x i8] c"%s\0A\00"
@.str.7 = [21 x i8] c"parse_symbol_by_addr\00"
@.str.8 = [14 x i8] c"%p #library #\00"
define i32 @mem_parse_symbol(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca [5 x i8*]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i32 (i8*, i8*, i32)*, i32 (i8*, i8*, i32)** @mem_parse_symbol.parse_func
%13 = icmp eq i32 (i8*, i8*, i32)* %12, null
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i8*, i8** @mem_parse_symbol.handle
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %72
17: ; preds = %14, %3
store i8* null, i8** %9
%18 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i8* %18, i8** %9
%19 = load i8*, i8** %9
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %46
21: ; preds = %17
%22 = bitcast [5 x i8*]* %11 to i8*
%23 = bitcast i8* %22 to [5 x i8*]*
%24 = getelementptr inbounds [5 x i8*], [5 x i8*]* %23, i32 0, i32 0
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i32 0, i32 0), i8** %24
%25 = getelementptr inbounds [5 x i8*], [5 x i8*]* %23, i32 0, i32 1
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i32 0, i32 0), i8** %25
%26 = getelementptr inbounds [5 x i8*], [5 x i8*]* %23, i32 0, i32 2
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i32 0, i32 0), i8** %26
%27 = getelementptr inbounds [5 x i8*], [5 x i8*]* %23, i32 0, i32 3
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i32 0, i32 0), i8** %27
%28 = getelementptr inbounds [5 x i8*], [5 x i8*]* %23, i32 0, i32 4
store i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i32 0, i32 0), i8** %28
store i64 0, i64* %10
br label %29
29: ; preds = %42, %21
%30 = load i64, i64* %10
%31 = icmp ult i64 %30, 5
br i1 %31, label %32, label %45
32: ; preds = %29
%33 = load i64, i64* %10
%34 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i64 0, i64 %33
%35 = load i8*, i8** %34
%36 = load i32, i32* @RTLD_LAZY
%37 = call i8* @dlopen(i8* %35, i32 %36)
store i8* %37, i8** @mem_parse_symbol.handle
%38 = load i8*, i8** @mem_parse_symbol.handle
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %41
40: ; preds = %32
br label %45
41: ; preds = %32
br label %42
42: ; preds = %41
%43 = load i64, i64* %10
%44 = add i64 %43, 1
store i64 %44, i64* %10
br label %29
45: ; preds = %40, %29
br label %50
46: ; preds = %17
%47 = load i8*, i8** %9
%48 = load i32, i32* @RTLD_LAZY
%49 = call i8* @dlopen(i8* %47, i32 %48)
store i8* %49, i8** @mem_parse_symbol.handle
br label %50
50: ; preds = %46, %45
%51 = load i8*, i8** @mem_parse_symbol.handle
%52 = icmp eq i8* %51, null
br i1 %52, label %53, label %59
53: ; preds = %50
%54 = load i32, i32* @stderr
%55 = call i8* (...) @dlerror()
%56 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %55)
%57 = load i8*, i8** %6
%58 = getelementptr inbounds i8, i8* %57, i64 0
store i8 0, i8* %58
store i32 0, i32* %4
br label %91
59: ; preds = %50
%60 = load i8*, i8** @mem_parse_symbol.handle
%61 = call i64 @dlsym(i8* %60, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%62 = inttoptr i64 %61 to i32 (i8*, i8*, i32)*
store i32 (i8*, i8*, i32)* %62, i32 (i8*, i8*, i32)** @mem_parse_symbol.parse_func
%63 = load i32 (i8*, i8*, i32)*, i32 (i8*, i8*, i32)** @mem_parse_symbol.parse_func
%64 = icmp eq i32 (i8*, i8*, i32)* %63, null
br i1 %64, label %65, label %71
65: ; preds = %59
%66 = load i32, i32* @stderr
%67 = call i8* (...) @dlerror()
%68 = call i32 @fprintf(i32 %66, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %67)
%69 = load i8*, i8** %6
%70 = getelementptr inbounds i8, i8* %69, i64 0
store i8 0, i8* %70
store i32 0, i32* %4
br label %91
71: ; preds = %59
br label %72
72: ; preds = %71, %14
%73 = load i32 (i8*, i8*, i32)*, i32 (i8*, i8*, i32)** @mem_parse_symbol.parse_func
%74 = load i8*, i8** %5
%75 = load i8*, i8** %6
%76 = load i32, i32* %7
%77 = call i32 %73(i8* %74, i8* %75, i32 %76)
store i32 %77, i32* %8
%78 = load i32, i32* %8
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %90
80: ; preds = %72
%81 = load i8*, i8** %6
%82 = load i32, i32* %7
%83 = load i8*, i8** %5
%84 = call i32 @snprintf(i8* %81, i32 %82, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i8* %83)
%85 = load i8*, i8** %6
%86 = load i32, i32* %7
%87 = sub nsw i32 %86, 1
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds i8, i8* %85, i64 %88
store i8 0, i8* %89
br label %90
90: ; preds = %80, %72
store i32 1, i32* %4
br label %91
91: ; preds = %90, %65, %53
%92 = load i32, i32* %4
ret i32 %92
}

LLVM IR (-O2):
@mem_parse_symbol.handle = internal unnamed_addr global i8* null
@mem_parse_symbol.parse_func = internal unnamed_addr global i32 (i8*, i8*, i32)* null
@.str = [15 x i8] c"LIBSYMBOL_PATH\00"
@.str.1 = [15 x i8] c"./libsymbol.so\00"
@.str.2 = [18 x i8] c"/lib/libsymbol.so\00"
@.str.3 = [22 x i8] c"/usr/lib/libsymbol.so\00"
@.str.4 = [20 x i8] c"/lib64/libsymbol.so\00"
@.str.5 = [24 x i8] c"/usr/lib64/libsymbol.so\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.6 = [4 x i8] c"%s\0A\00"
@.str.7 = [21 x i8] c"parse_symbol_by_addr\00"
@.str.8 = [14 x i8] c"%p #library #\00"
define i32 @mem_parse_symbol(i8* %0, i8* %1, i32 %2) {
%4 = load i32 (i8*, i8*, i32)*, i32 (i8*, i8*, i32)** @mem_parse_symbol.parse_func
%5 = icmp eq i32 (i8*, i8*, i32)* %4, null
%6 = load i8*, i8** @mem_parse_symbol.handle
%7 = icmp eq i8* %6, null
%8 = select i1 %5, i1 true, i1 %7
br i1 %8, label %9, label %48
9: ; preds = %3
%10 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i8* %10, null
%12 = load i32, i32* @RTLD_LAZY
br i1 %11, label %13, label %32
13: ; preds = %9
%14 = tail call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %12)
store i8* %14, i8** @mem_parse_symbol.handle
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %39
16: ; preds = %13
%17 = load i32, i32* @RTLD_LAZY
%18 = tail call i8* @dlopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %17)
store i8* %18, i8** @mem_parse_symbol.handle
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %39
20: ; preds = %16
%21 = load i32, i32* @RTLD_LAZY
%22 = tail call i8* @dlopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %21)
store i8* %22, i8** @mem_parse_symbol.handle
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %39
24: ; preds = %20
%25 = load i32, i32* @RTLD_LAZY
%26 = tail call i8* @dlopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %25)
store i8* %26, i8** @mem_parse_symbol.handle
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %39
28: ; preds = %24
%29 = load i32, i32* @RTLD_LAZY
%30 = tail call i8* @dlopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %29)
store i8* %30, i8** @mem_parse_symbol.handle
%31 = icmp eq i8* %30, null
br i1 %31, label %35, label %39
32: ; preds = %9
%33 = tail call i8* @dlopen(i8* %10, i32 %12)
store i8* %33, i8** @mem_parse_symbol.handle
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %39
35: ; preds = %28, %32
%36 = load i32, i32* @stderr
%37 = tail call i8* (...) @dlerror()
%38 = tail call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %37)
br label %57
39: ; preds = %13, %16, %20, %24, %28, %32
%40 = phi i8* [ %33, %32 ], [ %14, %13 ], [ %18, %16 ], [ %22, %20 ], [ %26, %24 ], [ %30, %28 ]
%41 = tail call i64 @dlsym(i8* %40, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%42 = inttoptr i64 %41 to i32 (i8*, i8*, i32)*
store i32 (i8*, i8*, i32)* %42, i32 (i8*, i8*, i32)** @mem_parse_symbol.parse_func
%43 = icmp eq i64 %41, 0
br i1 %43, label %44, label %48
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = tail call i8* (...) @dlerror()
%47 = tail call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %46)
br label %57
48: ; preds = %39, %3
%49 = phi i32 (i8*, i8*, i32)* [ %42, %39 ], [ %4, %3 ]
%50 = tail call i32 %49(i8* %0, i8* %1, i32 %2)
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %60
52: ; preds = %48
%53 = tail call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i8* %0)
%54 = add nsw i32 %2, -1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %1, i64 %55
br label %57
57: ; preds = %44, %35, %52
%58 = phi i8* [ %56, %52 ], [ %1, %35 ], [ %1, %44 ]
%59 = phi i32 [ 1, %52 ], [ 0, %35 ], [ 0, %44 ]
store i8 0, i8* %58
br label %60
60: ; preds = %57, %48
%61 = phi i32 [ 1, %48 ], [ %59, %57 ]
ret i32 %61
}