#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 /*<<< orphan*/ scode;

/* Variables and functions */
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ memset(void *, int, int);
scalar_t__ mprotect(void *, int, int);
int /*<<< orphan*/ perror(char *);
void stub1();
char *valloc(int);
int main() {
  char *scode = valloc(4096);
  if (mprotect((void *)scode, sizeof(scode),
               PROT_READ | PROT_WRITE | PROT_EXEC)) {
    perror("mprotect");
  }
  memset((void *)scode, 0xff, 4096);
  void (*fp)() = (void (*)())scode;
  (*fp)();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x1000,%edi
call   1040 <valloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e90(%rip),%rax
mov    (%rax),%edx
mov    0x2e5f(%rip),%rax
or     (%rax),%edx
mov    0x2e66(%rip),%rax
or     (%rax),%edx
mov    $0x8,%esi
call   1050 <mprotect@plt>
cmp    $0x0,%rax
je     119c <main+0x5c>
lea    0xe69(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rdi
mov    $0xff,%esi
mov    $0x1000,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   *-0x18(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x1000,%edi
call   1040 <valloc@plt>
mov    0x2ea6(%rip),%rcx
mov    0x2e77(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %rax,%rbx
mov    0x2e79(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
mov    $0x8,%esi
call   1050 <mprotect@plt>
test   %rax,%rax
jne    1195 <main+0x55>
mov    %rbx,%rdi
mov    $0xff,%esi
mov    $0x1000,%edx
call   1030 <memset@plt>
xor    %eax,%eax
call   *%rbx
xor    %eax,%eax
pop    %rbx
ret
lea    0xe64(%rip),%rdi
call   1060 <perror@plt>
jmp    117b <main+0x3b>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@.str = [9 x i8] c"mprotect\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca void (...)*
store i32 0, i32* %1
%4 = call i8* @valloc(i32 4096)
store i8* %4, i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @PROT_READ
%7 = load i32, i32* @PROT_WRITE
%8 = or i32 %6, %7
%9 = load i32, i32* @PROT_EXEC
%10 = or i32 %8, %9
%11 = call i64 @mprotect(i8* %5, i32 8, i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = load i8*, i8** %2
%17 = call i32 @memset(i8* %16, i32 255, i32 4096)
%18 = load i8*, i8** %2
%19 = bitcast i8* %18 to void (...)*
store void (...)* %19, void (...)** %3
%20 = load void (...)*, void (...)** %3
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@.str = [9 x i8] c"mprotect\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(4096) i8* @valloc(i32 4096)
%2 = load i32, i32* @PROT_READ
%3 = load i32, i32* @PROT_WRITE
%4 = or i32 %3, %2
%5 = load i32, i32* @PROT_EXEC
%6 = or i32 %4, %5
%7 = tail call i64 @mprotect(i8* %1, i32 8, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = tail call i32 @memset(i8* %1, i32 255, i32 4096)
%13 = bitcast i8* %1 to void (...)*
tail call void (...) %13()
ret i32 0
}