#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 */
char *dlerror();
char *strstr(char const *, char *);
const char *mdp_dlerror(void) {
  const char *err = dlerror();
  if (!err)
    return "Unknown error.";
  const char *endofpath = strstr(err, ".so"
                                      ": ");
  if (!endofpath)
    return err;
  return (endofpath + sizeof(".so") + 1);
}


x86 assembly (clang, -O0):
<mdp_dlerror>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114e <mdp_dlerror+0x2e>
lea    0xebb(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    118a <mdp_dlerror+0x6a>
mov    -0x10(%rbp),%rdi
lea    0xeb6(%rip),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117a <mdp_dlerror+0x5a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    118a <mdp_dlerror+0x6a>
mov    -0x18(%rbp),%rax
add    $0x4,%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mdp_dlerror>:
push   %rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
je     114f <mdp_dlerror+0x2f>
mov    %rax,%rbx
lea    0xed8(%rip),%rsi
mov    %rax,%rdi
call   1040 <strstr@plt>
mov    %rax,%rcx
add    $0x5,%rax
test   %rcx,%rcx
cmove  %rbx,%rax
pop    %rbx
ret
lea    0xeaa(%rip),%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Unknown error.\00"
@.str.1 = [6 x i8] c".so: \00"
define i8* @mdp_dlerror() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = call i8* (...) @dlerror()
store i8* %4, i8** %2
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %0
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %1
br label %19
8: ; preds = %0
%9 = load i8*, i8** %2
%10 = call i8* @strstr(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = icmp ne i8* %11, null
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = load i8*, i8** %2
store i8* %14, i8** %1
br label %19
15: ; preds = %8
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i64 4
%18 = getelementptr inbounds i8, i8* %17, i64 1
store i8* %18, i8** %1
br label %19
19: ; preds = %15, %13, %7
%20 = load i8*, i8** %1
ret i8* %20
}

LLVM IR (-O2):
@.str = [15 x i8] c"Unknown error.\00"
@.str.1 = [6 x i8] c".so: \00"
define i8* @mdp_dlerror() {
%1 = tail call i8* (...) @dlerror()
%2 = icmp eq i8* %1, null
br i1 %2, label %8, label %3
3: ; preds = %0
%4 = tail call i8* @strstr(i8* %1, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i8* %4, null
%6 = getelementptr inbounds i8, i8* %4, i64 5
%7 = select i1 %5, i8* %1, i8* %6
br label %8
8: ; preds = %0, %3
%9 = phi i8* [ %7, %3 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %0 ]
ret i8* %9
}