#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*/ _SC_PAGESIZE;
int mprotect(void *, int, int);
int sysconf(int /*<<< orphan*/);
int ChangeMemPermissions(void *addr, int permission) {
  int PageSize = sysconf(_SC_PAGESIZE);
  addr -= (unsigned long)addr % PageSize;
  if (mprotect(addr, PageSize, permission) == -1) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ChangeMemPermissions>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2eb2(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%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    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1030 <mprotect@plt>
cmp    $0xffffffff,%eax
jne    1181 <ChangeMemPermissions+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    1188 <ChangeMemPermissions+0x68>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ChangeMemPermissions>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2eb9(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %eax,%esi
movslq %eax,%rcx
mov    %rbx,%rax
or     %rcx,%rax
shr    $0x20,%rax
je     1151 <ChangeMemPermissions+0x31>
mov    %rbx,%rax
xor    %edx,%edx
div    %rcx
jmp    1157 <ChangeMemPermissions+0x37>
mov    %ebx,%eax
xor    %edx,%edx
div    %esi
sub    %rdx,%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1030 <mprotect@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
sete   %cl
neg    %ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
define i32 @ChangeMemPermissions(i8* %0, i32 %1) {
%3 = load i32, i32* @_SC_PAGESIZE
%4 = tail call i32 @sysconf(i32 %3)
%5 = ptrtoint i8* %0 to i64
%6 = sext i32 %4 to i64
%7 = urem i64 %5, %6
%8 = sub i64 0, %7
%9 = getelementptr i8, i8* %0, i64 %8
%10 = tail call i32 @mprotect(i8* %9, i32 %4, i32 %1)
%11 = icmp eq i32 %10, -1
%12 = sext i1 %11 to i32
ret i32 %12
}