#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*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int);
void *dlsym(void *, char *);
void *load_obj(char *fn, void **odl, char **oerr) {
  void *dl;
  char *err;
  dlerror();
  dl = dlopen(fn, RTLD_LOCAL | RTLD_NOW);
  if (!dl) {
    if (oerr) {
      *oerr = dlerror();
    }
    goto out_dl;
  }
  if (odl) {
    *odl = dl;
  }
  dlerror();
  void *vt = dlsym(dl, "_plugin_vtable");
  err = dlerror();
  if (err) {
    if (oerr) {
      *oerr = err;
    }
    goto out_sym;
  }
  return vt;
out_sym:
  dlclose(dl);
out_dl:
  return NULL;
}


x86 assembly (clang, -O0):
<load_obj>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a6 <load_obj+0x66>
cmpq   $0x0,-0x20(%rbp)
je     11a1 <load_obj+0x61>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    121e <load_obj+0xde>
cmpq   $0x0,-0x18(%rbp)
je     11bc <load_obj+0x7c>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x28(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1208 <load_obj+0xc8>
cmpq   $0x0,-0x20(%rbp)
je     1203 <load_obj+0xc3>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    1215 <load_obj+0xd5>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1226 <load_obj+0xe6>
mov    -0x28(%rbp),%rdi
call   1060 <dlclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_obj>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    0x2e74(%rip),%rax
mov    0x2e85(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11b6 <load_obj+0x76>
mov    %rax,%rbx
test   %r15,%r15
je     117f <load_obj+0x3f>
mov    %rbx,(%r15)
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe73(%rip),%rsi
mov    %rbx,%rdi
call   1050 <dlsym@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
je     11cd <load_obj+0x8d>
test   %r14,%r14
je     11ac <load_obj+0x6c>
mov    %rax,(%r14)
mov    %rbx,%rdi
call   1060 <dlclose@plt>
jmp    11ca <load_obj+0x8a>
test   %r14,%r14
je     11ca <load_obj+0x8a>
xor    %r15d,%r15d
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rax,(%r14)
jmp    11cd <load_obj+0x8d>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@RTLD_LOCAL = global i32 0
@RTLD_NOW = global i32 0
@.str = [15 x i8] c"_plugin_vtable\00"
define i8* @load_obj(i8* %0, i8** writeonly %1, i8** writeonly %2) {
%4 = tail call i8* (...) @dlerror()
%5 = load i32, i32* @RTLD_LOCAL
%6 = load i32, i32* @RTLD_NOW
%7 = or i32 %6, %5
%8 = tail call i8* @dlopen(i8* %0, i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = icmp eq i8** %2, null
br i1 %11, label %27, label %12
12: ; preds = %10
%13 = tail call i8* (...) @dlerror()
store i8* %13, i8** %2
br label %27
14: ; preds = %3
%15 = icmp eq i8** %1, null
br i1 %15, label %17, label %16
16: ; preds = %14
store i8* %8, i8** %1
br label %17
17: ; preds = %16, %14
%18 = tail call i8* (...) @dlerror()
%19 = tail call i8* @dlsym(i8* %8, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%20 = tail call i8* (...) @dlerror()
%21 = icmp eq i8* %20, null
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = icmp eq i8** %2, null
br i1 %23, label %25, label %24
24: ; preds = %22
store i8* %20, i8** %2
br label %25
25: ; preds = %22, %24
%26 = tail call i32 @dlclose(i8* %8)
br label %27
27: ; preds = %25, %12, %10, %17
%28 = phi i8* [ %19, %17 ], [ null, %10 ], [ null, %12 ], [ null, %25 ]
ret i8* %28
}