#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 /*<<< orphan*/ RTLD_NOW;
int /*<<< orphan*/ dlclose(void *);
void *dlopen(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ dlsym(void *, char *);
int /*<<< orphan*/ puts(char *);
void __attribute__((__constructor__)) constr(void) {
  void *handle;
  handle = dlopen(NULL, RTLD_NOW);
  if (handle == NULL) {
    puts("Cannot get handle to own object");
    return;
  }
  dlsym(handle, "main");
  puts("called dlsym() to get main");
  dlclose(handle);
}


x86 assembly (clang, -O0):
<constr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edi
call   1040 <dlopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    117a <constr+0x3a>
lea    0xe90(%rip),%rdi
call   1030 <puts@plt>
jmp    119f <constr+0x5f>
mov    -0x8(%rbp),%rdi
lea    0xeb1(%rip),%rsi
call   1050 <dlsym@plt>
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%rdi
call   1060 <dlclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<constr>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1040 <dlopen@plt>
test   %rax,%rax
je     117d <constr+0x3d>
mov    %rax,%rbx
lea    0xed6(%rip),%rsi
mov    %rax,%rdi
call   1050 <dlsym@plt>
lea    0xeb1(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
pop    %rbx
jmp    1060 <dlclose@plt>
lea    0xe7c(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>

LLVM IR (-O0):
@RTLD_NOW = global i32 0
@.str = [32 x i8] c"Cannot get handle to own object\00"
@.str.1 = [5 x i8] c"main\00"
@.str.2 = [27 x i8] c"called dlsym() to get main\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @constr, i8* null }]
define void @constr() {
%1 = alloca i8*
%2 = load i32, i32* @RTLD_NOW
%3 = call i8* @dlopen(i32* null, i32 %2)
store i8* %3, i8** %1
%4 = load i8*, i8** %1
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @puts(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %14
8: ; preds = %0
%9 = load i8*, i8** %1
%10 = call i32 @dlsym(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @puts(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%12 = load i8*, i8** %1
%13 = call i32 @dlclose(i8* %12)
br label %14
14: ; preds = %8, %6
ret void
}

LLVM IR (-O2):
@RTLD_NOW = global i32 0
@.str = [32 x i8] c"Cannot get handle to own object\00"
@.str.1 = [5 x i8] c"main\00"
@.str.2 = [27 x i8] c"called dlsym() to get main\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @constr, i8* null }]
define void @constr() {
%1 = load i32, i32* @RTLD_NOW
%2 = tail call i8* @dlopen(i32* null, i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %10
6: ; preds = %0
%7 = tail call i32 @dlsym(i8* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i32 @dlclose(i8* %2)
br label %10
10: ; preds = %6, %4
ret void
}