#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 */
struct esh_plugin {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
struct esh_plugin *dlsym(void *, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
struct esh_plugin *load_plugin(char *modname) {
  printf("Loading %s ...", modname);
  fflush(stdout);
  void *handle = dlopen(modname, RTLD_LAZY);
  if (handle == NULL) {
    fprintf(stderr, "Could not open %s: %s\n", modname, dlerror());
    return NULL;
  }
  struct esh_plugin *p = dlsym(handle, "esh_module");
  if (p == NULL) {
    fprintf(stderr, "%s does not define %s\n", modname, "esh_module");
    dlclose(handle);
    return NULL;
  }
  printf("done.\n");
  return p;
}


x86 assembly (clang, -O0):
<load_plugin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11fb <load_plugin+0x8b>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xe26(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1266 <load_plugin+0xf6>
mov    -0x18(%rbp),%rdi
lea    0xe20(%rip),%rsi
call   1080 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1250 <load_plugin+0xe0>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe03(%rip),%rsi
lea    0xdf1(%rip),%rcx
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <dlclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1266 <load_plugin+0xf6>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_plugin>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe71(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     11e5 <load_plugin+0x65>
mov    %rax,%rbx
lea    0xe5f(%rip),%rsi
mov    %rax,%rdi
call   1090 <dlsym@plt>
test   %rax,%rax
je     120e <load_plugin+0x8e>
mov    %rax,%r15
lea    0xe6a(%rip),%rdi
call   1040 <puts@plt>
jmp    1238 <load_plugin+0xb8>
mov    0x2df4(%rip),%rax
mov    (%rax),%ebx
xor    %r15d,%r15d
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe10(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
jmp    1238 <load_plugin+0xb8>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
lea    0xe01(%rip),%rcx
mov    %r14,%rdx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   10a0 <dlclose@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.esh_plugin = type { i32 }
@.str = [15 x i8] c"Loading %s ...\00"
@stdout = global i32 0
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"Could not open %s: %s\0A\00"
@.str.2 = [11 x i8] c"esh_module\00"
@.str.3 = [23 x i8] c"%s does not define %s\0A\00"
@.str.4 = [7 x i8] c"done.\0A\00"
define %struct.esh_plugin* @load_plugin(i8* %0) {
%2 = alloca %struct.esh_plugin*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.esh_plugin*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @stdout
%9 = call i32 @fflush(i32 %8)
%10 = load i8*, i8** %3
%11 = load i32, i32* @RTLD_LAZY
%12 = call i8* @dlopen(i8* %10, i32 %11)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %3
%18 = call i8* (...) @dlerror()
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %18)
store %struct.esh_plugin* null, %struct.esh_plugin** %2
br label %34
20: ; preds = %1
%21 = load i8*, i8** %4
%22 = call %struct.esh_plugin* @dlsym(i8* %21, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store %struct.esh_plugin* %22, %struct.esh_plugin** %5
%23 = load %struct.esh_plugin*, %struct.esh_plugin** %5
%24 = icmp eq %struct.esh_plugin* %23, null
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %3
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %27, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%29 = load i8*, i8** %4
%30 = call i32 @dlclose(i8* %29)
store %struct.esh_plugin* null, %struct.esh_plugin** %2
br label %34
31: ; preds = %20
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%33 = load %struct.esh_plugin*, %struct.esh_plugin** %5
store %struct.esh_plugin* %33, %struct.esh_plugin** %2
br label %34
34: ; preds = %31, %25, %15
%35 = load %struct.esh_plugin*, %struct.esh_plugin** %2
ret %struct.esh_plugin* %35
}

LLVM IR (-O2):
%struct.esh_plugin = type { i32 }
@.str = [15 x i8] c"Loading %s ...\00"
@stdout = global i32 0
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"Could not open %s: %s\0A\00"
@.str.2 = [11 x i8] c"esh_module\00"
@.str.3 = [23 x i8] c"%s does not define %s\0A\00"
@str = [6 x i8] c"done.\00"
define %struct.esh_plugin* @load_plugin(i8* %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = load i32, i32* @stdout
%4 = tail call i32 @fflush(i32 %3)
%5 = load i32, i32* @RTLD_LAZY
%6 = tail call i8* @dlopen(i8* %0, i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = tail call i8* (...) @dlerror()
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %10)
br label %21
12: ; preds = %1
%13 = tail call %struct.esh_plugin* @dlsym(i8* %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq %struct.esh_plugin* %13, null
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @dlclose(i8* %6)
br label %21
19: ; preds = %12
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %21
21: ; preds = %15, %19, %8
%22 = phi %struct.esh_plugin* [ null, %8 ], [ null, %15 ], [ %13, %19 ]
ret %struct.esh_plugin* %22
}