#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;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
char *dlsym(void *, char *);
int /*<<< orphan*/ printf(char *, char *);
int main(void) {
  void *handle;
  char *c;
  if ((handle = dlopen("bug-dlsym1-lib1.so", RTLD_NOW)) == NULL) {
    printf("dlopen(\"bug-dlsym1-lib1.so\"): %s\n", dlerror());
    abort();
  }
  if ((c = dlsym(handle, "dlopen_test_variable")) == NULL) {
    printf("dlsym(handle, \"dlopen_test_variable\"): %s\n", dlerror());
    abort();
  }
  (void)dlclose(handle);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11af <main+0x4f>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe70(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x10(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1070 <dlsym@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11ea <main+0x8a>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe6c(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x10(%rbp),%rdi
call   1080 <dlclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     119e <main+0x3e>
mov    %rax,%rbx
lea    0xeb0(%rip),%rsi
mov    %rax,%rdi
call   1070 <dlsym@plt>
test   %rax,%rax
je     11ae <main+0x4e>
mov    %rbx,%rdi
call   1080 <dlclose@plt>
xor    %eax,%eax
pop    %rbx
ret
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe67(%rip),%rdi
jmp    11bc <main+0x5c>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe8e(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"bug-dlsym1-lib1.so\00"
@RTLD_NOW = global i32 0
@.str.1 = [34 x i8] c"dlopen(\22bug-dlsym1-lib1.so\22): %s\0A\00"
@.str.2 = [21 x i8] c"dlopen_test_variable\00"
@.str.3 = [43 x i8] c"dlsym(handle, \22dlopen_test_variable\22): %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
%4 = load i32, i32* @RTLD_NOW
%5 = call i8* @dlopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %4)
store i8* %5, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i8* (...) @dlerror()
%9 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = call i32 (...) @abort()
unreachable
11: ; preds = %0
%12 = load i8*, i8** %2
%13 = call i8* @dlsym(i8* %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i8* %13, i8** %3
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = call i8* (...) @dlerror()
%17 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i8* %16)
%18 = call i32 (...) @abort()
unreachable
19: ; preds = %11
%20 = load i8*, i8** %2
%21 = call i32 @dlclose(i8* %20)
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"bug-dlsym1-lib1.so\00"
@RTLD_NOW = global i32 0
@.str.1 = [34 x i8] c"dlopen(\22bug-dlsym1-lib1.so\22): %s\0A\00"
@.str.2 = [21 x i8] c"dlopen_test_variable\00"
@.str.3 = [43 x i8] c"dlsym(handle, \22dlopen_test_variable\22): %s\0A\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_NOW
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = tail call i8* (...) @dlerror()
%6 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%7 = tail call i32 (...) @abort()
unreachable
8: ; preds = %0
%9 = tail call i8* @dlsym(i8* %2, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = tail call i8* (...) @dlerror()
%13 = tail call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%14 = tail call i32 (...) @abort()
unreachable
15: ; preds = %8
%16 = tail call i32 @dlclose(i8* %2)
ret i32 0
}