#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 /*<<< orphan*/ RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ stdout;
int loadso(const char *file) {
  void *h = dlopen(file, RTLD_LAZY);
  if (NULL != h) {
    fprintf(stdout, "load file %s Ok, handle=%p\n", file, h);
    dlclose(h);
  } else {
    fprintf(stdout, "load file %s failed, err=%s\n", file, dlerror());
  }
  return 0;
}


x86 assembly (clang, -O0):
<loadso>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     1199 <loadso+0x59>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe7a(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <dlclose@plt>
jmp    11ca <loadso+0x8a>
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rdx
mov    %rax,%rcx
lea    0xe57(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadso>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,%rbp
mov    0x2e81(%rip),%rax
mov    (%rax),%r14d
test   %rbp,%rbp
je     1186 <loadso+0x46>
lea    0xe92(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %rbp,%rcx
call   1050 <fprintf@plt>
mov    %rbp,%rdi
call   1060 <dlclose@plt>
jmp    11a2 <loadso+0x62>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe88(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@RTLD_LAZY = global i32 0
@stdout = global i32 0
@.str = [28 x i8] c"load file %s Ok, handle=%p\0A\00"
@.str.1 = [29 x i8] c"load file %s failed, err=%s\0A\00"
define i32 @loadso(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = load i32, i32* @RTLD_LAZY
%6 = call i8* @dlopen(i8* %4, i32 %5)
store i8* %6, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* null, %7
br i1 %8, label %9, label %16
9: ; preds = %1
%10 = load i32, i32* @stdout
%11 = load i8*, i8** %2
%12 = load i8*, i8** %3
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %12)
%14 = load i8*, i8** %3
%15 = call i32 @dlclose(i8* %14)
br label %21
16: ; preds = %1
%17 = load i32, i32* @stdout
%18 = load i8*, i8** %2
%19 = call i8* (...) @dlerror()
%20 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %18, i8* %19)
br label %21
21: ; preds = %16, %9
ret i32 0
}

LLVM IR (-O2):
@RTLD_LAZY = global i32 0
@stdout = global i32 0
@.str = [28 x i8] c"load file %s Ok, handle=%p\0A\00"
@.str.1 = [29 x i8] c"load file %s failed, err=%s\0A\00"
define i32 @loadso(i8* %0) {
%2 = load i32, i32* @RTLD_LAZY
%3 = tail call i8* @dlopen(i8* %0, i32 %2)
%4 = icmp eq i8* %3, null
%5 = load i32, i32* @stdout
br i1 %4, label %9, label %6
6: ; preds = %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %3)
%8 = tail call i32 @dlclose(i8* %3)
br label %12
9: ; preds = %1
%10 = tail call i8* (...) @dlerror()
%11 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %10)
br label %12
12: ; preds = %9, %6
ret i32 0
}