#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 */
scalar_t__ EINVAL;
int /*<<< orphan*/ _SC_PAGESIZE;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int mprotect(void *, size_t, int);
int /*<<< orphan*/ perror(char *);
long sysconf(int /*<<< orphan*/);
int do_mprotect(const void *addr, size_t len, int prot) {
  void *ptr;
  int retval;
  long PAGESIZE = sysconf(_SC_PAGESIZE);
  ptr = (char *)(((unsigned long)addr) & ~(PAGESIZE - 1));
  retval = mprotect(ptr, len, prot);
  if (retval != 0 && errno == EINVAL) {
    perror("could not mprotect():");
    exit(1);
  }
  return retval;
}


x86 assembly (clang, -O0):
<do_mprotect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
xor    $0xffffffffffffffff,%rcx
and    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <mprotect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11c9 <do_mprotect+0x89>
mov    0x2e38(%rip),%rax
mov    (%rax),%rax
mov    0x2e26(%rip),%rcx
cmp    (%rcx),%rax
jne    11c9 <do_mprotect+0x89>
lea    0xe46(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_mprotect>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
neg    %rax
and    %rax,%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <mprotect@plt>
test   %eax,%eax
je     1189 <do_mprotect+0x49>
mov    0x2e5e(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e4c(%rip),%rdx
cmp    (%rdx),%rcx
je     118e <do_mprotect+0x4e>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe6b(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@.str = [22 x i8] c"could not mprotect():\00"
define i32 @do_mprotect(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%10 = load i32, i32* @_SC_PAGESIZE
%11 = call i64 @sysconf(i32 %10)
store i64 %11, i64* %9
%12 = load i8*, i8** %4
%13 = ptrtoint i8* %12 to i64
%14 = load i64, i64* %9
%15 = sub nsw i64 %14, 1
%16 = xor i64 %15, -1
%17 = and i64 %13, %16
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = load i64, i64* %5
%21 = load i32, i32* %6
%22 = call i32 @mprotect(i8* %19, i64 %20, i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %3
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINVAL
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25, %3
%33 = load i32, i32* %8
ret i32 %33
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@.str = [22 x i8] c"could not mprotect():\00"
define i32 @do_mprotect(i8* %0, i64 %1, i32 %2) {
%4 = load i32, i32* @_SC_PAGESIZE
%5 = tail call i64 @sysconf(i32 %4)
%6 = ptrtoint i8* %0 to i64
%7 = sub i64 0, %5
%8 = and i64 %7, %6
%9 = inttoptr i64 %8 to i8*
%10 = tail call i32 @mprotect(i8* %9, i64 %1, i32 %2)
%11 = icmp eq i32 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %3
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINVAL
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %12, %3
ret i32 %10
}