#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_DEEPBIND;
int RTLD_LOCAL;
int RTLD_NOW;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int);
void *dlsym(void *, char const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
void stub1();
int LoadAndRun(char *dlfile, char const *entry_pt, int verbosity) {
  void *dlhandle;
  void (*fn)(void);
  char *error;
  if (verbosity >= 2) {
    fprintf(stderr, "Requested %s to be loaded and run\n", dlfile);
  }
  dlhandle = dlopen(dlfile, RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND);
  if (NULL == dlhandle) {
    if (verbosity >= 1) {
      fprintf(stderr, "Load of %s failed\n", dlfile);
      fprintf(stderr, "Error: %s\n", dlerror());
    }
    return 0;
  }
  *(void **)&fn = dlsym(dlhandle, entry_pt);
  if (NULL == fn || NULL != (error = dlerror())) {
    if (verbosity >= 1) {
      fprintf(stderr, "Entry point %s not found in %s", entry_pt, dlfile);
    }
    return 0;
  }
  if (verbosity >= 1) {
    fprintf(stderr, "Calling entry point %s\n", entry_pt);
  }
  (*fn)();
  if (verbosity >= 1) {
    fprintf(stderr, "Done, closing handle\n");
  }
  dlclose(dlhandle);
  return 1;
}


x86 assembly (clang, -O0):
<LoadAndRun>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jl     1188 <LoadAndRun+0x38>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe7f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2e24(%rip),%rax
or     (%rax),%esi
mov    0x2e43(%rip),%rax
or     (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1214 <LoadAndRun+0xc4>
cmpl   $0x1,-0x1c(%rbp)
jl     1208 <LoadAndRun+0xb8>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d8 <LoadAndRun+0x188>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
je     124a <LoadAndRun+0xfa>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rcx
mov    %rcx,-0x38(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     127f <LoadAndRun+0x12f>
cmpl   $0x1,-0x1c(%rbp)
jl     1273 <LoadAndRun+0x123>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d8 <LoadAndRun+0x188>
cmpl   $0x1,-0x1c(%rbp)
jl     12a4 <LoadAndRun+0x154>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdc3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
call   *-0x30(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jl     12c8 <LoadAndRun+0x178>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    0xdb7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1070 <dlclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<LoadAndRun>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
cmp    $0x2,%edx
jl     117f <LoadAndRun+0x2f>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
lea    0xe8b(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2e5a(%rip),%rax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e50(%rip),%rax
or     (%rax),%esi
mov    %r15,%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11f1 <LoadAndRun+0xa1>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <dlsym@plt>
test   %rax,%rax
je     11c9 <LoadAndRun+0x79>
mov    %rax,%r12
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
je     1235 <LoadAndRun+0xe5>
test   %ebp,%ebp
jle    1231 <LoadAndRun+0xe1>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
xor    %ebx,%ebx
mov    %r14,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    127a <LoadAndRun+0x12a>
test   %ebp,%ebp
jle    1231 <LoadAndRun+0xe1>
mov    0x2dd4(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe1d(%rip),%rsi
xor    %ebx,%ebx
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%rbp),%ebp
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe13(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    127a <LoadAndRun+0x12a>
xor    %ebx,%ebx
jmp    127a <LoadAndRun+0x12a>
test   %ebp,%ebp
jle    126a <LoadAndRun+0x11a>
mov    0x2d90(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe16(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
call   *%r12
mov    0x0(%rbp),%edi
lea    0xe17(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    126d <LoadAndRun+0x11d>
call   *%r12
mov    %rbx,%rdi
call   1070 <dlclose@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [35 x i8] c"Requested %s to be loaded and run\0A\00"
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@RTLD_DEEPBIND = global i32 0
@.str.1 = [19 x i8] c"Load of %s failed\0A\00"
@.str.2 = [11 x i8] c"Error: %s\0A\00"
@.str.3 = [31 x i8] c"Entry point %s not found in %s\00"
@.str.4 = [24 x i8] c"Calling entry point %s\0A\00"
@.str.5 = [22 x i8] c"Done, closing handle\0A\00"
define i32 @LoadAndRun(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca void ()*
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
%12 = icmp sge i32 %11, 2
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %5
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %13, %3
%18 = load i8*, i8** %5
%19 = load i32, i32* @RTLD_NOW
%20 = load i32, i32* @RTLD_LOCAL
%21 = or i32 %19, %20
%22 = load i32, i32* @RTLD_DEEPBIND
%23 = or i32 %21, %22
%24 = call i8* @dlopen(i8* %18, i32 %23)
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = icmp eq i8* null, %25
br i1 %26, label %27, label %38
27: ; preds = %17
%28 = load i32, i32* %7
%29 = icmp sge i32 %28, 1
br i1 %29, label %30, label %37
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %5
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = load i32, i32* @stderr
%35 = call i8* (...) @dlerror()
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %30, %27
store i32 0, i32* %4
br label %74
38: ; preds = %17
%39 = load i8*, i8** %8
%40 = load i8*, i8** %6
%41 = call i8* @dlsym(i8* %39, i8* %40)
%42 = bitcast void ()** %9 to i8**
store i8* %41, i8** %42
%43 = load void ()*, void ()** %9
%44 = icmp eq void ()* null, %43
br i1 %44, label %48, label %45
45: ; preds = %38
%46 = call i8* (...) @dlerror()
store i8* %46, i8** %10
%47 = icmp ne i8* null, %46
br i1 %47, label %48, label %57
48: ; preds = %45, %38
%49 = load i32, i32* %7
%50 = icmp sge i32 %49, 1
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = load i32, i32* @stderr
%53 = load i8*, i8** %6
%54 = load i8*, i8** %5
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %53, i8* %54)
br label %56
56: ; preds = %51, %48
store i32 0, i32* %4
br label %74
57: ; preds = %45
%58 = load i32, i32* %7
%59 = icmp sge i32 %58, 1
br i1 %59, label %60, label %64
60: ; preds = %57
%61 = load i32, i32* @stderr
%62 = load i8*, i8** %6
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %62)
br label %64
64: ; preds = %60, %57
%65 = load void ()*, void ()** %9
%66 = load i32, i32* %7
%67 = icmp sge i32 %66, 1
br i1 %67, label %68, label %71
68: ; preds = %64
%69 = load i32, i32* @stderr
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %71
71: ; preds = %68, %64
%72 = load i8*, i8** %8
%73 = call i32 @dlclose(i8* %72)
store i32 1, i32* %4
br label %74
74: ; preds = %71, %56, %37
%75 = load i32, i32* %4
ret i32 %75
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [35 x i8] c"Requested %s to be loaded and run\0A\00"
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@RTLD_DEEPBIND = global i32 0
@.str.1 = [19 x i8] c"Load of %s failed\0A\00"
@.str.2 = [11 x i8] c"Error: %s\0A\00"
@.str.3 = [31 x i8] c"Entry point %s not found in %s\00"
@.str.4 = [24 x i8] c"Calling entry point %s\0A\00"
@.str.5 = [22 x i8] c"Done, closing handle\0A\00"
define i32 @LoadAndRun(i8* %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 1
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %8
8: ; preds = %5, %3
%9 = load i32, i32* @RTLD_NOW
%10 = load i32, i32* @RTLD_LOCAL
%11 = or i32 %10, %9
%12 = load i32, i32* @RTLD_DEEPBIND
%13 = or i32 %11, %12
%14 = tail call i8* @dlopen(i8* %0, i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %24
16: ; preds = %8
%17 = icmp sgt i32 %2, 0
br i1 %17, label %18, label %46
18: ; preds = %16
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%21 = load i32, i32* @stderr
%22 = tail call i8* (...) @dlerror()
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %22)
br label %46
24: ; preds = %8
%25 = tail call i8* @dlsym(i8* %14, i8* %1)
%26 = bitcast i8* %25 to void ()*
%27 = icmp eq i8* %25, null
br i1 %27, label %31, label %28
28: ; preds = %24
%29 = tail call i8* (...) @dlerror()
%30 = icmp eq i8* %29, null
br i1 %30, label %36, label %31
31: ; preds = %28, %24
%32 = icmp sgt i32 %2, 0
br i1 %32, label %33, label %46
33: ; preds = %31
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %1, i8* %0)
br label %46
36: ; preds = %28
%37 = icmp sgt i32 %2, 0
br i1 %37, label %38, label %43
38: ; preds = %36
%39 = load i32, i32* @stderr
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %1)
tail call void %26()
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %44
43: ; preds = %36
tail call void %26()
br label %44
44: ; preds = %43, %38
%45 = tail call i32 @dlclose(i8* %14)
br label %46
46: ; preds = %31, %33, %16, %18, %44
%47 = phi i32 [ 1, %44 ], [ 0, %18 ], [ 0, %16 ], [ 0, %33 ], [ 0, %31 ]
ret i32 %47
}