#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 */
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ exit(int);
int mprotect(void *, long, int);
int /*<<< orphan*/ perror(char *);
long sysconf(int /*<<< orphan*/);
void *test_thread(void *p) {
  char buf[4096];
  int rc;
  void *ptr;
  long pagesize = sysconf(_SC_PAGESIZE);
  ptr = (void *)(((unsigned long)buf) & ~(pagesize - 1));
  rc = mprotect(ptr, pagesize, PROT_READ | PROT_WRITE | PROT_EXEC);
  if (rc < 0) {
    perror("mprotect");
    exit(1);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<test_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %rax,-0x1028(%rbp)
lea    -0x1010(%rbp),%rax
mov    -0x1028(%rbp),%rcx
sub    $0x1,%rcx
xor    $0xffffffffffffffff,%rcx
and    %rcx,%rax
mov    %rax,-0x1020(%rbp)
mov    -0x1020(%rbp),%rdi
mov    -0x1028(%rbp),%rsi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
mov    0x2e26(%rip),%rax
or     (%rax),%edx
mov    0x2e2d(%rip),%rax
or     (%rax),%edx
call   1030 <mprotect@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jge    11db <test_thread+0x9b>
lea    0xe34(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %eax,%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_thread>:
sub    $0x1008,%rsp
mov    0x2e92(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %rax,%rcx
neg    %rcx
mov    0x2e96(%rip),%rsi
mov    0x2e5f(%rip),%rdx
mov    (%rdx),%edx
or     (%rsi),%edx
mov    %rsp,%rdi
and    %rcx,%rdi
mov    0x2e5e(%rip),%rcx
or     (%rcx),%edx
mov    %rax,%rsi
call   1030 <mprotect@plt>
test   %eax,%eax
js     1192 <test_thread+0x52>
xor    %eax,%eax
add    $0x1008,%rsp
ret
lea    0xe67(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@.str = [9 x i8] c"mprotect\00"
define noalias i8* @test_thread(i8* nocapture readnone %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @_SC_PAGESIZE
%5 = tail call i64 @sysconf(i32 %4)
%6 = ptrtoint [4096 x i8]* %2 to i64
%7 = sub i64 0, %5
%8 = and i64 %7, %6
%9 = inttoptr i64 %8 to i8*
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @PROT_EXEC
%14 = or i32 %12, %13
%15 = call i32 @mprotect(i8* %9, i64 %5, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %1
ret i8* null
}