#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 getpagesize();
int mprotect(void *, int, int);
int change_page_permission_of_address(void *addr) {
  int page_size = getpagesize();
  addr -= (unsigned long)addr % page_size;
  if (mprotect(addr, page_size, PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<change_page_permission_of_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpagesize@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
xor    %edx,%edx
div    %rcx
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e97(%rip),%rax
mov    (%rax),%edx
mov    0x2e66(%rip),%rax
or     (%rax),%edx
mov    0x2e6d(%rip),%rax
or     (%rax),%edx
call   1030 <mprotect@plt>
cmp    $0xffffffff,%eax
jne    118f <change_page_permission_of_address+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1196 <change_page_permission_of_address+0x76>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_page_permission_of_address>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpagesize@plt>
mov    %eax,%esi
movslq %eax,%rcx
mov    %rbx,%rax
or     %rcx,%rax
shr    $0x20,%rax
je     1146 <change_page_permission_of_address+0x26>
mov    %rbx,%rax
xor    %edx,%edx
div    %rcx
jmp    114c <change_page_permission_of_address+0x2c>
mov    %ebx,%eax
xor    %edx,%edx
div    %esi
mov    0x2ea5(%rip),%rcx
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
sub    %rdx,%rbx
mov    0x2e78(%rip),%rcx
or     (%rcx),%eax
mov    %rbx,%rdi
mov    %eax,%edx
call   1030 <mprotect@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
sete   %cl
neg    %ecx
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
define i32 @change_page_permission_of_address(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = call i32 (...) @getpagesize()
store i32 %5, i32* %4
%6 = load i8*, i8** %3
%7 = ptrtoint i8* %6 to i64
%8 = load i32, i32* %4
%9 = sext i32 %8 to i64
%10 = urem i64 %7, %9
%11 = load i8*, i8** %3
%12 = sub i64 0, %10
%13 = getelementptr i8, i8* %11, i64 %12
store i8* %13, i8** %3
%14 = load i8*, i8** %3
%15 = load i32, i32* %4
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %16, %17
%19 = load i32, i32* @PROT_EXEC
%20 = or i32 %18, %19
%21 = call i32 @mprotect(i8* %14, i32 %15, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %1
store i32 -1, i32* %2
br label %25
24: ; preds = %1
store i32 0, i32* %2
br label %25
25: ; preds = %24, %23
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
define i32 @change_page_permission_of_address(i8* %0) {
%2 = tail call i32 (...) @getpagesize()
%3 = ptrtoint i8* %0 to i64
%4 = sext i32 %2 to i64
%5 = urem i64 %3, %4
%6 = sub i64 0, %5
%7 = getelementptr i8, i8* %0, i64 %6
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @PROT_EXEC
%12 = or i32 %10, %11
%13 = tail call i32 @mprotect(i8* %7, i32 %2, i32 %12)
%14 = icmp eq i32 %13, -1
%15 = sext i1 %14 to i32
ret i32 %15
}