#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;
scalar_t__ dlclose(void *);
int /*<<< orphan*/ dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int main() {
  void *handle1 = dlopen("test.bundle", RTLD_LAZY);
  if (handle1 == NULL) {
    FAIL("dlclose-bundle-unload: dlopen(\"test.bundle\", RTLD_LAZY) failed "
         "with dlerror()=%s",
         dlerror());
    exit(0);
  }
  void *handle2 = dlopen("test.bundle", RTLD_LAZY);
  if (handle2 == NULL) {
    FAIL("dlclose-bundle-unload: dlopen(\"test.bundle\", RTLD_LAZY) failed "
         "with dlerror()=%s",
         dlerror());
    exit(0);
  }
  void *handle3 = dlopen("test.bundle", RTLD_LAZY);
  if (handle3 == NULL) {
    FAIL("dlclose-bundle-unload: dlopen(\"test.bundle\", RTLD_LAZY) failed "
         "with dlerror()=%s",
         dlerror());
    exit(0);
  }
  void *sym = dlsym(handle1, "foo");
  if (sym == NULL) {
    FAIL("dlclose-bundle-unload: dlsym(handle1, \"foo\") failed");
    exit(0);
  }
  if (dlclose(handle3) != 0) {
    FAIL("dlclose-bundle-unload: dlclose(handle3) != 0, dlerrr()=%s",
         dlerror());
    exit(0);
  }
  if (dlclose(handle2) != 0) {
    FAIL("dlclose-bundle-unload: dlclose(handle2) != 0, dlerrr()=%s",
         dlerror());
    exit(0);
  }
  if (dlclose(handle1) != 0) {
    FAIL("dlclose-bundle-unload: dlclose(handle1) != 0, dlerrr()=%s",
         dlerror());
    exit(0);
  }
  if (dlclose(handle1) == 0) {
    FAIL(
        "dlclose-bundle-unload: dlclose(handle4) == 0, but should have failed");
    exit(0);
  }
  PASS("dlclose-bundle-unload");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c1 <main+0x51>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    0xe2f(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1203 <main+0x93>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
lea    0xded(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1245 <main+0xd5>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xe0d(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1279 <main+0x109>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1080 <dlclose@plt>
cmp    $0x0,%rax
je     12aa <main+0x13a>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1080 <dlclose@plt>
cmp    $0x0,%rax
je     12db <main+0x16b>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1080 <dlclose@plt>
cmp    $0x0,%rax
je     130c <main+0x19c>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1080 <dlclose@plt>
cmp    $0x0,%rax
jne    1334 <main+0x1c4>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xe4d(%rip),%rdi
call   1050 <PASS@plt>
mov    0x2c99(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    0x2e5c(%rip),%rbx
mov    (%rbx),%esi
lea    0xe7b(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1225 <main+0xb5>
mov    %rax,%r15
mov    (%rbx),%esi
lea    0xe61(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1225 <main+0xb5>
mov    %rax,%r14
mov    (%rbx),%esi
lea    0xe4b(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1225 <main+0xb5>
mov    %rax,%rbx
lea    0xe94(%rip),%rsi
mov    %r15,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     1235 <main+0xc5>
mov    %rbx,%rdi
call   1080 <dlclose@plt>
test   %rax,%rax
jne    123e <main+0xce>
mov    %r14,%rdi
call   1080 <dlclose@plt>
test   %rax,%rax
jne    124e <main+0xde>
mov    %r15,%rdi
call   1080 <dlclose@plt>
test   %rax,%rax
jne    125e <main+0xee>
mov    %r15,%rdi
call   1080 <dlclose@plt>
test   %rax,%rax
je     127c <main+0x10c>
lea    0xf77(%rip),%rdi
call   1050 <PASS@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xdd9(%rip),%rdi
jmp    126c <main+0xfc>
lea    0xe25(%rip),%rdi
jmp    1283 <main+0x113>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe49(%rip),%rdi
jmp    126c <main+0xfc>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe73(%rip),%rdi
jmp    126c <main+0xfc>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe9d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xec0(%rip),%rdi
xor    %eax,%eax
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"test.bundle\00"
@RTLD_LAZY = global i32 0
@.str.1 = [81 x i8] c"dlclose-bundle-unload: dlopen(\22test.bundle\22, RTLD_LAZY) failed with dlerror()=%s\00"
@.str.2 = [4 x i8] c"foo\00"
@.str.3 = [52 x i8] c"dlclose-bundle-unload: dlsym(handle1, \22foo\22) failed\00"
@.str.4 = [58 x i8] c"dlclose-bundle-unload: dlclose(handle3) != 0, dlerrr()=%s\00"
@.str.5 = [58 x i8] c"dlclose-bundle-unload: dlclose(handle2) != 0, dlerrr()=%s\00"
@.str.6 = [58 x i8] c"dlclose-bundle-unload: dlclose(handle1) != 0, dlerrr()=%s\00"
@.str.7 = [69 x i8] c"dlclose-bundle-unload: dlclose(handle4) == 0, but should have failed\00"
@.str.8 = [22 x i8] c"dlclose-bundle-unload\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i32 0, i32* %1
%6 = load i32, i32* @RTLD_LAZY
%7 = call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %6)
store i8* %7, i8** %2
%8 = load i8*, i8** %2
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i32 (...) @dlerror()
%12 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %0
%15 = load i32, i32* @RTLD_LAZY
%16 = call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %15)
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = call i32 (...) @dlerror()
%21 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %14
%24 = load i32, i32* @RTLD_LAZY
%25 = call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %24)
store i8* %25, i8** %4
%26 = load i8*, i8** %4
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = call i32 (...) @dlerror()
%30 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %23
%33 = load i8*, i8** %2
%34 = call i8* @dlsym(i8* %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i8* %34, i8** %5
%35 = load i8*, i8** %5
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %32
%41 = load i8*, i8** %4
%42 = call i64 @dlclose(i8* %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = call i32 (...) @dlerror()
%46 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0), i32 %45)
%47 = call i32 @exit(i32 0)
unreachable
48: ; preds = %40
%49 = load i8*, i8** %3
%50 = call i64 @dlclose(i8* %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %48
%53 = call i32 (...) @dlerror()
%54 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0), i32 %53)
%55 = call i32 @exit(i32 0)
unreachable
56: ; preds = %48
%57 = load i8*, i8** %2
%58 = call i64 @dlclose(i8* %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %56
%61 = call i32 (...) @dlerror()
%62 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.6, i64 0, i64 0), i32 %61)
%63 = call i32 @exit(i32 0)
unreachable
64: ; preds = %56
%65 = load i8*, i8** %2
%66 = call i64 @dlclose(i8* %65)
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %64
%69 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.7, i64 0, i64 0))
%70 = call i32 @exit(i32 0)
unreachable
71: ; preds = %64
%72 = call i32 @PASS(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0))
%73 = load i32, i32* @EXIT_SUCCESS
ret i32 %73
}

