#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*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *dlsym(void *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int main(void) {
  void *lib;
  lib = dlopen("/data/local/tmp/ndk-tests/libbar.so", RTLD_NOW);
  if (lib == NULL) {
    fprintf(stderr, "Could not dlopen(\"libbar.so\"): %s\n", dlerror());
    return 1;
  }
  if (dlsym(lib, "foo2") == NULL) {
    fprintf(stderr, "Symbol 'foo2' is missing from shared library!!\n");
    return 2;
  }
  dlclose(lib);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b6 <main+0x66>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe81(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1203 <main+0xb3>
mov    -0x10(%rbp),%rdi
lea    0xe86(%rip),%rsi
call   1060 <dlsym@plt>
cmp    $0x0,%rax
jne    11f3 <main+0xa3>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1203 <main+0xb3>
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
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     118e <main+0x3e>
mov    %rax,%rbx
lea    0xed2(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     11b8 <main+0x68>
mov    %rbx,%rdi
call   1070 <dlclose@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e43(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe7f(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
ret
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [36 x i8] c"/data/local/tmp/ndk-tests/libbar.so\00"
@RTLD_NOW = global i32 0
@stderr = global i32 0
@.str.1 = [35 x i8] c"Could not dlopen(\22libbar.so\22): %s\0A\00"
@.str.2 = [5 x i8] c"foo2\00"
@.str.3 = [48 x i8] c"Symbol 'foo2' is missing from shared library!!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
store i32 0, i32* %1
%3 = load i32, i32* @RTLD_NOW
%4 = call i8* @dlopen(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %3)
store i8* %4, i8** %2
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i8* (...) @dlerror()
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %9)
store i32 1, i32* %1
br label %21
11: ; preds = %0
%12 = load i8*, i8** %2
%13 = call i32* @dlsym(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %21
18: ; preds = %11
%19 = load i8*, i8** %2
%20 = call i32 @dlclose(i8* %19)
store i32 0, i32* %1
br label %21
21: ; preds = %18, %15, %7
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
@.str = [36 x i8] c"/data/local/tmp/ndk-tests/libbar.so\00"
@RTLD_NOW = global i32 0
@stderr = global i32 0
@.str.1 = [35 x i8] c"Could not dlopen(\22libbar.so\22): %s\0A\00"
@.str.2 = [5 x i8] c"foo2\00"
@.str.3 = [48 x i8] c"Symbol 'foo2' is missing from shared library!!\0A\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_NOW
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([36 x i8], [36 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 = load i32, i32* @stderr
%6 = tail call i8* (...) @dlerror()
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %16
8: ; preds = %0
%9 = tail call i32* @dlsym(i8* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
br label %16
14: ; preds = %8
%15 = tail call i32 @dlclose(i8* %2)
br label %16
16: ; preds = %14, %11, %4
%17 = phi i32 [ 1, %4 ], [ 2, %11 ], [ 0, %14 ]
ret i32 %17
}