#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_LAZY;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  void (*zxd)(void);
  char *err;
  void *handle = dlopen("./libzx.so", RTLD_LAZY);
  if (handle == NULL) {
    printf("dlopen failed:%s\n", dlerror());
    return -1;
  }
  dlerror();
  *(void **)&zxd = dlsym(handle, "zxdate");
  if ((err = dlerror()) != NULL) {
    printf("dlsym failed\n");
    perror(err);
    return -1;
  }
  zxd();
  dlclose(handle);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b7 <main+0x57>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <main+0xbd>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x20(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   1070 <dlsym@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     120a <main+0xaa>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <main+0xbd>
call   *-0x10(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <dlclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rbx,%rbx
je     11c8 <main+0x58>
lea    0xe7b(%rip),%rsi
mov    %rbx,%rdi
call   1080 <dlsym@plt>
mov    %rax,%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
jne    11eb <main+0x7b>
call   *%r14
mov    %rbx,%rdi
call   1090 <dlclose@plt>
jmp    11de <main+0x6e>
lea    0xe3c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%r15
lea    0xe2f(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1070 <perror@plt>
jmp    11d9 <main+0x69>

LLVM IR (-O0):
@.str = [11 x i8] c"./libzx.so\00"
@RTLD_LAZY = global i32 0
@.str.1 = [18 x i8] c"dlopen failed:%s\0A\00"
@.str.2 = [7 x i8] c"zxdate\00"
@.str.3 = [14 x i8] c"dlsym failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca void ()*
%3 = alloca i8*
%4 = alloca i8*
store i32 0, i32* %1
%5 = load i32, i32* @RTLD_LAZY
%6 = call i8* @dlopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i8* (...) @dlerror()
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %10)
store i32 -1, i32* %1
br label %27
12: ; preds = %0
%13 = call i8* (...) @dlerror()
%14 = load i8*, i8** %4
%15 = call i8* @dlsym(i8* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%16 = bitcast void ()** %2 to i8**
store i8* %15, i8** %16
%17 = call i8* (...) @dlerror()
store i8* %17, i8** %3
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %12
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%21 = load i8*, i8** %3
%22 = call i32 @perror(i8* %21)
store i32 -1, i32* %1
br label %27
23: ; preds = %12
%24 = load void ()*, void ()** %2
%25 = load i8*, i8** %4
%26 = call i32 @dlclose(i8* %25)
store i32 0, i32* %1
br label %27
27: ; preds = %23, %19, %9
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [11 x i8] c"./libzx.so\00"
@RTLD_LAZY = global i32 0
@.str.1 = [18 x i8] c"dlopen failed:%s\0A\00"
@.str.2 = [7 x i8] c"zxdate\00"
@str = [13 x i8] c"dlsym failed\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_LAZY
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i8* %2, null
%4 = tail call i8* (...) @dlerror()
br i1 %3, label %5, label %7
5: ; preds = %0
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %17
7: ; preds = %0
%8 = tail call i8* @dlsym(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i8* (...) @dlerror()
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %7
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @perror(i8* %9)
br label %17
14: ; preds = %7
%15 = bitcast i8* %8 to void ()*
tail call void %15()
%16 = tail call i32 @dlclose(i8* %2)
br label %17
17: ; preds = %14, %11, %5
%18 = phi i32 [ -1, %5 ], [ -1, %11 ], [ 0, %14 ]
ret i32 %18
}