#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 void uint8_t;

/* Variables and functions */
int MAP_ANON;
void *MAP_FAILED;
int MAP_PRIVATE;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
void *mmap(void *, size_t, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ track_block(void *);
void *try_alloc_arena();
void *alloc_code(size_t *size) {
  static uint8_t *next = (uint8_t *)0x40000000;
  uint8_t *ret = try_alloc_arena();
  if (ret) {
    return ret;
  }
  if (*size & (4096 - 1)) {
    *size += 4096 - (*size & (4096 - 1));
  }
  ret = mmap(next, *size, PROT_EXEC | PROT_READ | PROT_WRITE,
             MAP_PRIVATE | MAP_ANON | 0, -1, 0);
  if (ret == MAP_FAILED) {
    perror("alloc_code");
    return NULL;
  }
  track_block(ret);
  next = ret + *size;
  return ret;
}


x86 assembly (clang, -O0):
<alloc_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <try_alloc_arena@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     116f <alloc_code+0x2f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1245 <alloc_code+0x105>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
and    $0xfff,%rax
cmp    $0x0,%rax
je     11a5 <alloc_code+0x65>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
and    $0xfff,%rax
mov    $0x1000,%ecx
sub    %rax,%rcx
mov    -0x10(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    0x2e94(%rip),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
mov    0x2e35(%rip),%rax
or     (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
mov    0x2deb(%rip),%rax
mov    (%rax),%ecx
mov    0x2dea(%rip),%rax
or     (%rax),%ecx
or     $0x0,%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2db8(%rip),%rcx
cmp    (%rcx),%rax
jne    1222 <alloc_code+0xe2>
lea    0xdf0(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1245 <alloc_code+0x105>
mov    -0x18(%rbp),%rdi
call   1030 <track_block@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    (%rcx),%rax
mov    %rax,0x2e03(%rip)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_code>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1050 <try_alloc_arena@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11dc <alloc_code+0x9c>
mov    (%r14),%rsi
test   $0xfff,%esi
je     1176 <alloc_code+0x36>
add    $0x1000,%rsi
and    $0xfffffffffffff000,%rsi
mov    %rsi,(%r14)
mov    0x2ec3(%rip),%rdi
mov    0x2e5c(%rip),%rax
mov    0x2e6d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e3a(%rip),%rax
or     (%rax),%edx
mov    0x2e21(%rip),%rax
mov    0x2e22(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2df6(%rip),%rax
cmp    (%rax),%rbx
je     11e7 <alloc_code+0xa7>
mov    %rbx,%rdi
call   1030 <track_block@plt>
mov    (%r14),%rax
add    %rbx,%rax
mov    %rax,0x2e64(%rip)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe12(%rip),%rdi
call   1060 <perror@plt>
xor    %ebx,%ebx
jmp    11dc <alloc_code+0x9c>

LLVM IR (-O0):
@alloc_code.next = internal global i8* inttoptr (i64 1073741824 to i8*)
@PROT_EXEC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANON = global i32 0
@MAP_FAILED = global i8* null
@.str = [11 x i8] c"alloc_code\00"
define i8* @alloc_code(i64* %0) {
%2 = alloca i8*
%3 = alloca i64*
%4 = alloca i8*
store i64* %0, i64** %3
%5 = call i8* (...) @try_alloc_arena()
store i8* %5, i8** %4
%6 = load i8*, i8** %4
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = load i8*, i8** %4
store i8* %9, i8** %2
br label %50
10: ; preds = %1
%11 = load i64*, i64** %3
%12 = load i64, i64* %11
%13 = and i64 %12, 4095
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %10
%16 = load i64*, i64** %3
%17 = load i64, i64* %16
%18 = and i64 %17, 4095
%19 = sub i64 4096, %18
%20 = load i64*, i64** %3
%21 = load i64, i64* %20
%22 = add i64 %21, %19
store i64 %22, i64* %20
br label %23
23: ; preds = %15, %10
%24 = load i8*, i8** @alloc_code.next
%25 = load i64*, i64** %3
%26 = load i64, i64* %25
%27 = load i32, i32* @PROT_EXEC
%28 = load i32, i32* @PROT_READ
%29 = or i32 %27, %28
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %29, %30
%32 = load i32, i32* @MAP_PRIVATE
%33 = load i32, i32* @MAP_ANON
%34 = or i32 %32, %33
%35 = or i32 %34, 0
%36 = call i8* @mmap(i8* %24, i64 %26, i32 %31, i32 %35, i32 -1, i32 0)
store i8* %36, i8** %4
%37 = load i8*, i8** %4
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %42
40: ; preds = %23
%41 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %50
42: ; preds = %23
%43 = load i8*, i8** %4
%44 = call i32 @track_block(i8* %43)
%45 = load i8*, i8** %4
%46 = load i64*, i64** %3
%47 = load i64, i64* %46
%48 = getelementptr i8, i8* %45, i64 %47
store i8* %48, i8** @alloc_code.next
%49 = load i8*, i8** %4
store i8* %49, i8** %2
br label %50
50: ; preds = %42, %40, %8
%51 = load i8*, i8** %2
ret i8* %51
}

LLVM IR (-O2):
@alloc_code.next = internal unnamed_addr global i8* inttoptr (i64 1073741824 to i8*)
@PROT_EXEC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANON = global i32 0
@MAP_FAILED = global i8* null
@.str = [11 x i8] c"alloc_code\00"
define i8* @alloc_code(i64* nocapture %0) {
%2 = tail call i8* (...) @try_alloc_arena()
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %31
4: ; preds = %1
%5 = load i64, i64* %0
%6 = and i64 %5, 4095
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = add i64 %5, 4096
%10 = and i64 %9, -4096
store i64 %10, i64* %0
br label %11
11: ; preds = %8, %4
%12 = phi i64 [ %10, %8 ], [ %5, %4 ]
%13 = load i8*, i8** @alloc_code.next
%14 = load i32, i32* @PROT_EXEC
%15 = load i32, i32* @PROT_READ
%16 = or i32 %15, %14
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %16, %17
%19 = load i32, i32* @MAP_PRIVATE
%20 = load i32, i32* @MAP_ANON
%21 = or i32 %20, %19
%22 = tail call i8* @mmap(i8* %13, i64 %12, i32 %18, i32 %21, i32 -1, i32 0)
%23 = load i8*, i8** @MAP_FAILED
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %27
25: ; preds = %11
%26 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %31
27: ; preds = %11
%28 = tail call i32 @track_block(i8* %22)
%29 = load i64, i64* %0
%30 = getelementptr i8, i8* %22, i64 %29
store i8* %30, i8** @alloc_code.next
br label %31
31: ; preds = %1, %27, %25
%32 = phi i8* [ null, %25 ], [ %22, %27 ], [ %2, %1 ]
ret i8* %32
}