#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_NOW;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(int /*<<< orphan*/ *, int);
void *dlsym(void *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int main(void) {
  void *lib = dlopen(NULL, RTLD_NOW | RTLD_GLOBAL);
  void *symbol;
  if (lib == NULL) {
    fprintf(stderr, "Could not open self-executable with dlopen(NULL) !!: %s\n",
            dlerror());
    return 1;
  }
  symbol = dlsym(lib, "foo");
  if (symbol == NULL) {
    fprintf(stderr, "Could not lookup symbol inside executable !!: %s\n",
            dlerror());
    return 2;
  }
  dlclose(lib);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
xor    %eax,%eax
mov    %eax,%edi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ba <main+0x6a>
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe57(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    121a <main+0xca>
mov    -0x10(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120a <main+0xba>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe44(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    121a <main+0xca>
mov    -0x10(%rbp),%rdi
call   1070 <dlclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e88(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edi,%edi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1192 <main+0x42>
mov    %rax,%rbx
lea    0xec0(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     11ba <main+0x6a>
mov    %rbx,%rdi
call   1070 <dlclose@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e37(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe57(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
ret
mov    0x2e0f(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe6c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x2,%eax
pop    %rbx
ret

LLVM IR (-O0):
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@stderr = global i32 0
@.str = [57 x i8] c"Could not open self-executable with dlopen(NULL) !!: %s\0A\00"
@.str.1 = [4 x i8] c"foo\00"
@.str.2 = [50 x i8] c"Could not lookup symbol inside executable !!: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
%4 = load i32, i32* @RTLD_NOW
%5 = load i32, i32* @RTLD_GLOBAL
%6 = or i32 %4, %5
%7 = call i8* @dlopen(i32* null, i32 %6)
store i8* %7, i8** %2
%8 = load i8*, i8** %2
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i8* (...) @dlerror()
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %12)
store i32 1, i32* %1
br label %26
14: ; preds = %0
%15 = load i8*, i8** %2
%16 = call i8* @dlsym(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = call i8* (...) @dlerror()
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %21)
store i32 2, i32* %1
br label %26
23: ; preds = %14
%24 = load i8*, i8** %2
%25 = call i32 @dlclose(i8* %24)
store i32 0, i32* %1
br label %26
26: ; preds = %23, %19, %10
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@stderr = global i32 0
@.str = [57 x i8] c"Could not open self-executable with dlopen(NULL) !!: %s\0A\00"
@.str.1 = [4 x i8] c"foo\00"
@.str.2 = [50 x i8] c"Could not lookup symbol inside executable !!: %s\0A\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_NOW
%2 = load i32, i32* @RTLD_GLOBAL
%3 = or i32 %2, %1
%4 = tail call i8* @dlopen(i32* null, i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = tail call i8* (...) @dlerror()
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %19
10: ; preds = %0
%11 = tail call i8* @dlsym(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i8* (...) @dlerror()
%16 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %15)
br label %19
17: ; preds = %10
%18 = tail call i32 @dlclose(i8* %4)
br label %19
19: ; preds = %17, %13, %6
%20 = phi i32 [ 1, %6 ], [ 2, %13 ], [ 0, %17 ]
ret i32 %20
}