#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;
int /*<<< orphan*/ dlclose(void *);
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
scalar_t__ foo1();
int /*<<< orphan*/ printf(char *);
int main() {
  void *dl;
  void *sym;
  int (*func)(void);
  if (foo1() != 0)
    return 1;
  dl = dlopen("pr21964-2b.so", RTLD_LAZY);
  if (!dl)
    return 2;
  sym = dlsym(dl, "__start___verbose");
  if (!sym)
    return 3;
  func = dlsym(dl, "foo2");
  if (!func)
    return 4;
  if (func() == 0)
    printf("PASS\n");
  dlclose(dl);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <foo1@plt>
cmp    $0x0,%rax
je     117c <main+0x2c>
movl   $0x1,-0x4(%rbp)
jmp    122a <main+0xda>
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ac <main+0x5c>
movl   $0x2,-0x4(%rbp)
jmp    122a <main+0xda>
mov    -0x10(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d7 <main+0x87>
movl   $0x3,-0x4(%rbp)
jmp    122a <main+0xda>
mov    -0x10(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1202 <main+0xb2>
movl   $0x4,-0x4(%rbp)
jmp    122a <main+0xda>
call   *-0x20(%rbp)
cmp    $0x0,%eax
jne    121a <main+0xca>
lea    0xe10(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1070 <dlclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1060 <foo1@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
je     1167 <main+0x17>
pop    %rbx
ret
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11ca <main+0x7a>
mov    %rax,%rbx
lea    0xe83(%rip),%rsi
mov    %rax,%rdi
call   1050 <dlsym@plt>
test   %rax,%rax
je     11d1 <main+0x81>
lea    0xe81(%rip),%rsi
mov    %rbx,%rdi
call   1050 <dlsym@plt>
test   %rax,%rax
je     11d8 <main+0x88>
call   *%rax
test   %eax,%eax
jne    11be <main+0x6e>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1070 <dlclose@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    $0x2,%eax
pop    %rbx
ret
mov    $0x3,%eax
pop    %rbx
ret
mov    $0x4,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [14 x i8] c"pr21964-2b.so\00"
@RTLD_LAZY = global i32 0
@.str.1 = [18 x i8] c"__start___verbose\00"
@.str.2 = [5 x i8] c"foo2\00"
@.str.3 = [6 x i8] c"PASS\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32 ()*
store i32 0, i32* %1
%5 = call i64 (...) @foo1()
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 1, i32* %1
br label %36
8: ; preds = %0
%9 = load i32, i32* @RTLD_LAZY
%10 = call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9)
store i8* %10, i8** %2
%11 = load i8*, i8** %2
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %8
store i32 2, i32* %1
br label %36
14: ; preds = %8
%15 = load i8*, i8** %2
%16 = call i8* @dlsym(i8* %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %14
store i32 3, i32* %1
br label %36
20: ; preds = %14
%21 = load i8*, i8** %2
%22 = call i8* @dlsym(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%23 = bitcast i8* %22 to i32 ()*
store i32 ()* %23, i32 ()** %4
%24 = load i32 ()*, i32 ()** %4
%25 = icmp ne i32 ()* %24, null
br i1 %25, label %27, label %26
26: ; preds = %20
store i32 4, i32* %1
br label %36
27: ; preds = %20
%28 = load i32 ()*, i32 ()** %4
%29 = call i32 %28()
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
%34 = load i8*, i8** %2
%35 = call i32 @dlclose(i8* %34)
store i32 0, i32* %1
br label %36
36: ; preds = %33, %26, %19, %13, %7
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
@.str = [14 x i8] c"pr21964-2b.so\00"
@RTLD_LAZY = global i32 0
@.str.1 = [18 x i8] c"__start___verbose\00"
@.str.2 = [5 x i8] c"foo2\00"
@str = [5 x i8] c"PASS\00"
define i32 @main() {
%1 = tail call i64 (...) @foo1()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %21
3: ; preds = %0
%4 = load i32, i32* @RTLD_LAZY
%5 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %21, label %7
7: ; preds = %3
%8 = tail call i8* @dlsym(i8* %5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %21, label %10
10: ; preds = %7
%11 = tail call i8* @dlsym(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %21, label %13
13: ; preds = %10
%14 = bitcast i8* %11 to i32 ()*
%15 = tail call i32 %14()
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %19
19: ; preds = %17, %13
%20 = tail call i32 @dlclose(i8* %5)
br label %21
21: ; preds = %10, %7, %3, %0, %19
%22 = phi i32 [ 0, %19 ], [ 1, %0 ], [ 2, %3 ], [ 3, %7 ], [ 4, %10 ]
ret i32 %22
}