#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 *);
unsigned long sysconf(int /*<<< orphan*/);
int do_mprotect(const void *addr, size_t len, int prot) {
  void *ptr;
  int retval;
  unsigned long pagesize = sysconf(_SC_PAGESIZE);
  unsigned long offset = (unsigned long)addr & (pagesize - 1);
  ptr = (void *)((unsigned long)addr - offset);
  len += offset;
  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    $0x40,%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
and    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%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     11dd <do_mprotect+0x9d>
mov    0x2e24(%rip),%rax
mov    (%rax),%rax
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%rax
jne    11dd <do_mprotect+0x9d>
lea    0xe32(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x24(%rbp),%eax
add    $0x40,%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>
add    $0xffffffffffffffff,%rax
and    %rbp,%rax
sub    %rax,%rbp
add    %rax,%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <mprotect@plt>
test   %eax,%eax
je     1190 <do_mprotect+0x50>
mov    0x2e57(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e45(%rip),%rdx
cmp    (%rdx),%rcx
je     1195 <do_mprotect+0x55>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe64(%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
%10 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%11 = load i32, i32* @_SC_PAGESIZE
%12 = call i64 @sysconf(i32 %11)
store i64 %12, i64* %9
%13 = load i8*, i8** %4
%14 = ptrtoint i8* %13 to i64
%15 = load i64, i64* %9
%16 = sub i64 %15, 1
%17 = and i64 %14, %16
store i64 %17, i64* %10
%18 = load i8*, i8** %4
%19 = ptrtoint i8* %18 to i64
%20 = load i64, i64* %10
%21 = sub i64 %19, %20
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %7
%23 = load i64, i64* %10
%24 = load i64, i64* %5
%25 = add i64 %24, %23
store i64 %25, i64* %5
%26 = load i8*, i8** %7
%27 = load i64, i64* %5
%28 = load i32, i32* %6
%29 = call i32 @mprotect(i8* %26, i64 %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %3
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINVAL
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %32, %3
%40 = load i32, i32* %8
ret i32 %40
}

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 = add i64 %5, -1
%8 = and i64 %7, %6
%9 = sub i64 %6, %8
%10 = inttoptr i64 %9 to i8*
%11 = add i64 %8, %1
%12 = tail call i32 @mprotect(i8* %10, i64 %11, i32 %2)
%13 = icmp eq i32 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %3
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINVAL
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %14, %3
ret i32 %12
}