#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;
void *dlopen(char *, int /*<<< orphan*/);
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int do_test(void) {
  char modname[sizeof "tst-tlsmod18aXX.so"];
  void *h[20];
  for (int i = 0; i < 20; i++) {
    snprintf(modname, sizeof modname, "tst-tlsmod18a%d.so", i);
    h[i] = dlopen(modname, RTLD_LAZY);
    if (h[i] == NULL) {
      printf("unexpectedly failed to open %s", modname);
      exit(1);
    }
  }
  for (int i = 0; i < 20; i++) {
    int (*fp)(void) = (int (*)(void))dlsym(h[i], "test");
    if (fp == NULL) {
      printf("cannot find test in tst-tlsmod18a%d.so", i);
      exit(1);
    }
    if (fp())
      exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0xc4(%rbp)
cmpl   $0x14,-0xc4(%rbp)
jge    11fc <do_test+0xac>
lea    -0x20(%rbp),%rdi
mov    -0xc4(%rbp),%ecx
mov    $0x13,%esi
lea    0xeb0(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,%rcx
movslq -0xc4(%rbp),%rax
mov    %rcx,-0xc0(%rbp,%rax,8)
ff
movslq -0xc4(%rbp),%rax
cmpq   $0x0,-0xc0(%rbp,%rax,8)
jne    11e3 <do_test+0x93>
lea    -0x20(%rbp),%rsi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11e8 <do_test+0x98>
mov    -0xc4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc4(%rbp)
jmp    1165 <do_test+0x15>
movl   $0x0,-0xc8(%rbp)
cmpl   $0x14,-0xc8(%rbp)
jge    1293 <do_test+0x143>
movslq -0xc8(%rbp),%rax
mov    -0xc0(%rbp,%rax,8),%rdi
ff
lea    0xdf6(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0xd0(%rbp)
cmpq   $0x0,-0xd0(%rbp)
jne    1261 <do_test+0x111>
mov    -0xc8(%rbp),%esi
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
call   *-0xd0(%rbp)
cmp    $0x0,%eax
je     127a <do_test+0x12a>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    127f <do_test+0x12f>
mov    -0xc8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc8(%rbp)
jmp    1206 <do_test+0xb6>
xor    %eax,%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xc8,%rsp
xor    %ebx,%ebx
lea    0xed1(%rip),%r14
mov    %rsp,%r15
mov    0x2e6f(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x13,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
call   1040 <snprintf@plt>
mov    (%r12),%esi
mov    %r15,%rdi
call   1050 <dlopen@plt>
mov    %rax,0x20(%rsp,%rbx,8)
test   %rax,%rax
je     11f3 <do_test+0xa3>
add    $0x1,%rbx
cmp    $0x14,%rbx
jne    1180 <do_test+0x30>
xor    %ebx,%ebx
lea    0xe64(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    0x20(%rsp,%rbx,8),%rdi
mov    %r14,%rsi
call   1060 <dlsym@plt>
test   %rax,%rax
je     120e <do_test+0xbe>
call   *%rax
test   %eax,%eax
jne    121e <do_test+0xce>
add    $0x1,%rbx
cmp    $0x14,%rbx
jne    11c0 <do_test+0x70>
xor    %eax,%eax
add    $0xc8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe06(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe0f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"tst-tlsmod18a%d.so\00"
@RTLD_LAZY = global i32 0
@.str.1 = [31 x i8] c"unexpectedly failed to open %s\00"
@.str.2 = [5 x i8] c"test\00"
@.str.3 = [39 x i8] c"cannot find test in tst-tlsmod18a%d.so\00"
define i32 @do_test() {
%1 = alloca [19 x i8]
%2 = alloca [20 x i8*]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32 ()*
store i32 0, i32* %3
br label %6
6: ; preds = %29, %0
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 20
br i1 %8, label %9, label %32
9: ; preds = %6
%10 = getelementptr inbounds [19 x i8], [19 x i8]* %1, i64 0, i64 0
%11 = load i32, i32* %3
%12 = call i32 @snprintf(i8* %10, i32 19, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [19 x i8], [19 x i8]* %1, i64 0, i64 0
%14 = load i32, i32* @RTLD_LAZY
%15 = call i8* @dlopen(i8* %13, i32 %14)
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 %17
store i8* %15, i8** %18
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 %20
%22 = load i8*, i8** %21
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %28
24: ; preds = %9
%25 = getelementptr inbounds [19 x i8], [19 x i8]* %1, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %9
br label %29
29: ; preds = %28
%30 = load i32, i32* %3
%31 = add nsw i32 %30, 1
store i32 %31, i32* %3
br label %6
32: ; preds = %6
store i32 0, i32* %4
br label %33
33: ; preds = %56, %32
%34 = load i32, i32* %4
%35 = icmp slt i32 %34, 20
br i1 %35, label %36, label %59
36: ; preds = %33
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 %38
%40 = load i8*, i8** %39
%41 = call i64 @dlsym(i8* %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%42 = inttoptr i64 %41 to i32 ()*
store i32 ()* %42, i32 ()** %5
%43 = load i32 ()*, i32 ()** %5
%44 = icmp eq i32 ()* %43, null
br i1 %44, label %45, label %49
45: ; preds = %36
%46 = load i32, i32* %4
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %36
%50 = load i32 ()*, i32 ()** %5
%51 = call i32 %50()
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %49
br label %56
56: ; preds = %55
%57 = load i32, i32* %4
%58 = add nsw i32 %57, 1
store i32 %58, i32* %4
br label %33
59: ; preds = %33
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"tst-tlsmod18a%d.so\00"
@RTLD_LAZY = global i32 0
@.str.1 = [31 x i8] c"unexpectedly failed to open %s\00"
@.str.2 = [5 x i8] c"test\00"
@.str.3 = [39 x i8] c"cannot find test in tst-tlsmod18a%d.so\00"
define i32 @do_test() {
%1 = alloca [19 x i8]
%2 = alloca [20 x i8*]
%3 = getelementptr inbounds [19 x i8], [19 x i8]* %1, i64 0, i64 0
%4 = bitcast [20 x i8*]* %2 to i8*
br label %8
5: ; preds = %8
%6 = add nuw nsw i64 %9, 1
%7 = icmp eq i64 %6, 20
br i1 %7, label %20, label %8
8: ; preds = %0, %5
%9 = phi i64 [ 0, %0 ], [ %6, %5 ]
%10 = trunc i64 %9 to i32
%11 = call i32 @snprintf(i8* %3, i32 19, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @RTLD_LAZY
%13 = call i8* @dlopen(i8* %3, i32 %12)
%14 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 %9
store i8* %13, i8** %14
%15 = icmp eq i8* %13, null
br i1 %15, label %16, label %5
16: ; preds = %8
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %36
ret i32 0
20: ; preds = %5, %36
%21 = phi i64 [ %37, %36 ], [ 0, %5 ]
%22 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 %21
%23 = load i8*, i8** %22
%24 = call i64 @dlsym(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = trunc i64 %21 to i32
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %20
%31 = inttoptr i64 %24 to i32 ()*
%32 = call i32 %31()
%33 = icmp eq i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %30
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %30
%37 = add nuw nsw i64 %21, 1
%38 = icmp eq i64 %37, 20
br i1 %38, label %19, label %20
}