#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 */
scalar_t__ RTLD_GLOBAL;
scalar_t__ RTLD_LAZY;
char *dlerror();
void *dlopen(char *, scalar_t__);
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, char *);
int ThisDllObj(int mode, int fprint, char *dll, char *name) {
  void *handle;
  int ad = 0;
  if ((mode == 0x3) || (mode == 0x4)) {
    if ((handle = dlopen(dll, RTLD_LAZY + RTLD_GLOBAL)) == NULL) {
      printf("ThisDllObj: lib %s not found\n", dll);
      printf(" - dlerror: %s\n", dlerror());
      exit(-1);
    } else {
      ad = (int)dlsym((void *)handle, name);
      if (ad == 0) {
        printf("ThisDllObj: symbol %s not found\n", name);
        exit(-1);
      }
    }
  }
  return ad;
}


x86 assembly (clang, -O0):
<ThisDllObj>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x3,-0x4(%rbp)
je     1181 <ThisDllObj+0x31>
cmpl   $0x4,-0x4(%rbp)
jne    121a <ThisDllObj+0xca>
mov    -0x10(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%rsi
mov    0x2e52(%rip),%rax
add    (%rax),%rsi
call   1050 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11dc <ThisDllObj+0x8c>
mov    -0x10(%rbp),%rsi
lea    0xe49(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe51(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <dlsym@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1210 <ThisDllObj+0xc0>
mov    -0x18(%rbp),%rsi
lea    0xe2d(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    1215 <ThisDllObj+0xc5>
jmp    121a <ThisDllObj+0xca>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ThisDllObj>:
push   %r14
push   %rbx
push   %rax
add    $0xfffffffd,%edi
xor    %eax,%eax
cmp    $0x1,%edi
ja     1194 <ThisDllObj+0x44>
mov    %rcx,%r14
mov    %rdx,%rbx
mov    0x2e6d(%rip),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%rsi
add    (%rax),%rsi
mov    %rdx,%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
je     119c <ThisDllObj+0x4c>
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <dlsym@plt>
test   %eax,%eax
je     11be <ThisDllObj+0x6e>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe5d(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe65(%rip),%rdi
mov    %rax,%rsi
jmp    11c8 <ThisDllObj+0x78>
lea    0xe69(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@RTLD_LAZY = global i64 0
@RTLD_GLOBAL = global i64 0
@.str = [30 x i8] c"ThisDllObj: lib %s not found\0A\00"
@.str.1 = [16 x i8] c" - dlerror: %s\0A\00"
@.str.2 = [33 x i8] c"ThisDllObj: symbol %s not found\0A\00"
define i32 @ThisDllObj(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
store i32 0, i32* %10
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, 3
br i1 %12, label %16, label %13
13: ; preds = %4
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, 4
br i1 %15, label %16, label %42
16: ; preds = %13, %4
%17 = load i8*, i8** %7
%18 = load i64, i64* @RTLD_LAZY
%19 = load i64, i64* @RTLD_GLOBAL
%20 = add nsw i64 %18, %19
%21 = call i8* @dlopen(i8* %17, i64 %20)
store i8* %21, i8** %9
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %29
23: ; preds = %16
%24 = load i8*, i8** %7
%25 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = call i8* (...) @dlerror()
%27 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %16
%30 = load i8*, i8** %9
%31 = load i8*, i8** %8
%32 = call i64 @dlsym(i8* %30, i8* %31)
%33 = trunc i64 %32 to i32
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = load i8*, i8** %8
%38 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %29
br label %41
41: ; preds = %40
br label %42
42: ; preds = %41, %13
%43 = load i32, i32* %10
ret i32 %43
}

LLVM IR (-O2):
@RTLD_LAZY = global i64 0
@RTLD_GLOBAL = global i64 0
@.str = [30 x i8] c"ThisDllObj: lib %s not found\0A\00"
@.str.1 = [16 x i8] c" - dlerror: %s\0A\00"
@.str.2 = [33 x i8] c"ThisDllObj: symbol %s not found\0A\00"
define i32 @ThisDllObj(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = add i32 %0, -3
%6 = icmp ult i32 %5, 2
br i1 %6, label %7, label %25
7: ; preds = %4
%8 = load i64, i64* @RTLD_LAZY
%9 = load i64, i64* @RTLD_GLOBAL
%10 = add nsw i64 %9, %8
%11 = tail call i8* @dlopen(i8* %2, i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %7
%14 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %2)
%15 = tail call i8* (...) @dlerror()
%16 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %7
%19 = tail call i64 @dlsym(i8* %11, i8* %3)
%20 = trunc i64 %19 to i32
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%24 = tail call i32 @exit(i32 -1)
unreachable
25: ; preds = %18, %4
%26 = phi i32 [ %20, %18 ], [ 0, %4 ]
ret i32 %26
}