#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_LAZY;
char *dlerror();
void *dlopen(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *, char *);
int createDllHandle(void **dllHandle, const char *dllPath) {
  void *pdlHandle;
  char *pszErr;
  *dllHandle = NULL;
  pdlHandle = dlopen(dllPath, RTLD_LAZY);
  pszErr = dlerror();
  if (!pdlHandle || pszErr) {
    printf("open dll from %s failed when call dlopen,error:%s\n", dllPath,
           pszErr == NULL ? "NULL" : pszErr);
    return -1;
  }
  *dllHandle = pdlHandle;
  return 0;
}


x86 assembly (clang, -O0):
<createDllHandle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1182 <createDllHandle+0x52>
cmpq   $0x0,-0x28(%rbp)
je     11cd <createDllHandle+0x9d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a5 <createDllHandle+0x75>
lea    0xe97(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    11ad <createDllHandle+0x7d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
lea    0xe44(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <createDllHandle+0xaf>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createDllHandle>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
movq   $0x0,(%rdi)
mov    0x2e97(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1050 <dlopen@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rbx,%rbx
je     116e <createDllHandle+0x3e>
test   %rax,%rax
jne    116e <createDllHandle+0x3e>
mov    %rbx,(%r15)
xor    %eax,%eax
jmp    1190 <createDllHandle+0x60>
test   %rax,%rax
lea    0xebb(%rip),%rdx
cmovne %rax,%rdx
lea    0xe7d(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@RTLD_LAZY = global i32 0
@.str = [51 x i8] c"open dll from %s failed when call dlopen,error:%s\0A\00"
@.str.1 = [5 x i8] c"NULL\00"
define i32 @createDllHandle(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8** %0, i8*** %4
store i8* %1, i8** %5
%8 = load i8**, i8*** %4
store i8* null, i8** %8
%9 = load i8*, i8** %5
%10 = load i32, i32* @RTLD_LAZY
%11 = call i8* @dlopen(i8* %9, i32 %10)
store i8* %11, i8** %6
%12 = call i8* (...) @dlerror()
store i8* %12, i8** %7
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %28
18: ; preds = %15, %2
%19 = load i8*, i8** %5
%20 = load i8*, i8** %7
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %18
br label %25
23: ; preds = %18
%24 = load i8*, i8** %7
br label %25
25: ; preds = %23, %22
%26 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %22 ], [ %24, %23 ]
%27 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %26)
store i32 -1, i32* %3
br label %31
28: ; preds = %15
%29 = load i8*, i8** %6
%30 = load i8**, i8*** %4
store i8* %29, i8** %30
store i32 0, i32* %3
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@RTLD_LAZY = global i32 0
@.str = [51 x i8] c"open dll from %s failed when call dlopen,error:%s\0A\00"
@.str.1 = [5 x i8] c"NULL\00"
define i32 @createDllHandle(i8** nocapture writeonly %0, i8* %1) {
store i8* null, i8** %0
%3 = load i32, i32* @RTLD_LAZY
%4 = tail call i8* @dlopen(i8* %1, i32 %3)
%5 = tail call i8* (...) @dlerror()
%6 = icmp eq i8* %4, null
%7 = icmp ne i8* %5, null
%8 = select i1 %6, i1 true, i1 %7
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = icmp eq i8* %5, null
%11 = select i1 %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %5
%12 = tail call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %11)
br label %14
13: ; preds = %2
store i8* %4, i8** %0
br label %14
14: ; preds = %13, %9
%15 = phi i32 [ -1, %9 ], [ 0, %13 ]
ret i32 %15
}