#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 /*<<< orphan*/ PROT_EXEC;
int getpagesize();
int /*<<< orphan*/ mprotect(void *, size_t const, int /*<<< orphan*/);
void stub1();
void shellcode(void *buf, const size_t len) {
  const int pagesize = getpagesize();
  void *page = (void *)((uintptr_t)buf & ~(pagesize - 1));
  mprotect(page, (len / pagesize + 1) * pagesize, PROT_EXEC);
  ((void (*)())buf)();
}


x86 assembly (clang, -O0):
<shellcode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpagesize@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
movslq %ecx,%rcx
and    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
xor    %edx,%edx
div    %rcx
mov    %rax,%rsi
add    $0x1,%rsi
movslq -0x14(%rbp),%rax
imul   %rax,%rsi
mov    0x2e70(%rip),%rax
mov    (%rax),%edx
call   1030 <mprotect@plt>
mov    $0x0,%al
call   *-0x8(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shellcode>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpagesize@plt>
mov    %eax,%ecx
neg    %eax
movslq %eax,%rdi
mov    %rbx,%r15
and    %rbx,%rdi
movslq %ecx,%rsi
mov    %r14,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     1158 <shellcode+0x38>
mov    %r14,%rax
xor    %edx,%edx
div    %rsi
jmp    115f <shellcode+0x3f>
mov    %r14d,%eax
xor    %edx,%edx
div    %ecx
add    $0x1,%rax
imul   %rsi,%rax
mov    0x2e7a(%rip),%rcx
mov    (%rcx),%edx
mov    %rax,%rsi
call   1030 <mprotect@plt>
xor    %eax,%eax
mov    %r15,%rcx
pop    %rbx
pop    %r14
pop    %r15
jmp    *%rcx

LLVM IR (-O0):
@PROT_EXEC = global i32 0
define void @shellcode(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = call i32 (...) @getpagesize()
store i32 %7, i32* %5
%8 = load i8*, i8** %3
%9 = ptrtoint i8* %8 to i64
%10 = load i32, i32* %5
%11 = sub nsw i32 %10, 1
%12 = xor i32 %11, -1
%13 = sext i32 %12 to i64
%14 = and i64 %9, %13
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = load i64, i64* %4
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = udiv i64 %17, %19
%21 = add i64 %20, 1
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = mul i64 %21, %23
%25 = load i32, i32* @PROT_EXEC
%26 = call i32 @mprotect(i8* %16, i64 %24, i32 %25)
%27 = load i8*, i8** %3
%28 = bitcast i8* %27 to void (...)*
ret void
}

LLVM IR (-O2):
@PROT_EXEC = global i32 0
define void @shellcode(i8* %0, i64 %1) {
%3 = tail call i32 (...) @getpagesize()
%4 = ptrtoint i8* %0 to i64
%5 = sub i32 0, %3
%6 = sext i32 %5 to i64
%7 = and i64 %6, %4
%8 = inttoptr i64 %7 to i8*
%9 = sext i32 %3 to i64
%10 = udiv i64 %1, %9
%11 = add i64 %10, 1
%12 = mul i64 %11, %9
%13 = load i32, i32* @PROT_EXEC
%14 = tail call i32 @mprotect(i8* %8, i64 %12, i32 %13)
%15 = bitcast i8* %0 to void (...)*
tail call void (...) %15()
ret void
}