#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;
int /*<<< orphan*/ dlerror();
void *dlopen(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
void *dbgsysLoadLibrary(const char *name, char *err_buf, int err_buflen) {
  void *result;
  result = dlopen(name, RTLD_LAZY);
  if (result == NULL) {
    (void)strncpy(err_buf, dlerror(), err_buflen - 2);
    err_buf[err_buflen - 1] = '\0';
  }
  return result;
}


x86 assembly (clang, -O0):
<dbgsysLoadLibrary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1195 <dbgsysLoadLibrary+0x65>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
mov    -0x14(%rbp),%edx
sub    $0x2,%edx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dbgsysLoadLibrary>:
push   %r15
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    116f <dbgsysLoadLibrary+0x3f>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    -0x2(%r14),%edx
mov    %r15,%rdi
mov    %eax,%esi
call   1040 <strncpy@plt>
movslq %r14d,%rax
movb   $0x0,-0x1(%rax,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@RTLD_LAZY = global i32 0
define i8* @dbgsysLoadLibrary(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = load i32, i32* @RTLD_LAZY
%10 = call i8* @dlopen(i8* %8, i32 %9)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %24
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i32 (...) @dlerror()
%16 = load i32, i32* %6
%17 = sub nsw i32 %16, 2
%18 = call i32 @strncpy(i8* %14, i32 %15, i32 %17)
%19 = load i8*, i8** %5
%20 = load i32, i32* %6
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %13, %3
%25 = load i8*, i8** %7
ret i8* %25
}

LLVM IR (-O2):
@RTLD_LAZY = global i32 0
define i8* @dbgsysLoadLibrary(i8* %0, i8* %1, i32 %2) {
%4 = load i32, i32* @RTLD_LAZY
%5 = tail call i8* @dlopen(i8* %0, i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %14
7: ; preds = %3
%8 = tail call i32 (...) @dlerror()
%9 = add nsw i32 %2, -2
%10 = tail call i32 @strncpy(i8* %1, i32 %8, i32 %9)
%11 = add nsw i32 %2, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %7, %3
ret i8* %5
}