#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;
int /*<<< orphan*/ callRun(void *);
int dlclose(void *);
char *dlerror();
void *dlopen(char const *, int);
int /*<<< orphan*/ printf(char *, char const *, ...);
void runLib(const char *fileName) {
  printf("%s: Loading\n", fileName);
  void *libHandle = dlopen(fileName, RTLD_NOW | RTLD_LOCAL);
  if (libHandle == NULL) {
    printf("%s: Trouble loading: %s\n\n", fileName, dlerror());
    return;
  }
  printf("%s: Running\n", fileName);
  callRun(libHandle);
  printf("%s: Closing\n", fileName);
  int result = dlclose(libHandle);
  if (result != 0) {
    printf("%s: Trouble closing: %s\n\n", fileName, dlerror());
  }
  printf("%s: Done.\n\n", fileName);
}


x86 assembly (clang, -O0):
<runLib>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    0x2e4e(%rip),%rax
or     (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c1 <runLib+0x71>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123a <runLib+0xea>
mov    -0x8(%rbp),%rsi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <callRun@plt>
mov    -0x8(%rbp),%rsi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1070 <dlclose@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1228 <runLib+0xd8>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runLib>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xea2(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e79(%rip),%rax
mov    0x2e5a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
je     11f3 <runLib+0xa3>
mov    %rax,%rbx
lea    0xe96(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <callRun@plt>
lea    0xe8a(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1070 <dlclose@plt>
test   %eax,%eax
je     11db <runLib+0x8b>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe73(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe79(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <printf@plt>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe0c(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"%s: Loading\0A\00"
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@.str.1 = [26 x i8] c"%s: Trouble loading: %s\0A\0A\00"
@.str.2 = [13 x i8] c"%s: Running\0A\00"
@.str.3 = [13 x i8] c"%s: Closing\0A\00"
@.str.4 = [26 x i8] c"%s: Trouble closing: %s\0A\0A\00"
@.str.5 = [12 x i8] c"%s: Done.\0A\0A\00"
define void @runLib(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = load i8*, i8** %2
%8 = load i32, i32* @RTLD_NOW
%9 = load i32, i32* @RTLD_LOCAL
%10 = or i32 %8, %9
%11 = call i8* @dlopen(i8* %7, i32 %10)
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = call i8* (...) @dlerror()
%17 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %15, i8* %16)
br label %36
18: ; preds = %1
%19 = load i8*, i8** %2
%20 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = load i8*, i8** %3
%22 = call i32 @callRun(i8* %21)
%23 = load i8*, i8** %2
%24 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %3
%26 = call i32 @dlclose(i8* %25)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %18
%30 = load i8*, i8** %2
%31 = call i8* (...) @dlerror()
%32 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %30, i8* %31)
br label %33
33: ; preds = %29, %18
%34 = load i8*, i8** %2
%35 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %33, %14
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"%s: Loading\0A\00"
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@.str.1 = [26 x i8] c"%s: Trouble loading: %s\0A\0A\00"
@.str.2 = [13 x i8] c"%s: Running\0A\00"
@.str.3 = [13 x i8] c"%s: Closing\0A\00"
@.str.4 = [26 x i8] c"%s: Trouble closing: %s\0A\0A\00"
@.str.5 = [12 x i8] c"%s: Done.\0A\0A\00"
define void @runLib(i8* %0) {
%2 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = load i32, i32* @RTLD_NOW
%4 = load i32, i32* @RTLD_LOCAL
%5 = or i32 %4, %3
%6 = tail call i8* @dlopen(i8* %0, i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = tail call i8* (...) @dlerror()
%10 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %9)
br label %22
11: ; preds = %1
%12 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%13 = tail call i32 @callRun(i8* %6)
%14 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%15 = tail call i32 @dlclose(i8* %6)
%16 = icmp eq i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %11
%18 = tail call i8* (...) @dlerror()
%19 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %18)
br label %20
20: ; preds = %17, %11
%21 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %22
22: ; preds = %20, %8
ret void
}