#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_LAZY;
int RTLD_LOCAL;
char *dlerror();
void *dlopen(char const *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ stderr;
void *do_dlopen(const char *pathname) {
  int mode = RTLD_LAZY | RTLD_LOCAL;
  void *handle = dlopen(pathname, mode);
  if (handle == NULL) {
    fprintf(stderr, "dlopen failed for %s: %s", pathname, dlerror());
    exit(1);
  } else {
    return handle;
  }
}


x86 assembly (clang, -O0):
<do_dlopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    0x2e6c(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b7 <do_dlopen+0x77>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xe58(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_dlopen>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e8b(%rip),%rax
mov    0x2e74(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1169 <do_dlopen+0x29>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e70(%rip),%rax
mov    (%rax),%ebp
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe80(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@RTLD_LAZY = global i32 0
@RTLD_LOCAL = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"dlopen failed for %s: %s\00"
define i8* @do_dlopen(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i32, i32* @RTLD_LAZY
%6 = load i32, i32* @RTLD_LOCAL
%7 = or i32 %5, %6
store i32 %7, i32* %3
%8 = load i8*, i8** %2
%9 = load i32, i32* %3
%10 = call i8* @dlopen(i8* %8, i32 %9)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %19
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %2
%16 = call i8* (...) @dlerror()
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %1
%20 = load i8*, i8** %4
ret i8* %20
}

LLVM IR (-O2):
@RTLD_LAZY = global i32 0
@RTLD_LOCAL = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"dlopen failed for %s: %s\00"
define i8* @do_dlopen(i8* %0) {
%2 = load i32, i32* @RTLD_LAZY
%3 = load i32, i32* @RTLD_LOCAL
%4 = or i32 %3, %2
%5 = tail call i8* @dlopen(i8* %0, i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i8* (...) @dlerror()
%10 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
ret i8* %5
}