#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 MAP_ANONYMOUS;
char *MAP_FAILED;
int MAP_GROWSDOWN;
int MAP_PRIVATE;
int PROT_GROWSDOWN;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ err(int, char *);
scalar_t__ errno;
char *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int mprotect(char *, long, int);
scalar_t__ munmap(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
long sysconf(int /*<<< orphan*/);
int main(void) {
  errno = 0;
  long page_size = sysconf(_SC_PAGESIZE);
  if (page_size == -1 && errno) {
    err(1, "sysconf");
  }
  char *ptr =
      mmap(NULL, 3 * page_size, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  if (ptr == MAP_FAILED) {
    err(1, "mmap");
  }
  int x = mprotect(ptr + page_size, page_size,
                   PROT_READ | PROT_WRITE | PROT_GROWSDOWN);
  if (x >= 0) {
    printf("mprotect succeeded unexpectedly!\n");
    return 1;
  }
  if (errno != EINVAL) {
    printf("Wrong errno value: %d\n", errno);
    return 1;
  }
  if (munmap(ptr, 3 * page_size) < 0) {
    err(1, "munmap");
  }
  ptr = mmap(NULL, 3 * page_size, PROT_READ,
             MAP_ANONYMOUS | MAP_PRIVATE | MAP_GROWSDOWN, -1, 0);
  if (ptr == MAP_FAILED) {
    err(1, "mmap");
  }
  if (mprotect(ptr + page_size, page_size,
               PROT_READ | PROT_WRITE | PROT_GROWSDOWN) < 0) {
    err(1, "mprotect");
  }
  *(volatile char *)ptr = 'a';
  if (*(volatile char *)ptr != 'a') {
    printf("Value was not written to memory!\n");
    return 1;
  }
  puts("TEST OK");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0xffffffffffffffff,-0x10(%rbp)
jne    11cc <main+0x5c>
mov    0x2e17(%rip),%rax
cmpq   $0x0,(%rax)
je     11cc <main+0x5c>
mov    $0x1,%edi
lea    0xe39(%rip),%rsi
call   1060 <err@plt>
imul   $0x3,-0x10(%rbp),%rax
mov    %eax,%esi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
mov    0x2df5(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc4(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d91(%rip),%rcx
cmp    (%rcx),%rax
jne    1229 <main+0xb9>
mov    $0x1,%edi
lea    0xde4(%rip),%rsi
call   1060 <err@plt>
mov    -0x18(%rbp),%rdi
add    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
mov    0x2d7b(%rip),%rax
or     (%rax),%edx
mov    0x2d5a(%rip),%rax
or     (%rax),%edx
call   1080 <mprotect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     127c <main+0x10c>
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d7 <main+0x267>
mov    0x2d45(%rip),%rax
mov    (%rax),%rax
mov    0x2d2b(%rip),%rcx
cmp    (%rcx),%rax
je     12ba <main+0x14a>
mov    0x2d2b(%rip),%rax
mov    (%rax),%rsi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d7 <main+0x267>
mov    -0x18(%rbp),%rdi
imul   $0x3,-0x10(%rbp),%rax
mov    %eax,%esi
call   1070 <munmap@plt>
cmp    $0x0,%rax
jge    12e5 <main+0x175>
mov    $0x1,%edi
lea    0xd66(%rip),%rsi
call   1060 <err@plt>
imul   $0x3,-0x10(%rbp),%rax
mov    %eax,%esi
mov    0x2d05(%rip),%rax
mov    (%rax),%edx
mov    0x2cdc(%rip),%rax
mov    (%rax),%ecx
mov    0x2cab(%rip),%rax
or     (%rax),%ecx
mov    0x2c8a(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2c6f(%rip),%rcx
cmp    (%rcx),%rax
jne    134b <main+0x1db>
mov    $0x1,%edi
lea    0xcc2(%rip),%rsi
call   1060 <err@plt>
mov    -0x18(%rbp),%rdi
add    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2c9a(%rip),%rax
mov    (%rax),%edx
mov    0x2c59(%rip),%rax
or     (%rax),%edx
mov    0x2c38(%rip),%rax
or     (%rax),%edx
call   1080 <mprotect@plt>
cmp    $0x0,%eax
jge    1391 <main+0x221>
mov    $0x1,%edi
lea    0xcc1(%rip),%rsi
call   1060 <err@plt>
mov    -0x18(%rbp),%rax
movb   $0x61,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%al
movsbl %al,%eax
cmp    $0x61,%eax
je     13c4 <main+0x254>
lea    0xca5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d7 <main+0x267>
lea    0xcad(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e46(%rip),%r12
movq   $0x0,(%r12)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
jne    11b9 <main+0x49>
cmpq   $0x0,(%r12)
je     11b9 <main+0x49>
lea    0xe51(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
lea    (%rbx,%rbx,2),%r14d
mov    0x2e34(%rip),%r15
mov    (%r15),%edx
mov    0x2e0a(%rip),%rax
mov    0x2ddb(%rip),%r13
mov    0x0(%r13),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2da8(%rip),%rax
cmp    (%rax),%rbp
jne    120e <main+0x9e>
lea    0xe04(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
or     (%r15),%edx
lea    (%rbx,%rbp,1),%rdi
mov    0x2d83(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rsi
call   1080 <mprotect@plt>
test   %eax,%eax
js     1244 <main+0xd4>
lea    0xe23(%rip),%rdi
call   1030 <puts@plt>
jmp    1329 <main+0x1b9>
mov    (%r12),%rsi
mov    0x2d69(%rip),%rax
cmp    (%rax),%rsi
jne    131b <main+0x1ab>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1070 <munmap@plt>
test   %rax,%rax
jns    1279 <main+0x109>
lea    0xdb5(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    0x0(%r13),%ecx
mov    0x2d54(%rip),%rax
or     (%rax),%ecx
mov    (%r15),%edx
mov    0x2d08(%rip),%rax
or     (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2cf1(%rip),%rax
cmp    (%rax),%rbp
mov    0x2d07(%rip),%r14
mov    0x2ce8(%rip),%r12
jne    12d3 <main+0x163>
lea    0xd3f(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    %rbx,%rdi
add    %rbp,%rdi
mov    (%r14),%edx
or     (%r15),%edx
or     (%r12),%edx
mov    %rbx,%rsi
call   1080 <mprotect@plt>
test   %eax,%eax
jns    1300 <main+0x190>
lea    0xd35(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
movb   $0x61,0x0(%rbp)
mov    0x0(%rbp),%al
cmp    $0x61,%al
jne    133d <main+0x1cd>
lea    0xd22(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    132e <main+0x1be>
lea    0xceb(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf8(%rip),%rdi
jmp    123a <main+0xca>

LLVM IR (-O0):
@errno = global i64 0
@_SC_PAGESIZE = global i32 0
@.str = [8 x i8] c"sysconf\00"
@PROT_READ = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@PROT_WRITE = global i32 0
@PROT_GROWSDOWN = global i32 0
@.str.2 = [34 x i8] c"mprotect succeeded unexpectedly!\0A\00"
@EINVAL = global i64 0
@.str.3 = [23 x i8] c"Wrong errno value: %d\0A\00"
@.str.4 = [7 x i8] c"munmap\00"
@MAP_GROWSDOWN = global i32 0
@.str.5 = [9 x i8] c"mprotect\00"
@.str.6 = [34 x i8] c"Value was not written to memory!\0A\00"
@.str.7 = [8 x i8] c"TEST OK\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %1
store i64 0, i64* @errno
%5 = load i32, i32* @_SC_PAGESIZE
%6 = call i64 @sysconf(i32 %5)
store i64 %6, i64* %2
%7 = load i64, i64* %2
%8 = icmp eq i64 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %0
%10 = load i64, i64* @errno
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %9, %0
%15 = load i64, i64* %2
%16 = mul nsw i64 3, %15
%17 = trunc i64 %16 to i32
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @MAP_ANONYMOUS
%20 = load i32, i32* @MAP_PRIVATE
%21 = or i32 %19, %20
%22 = call i8* @mmap(i32* null, i32 %17, i32 %18, i32 %21, i32 -1, i32 0)
store i8* %22, i8** %3
%23 = load i8*, i8** %3
%24 = load i8*, i8** @MAP_FAILED
%25 = icmp eq i8* %23, %24
br i1 %25, label %26, label %28
26: ; preds = %14
%27 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %14
%29 = load i8*, i8** %3
%30 = load i64, i64* %2
%31 = getelementptr inbounds i8, i8* %29, i64 %30
%32 = load i64, i64* %2
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @PROT_WRITE
%35 = or i32 %33, %34
%36 = load i32, i32* @PROT_GROWSDOWN
%37 = or i32 %35, %36
%38 = call i32 @mprotect(i8* %31, i64 %32, i32 %37)
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = icmp sge i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %28
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %99
43: ; preds = %28
%44 = load i64, i64* @errno
%45 = load i64, i64* @EINVAL
%46 = icmp ne i64 %44, %45
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i64, i64* @errno
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i64 %48)
store i32 1, i32* %1
br label %99
50: ; preds = %43
%51 = load i8*, i8** %3
%52 = load i64, i64* %2
%53 = mul nsw i64 3, %52
%54 = trunc i64 %53 to i32
%55 = call i64 @munmap(i8* %51, i32 %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %50
%58 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %59
59: ; preds = %57, %50
%60 = load i64, i64* %2
%61 = mul nsw i64 3, %60
%62 = trunc i64 %61 to i32
%63 = load i32, i32* @PROT_READ
%64 = load i32, i32* @MAP_ANONYMOUS
%65 = load i32, i32* @MAP_PRIVATE
%66 = or i32 %64, %65
%67 = load i32, i32* @MAP_GROWSDOWN
%68 = or i32 %66, %67
%69 = call i8* @mmap(i32* null, i32 %62, i32 %63, i32 %68, i32 -1, i32 0)
store i8* %69, i8** %3
%70 = load i8*, i8** %3
%71 = load i8*, i8** @MAP_FAILED
%72 = icmp eq i8* %70, %71
br i1 %72, label %73, label %75
73: ; preds = %59
%74 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %75
75: ; preds = %73, %59
%76 = load i8*, i8** %3
%77 = load i64, i64* %2
%78 = getelementptr inbounds i8, i8* %76, i64 %77
%79 = load i64, i64* %2
%80 = load i32, i32* @PROT_READ
%81 = load i32, i32* @PROT_WRITE
%82 = or i32 %80, %81
%83 = load i32, i32* @PROT_GROWSDOWN
%84 = or i32 %82, %83
%85 = call i32 @mprotect(i8* %78, i64 %79, i32 %84)
%86 = icmp slt i32 %85, 0
br i1 %86, label %87, label %89
87: ; preds = %75
%88 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %89
89: ; preds = %87, %75
%90 = load i8*, i8** %3
store volatile i8 97, i8* %90
%91 = load i8*, i8** %3
%92 = load volatile i8, i8* %91
%93 = sext i8 %92 to i32
%94 = icmp ne i32 %93, 97
br i1 %94, label %95, label %97
95: ; preds = %89
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %99
97: ; preds = %89
%98 = call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %1
br label %99
99: ; preds = %97, %95, %47, %41
%100 = load i32, i32* %1
ret i32 %100
}

LLVM IR (-O2):
@errno = global i64 0
@_SC_PAGESIZE = global i32 0
@.str = [8 x i8] c"sysconf\00"
@PROT_READ = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@PROT_WRITE = global i32 0
@PROT_GROWSDOWN = global i32 0
@EINVAL = global i64 0
@.str.3 = [23 x i8] c"Wrong errno value: %d\0A\00"
@.str.4 = [7 x i8] c"munmap\00"
@MAP_GROWSDOWN = global i32 0
@.str.5 = [9 x i8] c"mprotect\00"
@.str.7 = [8 x i8] c"TEST OK\00"
@str = [33 x i8] c"Value was not written to memory!\00"
@str.8 = [33 x i8] c"mprotect succeeded unexpectedly!\00"
define i32 @main() {
store i64 0, i64* @errno
%1 = load i32, i32* @_SC_PAGESIZE
%2 = tail call i64 @sysconf(i32 %1)
%3 = icmp eq i64 %2, -1
%4 = load i64, i64* @errno
%5 = icmp ne i64 %4, 0
%6 = select i1 %3, i1 %5, i1 false
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = trunc i64 %2 to i32
%11 = mul i32 %10, 3
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @MAP_ANONYMOUS
%14 = load i32, i32* @MAP_PRIVATE
%15 = or i32 %14, %13
%16 = tail call i8* @mmap(i32* null, i32 %11, i32 %12, i32 %15, i32 -1, i32 0)
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %19, label %21
19: ; preds = %9
%20 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %9
%22 = getelementptr inbounds i8, i8* %16, i64 %2
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @PROT_WRITE
%25 = or i32 %24, %23
%26 = load i32, i32* @PROT_GROWSDOWN
%27 = or i32 %25, %26
%28 = tail call i32 @mprotect(i8* %22, i64 %2, i32 %27)
%29 = icmp sgt i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.8, i64 0, i64 0))
br label %73
32: ; preds = %21
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINVAL
%35 = icmp eq i64 %33, %34
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i64 %33)
br label %73
38: ; preds = %32
%39 = tail call i64 @munmap(i8* %16, i32 %11)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
%44 = load i32, i32* @PROT_READ
%45 = load i32, i32* @MAP_ANONYMOUS
%46 = load i32, i32* @MAP_PRIVATE
%47 = or i32 %46, %45
%48 = load i32, i32* @MAP_GROWSDOWN
%49 = or i32 %47, %48
%50 = tail call i8* @mmap(i32* null, i32 %11, i32 %44, i32 %49, i32 -1, i32 0)
%51 = load i8*, i8** @MAP_FAILED
%52 = icmp eq i8* %50, %51
br i1 %52, label %53, label %55
53: ; preds = %43
%54 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %55
55: ; preds = %53, %43
%56 = getelementptr inbounds i8, i8* %50, i64 %2
%57 = load i32, i32* @PROT_READ
%58 = load i32, i32* @PROT_WRITE
%59 = or i32 %58, %57
%60 = load i32, i32* @PROT_GROWSDOWN
%61 = or i32 %59, %60
%62 = tail call i32 @mprotect(i8* %56, i64 %2, i32 %61)
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %55
%65 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %66
66: ; preds = %64, %55
store volatile i8 97, i8* %50
%67 = load volatile i8, i8* %50
%68 = icmp eq i8 %67, 97
br i1 %68, label %71, label %69
69: ; preds = %66
%70 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %73
71: ; preds = %66
%72 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
br label %73
73: ; preds = %71, %69, %36, %30
%74 = phi i32 [ 1, %30 ], [ 1, %36 ], [ 1, %69 ], [ 0, %71 ]
ret i32 %74
}