#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_GLOBAL;
int RTLD_NOW;
char *dlerror();
void *dlopen(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  void *libhandle;
  char *library_name = "./pluginv2.so";
  printf("mainv2: inside main, loading pluginv2.so from mainv2.so\n");
  libhandle = dlopen(library_name, RTLD_NOW | RTLD_GLOBAL);
  if (libhandle == NULL)
    printf("Failed to load %s: %s\n", library_name, dlerror());
  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)
lea    0xeb3(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xeb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    0x2e75(%rip),%rax
or     (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ad <main+0x7d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xedd(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e8c(%rip),%rax
mov    0x2e8d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
jne    118f <main+0x4f>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe90(%rip),%rdi
lea    0xe7b(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [14 x i8] c"./pluginv2.so\00"
@.str.1 = [57 x i8] c"mainv2: inside main, loading pluginv2.so from mainv2.so\0A\00"
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@.str.2 = [23 x i8] c"Failed to load %s: %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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %7
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0))
%9 = load i8*, i8** %7
%10 = load i32, i32* @RTLD_NOW
%11 = load i32, i32* @RTLD_GLOBAL
%12 = or i32 %10, %11
%13 = call i8* @dlopen(i8* %9, i32 %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i8*, i8** %7
%18 = call i8* (...) @dlerror()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %17, i8* %18)
br label %20
20: ; preds = %16, %2
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"./pluginv2.so\00"
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@.str.2 = [23 x i8] c"Failed to load %s: %s\0A\00"
@str = [56 x i8] c"mainv2: inside main, loading pluginv2.so from mainv2.so\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str, i64 0, i64 0))
%4 = load i32, i32* @RTLD_NOW
%5 = load i32, i32* @RTLD_GLOBAL
%6 = or i32 %5, %4
%7 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i8* (...) @dlerror()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %9, %2
ret i32 0
}