#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 /*<<< orphan*/ XFAIL(char *);
scalar_t__ dlclose(void *);
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int main() {
  void *handle1 = dlopen("test.bundle", RTLD_LAZY);
  void *handle2 = dlopen("test.bundle", RTLD_LAZY);
  void *handle3 = dlopen("test.dylib", RTLD_LAZY);
  void *handle4 = dlopen("test.dylib", RTLD_LAZY);
  if ((NULL == handle1) || (NULL == handle2)) {
    FAIL("dlopen(\"test.bundle\") failed");
  }
  if ((NULL == handle3) || (NULL == handle4)) {
    FAIL("dlopen(\"test.dylib\") failed");
  }
  if (handle1 != handle2) {
    FAIL("dlopen handle1 and handle2 are not equal %p != %p", handle1, handle2);
  }
  if (handle3 != handle4) {
    FAIL("dlopen handle3 and handle4 are not equal %p != %p", handle3, handle4);
  }
  if (dlclose(handle4)) {
    XFAIL("Could not close handle4");
  }
  if (dlclose(handle2)) {
    FAIL("Could not close handle2");
  }
  void *sym = dlsym(handle1, "foo");
  if (sym == NULL) {
    FAIL("dlsym(handle1, \"foo\") failed");
    exit(0);
  }
  sym = dlsym(handle3, "foo");
  if (sym == NULL) {
    FAIL("dlsym(handle3, \"foo\") failed");
    exit(0);
  }
  if (dlclose(handle1)) {
    XFAIL("Could not close handle1");
  }
  if (dlclose(handle3)) {
    XFAIL("Could not close handle3");
  }
  PASS("dlopen-multi");
  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   1030 <dlopen@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0xe58(%rip),%rdi
call   1030 <dlopen@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
call   1030 <dlopen@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    0xe32(%rip),%rdi
call   1030 <dlopen@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     11fb <main+0x8b>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    1209 <main+0x99>
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
je     1221 <main+0xb1>
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    122f <main+0xbf>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     1253 <main+0xe3>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     1277 <main+0x107>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
mov    -0x28(%rbp),%rdi
call   1080 <dlclose@plt>
cmp    $0x0,%rax
je     1296 <main+0x126>
lea    0xe23(%rip),%rdi
call   1050 <XFAIL@plt>
mov    -0x18(%rbp),%rdi
call   1080 <dlclose@plt>
cmp    $0x0,%rax
je     12b7 <main+0x147>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
mov    -0x10(%rbp),%rdi
lea    0xe22(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12eb <main+0x17b>
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0xdee(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    131f <main+0x1af>
lea    0xdf4(%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     133e <main+0x1ce>
lea    0xde9(%rip),%rdi
call   1050 <XFAIL@plt>
mov    -0x20(%rbp),%rdi
call   1080 <dlclose@plt>
cmp    $0x0,%rax
je     135d <main+0x1ed>
lea    0xde2(%rip),%rdi
call   1050 <XFAIL@plt>
lea    0xdee(%rip),%rdi
call   1040 <PASS@plt>
mov    0x2c70(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    0x2e58(%rip),%rbx
mov    (%rbx),%esi
lea    0xe77(%rip),%r14
mov    %r14,%rdi
call   1030 <dlopen@plt>
mov    %rax,%r15
mov    (%rbx),%esi
mov    %r14,%rdi
call   1030 <dlopen@plt>
mov    %rax,%r12
mov    (%rbx),%esi
lea    0xe62(%rip),%r13
mov    %r13,%rdi
call   1030 <dlopen@plt>
mov    %rax,%r14
mov    (%rbx),%esi
mov    %r13,%rdi
call   1030 <dlopen@plt>
mov    %rax,%rbx
test   %r15,%r15
je     11cc <main+0x5c>
test   %r12,%r12
jne    11da <main+0x6a>
lea    0xe44(%rip),%rdi
xor    %eax,%eax
call   1090 <FAIL@plt>
test   %r14,%r14
je     11e4 <main+0x74>
test   %rbx,%rbx
jne    11f2 <main+0x82>
lea    0xe49(%rip),%rdi
xor    %eax,%eax
call   1090 <FAIL@plt>
cmp    %r12,%r15
je     120b <main+0x9b>
lea    0xe52(%rip),%rdi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1090 <FAIL@plt>
cmp    %rbx,%r14
je     1224 <main+0xb4>
lea    0xe6b(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <FAIL@plt>
mov    %rbx,%rdi
call   1080 <dlclose@plt>
test   %rax,%rax
je     123d <main+0xcd>
lea    0xe7c(%rip),%rdi
call   1050 <XFAIL@plt>
mov    %r12,%rdi
call   1080 <dlclose@plt>
test   %rax,%rax
je     1258 <main+0xe8>
lea    0xe7b(%rip),%rdi
xor    %eax,%eax
call   1090 <FAIL@plt>
lea    0xe85(%rip),%rsi
mov    %r15,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     12d1 <main+0x161>
lea    0xe71(%rip),%rsi
mov    %r14,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     12da <main+0x16a>
mov    %r15,%rdi
call   1080 <dlclose@plt>
test   %rax,%rax
je     1299 <main+0x129>
lea    0xe8e(%rip),%rdi
call   1050 <XFAIL@plt>
mov    %r14,%rdi
call   1080 <dlclose@plt>
test   %rax,%rax
je     12b2 <main+0x142>
lea    0xe8d(%rip),%rdi
call   1050 <XFAIL@plt>
lea    0xe99(%rip),%rdi
call   1040 <PASS@plt>
mov    0x2d1b(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe10(%rip),%rdi
jmp    12e1 <main+0x171>
lea    0xe24(%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 = [11 x i8] c"test.dylib\00"
@.str.2 = [29 x i8] c"dlopen(\22test.bundle\22) failed\00"
@.str.3 = [28 x i8] c"dlopen(\22test.dylib\22) failed\00"
@.str.4 = [50 x i8] c"dlopen handle1 and handle2 are not equal %p != %p\00"
@.str.5 = [50 x i8] c"dlopen handle3 and handle4 are not equal %p != %p\00"
@.str.6 = [24 x i8] c"Could not close handle4\00"
@.str.7 = [24 x i8] c"Could not close handle2\00"
@.str.8 = [4 x i8] c"foo\00"
@.str.9 = [29 x i8] c"dlsym(handle1, \22foo\22) failed\00"
@.str.10 = [29 x i8] c"dlsym(handle3, \22foo\22) failed\00"
@.str.11 = [24 x i8] c"Could not close handle1\00"
@.str.12 = [24 x i8] c"Could not close handle3\00"
@.str.13 = [13 x i8] c"dlopen-multi\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i32 0, i32* %1
%7 = load i32, i32* @RTLD_LAZY
%8 = call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %7)
store i8* %8, i8** %2
%9 = load i32, i32* @RTLD_LAZY
%10 = call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9)
store i8* %10, i8** %3
%11 = load i32, i32* @RTLD_LAZY
%12 = call i8* @dlopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %11)
store i8* %12, i8** %4
%13 = load i32, i32* @RTLD_LAZY
%14 = call i8* @dlopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i8* %14, i8** %5
%15 = load i8*, i8** %2
%16 = icmp eq i8* null, %15
br i1 %16, label %20, label %17
17: ; preds = %0
%18 = load i8*, i8** %3
%19 = icmp eq i8* null, %18
br i1 %19, label %20, label %22
20: ; preds = %17, %0
%21 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = load i8*, i8** %4
%24 = icmp eq i8* null, %23
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i8*, i8** %5
%27 = icmp eq i8* null, %26
br i1 %27, label %28, label %30
28: ; preds = %25, %22
%29 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = load i8*, i8** %2
%32 = load i8*, i8** %3
%33 = icmp ne i8* %31, %32
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i8*, i8** %2
%36 = load i8*, i8** %3
%37 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0), i8* %35, i8* %36)
br label %38
38: ; preds = %34, %30
%39 = load i8*, i8** %4
%40 = load i8*, i8** %5
%41 = icmp ne i8* %39, %40
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load i8*, i8** %4
%44 = load i8*, i8** %5
%45 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0), i8* %43, i8* %44)
br label %46
46: ; preds = %42, %38
%47 = load i8*, i8** %5
%48 = call i64 @dlclose(i8* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @XFAIL(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = load i8*, i8** %3
%54 = call i64 @dlclose(i8* %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %58
58: ; preds = %56, %52
%59 = load i8*, i8** %2
%60 = call i8* @dlsym(i8* %59, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
store i8* %60, i8** %6
%61 = load i8*, i8** %6
%62 = icmp eq i8* %61, null
br i1 %62, label %63, label %66
63: ; preds = %58
%64 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0))
%65 = call i32 @exit(i32 0)
unreachable
66: ; preds = %58
%67 = load i8*, i8** %4
%68 = call i8* @dlsym(i8* %67, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
store i8* %68, i8** %6
%69 = load i8*, i8** %6
%70 = icmp eq i8* %69, null
br i1 %70, label %71, label %74
71: ; preds = %66
%72 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.10, i64 0, i64 0))
%73 = call i32 @exit(i32 0)
unreachable
74: ; preds = %66
%75 = load i8*, i8** %2
%76 = call i64 @dlclose(i8* %75)
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %74
%79 = call i32 @XFAIL(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.11, i64 0, i64 0))
br label %80
80: ; preds = %78, %74
%81 = load i8*, i8** %4
%82 = call i64 @dlclose(i8* %81)
%83 = icmp ne i64 %82, 0
br i1 %83, label %84, label %86
84: ; preds = %80
%85 = call i32 @XFAIL(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.12, i64 0, i64 0))
br label %86
86: ; preds = %84, %80
%87 = call i32 @PASS(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.13, i64 0, i64 0))
%88 = load i32, i32* @EXIT_SUCCESS
ret i32 %88
}

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