#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_NOLOAD;
int RTLD_NOW;
int /*<<< orphan*/ *dlopen(char *, int);
void *dlsym(void *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void stub1();
int load_plugin(char *path) {
  if (dlopen(path, RTLD_NOW | RTLD_NOLOAD) != NULL) {
    return 127;
  }
  void *plugin = dlopen(path, RTLD_NOW);
  if (plugin != NULL) {
    void (*load)(void);
    *(void **)&load = dlsym(plugin, "plugin_load");
    if (load != NULL) {
      (*load)();
    } else {
      fputs("Plugin didn't have a plugin_load function\n", stderr);
      return 1;
    }
  } else {
    fprintf(stderr, "Unable to open  plugin file '%s'\n", path);
    return 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<load_plugin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
call   1040 <dlopen@plt>
cmp    $0x0,%rax
je     117d <load_plugin+0x3d>
movl   $0x7f,-0x4(%rbp)
jmp    1217 <load_plugin+0xd7>
mov    -0x10(%rbp),%rdi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11eb <load_plugin+0xab>
mov    -0x18(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11c5 <load_plugin+0x85>
call   *-0x20(%rbp)
jmp    11e6 <load_plugin+0xa6>
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
lea    0xe37(%rip),%rdi
call   1030 <fputs@plt>
movl   $0x1,-0x4(%rbp)
jmp    1217 <load_plugin+0xd7>
jmp    1210 <load_plugin+0xd0>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe38(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1217 <load_plugin+0xd7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_plugin>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e9a(%rip),%r14
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
call   1040 <dlopen@plt>
mov    %rax,%rcx
mov    $0x7f,%eax
test   %rcx,%rcx
jne    11d3 <load_plugin+0x93>
mov    (%r14),%esi
mov    %rbx,%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1197 <load_plugin+0x57>
lea    0xe7d(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11b6 <load_plugin+0x76>
call   *%rbx
jmp    11cb <load_plugin+0x8b>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    $0x2,%eax
jmp    11d3 <load_plugin+0x93>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    0xe46(%rip),%rdi
call   1030 <fputs@plt>
xor    %eax,%eax
test   %rbx,%rbx
sete   %al
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@RTLD_NOW = global i32 0
@RTLD_NOLOAD = global i32 0
@.str = [12 x i8] c"plugin_load\00"
@.str.1 = [43 x i8] c"Plugin didn't have a plugin_load function\0A\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"Unable to open plugin file '%s'\0A\00"
define i32 @load_plugin(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca void ()*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @RTLD_NOW
%8 = load i32, i32* @RTLD_NOLOAD
%9 = or i32 %7, %8
%10 = call i32* @dlopen(i8* %6, i32 %9)
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 127, i32* %2
br label %37
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = load i32, i32* @RTLD_NOW
%16 = call i32* @dlopen(i8* %14, i32 %15)
%17 = bitcast i32* %16 to i8*
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %32
20: ; preds = %13
%21 = load i8*, i8** %4
%22 = call i8* @dlsym(i8* %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%23 = bitcast void ()** %5 to i8**
store i8* %22, i8** %23
%24 = load void ()*, void ()** %5
%25 = icmp ne void ()* %24, null
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = load void ()*, void ()** %5
br label %31
28: ; preds = %20
%29 = load i32, i32* @stderr
%30 = call i32 @fputs(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %29)
store i32 1, i32* %2
br label %37
31: ; preds = %26
br label %36
32: ; preds = %13
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %3
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %34)
store i32 2, i32* %2
br label %37
36: ; preds = %31
store i32 0, i32* %2
br label %37
37: ; preds = %36, %32, %28, %12
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@RTLD_NOW = global i32 0
@RTLD_NOLOAD = global i32 0
@.str = [12 x i8] c"plugin_load\00"
@.str.1 = [43 x i8] c"Plugin didn't have a plugin_load function\0A\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"Unable to open plugin file '%s'\0A\00"
define i32 @load_plugin(i8* %0) {
%2 = load i32, i32* @RTLD_NOW
%3 = load i32, i32* @RTLD_NOLOAD
%4 = or i32 %3, %2
%5 = tail call i32* @dlopen(i8* %0, i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %25
7: ; preds = %1
%8 = load i32, i32* @RTLD_NOW
%9 = tail call i32* @dlopen(i8* %0, i32 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %22, label %11
11: ; preds = %7
%12 = bitcast i32* %9 to i8*
%13 = tail call i8* @dlsym(i8* %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = bitcast i8* %13 to void ()*
tail call void %16()
br label %20
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = tail call i32 @fputs(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %15, %17
%21 = zext i1 %14 to i32
br label %25
22: ; preds = %7
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %25
25: ; preds = %20, %22, %1
%26 = phi i32 [ 127, %1 ], [ 2, %22 ], [ %21, %20 ]
ret i32 %26
}