#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 */
typedef void *(*funcType)(char *, void **);
typedef void *callFunction;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ RTLD_LAZY;
scalar_t__ checkForError(int /*<<< orphan*/);
int /*<<< orphan*/ dlclose(void *);
int /*<<< orphan*/ dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
void *doFunction(char *fName, void **argv) {
  void *handle;
  void *funcPtr;
  void *returnVal;
  FILE *filePtr = NULL;
  typedef void *(*funcType)(char *, void **);
  if (1) {
    handle = dlopen("lib/libcallFunction.dll", RTLD_LAZY);
  } else {
    handle = dlopen("so/libcallFunction.so", RTLD_LAZY);
  }
  if (!handle) {
    checkForError(dlerror());
    return NULL;
  }
  dlerror();
  funcPtr = dlsym(handle, "callFunction");
  if (checkForError(dlerror()) == 0) {
    funcType callFunction = (funcType)funcPtr;
    returnVal = (callFunction)(fName, argv);
  }
  dlclose(handle);
  handle = NULL;
  return returnVal;
}


x86 assembly (clang, -O0):
<doFunction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a7 <doFunction+0x57>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%edi
call   1040 <checkForError@plt>
movq   $0x0,-0x8(%rbp)
jmp    120d <doFunction+0xbd>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x20(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%edi
call   1040 <checkForError@plt>
cmp    $0x0,%rax
jne    11f4 <doFunction+0xa4>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   *%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <dlclose@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doFunction>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rbx,%rbx
je     11be <doFunction+0x6e>
lea    0xe8e(%rip),%rsi
mov    %rbx,%rdi
call   1060 <dlsym@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %eax,%edi
call   1040 <checkForError@plt>
test   %rax,%rax
jne    11b4 <doFunction+0x64>
mov    %r15,%rdi
mov    %r14,%rsi
call   *%r12
mov    %rax,%r13
mov    %rbx,%rdi
call   1070 <dlclose@plt>
jmp    11c8 <doFunction+0x78>
mov    %eax,%edi
call   1040 <checkForError@plt>
xor    %r13d,%r13d
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [24 x i8] c"lib/libcallFunction.dll\00"
@RTLD_LAZY = global i32 0
@.str.1 = [13 x i8] c"callFunction\00"
define i8* @doFunction(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i8* (i8*, i8**)*
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32* null, i32** %9
%11 = load i32, i32* @RTLD_LAZY
%12 = call i8* @dlopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %11)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = call i32 (...) @dlerror()
%17 = call i64 @checkForError(i32 %16)
store i8* null, i8** %3
br label %36
18: ; preds = %2
%19 = call i32 (...) @dlerror()
%20 = load i8*, i8** %6
%21 = call i8* @dlsym(i8* %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i8* %21, i8** %7
%22 = call i32 (...) @dlerror()
%23 = call i64 @checkForError(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %18
%26 = load i8*, i8** %7
%27 = bitcast i8* %26 to i8* (i8*, i8**)*
store i8* (i8*, i8**)* %27, i8* (i8*, i8**)** %10
%28 = load i8* (i8*, i8**)*, i8* (i8*, i8**)** %10
%29 = load i8*, i8** %4
%30 = load i8**, i8*** %5
%31 = call i8* %28(i8* %29, i8** %30)
store i8* %31, i8** %8
br label %32
32: ; preds = %25, %18
%33 = load i8*, i8** %6
%34 = call i32 @dlclose(i8* %33)
store i8* null, i8** %6
%35 = load i8*, i8** %8
store i8* %35, i8** %3
br label %36
36: ; preds = %32, %15
%37 = load i8*, i8** %3
ret i8* %37
}

LLVM IR (-O2):
@.str = [24 x i8] c"lib/libcallFunction.dll\00"
@RTLD_LAZY = global i32 0
@.str.1 = [13 x i8] c"callFunction\00"
define i8* @doFunction(i8* %0, i8** %1) {
%3 = load i32, i32* @RTLD_LAZY
%4 = tail call i8* @dlopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i8* %4, null
%6 = tail call i32 (...) @dlerror()
br i1 %5, label %7, label %9
7: ; preds = %2
%8 = tail call i64 @checkForError(i32 %6)
br label %20
9: ; preds = %2
%10 = tail call i8* @dlsym(i8* %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 (...) @dlerror()
%12 = tail call i64 @checkForError(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = bitcast i8* %10 to i8* (i8*, i8**)*
%16 = tail call i8* %15(i8* %0, i8** %1)
br label %17
17: ; preds = %14, %9
%18 = phi i8* [ %16, %14 ], [ undef, %9 ]
%19 = tail call i32 @dlclose(i8* %4)
br label %20
20: ; preds = %17, %7
%21 = phi i8* [ %18, %17 ], [ null, %7 ]
ret i8* %21
}