#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 RTLD_GLOBAL;
int RTLD_LAZY;
int RTLD_NOW;
void *dlopen(char *, int);
void *dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ puts(char *);
int do_test(void) {
  void *h = dlopen("tst-tlsmod16a.so", RTLD_LAZY | RTLD_GLOBAL);
  if (h == NULL) {
    puts("unexpectedly failed to open tst-tlsmod16a.so");
    exit(1);
  }
  void *p = dlsym(h, "tlsvar");
  h = dlopen("tst-tlsmod16b.so", RTLD_NOW | RTLD_GLOBAL);
  if (h == NULL) {
    return 0;
  }
  puts("unexpectedly succeeded to open tst-tlsmod16b.so");
  void *(*fp)(void) = (void *(*)(void))dlsym(h, "in_dso");
  if (fp == NULL) {
    puts("cannot find in_dso");
    exit(1);
  }
  if (fp() != p) {
    puts("returned values do not match");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e90(%rip),%rax
or     (%rax),%esi
lea    0xebb(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118b <do_test+0x4b>
lea    0xe84(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xe97(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d8 <do_test+0x98>
movl   $0x0,-0x4(%rbp)
jmp    1243 <do_test+0x103>
lea    0xe55(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1219 <do_test+0xd9>
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
call   *-0x20(%rbp)
cmp    -0x18(%rbp),%rax
je     123c <do_test+0xfc>
lea    0xe4a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %r14
push   %rbx
push   %rax
mov    0x2e85(%rip),%rax
mov    0x2e96(%rip),%rbx
mov    (%rbx),%esi
or     (%rax),%esi
lea    0xebf(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11c9 <do_test+0x89>
lea    0xebf(%rip),%rsi
mov    %rax,%rdi
call   1050 <dlsym@plt>
mov    %rax,%r14
mov    0x2e60(%rip),%rax
mov    (%rbx),%esi
or     (%rax),%esi
lea    0xec8(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11bf <do_test+0x7f>
mov    %rax,%rbx
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
lea    0xec5(%rip),%rsi
mov    %rbx,%rdi
call   1050 <dlsym@plt>
test   %rax,%rax
je     11d2 <do_test+0x92>
call   *%rax
cmp    %r14,%rax
jne    11db <do_test+0x9b>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe30(%rip),%rdi
jmp    11e2 <do_test+0xa2>
lea    0xe8b(%rip),%rdi
jmp    11e2 <do_test+0xa2>
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"tst-tlsmod16a.so\00"
@RTLD_LAZY = global i32 0
@RTLD_GLOBAL = global i32 0
@.str.1 = [45 x i8] c"unexpectedly failed to open tst-tlsmod16a.so\00"
@.str.2 = [7 x i8] c"tlsvar\00"
@.str.3 = [17 x i8] c"tst-tlsmod16b.so\00"
@RTLD_NOW = global i32 0
@.str.4 = [48 x i8] c"unexpectedly succeeded to open tst-tlsmod16b.so\00"
@.str.5 = [7 x i8] c"in_dso\00"
@.str.6 = [19 x i8] c"cannot find in_dso\00"
@.str.7 = [29 x i8] c"returned values do not match\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8* ()*
%5 = load i32, i32* @RTLD_LAZY
%6 = load i32, i32* @RTLD_GLOBAL
%7 = or i32 %5, %6
%8 = call i8* @dlopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %7)
store i8* %8, i8** %2
%9 = load i8*, i8** %2
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @puts(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i8*, i8** %2
%16 = call i8* @dlsym(i8* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i8* %16, i8** %3
%17 = load i32, i32* @RTLD_NOW
%18 = load i32, i32* @RTLD_GLOBAL
%19 = or i32 %17, %18
%20 = call i8* @dlopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %19)
store i8* %20, i8** %2
%21 = load i8*, i8** %2
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %14
store i32 0, i32* %1
br label %43
24: ; preds = %14
%25 = call i32 @puts(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0))
%26 = load i8*, i8** %2
%27 = call i8* @dlsym(i8* %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%28 = bitcast i8* %27 to i8* ()*
store i8* ()* %28, i8* ()** %4
%29 = load i8* ()*, i8* ()** %4
%30 = icmp eq i8* ()* %29, null
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %24
%35 = load i8* ()*, i8* ()** %4
%36 = call i8* %35()
%37 = load i8*, i8** %3
%38 = icmp ne i8* %36, %37
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = call i32 @puts(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %34
store i32 0, i32* %1
br label %43
43: ; preds = %42, %23
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@.str = [17 x i8] c"tst-tlsmod16a.so\00"
@RTLD_LAZY = global i32 0
@RTLD_GLOBAL = global i32 0
@.str.1 = [45 x i8] c"unexpectedly failed to open tst-tlsmod16a.so\00"
@.str.2 = [7 x i8] c"tlsvar\00"
@.str.3 = [17 x i8] c"tst-tlsmod16b.so\00"
@RTLD_NOW = global i32 0
@.str.4 = [48 x i8] c"unexpectedly succeeded to open tst-tlsmod16b.so\00"
@.str.5 = [7 x i8] c"in_dso\00"
@.str.6 = [19 x i8] c"cannot find in_dso\00"
@.str.7 = [29 x i8] c"returned values do not match\00"
define i32 @do_test() {
%1 = load i32, i32* @RTLD_LAZY
%2 = load i32, i32* @RTLD_GLOBAL
%3 = or i32 %2, %1
%4 = tail call i8* @dlopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = tail call i8* @dlsym(i8* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%11 = load i32, i32* @RTLD_NOW
%12 = load i32, i32* @RTLD_GLOBAL
%13 = or i32 %12, %11
%14 = tail call i8* @dlopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %30, label %16
16: ; preds = %9
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0))
%18 = tail call i8* @dlsym(i8* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = bitcast i8* %18 to i8* ()*
%25 = tail call i8* %24()
%26 = icmp eq i8* %25, %10
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %23, %9
ret i32 0
}