#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*/ dlclose(void *);
char *dlerror();
void *dlopen(char const *, int);
void *dlsym(void *, char const *);
int /*<<< orphan*/ printf(char *, char const *, char *);
void load_and_sym(const char *path, const char *sym, int flags) {
  void *lib = dlopen(path, flags);
  printf("dlopen %s %s\n", (path == NULL) ? "<NULL>" : path,
         (lib == NULL) ? "failure" : "success");
  if (lib == NULL) {
    printf("dlerror loading %s: %s\n", (path == NULL) ? "<NULL>" : path,
           dlerror());
  } else {
    if (sym != NULL) {
      void *addr = dlsym(lib, sym);
      printf("dlsym %s %s\n", sym, (addr == NULL) ? "failure" : "success");
    }
    dlclose(lib);
  }
}


x86 assembly (clang, -O0):
<load_and_sym>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    118e <load_and_sym+0x3e>
lea    0xe89(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    1196 <load_and_sym+0x46>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    0xe78(%rip),%rdx
lea    0xe69(%rip),%rax
cmp    $0x0,%rcx
cmove  %rax,%rdx
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1215 <load_and_sym+0xc5>
cmpq   $0x0,-0x8(%rbp)
jne    11e6 <load_and_sym+0x96>
lea    0xe31(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    11ee <load_and_sym+0x9e>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
lea    0xe1a(%rip),%rdi
call   1040 <printf@plt>
jmp    1264 <load_and_sym+0x114>
cmpq   $0x0,-0x10(%rbp)
je     125b <load_and_sym+0x10b>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rcx
lea    0xddd(%rip),%rdx
lea    0xdce(%rip),%rax
cmp    $0x0,%rcx
cmove  %rax,%rdx
lea    0xde7(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <dlclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_and_sym>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %edx,%esi
call   1050 <dlopen@plt>
mov    %rax,%r12
test   %rbx,%rbx
lea    0xe9b(%rip),%r13
cmovne %rbx,%r13
test   %rax,%rax
lea    0xe94(%rip),%rbx
lea    0xe95(%rip),%r15
mov    %r15,%rdx
cmove  %rbx,%rdx
lea    0xe6a(%rip),%rdi
mov    %r13,%rsi
call   1040 <printf@plt>
test   %r12,%r12
je     11dd <load_and_sym+0x8d>
test   %r14,%r14
je     11cc <load_and_sym+0x7c>
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <dlsym@plt>
test   %rax,%rax
cmove  %rbx,%r15
lea    0xe7c(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <printf@plt>
mov    %r12,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1070 <dlclose@plt>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe3a(%rip),%rdi
mov    %r13,%rsi
mov    %rax,%rdx
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"dlopen %s %s\0A\00"
@.str.1 = [7 x i8] c"<NULL>\00"
@.str.2 = [8 x i8] c"failure\00"
@.str.3 = [8 x i8] c"success\00"
@.str.4 = [24 x i8] c"dlerror loading %s: %s\0A\00"
@.str.5 = [13 x i8] c"dlsym %s %s\0A\00"
define void @load_and_sym(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = load i32, i32* %6
%11 = call i8* @dlopen(i8* %9, i32 %10)
store i8* %11, i8** %7
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
br label %17
15: ; preds = %3
%16 = load i8*, i8** %4
br label %17
17: ; preds = %15, %14
%18 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %14 ], [ %16, %15 ]
%19 = load i8*, i8** %7
%20 = icmp eq i8* %19, null
%21 = zext i1 %20 to i64
%22 = select i1 %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0)
%23 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %22)
%24 = load i8*, i8** %7
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %36
26: ; preds = %17
%27 = load i8*, i8** %4
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %26
br label %32
30: ; preds = %26
%31 = load i8*, i8** %4
br label %32
32: ; preds = %30, %29
%33 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %29 ], [ %31, %30 ]
%34 = call i8* (...) @dlerror()
%35 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %33, i8* %34)
br label %52
36: ; preds = %17
%37 = load i8*, i8** %5
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %49
39: ; preds = %36
%40 = load i8*, i8** %7
%41 = load i8*, i8** %5
%42 = call i8* @dlsym(i8* %40, i8* %41)
store i8* %42, i8** %8
%43 = load i8*, i8** %5
%44 = load i8*, i8** %8
%45 = icmp eq i8* %44, null
%46 = zext i1 %45 to i64
%47 = select i1 %45, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0)
%48 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %43, i8* %47)
br label %49
49: ; preds = %39, %36
%50 = load i8*, i8** %7
%51 = call i32 @dlclose(i8* %50)
br label %52
52: ; preds = %49, %32
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"dlopen %s %s\0A\00"
@.str.1 = [7 x i8] c"<NULL>\00"
@.str.2 = [8 x i8] c"failure\00"
@.str.3 = [8 x i8] c"success\00"
@.str.4 = [24 x i8] c"dlerror loading %s: %s\0A\00"
@.str.5 = [13 x i8] c"dlsym %s %s\0A\00"
define void @load_and_sym(i8* %0, i8* %1, i32 %2) {
%4 = tail call i8* @dlopen(i8* %0, i32 %2)
%5 = icmp eq i8* %0, null
%6 = select i1 %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0
%7 = icmp eq i8* %4, null
%8 = select i1 %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %6, i8* %8)
br i1 %7, label %10, label %13
10: ; preds = %3
%11 = tail call i8* (...) @dlerror()
%12 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %6, i8* %11)
br label %22
13: ; preds = %3
%14 = icmp eq i8* %1, null
br i1 %14, label %20, label %15
15: ; preds = %13
%16 = tail call i8* @dlsym(i8* %4, i8* %1)
%17 = icmp eq i8* %16, null
%18 = select i1 %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0)
%19 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %1, i8* %18)
br label %20
20: ; preds = %15, %13
%21 = tail call i32 @dlclose(i8* %4)
br label %22
22: ; preds = %20, %10
ret void
}