#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*/ free(void *);
int /*<<< orphan*/ logx(int, char *);
void *malloc(int);
int /*<<< orphan*/ printf(char *);
void *mymalloc(int size) {
  static void *safe;
  void *p;
  if (!size)
    size = 1;
  if (!safe) {
    safe = malloc(256 * 1024);
    if (!safe) {
      logx(1, "Out of memory!\n");
      printf("Out of memory!\n");
      exit(3);
    }
  }
  p = malloc(size);
  if (!p) {
    logx(1, "Running low on memory!\n");
    if (safe) {
      free(safe);
    }
    p = malloc(size);
    if (!p) {
      logx(1, "Out of memory!\n");
      printf("Out of memory!\n");
      exit(3);
    }
  }
  return (p);
}


x86 assembly (clang, -O0):
<mymalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    116c <mymalloc+0x1c>
movl   $0x1,-0x4(%rbp)
cmpq   $0x0,0x2edc(%rip)
jne    11c5 <mymalloc+0x75>
mov    $0x40000,%edi
call   1060 <malloc@plt>
mov    %rax,0x2ec5(%rip)
cmpq   $0x0,0x2ebd(%rip)
jne    11c0 <mymalloc+0x70>
mov    $0x1,%edi
lea    0xe5b(%rip),%rsi
call   1040 <logx@plt>
lea    0xe4f(%rip),%rdi
call   1050 <printf@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
jmp    11c5 <mymalloc+0x75>
mov    -0x4(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    124a <mymalloc+0xfa>
mov    $0x1,%edi
lea    0xe28(%rip),%rsi
call   1040 <logx@plt>
cmpq   $0x0,0x2e5b(%rip)
je     1207 <mymalloc+0xb7>
mov    0x2e4e(%rip),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1245 <mymalloc+0xf5>
mov    $0x1,%edi
lea    0xdd6(%rip),%rsi
call   1040 <logx@plt>
lea    0xdca(%rip),%rdi
call   1050 <printf@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
jmp    124a <mymalloc+0xfa>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mymalloc>:
push   %rbx
test   %edi,%edi
mov    $0x1,%ebx
cmovne %edi,%ebx
cmpq   $0x0,0x2eed(%rip)
jne    117b <mymalloc+0x2b>
mov    $0x40000,%edi
call   1060 <malloc@plt>
mov    %rax,0x2eda(%rip)
test   %rax,%rax
je     11b7 <mymalloc+0x67>
mov    %ebx,%edi
call   1060 <malloc@plt>
test   %rax,%rax
jne    11b5 <mymalloc+0x65>
lea    0xe82(%rip),%rsi
mov    $0x1,%edi
call   1050 <logx@plt>
mov    0x2eb1(%rip),%rdi
test   %rdi,%rdi
je     11a9 <mymalloc+0x59>
call   1030 <free@plt>
mov    %ebx,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11b7 <mymalloc+0x67>
pop    %rbx
ret
lea    0xe42(%rip),%rsi
mov    $0x1,%edi
call   1050 <logx@plt>
lea    0xe59(%rip),%rdi
call   1040 <puts@plt>
mov    $0x3,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@mymalloc.safe = internal global i8* null
@.str = [16 x i8] c"Out of memory!\0A\00"
@.str.1 = [24 x i8] c"Running low on memory!\0A\00"
define i8* @mymalloc(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = icmp ne i32 %4, 0
br i1 %5, label %7, label %6
6: ; preds = %1
store i32 1, i32* %2
br label %7
7: ; preds = %6, %1
%8 = load i8*, i8** @mymalloc.safe
%9 = icmp ne i8* %8, null
br i1 %9, label %19, label %10
10: ; preds = %7
%11 = call i8* @malloc(i32 262144)
store i8* %11, i8** @mymalloc.safe
%12 = load i8*, i8** @mymalloc.safe
%13 = icmp ne i8* %12, null
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = call i32 @logx(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 3)
unreachable
18: ; preds = %10
br label %19
19: ; preds = %18, %7
%20 = load i32, i32* %2
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %3
%22 = load i8*, i8** %3
%23 = icmp ne i8* %22, null
br i1 %23, label %41, label %24
24: ; preds = %19
%25 = call i32 @logx(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%26 = load i8*, i8** @mymalloc.safe
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i8*, i8** @mymalloc.safe
%30 = call i32 @free(i8* %29)
br label %31
31: ; preds = %28, %24
%32 = load i32, i32* %2
%33 = call i8* @malloc(i32 %32)
store i8* %33, i8** %3
%34 = load i8*, i8** %3
%35 = icmp ne i8* %34, null
br i1 %35, label %40, label %36
36: ; preds = %31
%37 = call i32 @logx(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%38 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%39 = call i32 @exit(i32 3)
unreachable
40: ; preds = %31
br label %41
41: ; preds = %40, %19
%42 = load i8*, i8** %3
ret i8* %42
}

LLVM IR (-O2):
@mymalloc.safe = internal unnamed_addr global i8* null
@.str = [16 x i8] c"Out of memory!\0A\00"
@.str.1 = [24 x i8] c"Running low on memory!\0A\00"
@str.2 = [15 x i8] c"Out of memory!\00"
define noalias i8* @mymalloc(i32 %0) {
%2 = icmp eq i32 %0, 0
%3 = select i1 %2, i32 1, i32 %0
%4 = load i8*, i8** @mymalloc.safe
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %13
6: ; preds = %1
%7 = tail call dereferenceable_or_null(262144) i8* @malloc(i32 262144)
store i8* %7, i8** @mymalloc.safe
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = tail call i32 @logx(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.2, i64 0, i64 0))
%12 = tail call i32 @exit(i32 3)
unreachable
13: ; preds = %6, %1
%14 = tail call i8* @malloc(i32 %3)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %29
16: ; preds = %13
%17 = tail call i32 @logx(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%18 = load i8*, i8** @mymalloc.safe
%19 = icmp eq i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = tail call i32 @free(i8* %18)
br label %22
22: ; preds = %20, %16
%23 = tail call i8* @malloc(i32 %3)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = tail call i32 @logx(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.2, i64 0, i64 0))
%28 = tail call i32 @exit(i32 3)
unreachable
29: ; preds = %22, %13
%30 = phi i8* [ %14, %13 ], [ %23, %22 ]
ret i8* %30
}