#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 */
typedef int uint64_t;

/* Variables and functions */
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ assert(int);
int mprotect(void *, int, int);
int sysconf(int /*<<< orphan*/);
int unprotectMemAccess(void *p, int len) {
  uint64_t addr = (uint64_t)(p);
  if (p == NULL || len == 0)
    return -1;
  assert((addr % (sysconf(_SC_PAGESIZE))) == 0 &&
         (len % (sysconf(_SC_PAGESIZE))) == 0 && len != 0);
  return mprotect(p, (sysconf(_SC_PAGESIZE)), PROT_READ | PROT_WRITE);
}


x86 assembly (clang, -O0):
<unprotectMemAccess>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     115b <unprotectMemAccess+0x2b>
cmpl   $0x0,-0x14(%rbp)
jne    1167 <unprotectMemAccess+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <unprotectMemAccess+0xd8>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cltd
idiv   %ecx
xor    %eax,%eax
cmp    $0x0,%edx
mov    %al,-0x19(%rbp)
jne    11c5 <unprotectMemAccess+0x95>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cltd
idiv   %ecx
xor    %eax,%eax
cmp    $0x0,%edx
mov    %al,-0x19(%rbp)
jne    11c5 <unprotectMemAccess+0x95>
cmpl   $0x0,-0x14(%rbp)
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    0x2dd2(%rip),%rax
or     (%rax),%edx
call   1040 <mprotect@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unprotectMemAccess>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     11a2 <unprotectMemAccess+0x72>
mov    %esi,%ebp
test   %esi,%esi
je     11a2 <unprotectMemAccess+0x72>
mov    %rdi,%rbx
mov    0x2e97(%rip),%r14
mov    (%r14),%edi
call   1050 <sysconf@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
xor    %edi,%edi
test   %edx,%edx
jne    1175 <unprotectMemAccess+0x45>
mov    (%r14),%edi
call   1050 <sysconf@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
xor    %edi,%edi
test   %edx,%edx
sete   %dil
call   1030 <assert@plt>
mov    (%r14),%edi
call   1050 <sysconf@plt>
mov    0x2e6f(%rip),%rcx
mov    0x2e40(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %rbx,%rdi
mov    %eax,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <mprotect@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
define i32 @unprotectMemAccess(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 i8*, i8** %4
%8 = ptrtoint i8* %7 to i32
store i32 %8, i32* %6
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11, %2
store i32 -1, i32* %3
br label %41
15: ; preds = %11
%16 = load i32, i32* %6
%17 = load i32, i32* @_SC_PAGESIZE
%18 = call i32 @sysconf(i32 %17)
%19 = srem i32 %16, %18
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %15
%22 = load i32, i32* %5
%23 = load i32, i32* @_SC_PAGESIZE
%24 = call i32 @sysconf(i32 %23)
%25 = srem i32 %22, %24
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i32, i32* %5
%29 = icmp ne i32 %28, 0
br label %30
30: ; preds = %27, %21, %15
%31 = phi i1 [ false, %21 ], [ false, %15 ], [ %29, %27 ]
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i8*, i8** %4
%35 = load i32, i32* @_SC_PAGESIZE
%36 = call i32 @sysconf(i32 %35)
%37 = load i32, i32* @PROT_READ
%38 = load i32, i32* @PROT_WRITE
%39 = or i32 %37, %38
%40 = call i32 @mprotect(i8* %34, i32 %36, i32 %39)
store i32 %40, i32* %3
br label %41
41: ; preds = %30, %14
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
define i32 @unprotectMemAccess(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i32 %1, 0
%5 = or i1 %3, %4
br i1 %5, label %28, label %6
6: ; preds = %2
%7 = ptrtoint i8* %0 to i64
%8 = trunc i64 %7 to i32
%9 = load i32, i32* @_SC_PAGESIZE
%10 = tail call i32 @sysconf(i32 %9)
%11 = srem i32 %8, %10
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %6
%14 = load i32, i32* @_SC_PAGESIZE
%15 = tail call i32 @sysconf(i32 %14)
%16 = srem i32 %1, %15
%17 = icmp eq i32 %16, 0
%18 = zext i1 %17 to i32
br label %19
19: ; preds = %13, %6
%20 = phi i32 [ 0, %6 ], [ %18, %13 ]
%21 = tail call i32 @assert(i32 %20)
%22 = load i32, i32* @_SC_PAGESIZE
%23 = tail call i32 @sysconf(i32 %22)
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %25, %24
%27 = tail call i32 @mprotect(i8* %0, i32 %23, i32 %26)
br label %28
28: ; preds = %2, %19
%29 = phi i32 [ %27, %19 ], [ -1, %2 ]
ret i32 %29
}