#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_NOW;
int /*<<< orphan*/ dlclose(void *);
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ stderr;
char const *stub1(char const *);
int main(int argc, char *argv[]) {
  void *handler;
  const char *(*handlerfunc)(const char *);
  const char *ret;
  if (argc != 3) {
    fprintf(stderr, "Syntax: cexec <handler.so> <inputarg>\n");
    return -1;
  }
  handler = dlopen(argv[1], RTLD_NOW);
  *(void **)(&handlerfunc) = dlsym(handler, "handler");
  ret = (*handlerfunc)(argv[2]);
  printf("ret:%s\n", ret);
  dlclose(handler);
  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     1191 <main+0x41>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <main+0xa1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    0x10(%rcx),%rdi
call   *%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe53(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <dlclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    11a1 <main+0x51>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,%r14
lea    0xeaf(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
mov    0x10(%rbx),%rdi
call   *%rax
lea    0xea2(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    %r14,%rdi
call   1070 <dlclose@plt>
xor    %eax,%eax
jmp    11bb <main+0x6b>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [39 x i8] c"Syntax: cexec <handler.so> <inputarg>\0A\00"
@RTLD_NOW = global i32 0
@.str.1 = [8 x i8] c"handler\00"
@.str.2 = [8 x i8] c"ret:%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8* (i8*)*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %32
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @RTLD_NOW
%19 = call i8* @dlopen(i8* %17, i32 %18)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = call i8* @dlsym(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%22 = bitcast i8* (i8*)** %7 to i8**
store i8* %21, i8** %22
%23 = load i8* (i8*)*, i8* (i8*)** %7
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i8* %23(i8* %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = load i8*, i8** %6
%31 = call i32 @dlclose(i8* %30)
store i32 0, i32* %3
br label %32
32: ; preds = %14, %11
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [39 x i8] c"Syntax: cexec <handler.so> <inputarg>\0A\00"
@RTLD_NOW = global i32 0
@.str.1 = [8 x i8] c"handler\00"
@.str.2 = [8 x i8] c"ret:%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %19
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @RTLD_NOW
%11 = tail call i8* @dlopen(i8* %9, i32 %10)
%12 = tail call i8* @dlsym(i8* %11, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%13 = bitcast i8* %12 to i8* (i8*)*
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i8* %13(i8* %15)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = tail call i32 @dlclose(i8* %11)
br label %19
19: ; preds = %7, %4
%20 = phi i32 [ -1, %4 ], [ 0, %7 ]
ret i32 %20
}