#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*/ mprotect(void *, size_t, int);
uintptr_t sysconf(int /*<<< orphan*/);
void __enable_execute_stack(void *addr) {
  const uintptr_t pageSize = sysconf(_SC_PAGESIZE);
  const uintptr_t pageAlignMask = ~(pageSize - 1);
  uintptr_t p = (uintptr_t)addr;
  unsigned char *startPage = (unsigned char *)(p & pageAlignMask);
  unsigned char *endPage =
      (unsigned char *)((p + 48 + pageSize) & pageAlignMask);
  size_t length = endPage - startPage;
  (void)mprotect((void *)startPage, length, PROT_READ | PROT_WRITE | PROT_EXEC);
}


x86 assembly (clang, -O0):
<__enable_execute_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
xor    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
and    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x30,%rax
add    -0x10(%rbp),%rax
and    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2e64(%rip),%rax
mov    (%rax),%edx
mov    0x2e2b(%rip),%rax
or     (%rax),%edx
mov    0x2e32(%rip),%rax
or     (%rax),%edx
call   1030 <mprotect@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__enable_execute_stack>:
push   %rbx
mov    %rdi,%rbx
mov    0x2eb5(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
lea    (%rbx,%rax,1),%rsi
add    $0x30,%rsi
neg    %rax
and    %rax,%rsi
mov    %rax,%rdi
and    %rbx,%rdi
mov    0x2eab(%rip),%rax
mov    0x2e74(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
sub    %rdi,%rsi
mov    0x2e76(%rip),%rax
or     (%rax),%edx
pop    %rbx
jmp    1030 <mprotect@plt>

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
define void @__enable_execute_stack(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %2
%9 = load i32, i32* @_SC_PAGESIZE
%10 = call i64 @sysconf(i32 %9)
store i64 %10, i64* %3
%11 = load i64, i64* %3
%12 = sub i64 %11, 1
%13 = xor i64 %12, -1
store i64 %13, i64* %4
%14 = load i8*, i8** %2
%15 = ptrtoint i8* %14 to i64
store i64 %15, i64* %5
%16 = load i64, i64* %5
%17 = load i64, i64* %4
%18 = and i64 %16, %17
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %6
%20 = load i64, i64* %5
%21 = add i64 %20, 48
%22 = load i64, i64* %3
%23 = add i64 %21, %22
%24 = load i64, i64* %4
%25 = and i64 %23, %24
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = load i8*, i8** %6
%29 = ptrtoint i8* %27 to i64
%30 = ptrtoint i8* %28 to i64
%31 = sub i64 %29, %30
store i64 %31, i64* %8
%32 = load i8*, i8** %6
%33 = load i64, i64* %8
%34 = load i32, i32* @PROT_READ
%35 = load i32, i32* @PROT_WRITE
%36 = or i32 %34, %35
%37 = load i32, i32* @PROT_EXEC
%38 = or i32 %36, %37
%39 = call i32 @mprotect(i8* %32, i64 %33, i32 %38)
ret void
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
define void @__enable_execute_stack(i8* %0) {
%2 = load i32, i32* @_SC_PAGESIZE
%3 = tail call i64 @sysconf(i32 %2)
%4 = sub i64 0, %3
%5 = ptrtoint i8* %0 to i64
%6 = and i64 %4, %5
%7 = inttoptr i64 %6 to i8*
%8 = add i64 %5, 48
%9 = add i64 %8, %3
%10 = and i64 %9, %4
%11 = sub i64 %10, %6
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %13, %12
%15 = load i32, i32* @PROT_EXEC
%16 = or i32 %14, %15
%17 = tail call i32 @mprotect(i8* %7, i64 %11, i32 %16)
ret void
}