#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ strlcpy(char *, char const *, int);
char *strstr(char *, char *);
int exec_mbem(const char *str) {
  char buf[128];
  strlcpy(buf, str, sizeof(buf));
  char *ptr = strstr(buf, "@mod:");
  if (ptr) {
    ptr = strstr(buf + 5, "#");
    if (ptr) {
      *ptr = '\0';
      ++ptr;
    }
    void *hdll = dlopen(buf + 5, RTLD_LAZY);
    if (hdll) {
      int (*func)();
      *(void **)(&func) = dlsym(hdll, ptr ? ptr : "mod_main");
      if (func)
        func();
      dlclose(hdll);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<exec_mbem>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x80,%edx
call   1040 <strlcpy@plt>
lea    -0x90(%rbp),%rdi
lea    0xe7e(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
je     1295 <exec_mbem+0x145>
lea    -0x90(%rbp),%rdi
add    $0x5,%rdi
lea    0xe58(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
je     11e4 <exec_mbem+0x94>
mov    -0x98(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x98(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x98(%rbp)
lea    -0x90(%rbp),%rdi
add    $0x5,%rdi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
call   1030 <dlopen@plt>
mov    %rax,-0xa0(%rbp)
cmpq   $0x0,-0xa0(%rbp)
je     1290 <exec_mbem+0x140>
mov    -0xa0(%rbp),%rax
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0x98(%rbp)
je     1241 <exec_mbem+0xf1>
mov    -0x98(%rbp),%rax
mov    %rax,-0xb8(%rbp)
jmp    1254 <exec_mbem+0x104>
lea    0xdc0(%rip),%rax
mov    %rax,-0xb8(%rbp)
jmp    1254 <exec_mbem+0x104>
mov    -0xb0(%rbp),%rdi
mov    -0xb8(%rbp),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0xa8(%rbp)
cmpq   $0x0,-0xa8(%rbp)
je     1284 <exec_mbem+0x134>
mov    $0x0,%al
call   *-0xa8(%rbp)
mov    -0xa0(%rbp),%rdi
call   1060 <dlclose@plt>
jmp    1295 <exec_mbem+0x145>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_mbem>:
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rdi,%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x80,%edx
call   1050 <strlcpy@plt>
lea    0xe7c(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strstr@plt>
test   %rax,%rax
je     11f9 <exec_mbem+0x99>
lea    0x5(%rsp),%r14
mov    %r14,%rdi
mov    $0x23,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     11b4 <exec_mbem+0x54>
mov    %rax,%rbx
movb   $0x0,(%rax)
add    $0x1,%rbx
jmp    11b6 <exec_mbem+0x56>
xor    %ebx,%ebx
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11f9 <exec_mbem+0x99>
mov    %rax,%r14
test   %rbx,%rbx
lea    0xe2d(%rip),%rsi
cmovne %rbx,%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     11f1 <exec_mbem+0x91>
mov    %rax,%rcx
xor    %eax,%eax
call   *%rcx
mov    %r14,%rdi
call   1070 <dlclose@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [6 x i8] c"@mod:\00"
@.str.1 = [2 x i8] c"#\00"
@RTLD_LAZY = global i32 0
@.str.2 = [9 x i8] c"mod_main\00"
define i32 @exec_mbem(i8* %0) {
%2 = alloca i8*
%3 = alloca [128 x i8]
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32 (...)*
store i8* %0, i8** %2
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%8 = load i8*, i8** %2
%9 = call i32 @strlcpy(i8* %7, i8* %8, i32 128)
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%11 = call i8* @strstr(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %51
14: ; preds = %1
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%16 = getelementptr inbounds i8, i8* %15, i64 5
%17 = call i8* @strstr(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i8*, i8** %4
store i8 0, i8* %21
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %4
br label %24
24: ; preds = %20, %14
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%26 = getelementptr inbounds i8, i8* %25, i64 5
%27 = load i32, i32* @RTLD_LAZY
%28 = call i8* @dlopen(i8* %26, i32 %27)
store i8* %28, i8** %5
%29 = load i8*, i8** %5
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %50
31: ; preds = %24
%32 = load i8*, i8** %5
%33 = load i8*, i8** %4
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = load i8*, i8** %4
br label %38
37: ; preds = %31
br label %38
38: ; preds = %37, %35
%39 = phi i8* [ %36, %35 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), %37 ]
%40 = call i8* @dlsym(i8* %32, i8* %39)
%41 = bitcast i32 (...)** %6 to i8**
store i8* %40, i8** %41
%42 = load i32 (...)*, i32 (...)** %6
%43 = icmp ne i32 (...)* %42, null
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = load i32 (...)*, i32 (...)** %6
%46 = call i32 (...) %45()
br label %47
47: ; preds = %44, %38
%48 = load i8*, i8** %5
%49 = call i32 @dlclose(i8* %48)
br label %50
50: ; preds = %47, %24
br label %51
51: ; preds = %50, %1
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"@mod:\00"
@RTLD_LAZY = global i32 0
@.str.2 = [9 x i8] c"mod_main\00"
define i32 @exec_mbem(i8* %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = call i32 @strlcpy(i8* %3, i8* %0, i32 128)
%5 = call i8* @strstr(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %28, label %7
7: ; preds = %1
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 5
%9 = call i8* @strchr(i8* %8, i32 35)
%10 = icmp eq i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %7
store i8 0, i8* %9
%12 = getelementptr inbounds i8, i8* %9, i64 1
br label %13
13: ; preds = %11, %7
%14 = phi i8* [ %12, %11 ], [ null, %7 ]
%15 = load i32, i32* @RTLD_LAZY
%16 = call i8* @dlopen(i8* %8, i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %28, label %18
18: ; preds = %13
%19 = icmp eq i8* %14, null
%20 = select i1 %19, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %14
%21 = call i8* @dlsym(i8* %16, i8* %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %18
%24 = bitcast i8* %21 to i32 (...)*
%25 = call i32 (...) %24()
br label %26
26: ; preds = %23, %18
%27 = call i32 @dlclose(i8* %16)
br label %28
28: ; preds = %13, %26, %1
ret i32 0
}