#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_NOW;
void *dlopen(char *, int /*<<< orphan*/);
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ puts(char *);
int do_test(void) {
  void *h = dlopen("tst-tlsmod15a.so", RTLD_NOW);
  if (h != NULL) {
    puts("unexpectedly succeeded to open tst-tlsmod15a.so");
    exit(1);
  }
  h = dlopen("tst-tlsmod15b.so", RTLD_NOW);
  if (h == NULL) {
    puts("failed to open tst-tlsmod15b.so");
    exit(1);
  }
  int (*fp)(void) = (int (*)(void))dlsym(h, "in_dso");
  if (fp == NULL) {
    puts("cannot find in_dso");
    exit(1);
  }
  return fp();
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xec7(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1182 <do_test+0x42>
lea    0xe8d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
lea    0xead(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11bc <do_test+0x7c>
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f1 <do_test+0xb1>
lea    0xe6e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
call   *-0x10(%rbp)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbx
mov    0x2ea0(%rip),%rbx
mov    (%rbx),%esi
lea    0xece(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
jne    1185 <do_test+0x45>
mov    (%rbx),%esi
lea    0xedb(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     118e <do_test+0x4e>
lea    0xee7(%rip),%rsi
mov    %rax,%rdi
call   1050 <dlsym@plt>
test   %rax,%rax
je     1197 <do_test+0x57>
pop    %rbx
jmp    *%rax
lea    0xe74(%rip),%rdi
jmp    119e <do_test+0x5e>
lea    0xe9b(%rip),%rdi
jmp    119e <do_test+0x5e>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"tst-tlsmod15a.so\00"
@RTLD_NOW = global i32 0
@.str.1 = [48 x i8] c"unexpectedly succeeded to open tst-tlsmod15a.so\00"
@.str.2 = [17 x i8] c"tst-tlsmod15b.so\00"
@.str.3 = [32 x i8] c"failed to open tst-tlsmod15b.so\00"
@.str.4 = [7 x i8] c"in_dso\00"
@.str.5 = [19 x i8] c"cannot find in_dso\00"
define i32 @do_test() {
%1 = alloca i8*
%2 = alloca i32 ()*
%3 = load i32, i32* @RTLD_NOW
%4 = call i8* @dlopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %3)
store i8* %4, i8** %1
%5 = load i8*, i8** %1
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @puts(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i32, i32* @RTLD_NOW
%12 = call i8* @dlopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %11)
store i8* %12, i8** %1
%13 = load i8*, i8** %1
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = call i32 @puts(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = load i8*, i8** %1
%20 = call i64 @dlsym(i8* %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%21 = inttoptr i64 %20 to i32 ()*
store i32 ()* %21, i32 ()** %2
%22 = load i32 ()*, i32 ()** %2
%23 = icmp eq i32 ()* %22, null
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = load i32 ()*, i32 ()** %2
%29 = call i32 %28()
ret i32 %29
}

LLVM IR (-O2):
@.str = [17 x i8] c"tst-tlsmod15a.so\00"
@RTLD_NOW = global i32 0
@.str.1 = [48 x i8] c"unexpectedly succeeded to open tst-tlsmod15a.so\00"
@.str.2 = [17 x i8] c"tst-tlsmod15b.so\00"
@.str.3 = [32 x i8] c"failed to open tst-tlsmod15b.so\00"
@.str.4 = [7 x i8] c"in_dso\00"
@.str.5 = [19 x i8] c"cannot find in_dso\00"
define i32 @do_test() {
%1 = load i32, i32* @RTLD_NOW
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %7, label %4
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = load i32, i32* @RTLD_NOW
%9 = tail call i8* @dlopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %7
%15 = tail call i64 @dlsym(i8* %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %14
%21 = inttoptr i64 %15 to i32 ()*
%22 = tail call i32 %21()
ret i32 %22
}