#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;
void *mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
void *subhook_alloc_code(size_t size) {
  void *address;
  address = mmap(NULL, size, (PROT_READ | PROT_WRITE | PROT_EXEC),
                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  return address == MAP_FAILED ? NULL : address;
}


x86 assembly (clang, -O0):
<subhook_alloc_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
mov    0x2ed1(%rip),%rax
mov    (%rax),%edx
mov    0x2e98(%rip),%rax
or     (%rax),%edx
mov    0x2e9f(%rip),%rax
or     (%rax),%edx
mov    0x2e7e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e95(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e4a(%rip),%rcx
cmp    (%rcx),%rax
jne    1182 <subhook_alloc_code+0x72>
xor    %eax,%eax
mov    %rax,-0x18(%rbp)
jmp    118a <subhook_alloc_code+0x7a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<subhook_alloc_code>:
push   %rbx
mov    %rdi,%rsi
mov    0x2edd(%rip),%rax
mov    0x2ea6(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2eab(%rip),%rax
or     (%rax),%edx
mov    0x2e8a(%rip),%rax
mov    0x2ea3(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%rax
cmove  %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
define i8* @subhook_alloc_code(i64 %0) {
%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 = load i32, i32* @MAP_PRIVATE
%8 = load i32, i32* @MAP_ANONYMOUS
%9 = or i32 %8, %7
%10 = tail call i8* @mmap(i32* null, i64 %0, i32 %6, i32 %9, i32 -1, i32 0)
%11 = load i8*, i8** @MAP_FAILED
%12 = icmp eq i8* %10, %11
%13 = select i1 %12, i8* null, i8* %10
ret i8* %13
}