#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*/ FAIL(char *);
int /*<<< orphan*/ PASS(char *);
int /*<<< orphan*/ RTLD_LAZY;
void *dlopen(char *, int /*<<< orphan*/);
int main() {
  for (int i = 0; i < 10; ++i) {
    void *handle = dlopen("libstub.dylib", RTLD_LAZY);
    if (handle != NULL) {
      FAIL("weak-coalesce-stubs: load of libstub.dylib unexpectedly succeeded");
    }
  }
  dlopen("libbar.dylib", RTLD_LAZY);
  PASS("weak-coalesce-stubs");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    1193 <main+0x63>
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xea0(%rip),%rdi
call   1030 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1180 <main+0x50>
lea    0xe93(%rip),%rdi
call   1050 <FAIL@plt>
jmp    1185 <main+0x55>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1146 <main+0x16>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    0xead(%rip),%rdi
call   1030 <dlopen@plt>
lea    0xeae(%rip),%rdi
call   1040 <PASS@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2ea8(%rip),%rbx
mov    (%rbx),%esi
lea    0xebf(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     1157 <main+0x27>
lea    0xebc(%rip),%rdi
call   1050 <FAIL@plt>
mov    (%rbx),%esi
lea    0xea0(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     1176 <main+0x46>
lea    0xe9d(%rip),%rdi
call   1050 <FAIL@plt>
mov    (%rbx),%esi
lea    0xe81(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     1195 <main+0x65>
lea    0xe7e(%rip),%rdi
call   1050 <FAIL@plt>
mov    (%rbx),%esi
lea    0xe62(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     11b4 <main+0x84>
lea    0xe5f(%rip),%rdi
call   1050 <FAIL@plt>
mov    (%rbx),%esi
lea    0xe43(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     11d3 <main+0xa3>
lea    0xe40(%rip),%rdi
call   1050 <FAIL@plt>
mov    (%rbx),%esi
lea    0xe24(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     11f2 <main+0xc2>
lea    0xe21(%rip),%rdi
call   1050 <FAIL@plt>
mov    (%rbx),%esi
lea    0xe05(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     1211 <main+0xe1>
lea    0xe02(%rip),%rdi
call   1050 <FAIL@plt>
mov    (%rbx),%esi
lea    0xde6(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     1230 <main+0x100>
lea    0xde3(%rip),%rdi
call   1050 <FAIL@plt>
mov    (%rbx),%esi
lea    0xdc7(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     124f <main+0x11f>
lea    0xdc4(%rip),%rdi
call   1050 <FAIL@plt>
mov    (%rbx),%esi
lea    0xda8(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     126e <main+0x13e>
lea    0xda5(%rip),%rdi
call   1050 <FAIL@plt>
mov    (%rbx),%esi
lea    0xdd9(%rip),%rdi
call   1030 <dlopen@plt>
lea    0xdda(%rip),%rdi
call   1040 <PASS@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [14 x i8] c"libstub.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [66 x i8] c"weak-coalesce-stubs: load of libstub.dylib unexpectedly succeeded\00"
@.str.2 = [13 x i8] c"libbar.dylib\00"
@.str.3 = [20 x i8] c"weak-coalesce-stubs\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
store i32 0, i32* %2
br label %4
4: ; preds = %15, %0
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 10
br i1 %6, label %7, label %18
7: ; preds = %4
%8 = load i32, i32* @RTLD_LAZY
%9 = call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %8)
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = call i32 @FAIL(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %7
br label %15
15: ; preds = %14
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 1
store i32 %17, i32* %2
br label %4
18: ; preds = %4
%19 = load i32, i32* @RTLD_LAZY
%20 = call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = call i32 @PASS(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"libstub.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [66 x i8] c"weak-coalesce-stubs: load of libstub.dylib unexpectedly succeeded\00"
@.str.2 = [13 x i8] c"libbar.dylib\00"
@.str.3 = [20 x i8] c"weak-coalesce-stubs\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_LAZY
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %6, label %4
4: ; preds = %0
%5 = tail call i32 @FAIL(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = load i32, i32* @RTLD_LAZY
%8 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @FAIL(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
%13 = load i32, i32* @RTLD_LAZY
%14 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = tail call i32 @FAIL(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = load i32, i32* @RTLD_LAZY
%20 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = tail call i32 @FAIL(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = load i32, i32* @RTLD_LAZY
%26 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = icmp eq i8* %26, null
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = tail call i32 @FAIL(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = load i32, i32* @RTLD_LAZY
%32 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = icmp eq i8* %32, null
br i1 %33, label %36, label %34
34: ; preds = %30
%35 = tail call i32 @FAIL(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
%37 = load i32, i32* @RTLD_LAZY
%38 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %37)
%39 = icmp eq i8* %38, null
br i1 %39, label %42, label %40
40: ; preds = %36
%41 = tail call i32 @FAIL(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
%43 = load i32, i32* @RTLD_LAZY
%44 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %43)
%45 = icmp eq i8* %44, null
br i1 %45, label %48, label %46
46: ; preds = %42
%47 = tail call i32 @FAIL(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %48
48: ; preds = %46, %42
%49 = load i32, i32* @RTLD_LAZY
%50 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %49)
%51 = icmp eq i8* %50, null
br i1 %51, label %54, label %52
52: ; preds = %48
%53 = tail call i32 @FAIL(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %54
54: ; preds = %52, %48
%55 = load i32, i32* @RTLD_LAZY
%56 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %55)
%57 = icmp eq i8* %56, null
br i1 %57, label %60, label %58
58: ; preds = %54
%59 = tail call i32 @FAIL(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %60
60: ; preds = %58, %54
%61 = load i32, i32* @RTLD_LAZY
%62 = tail call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %61)
%63 = tail call i32 @PASS(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}