#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_GLOBAL;
int RTLD_LAZY;
char *dlerror();
void *dlopen(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
main(int argc, char *argv[]) {
  void *h;
  void (*s)();
  printf("loading: %s\n", argv[1]);
  h = dlopen(argv[1], RTLD_LAZY | RTLD_GLOBAL);
  if (h == 0) {
    fprintf(stderr, "dlopen failed: %s\n", dlerror());
    exit(-1);
  }
  printf("loaded...\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    0x2e54(%rip),%rax
or     (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d9 <main+0x89>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe43(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    %rsi,%rbx
mov    0x8(%rsi),%rsi
lea    0xe91(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbx),%rdi
mov    0x2e4f(%rip),%rax
mov    0x2e60(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <dlopen@plt>
test   %rax,%rax
jne    11c1 <main+0x61>
mov    0x2e3b(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe60(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    0xe58(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str = [13 x i8] c"loading: %s\0A\00"
@RTLD_LAZY = global i32 0
@RTLD_GLOBAL = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"dlopen failed: %s\0A\00"
@str = [10 x i8] c"loaded...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %4)
%6 = load i8*, i8** %3
%7 = load i32, i32* @RTLD_LAZY
%8 = load i32, i32* @RTLD_GLOBAL
%9 = or i32 %8, %7
%10 = tail call i8* @dlopen(i8* %6, i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i8* (...) @dlerror()
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 0)
unreachable
}