#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 PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
scalar_t__ mprotect(void *, size_t, int);
long sysconf(int /*<<< orphan*/);
void *subhook_unprotect(void *address, size_t size) {
  long pagesize;
  pagesize = sysconf(_SC_PAGESIZE);
  address = (void *)((long)address & ~(pagesize - 1));
  if (mprotect(address, size, PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
    return NULL;
  return address;
}


x86 assembly (clang, -O0):
<subhook_unprotect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
xor    $0xffffffffffffffff,%rcx
and    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e90(%rip),%rax
mov    (%rax),%edx
mov    0x2e57(%rip),%rax
or     (%rax),%edx
mov    0x2e5e(%rip),%rax
or     (%rax),%edx
call   1030 <mprotect@plt>
cmp    $0x0,%rax
je     1198 <subhook_unprotect+0x78>
movq   $0x0,-0x8(%rbp)
jmp    11a0 <subhook_unprotect+0x80>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<subhook_unprotect>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2eae(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,%rbx
mov    0x2eb5(%rip),%rax
mov    0x2e7e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
neg    %rbx
and    %r15,%rbx
mov    0x2e7d(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <mprotect@plt>
xor    %ecx,%ecx
test   %rax,%rax
cmove  %rbx,%rcx
mov    %rcx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
define i8* @subhook_unprotect(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i32, i32* @_SC_PAGESIZE
%8 = call i64 @sysconf(i32 %7)
store i64 %8, i64* %6
%9 = load i8*, i8** %4
%10 = ptrtoint i8* %9 to i64
%11 = load i64, i64* %6
%12 = sub nsw i64 %11, 1
%13 = xor i64 %12, -1
%14 = and i64 %10, %13
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = load i64, i64* %5
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %18, %19
%21 = load i32, i32* @PROT_EXEC
%22 = or i32 %20, %21
%23 = call i64 @mprotect(i8* %16, i64 %17, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %2
store i8* null, i8** %3
br label %28
26: ; preds = %2
%27 = load i8*, i8** %4
store i8* %27, i8** %3
br label %28
28: ; preds = %26, %25
%29 = load i8*, i8** %3
ret i8* %29
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
define i8* @subhook_unprotect(i8* %0, i64 %1) {
%3 = load i32, i32* @_SC_PAGESIZE
%4 = tail call i64 @sysconf(i32 %3)
%5 = ptrtoint i8* %0 to i64
%6 = sub i64 0, %4
%7 = and i64 %6, %5
%8 = inttoptr i64 %7 to i8*
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @PROT_EXEC
%13 = or i32 %11, %12
%14 = tail call i64 @mprotect(i8* %8, i64 %1, i32 %13)
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i8* %8, i8* null
ret i8* %16
}