#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*/ _SC_PAGESIZE;
char *memalign(int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ protectMemWrite(char *, int);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ unprotectMemAccess(char *, int);
int main(int argc, char *argv[]) {
  char *ptr = memalign((sysconf(_SC_PAGESIZE)), (sysconf(_SC_PAGESIZE)));
  printf("page size:%ld, ptr:%lu\n", (sysconf(_SC_PAGESIZE)), (uint64_t)ptr);
  printf("ptr %% pagesize == 0 ? %s\n",
         ((uint64_t)ptr % (sysconf(_SC_PAGESIZE))) == 0 ? "yes" : "no");
  protectMemWrite(ptr, (sysconf(_SC_PAGESIZE)));
  printf("will crash By SIGSEGV\n");
  *(ptr + 1) = '9';
  unprotectMemAccess(ptr, (sysconf(_SC_PAGESIZE)));
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,-0x30(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%esi
call   1050 <memalign@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %eax,-0x2c(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cltd
idiv   %ecx
lea    0xe63(%rip),%rsi
lea    0xe58(%rip),%rax
cmp    $0x0,%edx
cmove  %rax,%rsi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
call   1040 <protectMemWrite@plt>
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
movb   $0x39,0x1(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
call   1070 <unprotectMemAccess@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e7d(%rip),%r14
mov    (%r14),%edi
call   1070 <sysconf@plt>
mov    %eax,%ebx
mov    (%r14),%edi
call   1070 <sysconf@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1060 <memalign@plt>
mov    %rax,%rbx
mov    (%r14),%edi
call   1070 <sysconf@plt>
lea    0xe68(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r14),%edi
call   1070 <sysconf@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
test   %edx,%edx
lea    0xe78(%rip),%rax
lea    0xe75(%rip),%rsi
cmove  %rax,%rsi
lea    0xe4b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r14),%edi
call   1070 <sysconf@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <protectMemWrite@plt>
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
movb   $0x39,0x1(%rbx)
mov    (%r14),%edi
call   1070 <sysconf@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1080 <unprotectMemAccess@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@.str = [24 x i8] c"page size:%ld, ptr:%lu\0A\00"
@.str.1 = [27 x i8] c"ptr %% pagesize == 0 ? %s\0A\00"
@.str.2 = [4 x i8] c"yes\00"
@.str.3 = [3 x i8] c"no\00"
@.str.4 = [23 x i8] c"will crash By SIGSEGV\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
store i32 %0, i32* %3
store i8** %1, i8*** %4
%6 = load i32, i32* @_SC_PAGESIZE
%7 = call i32 @sysconf(i32 %6)
%8 = load i32, i32* @_SC_PAGESIZE
%9 = call i32 @sysconf(i32 %8)
%10 = call i8* @memalign(i32 %7, i32 %9)
store i8* %10, i8** %5
%11 = load i32, i32* @_SC_PAGESIZE
%12 = call i32 @sysconf(i32 %11)
%13 = load i8*, i8** %5
%14 = ptrtoint i8* %13 to i32
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %14)
%16 = load i8*, i8** %5
%17 = ptrtoint i8* %16 to i32
%18 = load i32, i32* @_SC_PAGESIZE
%19 = call i32 @sysconf(i32 %18)
%20 = srem i32 %17, %19
%21 = icmp eq i32 %20, 0
%22 = zext i1 %21 to i64
%23 = select i1 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %5
%26 = load i32, i32* @_SC_PAGESIZE
%27 = call i32 @sysconf(i32 %26)
%28 = call i32 @protectMemWrite(i8* %25, i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i64 1
store i8 57, i8* %31
%32 = load i8*, i8** %5
%33 = load i32, i32* @_SC_PAGESIZE
%34 = call i32 @sysconf(i32 %33)
%35 = call i32 @unprotectMemAccess(i8* %32, i32 %34)
ret i32 0
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@.str = [24 x i8] c"page size:%ld, ptr:%lu\0A\00"
@.str.1 = [27 x i8] c"ptr %% pagesize == 0 ? %s\0A\00"
@.str.2 = [4 x i8] c"yes\00"
@.str.3 = [3 x i8] c"no\00"
@str = [22 x i8] c"will crash By SIGSEGV\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @_SC_PAGESIZE
%4 = tail call i32 @sysconf(i32 %3)
%5 = load i32, i32* @_SC_PAGESIZE
%6 = tail call i32 @sysconf(i32 %5)
%7 = tail call i8* @memalign(i32 %4, i32 %6)
%8 = load i32, i32* @_SC_PAGESIZE
%9 = tail call i32 @sysconf(i32 %8)
%10 = ptrtoint i8* %7 to i64
%11 = trunc i64 %10 to i32
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %11)
%13 = load i32, i32* @_SC_PAGESIZE
%14 = tail call i32 @sysconf(i32 %13)
%15 = srem i32 %11, %14
%16 = icmp eq i32 %15, 0
%17 = select i1 %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @_SC_PAGESIZE
%20 = tail call i32 @sysconf(i32 %19)
%21 = tail call i32 @protectMemWrite(i8* %7, i32 %20)
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%23 = getelementptr inbounds i8, i8* %7, i64 1
store i8 57, i8* %23
%24 = load i32, i32* @_SC_PAGESIZE
%25 = tail call i32 @sysconf(i32 %24)
%26 = tail call i32 @unprotectMemAccess(i8* %7, i32 %25)
ret i32 0
}