#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();
void *dlsym(void *, char *);
int /*<<< orphan*/ printf(char *, ...);
int function2(void) {
  char *error;
  int (*function2_orig)(void);
  printf("Hello, world! from wrapper library\n");
  *(void **)(&function2_orig) = dlsym(((void *)-1), "function2");
  if (!function2_orig) {
    error = dlerror();
    printf("ERROR\tCould not get symbol from RTLD_NEXT handle: %s\n",
           error ? error : "");
    return 1;
  }
  if (function2_orig() != 0) {
    printf("ERROR\tOriginal function from RTLD_NEXT handle did not return "
           "correct value\n");
    return 1;
  }
  return 2;
}


x86 assembly (clang, -O0):
<function2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xec1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffffffffffff,%rdi
lea    0xed0(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b9 <function2+0x89>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118b <function2+0x5b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    119b <function2+0x6b>
lea    0xf1d(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    119b <function2+0x6b>
mov    -0x20(%rbp),%rsi
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e6 <function2+0xb6>
call   *-0x18(%rbp)
cmp    $0x0,%eax
je     11df <function2+0xaf>
lea    0xe98(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e6 <function2+0xb6>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<function2>:
push   %rax
lea    0xef8(%rip),%rdi
call   1040 <puts@plt>
lea    0xeac(%rip),%rsi
mov    $0xffffffffffffffff,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     1179 <function2+0x39>
call   *%rax
test   %eax,%eax
je     11a3 <function2+0x63>
lea    0xef1(%rip),%rdi
call   1040 <puts@plt>
jmp    119c <function2+0x5c>
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
lea    0xeb5(%rip),%rsi
cmovne %rax,%rsi
lea    0xe75(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
pop    %rcx
ret
mov    $0x2,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Hello, world! from wrapper library\0A\00"
@.str.1 = [10 x i8] c"function2\00"
@.str.2 = [54 x i8] c"ERROR\09Could not get symbol from RTLD_NEXT handle: %s\0A\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [76 x i8] c"ERROR\09Original function from RTLD_NEXT handle did not return correct value\0A\00"
define i32 @function2() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32 ()*
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%5 = call i8* @dlsym(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%6 = bitcast i32 ()** %3 to i8**
store i8* %5, i8** %6
%7 = load i32 ()*, i32 ()** %3
%8 = icmp ne i32 ()* %7, null
br i1 %8, label %19, label %9
9: ; preds = %0
%10 = call i8* (...) @dlerror()
store i8* %10, i8** %2
%11 = load i8*, i8** %2
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = load i8*, i8** %2
br label %16
15: ; preds = %9
br label %16
16: ; preds = %15, %13
%17 = phi i8* [ %14, %13 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), %15 ]
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i8* %17)
store i32 1, i32* %1
br label %26
19: ; preds = %0
%20 = load i32 ()*, i32 ()** %3
%21 = call i32 %20()
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %26
25: ; preds = %19
store i32 2, i32* %1
br label %26
26: ; preds = %25, %23, %16
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@.str.1 = [10 x i8] c"function2\00"
@.str.2 = [54 x i8] c"ERROR\09Could not get symbol from RTLD_NEXT handle: %s\0A\00"
@.str.3 = [1 x i8] zeroinitializer
@str = [35 x i8] c"Hello, world! from wrapper library\00"
@str.5 = [75 x i8] c"ERROR\09Original function from RTLD_NEXT handle did not return correct value\00"
define i32 @function2() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%2 = tail call i8* @dlsym(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %9
4: ; preds = %0
%5 = tail call i8* (...) @dlerror()
%6 = icmp eq i8* %5, null
%7 = select i1 %6, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i8* %5
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i8* %7)
br label %15
9: ; preds = %0
%10 = bitcast i8* %2 to i32 ()*
%11 = tail call i32 %10()
%12 = icmp eq i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @str.5, i64 0, i64 0))
br label %15
15: ; preds = %9, %13, %4
%16 = phi i32 [ 1, %13 ], [ 1, %4 ], [ 2, %9 ]
ret i32 %16
}