#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*/ STDERR_FILENO;
int /*<<< orphan*/ SYS_write;
char *dlerror();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ syscall(int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
void reportLoadingError(char *funcname) {
  fprintf(stderr, "%s\n", dlerror());
  char *msgPrefix = "dlsym failed: ";
  int msgSize = strlen(funcname) + strlen(msgPrefix) + 1;
  char newMsg[msgSize];
  int i = 0, index = 0;
  while (msgPrefix[i] != '\0') {
    newMsg[index] = msgPrefix[i];
    i++;
    index++;
  }
  i = 0;
  while (funcname[i] != '\0') {
    newMsg[index] = funcname[i];
    i++;
    index++;
  }
  newMsg[index] = '\n';
  newMsg[++index] = '\0';
  syscall(SYS_write, STDERR_FILENO, newMsg, msgSize);
}


x86 assembly (clang, -O0):
<reportLoadingError>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xe94(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe8c(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%ecx
mov    %eax,%edx
mov    %ecx,%eax
mov    %edx,%ecx
lea    0x1(%rax,%rcx,1),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1217 <reportLoadingError+0xd7>
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x2c(%rbp),%rdx
mov    (%rcx,%rdx,1),%dl
movslq -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11d5 <reportLoadingError+0x95>
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1260 <reportLoadingError+0x120>
mov    -0x38(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq -0x2c(%rbp),%rdx
mov    (%rcx,%rdx,1),%dl
movslq -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    121e <reportLoadingError+0xde>
mov    -0x38(%rbp),%rdx
movslq -0x30(%rbp),%rax
movb   $0xa,(%rdx,%rax,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
cltq
movb   $0x0,(%rdx,%rax,1)
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%ecx
call   1060 <syscall@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reportLoadingError>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
mov    0x2e84(%rip),%rax
mov    (%rax),%r14d
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe9b(%rip),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
lea    0xe82(%rip),%rdi
call   1040 <strlen@plt>
lea    (%r14,%rax,1),%ecx
add    $0x1,%ecx
lea    0xf(%rcx),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rsi
mov    %rsi,%rdx
sub    %rax,%rdx
neg    %rax
mov    %rdx,%rsp
movabs $0x6166206d79736c64,%rdi
20 66 61
mov    %rdi,(%rsi,%rax,1)
movabs $0x203a64656c696166,%rax
64 3a 20
mov    %rax,0x6(%rdx)
mov    (%r15),%bl
test   %bl,%bl
je     11e3 <reportLoadingError+0xa3>
mov    $0xe,%eax
nopl   (%rax)
mov    %bl,(%rdx,%rax,1)
movzbl -0xd(%r15,%rax,1),%ebx
add    $0x1,%rax
test   %bl,%bl
jne    11d0 <reportLoadingError+0x90>
jmp    11e8 <reportLoadingError+0xa8>
mov    $0xe,%eax
mov    %eax,%eax
movw   $0xa,(%rdx,%rax,1)
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
call   1060 <syscall@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [15 x i8] c"dlsym failed: \00"
@SYS_write = global i32 0
@STDERR_FILENO = global i32 0
define void @reportLoadingError(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i32, i32* @stderr
%10 = call i8* (...) @dlerror()
%11 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %10)
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8** %3
%12 = load i8*, i8** %2
%13 = call i32 @strlen(i8* %12)
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %13, %15
%17 = add nsw i32 %16, 1
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %5
%21 = alloca i8, i64 %19
store i64 %19, i64* %6
store i32 0, i32* %7
store i32 0, i32* %8
br label %22
22: ; preds = %30, %1
%23 = load i8*, i8** %3
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %43
30: ; preds = %22
%31 = load i8*, i8** %3
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %21, i64 %37
store i8 %35, i8* %38
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
%41 = load i32, i32* %8
%42 = add nsw i32 %41, 1
store i32 %42, i32* %8
br label %22
43: ; preds = %22
store i32 0, i32* %7
br label %44
44: ; preds = %52, %43
%45 = load i8*, i8** %2
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %65
52: ; preds = %44
%53 = load i8*, i8** %2
%54 = load i32, i32* %7
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
%57 = load i8, i8* %56
%58 = load i32, i32* %8
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %21, i64 %59
store i8 %57, i8* %60
%61 = load i32, i32* %7
%62 = add nsw i32 %61, 1
store i32 %62, i32* %7
%63 = load i32, i32* %8
%64 = add nsw i32 %63, 1
store i32 %64, i32* %8
br label %44
65: ; preds = %44
%66 = load i32, i32* %8
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %21, i64 %67
store i8 10, i8* %68
%69 = load i32, i32* %8
%70 = add nsw i32 %69, 1
store i32 %70, i32* %8
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %21, i64 %71
store i8 0, i8* %72
%73 = load i32, i32* @SYS_write
%74 = load i32, i32* @STDERR_FILENO
%75 = load i32, i32* %4
%76 = call i32 @syscall(i32 %73, i32 %74, i8* %21, i32 %75)
%77 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [15 x i8] c"dlsym failed: \00"
@SYS_write = global i32 0
@STDERR_FILENO = global i32 0
define void @reportLoadingError(i8* %0) {
%2 = load i32, i32* @stderr
%3 = tail call i8* (...) @dlerror()
%4 = tail call i32 @fprintf(i32 %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %3)
%5 = tail call i32 @strlen(i8* %0)
%6 = tail call i32 @strlen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%7 = add i32 %5, 1
%8 = add i32 %7, %6
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = load i8, i8* %0
%12 = icmp eq i8 %11, 0
br i1 %12, label %25, label %13
13: ; preds = %1, %13
%14 = phi i64 [ %18, %13 ], [ 0, %1 ]
%15 = phi i64 [ %19, %13 ], [ 14, %1 ]
%16 = phi i8 [ %21, %13 ], [ %11, %1 ]
%17 = getelementptr inbounds i8, i8* %10, i64 %15
store i8 %16, i8* %17
%18 = add nuw nsw i64 %14, 1
%19 = add nuw i64 %15, 1
%20 = getelementptr inbounds i8, i8* %0, i64 %18
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %13
23: ; preds = %13
%24 = trunc i64 %19 to i32
br label %25
25: ; preds = %23, %1
%26 = phi i32 [ 14, %1 ], [ %24, %23 ]
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %10, i64 %27
store i8 10, i8* %28
%29 = add nuw nsw i32 %26, 1
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %10, i64 %30
store i8 0, i8* %31
%32 = load i32, i32* @SYS_write
%33 = load i32, i32* @STDERR_FILENO
%34 = call i32 @syscall(i32 %32, i32 %33, i8* %10, i32 %8)
ret void
}