#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*/ puts(char *);
int do_test(void) {
  void *h = dlopen("tst-tlsmod17b.so", RTLD_LAZY);
  if (h == NULL) {
    puts("unexpectedly failed to open tst-tlsmod17b.so");
    exit(1);
  }
  int (*fp)(void) = (int (*)(void))dlsym(h, "tlsmod17b");
  if (fp == NULL) {
    puts("cannot find tlsmod17b");
    exit(1);
  }
  if (fp())
    exit(1);
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    0xec4(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1182 <do_test+0x42>
lea    0xe8d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xeac(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b7 <do_test+0x77>
lea    0xe85(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
call   *-0x10(%rbp)
cmp    $0x0,%eax
je     11cd <do_test+0x8d>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rax
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
lea    0xecb(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1179 <do_test+0x39>
lea    0xed7(%rip),%rsi
mov    %rax,%rdi
call   1050 <dlsym@plt>
test   %rax,%rax
je     1182 <do_test+0x42>
call   *%rax
test   %eax,%eax
jne    118e <do_test+0x4e>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe80(%rip),%rdi
jmp    1189 <do_test+0x49>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"tst-tlsmod17b.so\00"
@RTLD_LAZY = global i32 0
@.str.1 = [45 x i8] c"unexpectedly failed to open tst-tlsmod17b.so\00"
@.str.2 = [10 x i8] c"tlsmod17b\00"
@.str.3 = [22 x i8] c"cannot find tlsmod17b\00"
define i32 @do_test() {
%1 = alloca i8*
%2 = alloca i32 ()*
%3 = load i32, i32* @RTLD_LAZY
%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 eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @puts(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i8*, i8** %1
%12 = call i64 @dlsym(i8* %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%13 = inttoptr i64 %12 to i32 ()*
store i32 ()* %13, i32 ()** %2
%14 = load i32 ()*, i32 ()** %2
%15 = icmp eq i32 ()* %14, null
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = load i32 ()*, i32 ()** %2
%21 = call i32 %20()
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %19
ret i32 0
}

LLVM IR (-O2):
@.str = [17 x i8] c"tst-tlsmod17b.so\00"
@RTLD_LAZY = global i32 0
@.str.1 = [45 x i8] c"unexpectedly failed to open tst-tlsmod17b.so\00"
@.str.2 = [10 x i8] c"tlsmod17b\00"
@.str.3 = [22 x i8] c"cannot find tlsmod17b\00"
define i32 @do_test() {
%1 = load i32, i32* @RTLD_LAZY
%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 %4, label %7
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = tail call i64 @dlsym(i8* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %7
%14 = inttoptr i64 %8 to i32 ()*
%15 = tail call i32 %14()
%16 = icmp eq i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %13
ret i32 0
}