#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*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "Shared Object not specified\n");
  }
  const char *filename = argv[1];
  fprintf(stdout, "dlopen(\"%s\",...);\n", filename);
  dlerror();
  void *handle = dlopen(filename, RTLD_LOCAL | RTLD_LAZY);
  char *err = dlerror();
  if (err) {
    fprintf(stderr, "ERROR: %s\n", err);
    return -1;
  } else {
    return 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)
cmpl   $0x2,-0x8(%rbp)
jge    1167 <main+0x37>
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
lea    0xea0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe96(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
or     (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f1 <main+0xc1>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <main+0xc8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     1152 <main+0x22>
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
lea    0xeb5(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x8(%rbx),%rbx
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    0xeb7(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    0x2e40(%rip),%rax
mov    0x2e49(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1040 <dlopen@plt>
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
je     11be <main+0x8e>
mov    0x2e3a(%rip),%rcx
mov    (%rcx),%edi
lea    0xe81(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"Shared Object not specified\0A\00"
@stdout = global i32 0
@.str.1 = [19 x i8] c"dlopen(\22%s\22,...);\0A\00"
@RTLD_LOCAL = global i32 0
@RTLD_LAZY = global i32 0
@.str.2 = [11 x i8] c"ERROR: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %11, %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %6
%18 = load i32, i32* @stdout
%19 = load i8*, i8** %6
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = call i8* (...) @dlerror()
%22 = load i8*, i8** %6
%23 = load i32, i32* @RTLD_LOCAL
%24 = load i32, i32* @RTLD_LAZY
%25 = or i32 %23, %24
%26 = call i8* @dlopen(i8* %22, i32 %25)
store i8* %26, i8** %7
%27 = call i8* (...) @dlerror()
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %34
30: ; preds = %14
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %8
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i32 -1, i32* %3
br label %35
34: ; preds = %14
store i32 0, i32* %3
br label %35
35: ; preds = %34, %30
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"Shared Object not specified\0A\00"
@stdout = global i32 0
@.str.1 = [19 x i8] c"dlopen(\22%s\22,...);\0A\00"
@RTLD_LOCAL = global i32 0
@RTLD_LAZY = global i32 0
@.str.2 = [11 x i8] c"ERROR: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %4, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @stdout
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%12 = tail call i8* (...) @dlerror()
%13 = load i32, i32* @RTLD_LOCAL
%14 = load i32, i32* @RTLD_LAZY
%15 = or i32 %14, %13
%16 = tail call i8* @dlopen(i8* %9, i32 %15)
%17 = tail call i8* (...) @dlerror()
%18 = icmp eq i8* %17, null
br i1 %18, label %22, label %19
19: ; preds = %7
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %17)
br label %22
22: ; preds = %7, %19
%23 = phi i32 [ -1, %19 ], [ 0, %7 ]
ret i32 %23
}