LLVM IR (-O2):
@.str = [12 x i8] c"test.bundle\00"
@RTLD_LAZY = global i32 0
@.str.1 = [81 x i8] c"dlclose-bundle-unload: dlopen(\22test.bundle\22, RTLD_LAZY) failed with dlerror()=%s\00"
@.str.2 = [4 x i8] c"foo\00"
@.str.3 = [52 x i8] c"dlclose-bundle-unload: dlsym(handle1, \22foo\22) failed\00"
@.str.4 = [58 x i8] c"dlclose-bundle-unload: dlclose(handle3) != 0, dlerrr()=%s\00"
@.str.5 = [58 x i8] c"dlclose-bundle-unload: dlclose(handle2) != 0, dlerrr()=%s\00"
@.str.6 = [58 x i8] c"dlclose-bundle-unload: dlclose(handle1) != 0, dlerrr()=%s\00"
@.str.7 = [69 x i8] c"dlclose-bundle-unload: dlclose(handle4) == 0, but should have failed\00"
@.str.8 = [22 x i8] c"dlclose-bundle-unload\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 %8
4: ; preds = %0
%5 = tail call i32 (...) @dlerror()
%6 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %0
%9 = load i32, i32* @RTLD_LAZY
%10 = tail call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = tail call i32 (...) @dlerror()
%14 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = tail call i32 @exit(i32 0)
unreachable
16: ; preds = %8
%17 = load i32, i32* @RTLD_LAZY
%18 = tail call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = tail call i32 (...) @dlerror()
%22 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %16
%25 = tail call i8* @dlsym(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0))
%29 = tail call i32 @exit(i32 0)
unreachable
30: ; preds = %24
%31 = tail call i64 @dlclose(i8* %18)
%32 = icmp eq i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %30
%34 = tail call i32 (...) @dlerror()
%35 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = tail call i32 @exit(i32 0)
unreachable
37: ; preds = %30
%38 = tail call i64 @dlclose(i8* %10)
%39 = icmp eq i64 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %37
%41 = tail call i32 (...) @dlerror()
%42 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0), i32 %41)
%43 = tail call i32 @exit(i32 0)
unreachable
44: ; preds = %37
%45 = tail call i64 @dlclose(i8* %2)
%46 = icmp eq i64 %45, 0
br i1 %46, label %51, label %47
47: ; preds = %44
%48 = tail call i32 (...) @dlerror()
%49 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.6, i64 0, i64 0), i32 %48)
%50 = tail call i32 @exit(i32 0)
unreachable
51: ; preds = %44
%52 = tail call i64 @dlclose(i8* %2)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.7, i64 0, i64 0))
%56 = tail call i32 @exit(i32 0)
unreachable
57: ; preds = %51
%58 = tail call i32 @PASS(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0))
%59 = load i32, i32* @EXIT_SUCCESS
ret i32 %59
}