#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int uint64_t;
struct TYPE_3__ {
  unsigned long long r_addend;
  unsigned long long r_info;
};
typedef TYPE_1__ Elf64_Rela;

/* Variables and functions */
int PROT_READ;
int PROT_WRITE;
int getpagesize();
int mprotect(void *, int, int);
int hook_rela(Elf64_Rela *foundrela, void *func, int type) {
  int ret;
  int PAGE_SIZE = getpagesize();
  uint64_t prevpage = ((uint64_t)foundrela / PAGE_SIZE) * PAGE_SIZE;
  ret = mprotect((void *)prevpage, PAGE_SIZE, PROT_READ | PROT_WRITE);
  if (ret != 0)
    return -1;
  if (type == 1)
    foundrela->r_addend = (unsigned long long)func;
  else if (type == 2)
    foundrela->r_info = (unsigned long long)func;
  ret = mprotect((void *)prevpage, PAGE_SIZE, PROT_READ);
  if (ret != 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<hook_rela>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <getpagesize@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
cltd
idivl  -0x24(%rbp)
imul   -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
movslq -0x28(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    0x2e9e(%rip),%rax
mov    (%rax),%edx
mov    0x2e75(%rip),%rax
or     (%rax),%edx
call   1030 <mprotect@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1183 <hook_rela+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <hook_rela+0xcd>
cmpl   $0x1,-0x1c(%rbp)
jne    119d <hook_rela+0x7d>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    11b8 <hook_rela+0x98>
cmpl   $0x2,-0x1c(%rbp)
jne    11b3 <hook_rela+0x93>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
jmp    11b8 <hook_rela+0x98>
movslq -0x28(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
call   1030 <mprotect@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11e6 <hook_rela+0xc6>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <hook_rela+0xcd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hook_rela>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpagesize@plt>
mov    %eax,%ebp
mov    %ebx,%eax
cltd
idiv   %ebp
mov    %ebx,%eax
sub    %edx,%eax
movslq %eax,%r12
mov    0x2ea8(%rip),%r13
mov    0x2e81(%rip),%rax
mov    (%rax),%edx
or     0x0(%r13),%edx
mov    %r12,%rdi
mov    %ebp,%esi
call   1030 <mprotect@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    1199 <hook_rela+0x79>
cmp    $0x2,%r15d
je     1183 <hook_rela+0x63>
cmp    $0x1,%r15d
jne    1187 <hook_rela+0x67>
mov    %r14,(%rbx)
jmp    1187 <hook_rela+0x67>
mov    %r14,0x8(%rbx)
mov    0x0(%r13),%edx
mov    %r12,%rdi
mov    %ebp,%esi
call   1030 <mprotect@plt>
neg    %eax
sbb    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64, i64 }
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
define i32 @hook_rela(%struct.TYPE_3__* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.TYPE_3__*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store %struct.TYPE_3__* %0, %struct.TYPE_3__** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = call i32 (...) @getpagesize()
store i32 %11, i32* %9
%12 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%13 = ptrtoint %struct.TYPE_3__* %12 to i32
%14 = load i32, i32* %9
%15 = sdiv i32 %13, %14
%16 = load i32, i32* %9
%17 = mul nsw i32 %15, %16
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = sext i32 %18 to i64
%20 = inttoptr i64 %19 to i8*
%21 = load i32, i32* %9
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %22, %23
%25 = call i32 @mprotect(i8* %20, i32 %21, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %3
store i32 -1, i32* %4
br label %57
29: ; preds = %3
%30 = load i32, i32* %7
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i8*, i8** %6
%34 = ptrtoint i8* %33 to i64
%35 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%36 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %35, i32 0, i32 0
store i64 %34, i64* %36
br label %46
37: ; preds = %29
%38 = load i32, i32* %7
%39 = icmp eq i32 %38, 2
br i1 %39, label %40, label %45
40: ; preds = %37
%41 = load i8*, i8** %6
%42 = ptrtoint i8* %41 to i64
%43 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%44 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %43, i32 0, i32 1
store i64 %42, i64* %44
br label %45
45: ; preds = %40, %37
br label %46
46: ; preds = %45, %32
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = inttoptr i64 %48 to i8*
%50 = load i32, i32* %9
%51 = load i32, i32* @PROT_READ
%52 = call i32 @mprotect(i8* %49, i32 %50, i32 %51)
store i32 %52, i32* %8
%53 = load i32, i32* %8
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %46
store i32 -1, i32* %4
br label %57
56: ; preds = %46
store i32 0, i32* %4
br label %57
57: ; preds = %56, %55, %28
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64, i64 }
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
define i32 @hook_rela(%struct.TYPE_3__* %0, i8* %1, i32 %2) {
%4 = tail call i32 (...) @getpagesize()
%5 = ptrtoint %struct.TYPE_3__* %0 to i64
%6 = trunc i64 %5 to i32
%7 = srem i32 %6, %4
%8 = sub i32 %6, %7
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i8*
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %12, %11
%14 = tail call i32 @mprotect(i8* %10, i32 %4, i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %3
switch i32 %2, label %23 [
i32 1, label %17
i32 2, label %20
]
17: ; preds = %16
%18 = ptrtoint i8* %1 to i64
%19 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %0, i64 0, i32 0
store i64 %18, i64* %19
br label %23
20: ; preds = %16
%21 = ptrtoint i8* %1 to i64
%22 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %0, i64 0, i32 1
store i64 %21, i64* %22
br label %23
23: ; preds = %16, %20, %17
%24 = load i32, i32* @PROT_READ
%25 = tail call i32 @mprotect(i8* %10, i32 %4, i32 %24)
%26 = icmp ne i32 %25, 0
%27 = sext i1 %26 to i32
br label %28
28: ; preds = %23, %3
%29 = phi i32 [ -1, %3 ], [ %27, %23 ]
ret i32 %29
}