#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 RTLD_LOCAL;
int RTLD_NOW;
char *dlerror();
void *dlopen(char *, int);
void *dlsym(void *, char *);
int /*<<< orphan*/ printf(char *, char *, char *, ...);
int stub1(int, char **);
int main(int argc, char **argv) {
  if (argc < 3) {
    return 13;
  }
  void *lib = dlopen(argv[1], RTLD_NOW | RTLD_LOCAL);
  if (!lib) {
    printf("dlopen failed for %s: %s\n", argv[1], dlerror());
    return 11;
  }
  void *sym = dlsym(lib, argv[2]);
  if (!sym) {
    printf("dlsym failed for %s on library %s with error: %s\n", argv[2],
           argv[1], dlerror());
    return 12;
  }
  int (*f)(int argc, char **argv) = (int (*)(int, char **))sym;
  int exitcode = (*f)(argc, argv);
  return exitcode;
}


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)
cmpl   $0x3,-0x8(%rbp)
jge    116c <main+0x2c>
movl   $0xd,-0x4(%rbp)
jmp    1250 <main+0x110>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    0x2e4c(%rip),%rax
or     (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ce <main+0x8e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xb,-0x4(%rbp)
jmp    1250 <main+0x110>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1232 <main+0xf2>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xc,-0x4(%rbp)
jmp    1250 <main+0x110>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   *%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0xd,%eax
cmp    $0x3,%edi
jl     11e5 <main+0xa5>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
mov    0x2e87(%rip),%rax
mov    0x2e68(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <dlopen@plt>
test   %rax,%rax
je     1194 <main+0x54>
mov    0x10(%rbx),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     11ba <main+0x7a>
mov    %ebp,%edi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    *%rax
mov    0x8(%rbx),%rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe5a(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xb,%eax
jmp    11e5 <main+0xa5>
mov    0x8(%rbx),%rbp
mov    0x10(%rbx),%rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe4a(%rip),%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xc,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@.str = [26 x i8] c"dlopen failed for %s: %s\0A\00"
@.str.1 = [50 x i8] c"dlsym failed for %s on library %s with error: %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*
%8 = alloca i32 (i32, i8**)*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 13, i32* %3
br label %54
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @RTLD_NOW
%18 = load i32, i32* @RTLD_LOCAL
%19 = or i32 %17, %18
%20 = call i8* @dlopen(i8* %16, i32 %19)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %29, label %23
23: ; preds = %13
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i8* (...) @dlerror()
%28 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %27)
store i32 11, i32* %3
br label %54
29: ; preds = %13
%30 = load i8*, i8** %6
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i8* @dlsym(i8* %30, i8* %33)
store i8* %34, i8** %7
%35 = load i8*, i8** %7
%36 = icmp ne i8* %35, null
br i1 %36, label %46, label %37
37: ; preds = %29
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 2
%40 = load i8*, i8** %39
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i8* (...) @dlerror()
%45 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8* %40, i8* %43, i8* %44)
store i32 12, i32* %3
br label %54
46: ; preds = %29
%47 = load i8*, i8** %7
%48 = bitcast i8* %47 to i32 (i32, i8**)*
store i32 (i32, i8**)* %48, i32 (i32, i8**)** %8
%49 = load i32 (i32, i8**)*, i32 (i32, i8**)** %8
%50 = load i32, i32* %4
%51 = load i8**, i8*** %5
%52 = call i32 %49(i32 %50, i8** %51)
store i32 %52, i32* %9
%53 = load i32, i32* %9
store i32 %53, i32* %3
br label %54
54: ; preds = %46, %37, %23, %12
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@.str = [26 x i8] c"dlopen failed for %s: %s\0A\00"
@.str.1 = [50 x i8] c"dlsym failed for %s on library %s with error: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %29, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @RTLD_NOW
%8 = load i32, i32* @RTLD_LOCAL
%9 = or i32 %8, %7
%10 = tail call i8* @dlopen(i8* %6, i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %4
%13 = load i8*, i8** %5
%14 = tail call i8* (...) @dlerror()
%15 = tail call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %14)
br label %29
16: ; preds = %4
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i8* @dlsym(i8* %10, i8* %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i8*, i8** %17
%23 = load i8*, i8** %5
%24 = tail call i8* (...) @dlerror()
%25 = tail call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8* %22, i8* %23, i8* %24)
br label %29
26: ; preds = %16
%27 = bitcast i8* %19 to i32 (i32, i8**)*
%28 = tail call i32 %27(i32 %0, i8** %1)
br label %29
29: ; preds = %12, %26, %21, %2
%30 = phi i32 [ 13, %2 ], [ 11, %12 ], [ %28, %26 ], [ 12, %21 ]
ret i32 %30
}