#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 EXIT_SUCCESS;
int /*<<< orphan*/ FAIL(char *, ...);
int /*<<< orphan*/ PASS(char *);
int /*<<< orphan*/ RTLD_LAZY;
int dlclose(void *);
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int main() {
  void *handle = dlopen("test.bundle", RTLD_LAZY);
  if (handle == NULL) {
    FAIL("dlopen(\"test.bundle\", RTLD_LAZY) failed");
    exit(1);
  }
  int result = dlclose(handle);
  if (result != 0) {
    FAIL("dlclose() failed");
    exit(1);
  }
  result = dlclose(handle);
  if (result != -1) {
    FAIL(
        "dlclose() on released handle should have returned -1, but returned %d",
        result);
    exit(1);
  }
  result = dlclose((void *)0x12345);
  if (result != -1) {
    FAIL("dlclose() on bogus handle should have returned -1, but returned %d",
         result);
    exit(1);
  }
  PASS("dlclose-basic");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1030 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119b <main+0x4b>
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1070 <FAIL@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1060 <dlclose@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11c9 <main+0x79>
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1070 <FAIL@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1060 <dlclose@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     11fa <main+0xaa>
mov    -0x14(%rbp),%esi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1070 <FAIL@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    $0x12345,%edi
call   1060 <dlclose@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     122c <main+0xdc>
mov    -0x14(%rbp),%esi
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1070 <FAIL@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
lea    0xe9b(%rip),%rdi
call   1040 <PASS@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     11ad <main+0x5d>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <dlclose@plt>
test   %eax,%eax
jne    11b6 <main+0x66>
mov    %rbx,%rdi
call   1060 <dlclose@plt>
cmp    $0xffffffff,%eax
jne    11ce <main+0x7e>
mov    $0x12345,%edi
call   1060 <dlclose@plt>
cmp    $0xffffffff,%eax
jne    11d7 <main+0x87>
lea    0xf31(%rip),%rdi
call   1040 <PASS@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
lea    0xe58(%rip),%rdi
jmp    11bd <main+0x6d>
lea    0xe77(%rip),%rdi
xor    %eax,%eax
call   1070 <FAIL@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
lea    0xe70(%rip),%rdi
jmp    11de <main+0x8e>
lea    0xead(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <FAIL@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"test.bundle\00"
@RTLD_LAZY = global i32 0
@.str.1 = [40 x i8] c"dlopen(\22test.bundle\22, RTLD_LAZY) failed\00"
@.str.2 = [17 x i8] c"dlclose() failed\00"
@.str.3 = [70 x i8] c"dlclose() on released handle should have returned -1, but returned %d\00"
@.str.4 = [67 x i8] c"dlclose() on bogus handle should have returned -1, but returned %d\00"
@.str.5 = [14 x i8] c"dlclose-basic\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @RTLD_LAZY
%5 = call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4)
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i8*, i8** %2
%13 = call i32 @dlclose(i8* %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %11
%20 = load i8*, i8** %2
%21 = call i32 @dlclose(i8* %20)
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = icmp ne i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i32, i32* %3
%26 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %19
%29 = call i32 @dlclose(i8* inttoptr (i64 74565 to i8*))
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = icmp ne i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i32, i32* %3
%34 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %28
%37 = call i32 @PASS(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%38 = load i32, i32* @EXIT_SUCCESS
ret i32 %38
}

LLVM IR (-O2):
@.str = [12 x i8] c"test.bundle\00"
@RTLD_LAZY = global i32 0
@.str.1 = [40 x i8] c"dlopen(\22test.bundle\22, RTLD_LAZY) failed\00"
@.str.2 = [17 x i8] c"dlclose() failed\00"
@.str.3 = [70 x i8] c"dlclose() on released handle should have returned -1, but returned %d\00"
@.str.4 = [67 x i8] c"dlclose() on bogus handle should have returned -1, but returned %d\00"
@.str.5 = [14 x i8] c"dlclose-basic\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = load i32, i32* @RTLD_LAZY
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = tail call i32 @dlclose(i8* %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %7
%14 = tail call i32 @dlclose(i8* %2)
%15 = icmp eq i32 %14, -1
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = tail call i32 @dlclose(i8* inttoptr (i64 74565 to i8*))
%21 = icmp eq i32 %20, -1
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.4, i64 0, i64 0), i32 %20)
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %19
%26 = tail call i32 @PASS(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%27 = load i32, i32* @EXIT_SUCCESS
ret i32 %27
}