#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;
void *dlopen(char const *, int /*<<< orphan*/);
void *elf_hook(char const *, void *, char const *, void const *);
void *hook(const char *library_filename, const char *function_name,
           const void *substitution_address) {
  void *handle, *address;
  if (NULL == library_filename)
    return NULL;
  handle = dlopen(library_filename, RTLD_LAZY);
  if (NULL == handle)
    return NULL;
  address = ((NULL == handle) ? NULL : (void *)*(size_t const *)(handle));
  return elf_hook(library_filename, address, function_name,
                  substitution_address);
}


x86 assembly (clang, -O0):
<hook>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    114d <hook+0x2d>
movq   $0x0,-0x8(%rbp)
jmp    11bf <hook+0x9f>
mov    -0x10(%rbp),%rdi
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
call   1030 <dlopen@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    117c <hook+0x5c>
movq   $0x0,-0x8(%rbp)
jmp    11bf <hook+0x9f>
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1193 <hook+0x73>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    119e <hook+0x7e>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <elf_hook@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hook>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     115c <hook+0x3c>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
call   1030 <dlopen@plt>
test   %rax,%rax
je     115c <hook+0x3c>
mov    (%rax),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <elf_hook@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@RTLD_LAZY = global i32 0
define i8* @hook(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = icmp eq i8* null, %10
br i1 %11, label %12, label %13
12: ; preds = %3
store i8* null, i8** %4
br label %36
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = load i32, i32* @RTLD_LAZY
%16 = call i8* @dlopen(i8* %14, i32 %15)
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = icmp eq i8* null, %17
br i1 %18, label %19, label %20
19: ; preds = %13
store i8* null, i8** %4
br label %36
20: ; preds = %13
%21 = load i8*, i8** %8
%22 = icmp eq i8* null, %21
br i1 %22, label %23, label %24
23: ; preds = %20
br label %29
24: ; preds = %20
%25 = load i8*, i8** %8
%26 = bitcast i8* %25 to i64*
%27 = load i64, i64* %26
%28 = inttoptr i64 %27 to i8*
br label %29
29: ; preds = %24, %23
%30 = phi i8* [ null, %23 ], [ %28, %24 ]
store i8* %30, i8** %9
%31 = load i8*, i8** %5
%32 = load i8*, i8** %9
%33 = load i8*, i8** %6
%34 = load i8*, i8** %7
%35 = call i8* @elf_hook(i8* %31, i8* %32, i8* %33, i8* %34)
store i8* %35, i8** %4
br label %36
36: ; preds = %29, %19, %12
%37 = load i8*, i8** %4
ret i8* %37
}

LLVM IR (-O2):
@RTLD_LAZY = global i32 0
define i8* @hook(i8* %0, i8* %1, i8* %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %14, label %5
5: ; preds = %3
%6 = load i32, i32* @RTLD_LAZY
%7 = tail call i8* @dlopen(i8* %0, i32 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = bitcast i8* %7 to i64*
%11 = load i64, i64* %10
%12 = inttoptr i64 %11 to i8*
%13 = tail call i8* @elf_hook(i8* %0, i8* %12, i8* %1, i8* %2)
br label %14
14: ; preds = %5, %3, %9
%15 = phi i8* [ %13, %9 ], [ null, %3 ], [ null, %5 ]
ret i8* %15
}