#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
char *xmalloc(size_t size) {
  static char *heap = "";
  static int hlen = 0;
  char *t;
  size = ((size - 1) | 3) + 1;
  if (size >= 4096) {
    if ((t = malloc(size)) == NULL) {
      fprintf(stderr, "Out of allocation space\n");
      exit(1);
    }
  } else {
    if (size > hlen) {
      if ((heap = malloc(hlen = 4096)) == NULL) {
        fprintf(stderr, "Out of allocation space\n");
        exit(1);
      }
    }
    t = heap + (hlen -= size);
  }
  return t;
}


x86 assembly (clang, -O0):
<xmalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
sub    $0x1,%rax
or     $0x3,%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
cmpq   $0x1000,-0x8(%rbp)
jb     119b <xmalloc+0x6b>
mov    -0x8(%rbp),%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1196 <xmalloc+0x66>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    121d <xmalloc+0xed>
mov    -0x8(%rbp),%rax
movslq 0x2e9e(%rip),%rcx
cmp    %rcx,%rax
jbe    11f8 <xmalloc+0xc8>
movl   $0x1000,0x2e8b(%rip)
mov    $0x1000,%edi
call   1040 <malloc@plt>
mov    %rax,0x2e6e(%rip)
cmp    $0x0,%rax
jne    11f3 <xmalloc+0xc3>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    11f8 <xmalloc+0xc8>
mov    0x2e39(%rip),%rax
mov    -0x8(%rbp),%rdx
movslq 0x2e3a(%rip),%rcx
sub    %rdx,%rcx
mov    %ecx,0x2e31(%rip)
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmalloc>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rbx
or     $0x3,%rbx
add    $0x1,%rbx
cmp    $0x1000,%rbx
jb     115d <xmalloc+0x2d>
mov    %ebx,%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
test   %rax,%rax
jne    11c5 <xmalloc+0x95>
jmp    118f <xmalloc+0x5f>
movslq 0x2ee0(%rip),%r14
cmp    %r14,%rbx
jbe    11ae <xmalloc+0x7e>
movl   $0x1000,0x2ed1(%rip)
mov    $0x1000,%r14d
mov    $0x1000,%edi
call   1040 <malloc@plt>
mov    %rax,0x2eae(%rip)
test   %rax,%rax
jne    11b5 <xmalloc+0x85>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    0x2e83(%rip),%rax
sub    %ebx,%r14d
mov    %r14d,0x2e85(%rip)
movslq %r14d,%rcx
add    %rax,%rcx
mov    %rcx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@xmalloc.heap = internal global i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i32 0, i32 0)
@.str = [1 x i8] zeroinitializer
@xmalloc.hlen = internal global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"Out of allocation space\0A\00"
define i8* @xmalloc(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
store i64 %0, i64* %2
%4 = load i64, i64* %2
%5 = sub i64 %4, 1
%6 = or i64 %5, 3
%7 = add i64 %6, 1
store i64 %7, i64* %2
%8 = load i64, i64* %2
%9 = icmp uge i64 %8, 4096
br i1 %9, label %10, label %20
10: ; preds = %1
%11 = load i64, i64* %2
%12 = trunc i64 %11 to i32
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %3
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %10
br label %42
20: ; preds = %1
%21 = load i64, i64* %2
%22 = load i32, i32* @xmalloc.hlen
%23 = sext i32 %22 to i64
%24 = icmp ugt i64 %21, %23
br i1 %24, label %25, label %33
25: ; preds = %20
store i32 4096, i32* @xmalloc.hlen
%26 = call i8* @malloc(i32 4096)
store i8* %26, i8** @xmalloc.heap
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25
br label %33
33: ; preds = %32, %20
%34 = load i8*, i8** @xmalloc.heap
%35 = load i64, i64* %2
%36 = load i32, i32* @xmalloc.hlen
%37 = sext i32 %36 to i64
%38 = sub i64 %37, %35
%39 = trunc i64 %38 to i32
store i32 %39, i32* @xmalloc.hlen
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %34, i64 %40
store i8* %41, i8** %3
br label %42
42: ; preds = %33, %19
%43 = load i8*, i8** %3
ret i8* %43
}

LLVM IR (-O2):
@xmalloc.heap = internal unnamed_addr global i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0)
@.str = [1 x i8] zeroinitializer
@xmalloc.hlen = internal unnamed_addr global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"Out of allocation space\0A\00"
define i8* @xmalloc(i64 %0) {
%2 = add i64 %0, -1
%3 = or i64 %2, 3
%4 = add i64 %3, 1
%5 = icmp ugt i64 %4, 4095
br i1 %5, label %6, label %14
6: ; preds = %1
%7 = trunc i64 %4 to i32
%8 = tail call i8* @malloc(i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %34
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = load i32, i32* @xmalloc.hlen
%16 = sext i32 %15 to i64
%17 = icmp ugt i64 %4, %16
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = load i8*, i8** @xmalloc.heap
br label %27
20: ; preds = %14
store i32 4096, i32* @xmalloc.hlen
%21 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
store i8* %21, i8** @xmalloc.heap
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %18, %20
%28 = phi i32 [ %15, %18 ], [ 4096, %20 ]
%29 = phi i8* [ %19, %18 ], [ %21, %20 ]
%30 = trunc i64 %4 to i32
%31 = sub i32 %28, %30
store i32 %31, i32* @xmalloc.hlen
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
br label %34
34: ; preds = %6, %27
%35 = phi i8* [ %8, %6 ], [ %33, %27 ]
ret i8* %35
}