#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 */
scalar_t__ DebugModeOn();
int /*<<< orphan*/ RTLD_LAZY;
scalar_t__ dlerror();
void *dlopen(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           char const *);
int /*<<< orphan*/ stderr;
void *LoadObject(char const *sofile) {
  void *handle = dlopen(sofile, RTLD_LAZY);
  char const *load_error = (char *)dlerror();
  if (DebugModeOn() && handle == NULL)
    fprintf(stderr, "Failed to load %s - Error: %s\n", sofile, load_error);
  return handle;
}


x86 assembly (clang, -O0):
<LoadObject>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <DebugModeOn@plt>
cmp    $0x0,%rax
je     11a6 <LoadObject+0x66>
cmpq   $0x0,-0x10(%rbp)
jne    11a6 <LoadObject+0x66>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe5f(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<LoadObject>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <DebugModeOn@plt>
test   %rax,%rax
je     118f <LoadObject+0x4f>
test   %rbx,%rbx
jne    118f <LoadObject+0x4f>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1060 <fprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Failed to load %s - Error: %s\0A\00"
define i8* @LoadObject(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @RTLD_LAZY
%7 = call i8* @dlopen(i8* %5, i32 %6)
store i8* %7, i8** %3
%8 = call i64 (...) @dlerror()
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %4
%10 = call i64 (...) @DebugModeOn()
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %2
%18 = load i8*, i8** %4
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %18)
br label %20
20: ; preds = %15, %12, %1
%21 = load i8*, i8** %3
ret i8* %21
}

LLVM IR (-O2):
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Failed to load %s - Error: %s\0A\00"
define i8* @LoadObject(i8* %0) {
%2 = load i32, i32* @RTLD_LAZY
%3 = tail call i8* @dlopen(i8* %0, i32 %2)
%4 = tail call i64 (...) @dlerror()
%5 = tail call i64 (...) @DebugModeOn()
%6 = icmp ne i64 %5, 0
%7 = icmp eq i8* %3, null
%8 = select i1 %6, i1 %7, i1 false
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = inttoptr i64 %4 to i8*
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %10)
br label %13
13: ; preds = %9, %1
ret i8* %3
}