#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*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ puts(char *);
void __attribute__((__constructor__)) constr(void) {
  void *handle;
  void *m;
  handle = dlopen(NULL, RTLD_NOW);
  if (handle == NULL) {
    puts("Cannot get handle to own object");
    return;
  }
  m = 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    11a3 <constr+0x63>
mov    -0x8(%rbp),%rdi
lea    0xeb1(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x10(%rbp)
lea    0xe8b(%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 = alloca i8*
%3 = load i32, i32* @RTLD_NOW
%4 = call i8* @dlopen(i32* null, i32 %3)
store i8* %4, i8** %1
%5 = load i8*, i8** %1
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @puts(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %15
9: ; preds = %0
%10 = load i8*, i8** %1
%11 = call i8* @dlsym(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %11, i8** %2
%12 = call i32 @puts(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%13 = load i8*, i8** %1
%14 = call i32 @dlclose(i8* %13)
br label %15
15: ; preds = %9, %7
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 i8* @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
}