#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 *, int /*<<< orphan*/);
scalar_t__ dlsym(void *, char *);
int libtasTestFunc1();
int /*<<< orphan*/ printf(char *);
int main() {
  int ret;
  printf("Hooking a static function\n");
  ret = libtasTestFunc1();
  if (ret == 2) {
    printf("Successfully hooked!\n");
  } else {
    printf("Hooking failed!\n");
  }
  printf("Loading a dynamic library\n");
  void *handle = dlopen("libhooklib2.so", RTLD_LAZY);
  if (!handle) {
    printf("Cound not load library libhooklib2.so from runpath!\n");
  }
  printf("Hooking a dynamic function\n");
  int (*func)() = (int (*)())dlsym(handle, "libtasTestFunc2");
  if (!func) {
    printf("Cound not link to function libtasTestFunc2!\n");
  }
  ret = func();
  if (ret == 2) {
    printf("Successfully hooked!\n");
  } else {
    printf("Hooking failed!\n");
  }
  printf("Hooking a static function called by a dynamic function\n");
  int (*func2)() = (int (*)())dlsym(handle, "libtasTestCallingFunc3");
  if (!func2) {
    printf("Cound not link to function libtasTestCallingFunc3!\n");
  }
  ret = func2();
  if (ret == 2) {
    printf("Successfully hooked!\n");
  } else {
    printf("Hooking failed!\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <libtasTestFunc1@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1180 <main+0x40>
lea    0xea5(%rip),%rdi
call   1030 <printf@plt>
jmp    118c <main+0x4c>
lea    0xeaa(%rip),%rdi
call   1030 <printf@plt>
lea    0xeaf(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
lea    0xeb5(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c8 <main+0x88>
lea    0xea9(%rip),%rdi
call   1030 <printf@plt>
lea    0xed2(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xede(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ff <main+0xbf>
lea    0xed3(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   *-0x18(%rbp)
mov    %eax,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1222 <main+0xe2>
lea    0xe03(%rip),%rdi
call   1030 <printf@plt>
jmp    122e <main+0xee>
lea    0xe08(%rip),%rdi
call   1030 <printf@plt>
lea    0xec5(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xeed(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1265 <main+0x125>
lea    0xee9(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   *-0x20(%rbp)
mov    %eax,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1288 <main+0x148>
lea    0xd9d(%rip),%rdi
call   1030 <printf@plt>
jmp    1294 <main+0x154>
lea    0xda2(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xee7(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <libtasTestFunc1@plt>
cmp    $0x2,%eax
lea    0xffa(%rip),%r12
lea    0xfe3(%rip),%r14
mov    %r14,%rdi
cmove  %r12,%rdi
call   1030 <puts@plt>
lea    0xed1(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xe6c(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11ad <main+0x6d>
lea    0xec2(%rip),%rdi
call   1030 <puts@plt>
lea    0xeea(%rip),%rdi
call   1030 <puts@plt>
lea    0xe4f(%rip),%rsi
mov    %r15,%rdi
call   1050 <dlsym@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11dc <main+0x9c>
lea    0xee2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   *%rbx
cmp    $0x2,%eax
mov    %r14,%rdi
cmove  %r12,%rdi
call   1030 <puts@plt>
lea    0xeef(%rip),%rdi
call   1030 <puts@plt>
lea    0xe1d(%rip),%rsi
mov    %r15,%rdi
call   1050 <dlsym@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    121e <main+0xde>
lea    0xf03(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   *%rbx
cmp    $0x2,%eax
cmove  %r12,%r14
mov    %r14,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Hooking a static function\0A\00"
@.str.1 = [22 x i8] c"Successfully hooked!\0A\00"
@.str.2 = [17 x i8] c"Hooking failed!\0A\00"
@.str.3 = [27 x i8] c"Loading a dynamic library\0A\00"
@.str.4 = [15 x i8] c"libhooklib2.so\00"
@RTLD_LAZY = global i32 0
@.str.5 = [53 x i8] c"Cound not load library libhooklib2.so from runpath!\0A\00"
@.str.6 = [28 x i8] c"Hooking a dynamic function\0A\00"
@.str.7 = [16 x i8] c"libtasTestFunc2\00"
@.str.8 = [45 x i8] c"Cound not link to function libtasTestFunc2!\0A\00"
@.str.9 = [56 x i8] c"Hooking a static function called by a dynamic function\0A\00"
@.str.10 = [23 x i8] c"libtasTestCallingFunc3\00"
@.str.11 = [52 x i8] c"Cound not link to function libtasTestCallingFunc3!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32 (...)*
%5 = alloca i32 (...)*
store i32 0, i32* %1
%6 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%7 = call i32 (...) @libtasTestFunc1()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, 2
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %14
12: ; preds = %0
%13 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %10
%15 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%16 = load i32, i32* @RTLD_LAZY
%17 = call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %16)
store i8* %17, i8** %3
%18 = load i8*, i8** %3
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %14
%21 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0))
br label %22
22: ; preds = %20, %14
%23 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%24 = load i8*, i8** %3
%25 = call i64 @dlsym(i8* %24, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%26 = inttoptr i64 %25 to i32 (...)*
store i32 (...)* %26, i32 (...)** %4
%27 = load i32 (...)*, i32 (...)** %4
%28 = icmp ne i32 (...)* %27, null
br i1 %28, label %31, label %29
29: ; preds = %22
%30 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.8, i64 0, i64 0))
br label %31
31: ; preds = %29, %22
%32 = load i32 (...)*, i32 (...)** %4
%33 = call i32 (...) %32()
store i32 %33, i32* %2
%34 = load i32, i32* %2
%35 = icmp eq i32 %34, 2
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %40
38: ; preds = %31
%39 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %36
%41 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.9, i64 0, i64 0))
%42 = load i8*, i8** %3
%43 = call i64 @dlsym(i8* %42, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0))
%44 = inttoptr i64 %43 to i32 (...)*
store i32 (...)* %44, i32 (...)** %5
%45 = load i32 (...)*, i32 (...)** %5
%46 = icmp ne i32 (...)* %45, null
br i1 %46, label %49, label %47
47: ; preds = %40
%48 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.11, i64 0, i64 0))
br label %49
49: ; preds = %47, %40
%50 = load i32 (...)*, i32 (...)** %5
%51 = call i32 (...) %50()
store i32 %51, i32* %2
%52 = load i32, i32* %2
%53 = icmp eq i32 %52, 2
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %58
56: ; preds = %49
%57 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %58
58: ; preds = %56, %54
%59 = load i32, i32* %1
ret i32 %59
}

LLVM IR (-O2):
@.str.4 = [15 x i8] c"libhooklib2.so\00"
@RTLD_LAZY = global i32 0
@.str.7 = [16 x i8] c"libtasTestFunc2\00"
@.str.10 = [23 x i8] c"libtasTestCallingFunc3\00"
@str = [26 x i8] c"Hooking a static function\00"
@str.13 = [26 x i8] c"Loading a dynamic library\00"
@str.14 = [52 x i8] c"Cound not load library libhooklib2.so from runpath!\00"
@str.15 = [27 x i8] c"Hooking a dynamic function\00"
@str.16 = [44 x i8] c"Cound not link to function libtasTestFunc2!\00"
@str.18 = [55 x i8] c"Hooking a static function called by a dynamic function\00"
@str.19 = [51 x i8] c"Cound not link to function libtasTestCallingFunc3!\00"
@str.20 = [16 x i8] c"Hooking failed!\00"
@str.23 = [21 x i8] c"Successfully hooked!\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (...) @libtasTestFunc1()
%3 = icmp eq i32 %2, 2
%4 = select i1 %3, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @str.23, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @str.20, i64 0, i64 0)
%5 = tail call i32 @puts(i8* dereferenceable(1) %4)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.13, i64 0, i64 0))
%7 = load i32, i32* @RTLD_LAZY
%8 = tail call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.14, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.15, i64 0, i64 0))
%14 = tail call i64 @dlsym(i8* %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%15 = inttoptr i64 %14 to i32 (...)*
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.16, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = tail call i32 (...) %15()
%21 = icmp eq i32 %20, 2
%22 = select i1 %21, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @str.23, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @str.20, i64 0, i64 0)
%23 = tail call i32 @puts(i8* dereferenceable(1) %22)
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.18, i64 0, i64 0))
%25 = tail call i64 @dlsym(i8* %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.19, i64 0, i64 0))
br label %29
29: ; preds = %27, %19
%30 = inttoptr i64 %25 to i32 (...)*
%31 = tail call i32 (...) %30()
%32 = icmp eq i32 %31, 2
%33 = select i1 %32, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @str.23, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @str.20, i64 0, i64 0)
%34 = tail call i32 @puts(i8* dereferenceable(1) %33)
ret i32 0
}