#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 /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int);
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ printf(char *, char *);
void stub1();
int main(void) {
  int ret = 0;
  void *handle;
  void (*fcn)(void);
  handle = dlopen("./tmpdir/libdl6a.so", RTLD_GLOBAL | RTLD_LAZY);
  if (!handle) {
    printf("dlopen ./tmpdir/libdl6a.so: %s\n", dlerror());
    return 1;
  }
  fcn = (void (*)(void))dlsym(handle, "foo");
  if (!fcn) {
    printf("dlsym foo: %s\n", dlerror());
    ret += 1;
  } else {
    (*fcn)();
  }
  dlclose(handle);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    0x2e62(%rip),%rax
or     (%rax),%esi
lea    0xe81(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b5 <main+0x65>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe70(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    120a <main+0xba>
mov    -0x10(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f8 <main+0xa8>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe53(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11fb <main+0xab>
call   *-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <dlclose@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e8e(%rip),%rax
mov    0x2e77(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe94(%rip),%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
je     1193 <main+0x43>
mov    %rax,%rbx
lea    0xeb4(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     11b0 <main+0x60>
call   *%rax
xor    %ebp,%ebp
jmp    11cb <main+0x7b>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe73(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0x1,%ebp
jmp    11d3 <main+0x83>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe7a(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1070 <dlclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"./tmpdir/libdl6a.so\00"
@RTLD_GLOBAL = global i32 0
@RTLD_LAZY = global i32 0
@.str.1 = [32 x i8] c"dlopen ./tmpdir/libdl6a.so: %s\0A\00"
@.str.2 = [4 x i8] c"foo\00"
@.str.3 = [15 x i8] c"dlsym foo: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca void ()*
store i32 0, i32* %1
store i32 0, i32* %2
%5 = load i32, i32* @RTLD_GLOBAL
%6 = load i32, i32* @RTLD_LAZY
%7 = or i32 %5, %6
%8 = call i8* @dlopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %7)
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %0
%12 = call i8* (...) @dlerror()
%13 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %12)
store i32 1, i32* %1
br label %31
14: ; preds = %0
%15 = load i8*, i8** %3
%16 = call i64 @dlsym(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%17 = inttoptr i64 %16 to void ()*
store void ()* %17, void ()** %4
%18 = load void ()*, void ()** %4
%19 = icmp ne void ()* %18, null
br i1 %19, label %25, label %20
20: ; preds = %14
%21 = call i8* (...) @dlerror()
%22 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %21)
%23 = load i32, i32* %2
%24 = add nsw i32 %23, 1
store i32 %24, i32* %2
br label %27
25: ; preds = %14
%26 = load void ()*, void ()** %4
br label %27
27: ; preds = %25, %20
%28 = load i8*, i8** %3
%29 = call i32 @dlclose(i8* %28)
%30 = load i32, i32* %2
store i32 %30, i32* %1
br label %31
31: ; preds = %27, %11
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [20 x i8] c"./tmpdir/libdl6a.so\00"
@RTLD_GLOBAL = global i32 0
@RTLD_LAZY = global i32 0
@.str.1 = [32 x i8] c"dlopen ./tmpdir/libdl6a.so: %s\0A\00"
@.str.2 = [4 x i8] c"foo\00"
@.str.3 = [15 x i8] c"dlsym foo: %s\0A\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_GLOBAL
%2 = load i32, i32* @RTLD_LAZY
%3 = or i32 %2, %1
%4 = tail call i8* @dlopen(i8* getelementptr inbounds ([20 x i8], [20 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 i8* (...) @dlerror()
%8 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %7)
br label %20
9: ; preds = %0
%10 = tail call i64 @dlsym(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i8* (...) @dlerror()
%14 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %13)
br label %17
15: ; preds = %9
%16 = inttoptr i64 %10 to void ()*
tail call void %16()
br label %17
17: ; preds = %15, %12
%18 = phi i32 [ 0, %15 ], [ 1, %12 ]
%19 = tail call i32 @dlclose(i8* %4)
br label %20
20: ; preds = %17, %6
%21 = phi i32 [ %18, %17 ], [ 1, %6 ]
ret i32 %21
}