#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  void *handle;
  void (*setprogname)(const char *);
  char *(*getprogname)(void);
  char *error;
  handle = dlopen("libbsd.so", RTLD_LAZY);
  if (!handle) {
    fprintf(stderr, "%s\n", dlerror());
    exit(EXIT_FAILURE);
  }
  dlerror();
  *(void **)(&setprogname) = dlsym(handle, "setprogname");
  if ((error = dlerror()) != NULL) {
    fprintf(stderr, "%s\n", error);
    exit(EXIT_FAILURE);
  }
  *(void **)(&getprogname) = dlsym(handle, "getprogname");
  if ((error = dlerror()) != NULL) {
    fprintf(stderr, "%s\n", error);
    exit(EXIT_FAILURE);
  }
  setprogname("dlopenex");
  printf("My name is '%s'.\n", getprogname());
  dlclose(handle);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11dd <main+0x6d>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xe40(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x18(%rbp),%rdi
lea    0xe1f(%rip),%rsi
call   1070 <dlsym@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1234 <main+0xc4>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xde9(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xddb(%rip),%rsi
call   1070 <dlsym@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1284 <main+0x114>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xd99(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xd9b(%rip),%rdi
call   *-0x20(%rbp)
call   *-0x28(%rbp)
mov    %rax,%rsi
lea    0xd94(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <dlclose@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
jne    11ad <main+0x3d>
mov    0x2e4a(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe64(%rip),%rsi
mov    %ebx,%edi
jmp    1235 <main+0xc5>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe50(%rip),%rsi
mov    %rbx,%rdi
call   1070 <dlsym@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
jne    1225 <main+0xb5>
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdi
call   1070 <dlsym@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
jne    1225 <main+0xb5>
lea    0xe2c(%rip),%rdi
call   *%r14
call   *%r15
lea    0xe28(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1090 <dlclose@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2db4(%rip),%rcx
mov    (%rcx),%edi
lea    0xdd5(%rip),%rsi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"libbsd.so\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [12 x i8] c"setprogname\00"
@.str.3 = [12 x i8] c"getprogname\00"
@.str.4 = [9 x i8] c"dlopenex\00"
@.str.5 = [18 x i8] c"My name is '%s'.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca void (i8*)*
%8 = alloca i8* ()*
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @RTLD_LAZY
%11 = call i8* @dlopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %20, label %14
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i8* (...) @dlerror()
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = call i8* (...) @dlerror()
%22 = load i8*, i8** %6
%23 = call i8* @dlsym(i8* %22, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%24 = bitcast void (i8*)** %7 to i8**
store i8* %23, i8** %24
%25 = call i8* (...) @dlerror()
store i8* %25, i8** %9
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %33
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %9
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %20
%34 = load i8*, i8** %6
%35 = call i8* @dlsym(i8* %34, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%36 = bitcast i8* ()** %8 to i8**
store i8* %35, i8** %36
%37 = call i8* (...) @dlerror()
store i8* %37, i8** %9
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %45
39: ; preds = %33
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %9
%42 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %41)
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %33
%46 = load void (i8*)*, void (i8*)** %7
%47 = load i8* ()*, i8* ()** %8
%48 = call i8* %47()
%49 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %48)
%50 = load i8*, i8** %6
%51 = call i32 @dlclose(i8* %50)
%52 = load i32, i32* @EXIT_SUCCESS
%53 = call i32 @exit(i32 %52)
unreachable
}

LLVM IR (-O2):
@.str = [10 x i8] c"libbsd.so\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [12 x i8] c"setprogname\00"
@.str.3 = [12 x i8] c"getprogname\00"
@.str.4 = [9 x i8] c"dlopenex\00"
@.str.5 = [18 x i8] c"My name is '%s'.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @RTLD_LAZY
%4 = tail call i8* @dlopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i8* (...) @dlerror()
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = tail call i8* (...) @dlerror()
%14 = tail call i8* @dlsym(i8* %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i8* (...) @dlerror()
%16 = icmp eq i8* %15, null
br i1 %16, label %22, label %17
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %12
%23 = tail call i8* @dlsym(i8* %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%24 = tail call i8* (...) @dlerror()
%25 = icmp eq i8* %24, null
br i1 %25, label %31, label %26
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %22
%32 = bitcast i8* %23 to i8* ()*
%33 = bitcast i8* %14 to void (i8*)*
tail call void %33(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%34 = tail call i8* %32()
%35 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %34)
%36 = tail call i32 @dlclose(i8* %4)
%37 = load i32, i32* @EXIT_SUCCESS
%38 = tail call i32 @exit(i32 %37)
unreachable
}