#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 /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ assert(int);
int mprotect(void *, int, int /*<<< orphan*/);
int sysconf(int /*<<< orphan*/);
int protectMemWrite(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, len, PROT_READ);
}


x86 assembly (clang, -O0):
<protectMemWrite>:
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 <protectMemWrite+0x2b>
cmpl   $0x0,-0x14(%rbp)
jne    1167 <protectMemWrite+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ea <protectMemWrite+0xba>
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 <protectMemWrite+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 <protectMemWrite+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),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
call   1040 <mprotect@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<protectMemWrite>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1191 <protectMemWrite+0x61>
mov    %esi,%ebp
test   %esi,%esi
je     1191 <protectMemWrite+0x61>
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 <protectMemWrite+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    0x2e77(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %ebp,%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
define i32 @protectMemWrite(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 %38
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* %5
%36 = load i32, i32* @PROT_READ
%37 = call i32 @mprotect(i8* %34, i32 %35, i32 %36)
store i32 %37, i32* %3
br label %38
38: ; preds = %30, %14
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
define i32 @protectMemWrite(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 %24, 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* @PROT_READ
%23 = tail call i32 @mprotect(i8* %0, i32 %1, i32 %22)
br label %24
24: ; preds = %2, %19
%25 = phi i32 [ %23, %19 ], [ -1, %2 ]
ret i32 %25
}