#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;
scalar_t__ dlclose(void *);
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
int do_test(void) {
  void *p = dlopen("libpthread.so.0", RTLD_LAZY);
  if (p == NULL) {
    puts("failed to load libpthread.so.0");
    return 1;
  }
  if (dlclose(p) != 0) {
    puts("dlclose (libpthread.so.0) failed");
    return 1;
  }
  puts("seems to work");
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
lea    0xec7(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1174 <do_test+0x44>
lea    0xe9d(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b2 <do_test+0x82>
mov    -0x10(%rbp),%rdi
call   1050 <dlclose@plt>
cmp    $0x0,%rax
je     119f <do_test+0x6f>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b2 <do_test+0x82>
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbx
mov    0x2ea8(%rip),%rax
mov    (%rax),%esi
lea    0xece(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     116f <do_test+0x3f>
mov    %rax,%rdi
call   1050 <dlclose@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
lea    0xede(%rip),%rax
lea    0xeb6(%rip),%rdi
cmove  %rax,%rdi
jmp    117b <do_test+0x4b>
mov    $0x1,%ebx
lea    0xe85(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [16 x i8] c"libpthread.so.0\00"
@RTLD_LAZY = global i32 0
@.str.1 = [31 x i8] c"failed to load libpthread.so.0\00"
@.str.2 = [33 x i8] c"dlclose (libpthread.so.0) failed\00"
@.str.3 = [14 x i8] c"seems to work\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i8*
%3 = load i32, i32* @RTLD_LAZY
%4 = call i8* @dlopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %3)
store i8* %4, i8** %2
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @puts(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %17
9: ; preds = %0
%10 = load i8*, i8** %2
%11 = call i64 @dlclose(i8* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %17
15: ; preds = %9
%16 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %17
17: ; preds = %15, %13, %7
%18 = load i32, i32* %1
ret i32 %18
}

LLVM IR (-O2):
@.str = [16 x i8] c"libpthread.so.0\00"
@RTLD_LAZY = global i32 0
@.str.1 = [31 x i8] c"failed to load libpthread.so.0\00"
@.str.2 = [33 x i8] c"dlclose (libpthread.so.0) failed\00"
@.str.3 = [14 x i8] c"seems to work\00"
define i32 @do_test() {
%1 = load i32, i32* @RTLD_LAZY
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %10, label %4
4: ; preds = %0
%5 = tail call i64 @dlclose(i8* %2)
%6 = icmp eq i64 %5, 0
%7 = select i1 %6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0)
%8 = xor i1 %6, true
%9 = zext i1 %8 to i32
br label %10
10: ; preds = %4, %0
%11 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), %0 ], [ %7, %4 ]
%12 = phi i32 [ 1, %0 ], [ %9, %4 ]
%13 = tail call i32 @puts(i8* dereferenceable(1) %11)
ret i32 %12
}