#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 MAP_ANONYMOUS;
void *MAP_FAILED;
int MAP_PRIVATE;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ memcpy(void *, char *, int);
void *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
scalar_t__ mprotect(void *, int, int);
int /*<<< orphan*/ munmap(void *, int);
void *create_executable_code_x86_linux(char *machine_code, int size) {
  void *mem = mmap(NULL, size, PROT_READ | PROT_WRITE,
                   MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  if (mem == MAP_FAILED)
    return NULL;
  memcpy(mem, machine_code, size);
  if (mprotect(mem, size, PROT_READ | PROT_EXEC) < 0) {
    munmap(mem, size);
    return NULL;
  }
  return mem;
}


x86 assembly (clang, -O0):
<create_executable_code_x86_linux>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%esi
mov    0x2e9f(%rip),%rax
mov    (%rax),%edx
mov    0x2e66(%rip),%rax
or     (%rax),%edx
mov    0x2e75(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4c(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%rax
jne    11ad <create_executable_code_x86_linux+0x6d>
movq   $0x0,-0x8(%rbp)
jmp    1206 <create_executable_code_x86_linux+0xc6>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
call   1060 <mprotect@plt>
cmp    $0x0,%rax
jge    11fe <create_executable_code_x86_linux+0xbe>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <munmap@plt>
movq   $0x0,-0x8(%rbp)
jmp    1206 <create_executable_code_x86_linux+0xc6>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_executable_code_x86_linux>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2ea3(%rip),%r12
mov    0x2e6c(%rip),%rax
mov    (%rax),%edx
or     (%r12),%edx
mov    0x2e77(%rip),%rax
mov    0x2e50(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e28(%rip),%rax
cmp    (%rax),%rbx
je     11d2 <create_executable_code_x86_linux+0x92>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <memcpy@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
or     (%r12),%edx
mov    %rbx,%rdi
mov    %r15d,%esi
call   1060 <mprotect@plt>
test   %rax,%rax
js     11c5 <create_executable_code_x86_linux+0x85>
mov    %rbx,%rbp
jmp    11d2 <create_executable_code_x86_linux+0x92>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1050 <munmap@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@PROT_EXEC = global i32 0
define i8* @create_executable_code_x86_linux(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %8, %9
%11 = load i32, i32* @MAP_ANONYMOUS
%12 = load i32, i32* @MAP_PRIVATE
%13 = or i32 %11, %12
%14 = call i8* @mmap(i32* null, i32 %7, i32 %10, i32 %13, i32 -1, i32 0)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %19
18: ; preds = %2
store i8* null, i8** %3
br label %37
19: ; preds = %2
%20 = load i8*, i8** %6
%21 = load i8*, i8** %4
%22 = load i32, i32* %5
%23 = call i32 @memcpy(i8* %20, i8* %21, i32 %22)
%24 = load i8*, i8** %6
%25 = load i32, i32* %5
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @PROT_EXEC
%28 = or i32 %26, %27
%29 = call i64 @mprotect(i8* %24, i32 %25, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %19
%32 = load i8*, i8** %6
%33 = load i32, i32* %5
%34 = call i32 @munmap(i8* %32, i32 %33)
store i8* null, i8** %3
br label %37
35: ; preds = %19
%36 = load i8*, i8** %6
store i8* %36, i8** %3
br label %37
37: ; preds = %35, %31, %18
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@PROT_EXEC = global i32 0
define i8* @create_executable_code_x86_linux(i8* %0, i32 %1) {
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @PROT_WRITE
%5 = or i32 %4, %3
%6 = load i32, i32* @MAP_ANONYMOUS
%7 = load i32, i32* @MAP_PRIVATE
%8 = or i32 %7, %6
%9 = tail call i8* @mmap(i32* null, i32 %1, i32 %5, i32 %8, i32 -1, i32 0)
%10 = load i8*, i8** @MAP_FAILED
%11 = icmp eq i8* %9, %10
br i1 %11, label %21, label %12
12: ; preds = %2
%13 = tail call i32 @memcpy(i8* %9, i8* %0, i32 %1)
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_EXEC
%16 = or i32 %15, %14
%17 = tail call i64 @mprotect(i8* %9, i32 %1, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = tail call i32 @munmap(i8* %9, i32 %1)
br label %21
21: ; preds = %12, %2, %19
%22 = phi i8* [ null, %19 ], [ null, %2 ], [ %9, %12 ]
ret i8* %22
}