#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;
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int strlen(char *);
int stub1(int, char **, char **);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[], char *envp[]) {
  if (argc < 3) {
    write(2, "Usage: runso ./libsomelibrary.so main <args>\n", 63 - 18);
    return 1;
  }
  void *d = dlopen(argv[1], RTLD_LAZY);
  if (!d) {
    char *e = dlerror();
    write(2, e, strlen(e));
    write(2, "\n", 1);
    return 2;
  }
  void *m = dlsym(d, argv[2]);
  if (!m) {
    char *e = dlerror();
    write(2, e, strlen(e));
    write(2, "\n", 1);
    return 3;
  }
  return ((int (*)(int, char **, char **))m)(argc - 2, argv + 2, envp);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1196 <main+0x46>
mov    $0x2,%edi
lea    0xe80(%rip),%rsi
mov    $0x2d,%edx
call   1040 <write@plt>
movl   $0x1,-0x4(%rbp)
jmp    1292 <main+0x142>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
call   1060 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1209 <main+0xb9>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
mov    $0x2,%edi
call   1040 <write@plt>
mov    $0x2,%edi
lea    0xe39(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
movl   $0x2,-0x4(%rbp)
jmp    1292 <main+0x142>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1070 <dlsym@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1277 <main+0x127>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
mov    $0x2,%edi
call   1040 <write@plt>
mov    $0x2,%edi
lea    0xdcb(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
movl   $0x3,-0x4(%rbp)
jmp    1292 <main+0x142>
mov    -0x30(%rbp),%rax
mov    -0x8(%rbp),%edi
sub    $0x2,%edi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
mov    -0x18(%rbp),%rdx
call   *%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jg     1179 <main+0x29>
lea    0xea0(%rip),%rsi
mov    $0x2,%edi
mov    $0x2d,%edx
call   1040 <write@plt>
mov    $0x1,%ebx
jmp    1238 <main+0xe8>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
call   1060 <dlopen@plt>
test   %rax,%rax
je     11be <main+0x6e>
mov    0x10(%rbx),%rsi
mov    %rax,%rdi
call   1070 <dlsym@plt>
test   %rax,%rax
je     11fc <main+0xac>
add    $0x10,%rbx
add    $0xfffffffe,%ebp
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
pop    %rbx
pop    %r14
pop    %rbp
jmp    *%rax
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    $0x2,%ebx
mov    $0x2,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    0xe41(%rip),%rsi
mov    $0x2,%edi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    1238 <main+0xe8>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    0xe08(%rip),%rsi
mov    $0x2,%edi
mov    $0x1,%edx
call   1040 <write@plt>
mov    $0x3,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [46 x i8] c"Usage: runso ./libsomelibrary.so main <args>\0A\00"
@RTLD_LAZY = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @write(i32 2, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 45)
store i32 1, i32* %4
br label %55
16: ; preds = %3
%17 = load i8**, i8*** %6
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @RTLD_LAZY
%21 = call i8* @dlopen(i8* %19, i32 %20)
store i8* %21, i8** %8
%22 = load i8*, i8** %8
%23 = icmp ne i8* %22, null
br i1 %23, label %31, label %24
24: ; preds = %16
%25 = call i8* (...) @dlerror()
store i8* %25, i8** %9
%26 = load i8*, i8** %9
%27 = load i8*, i8** %9
%28 = call i32 @strlen(i8* %27)
%29 = call i32 @write(i32 2, i8* %26, i32 %28)
%30 = call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
store i32 2, i32* %4
br label %55
31: ; preds = %16
%32 = load i8*, i8** %8
%33 = load i8**, i8*** %6
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i8* @dlsym(i8* %32, i8* %35)
store i8* %36, i8** %10
%37 = load i8*, i8** %10
%38 = icmp ne i8* %37, null
br i1 %38, label %46, label %39
39: ; preds = %31
%40 = call i8* (...) @dlerror()
store i8* %40, i8** %11
%41 = load i8*, i8** %11
%42 = load i8*, i8** %11
%43 = call i32 @strlen(i8* %42)
%44 = call i32 @write(i32 2, i8* %41, i32 %43)
%45 = call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
store i32 3, i32* %4
br label %55
46: ; preds = %31
%47 = load i8*, i8** %10
%48 = bitcast i8* %47 to i32 (i32, i8**, i8**)*
%49 = load i32, i32* %5
%50 = sub nsw i32 %49, 2
%51 = load i8**, i8*** %6
%52 = getelementptr inbounds i8*, i8** %51, i64 2
%53 = load i8**, i8*** %7
%54 = call i32 %48(i32 %50, i8** %52, i8** %53)
store i32 %54, i32* %4
br label %55
55: ; preds = %46, %39, %24, %14
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@.str = [46 x i8] c"Usage: runso ./libsomelibrary.so main <args>\0A\00"
@RTLD_LAZY = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 45)
br label %32
7: ; preds = %3
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @RTLD_LAZY
%11 = tail call i8* @dlopen(i8* %9, i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %7
%14 = tail call i8* (...) @dlerror()
%15 = tail call i32 @strlen(i8* %14)
%16 = tail call i32 @write(i32 2, i8* %14, i32 %15)
%17 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %32
18: ; preds = %7
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = tail call i8* @dlsym(i8* %11, i8* %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = tail call i8* (...) @dlerror()
%25 = tail call i32 @strlen(i8* %24)
%26 = tail call i32 @write(i32 2, i8* %24, i32 %25)
%27 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %32
28: ; preds = %18
%29 = bitcast i8* %21 to i32 (i32, i8**, i8**)*
%30 = add nsw i32 %0, -2
%31 = tail call i32 %29(i32 %30, i8** %19, i8** %2)
br label %32
32: ; preds = %13, %28, %23, %5
%33 = phi i32 [ 1, %5 ], [ 2, %13 ], [ %31, %28 ], [ 3, %23 ]
ret i32 %33
}