#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*/ printf(char *, size_t);
void *realloc(void *, size_t);
int /*<<< orphan*/ xfree(void *);
void *xrealloc(void *ptr, size_t size) {
  if (size == 0) {
    xfree(ptr);
    return NULL;
  }
  if (size > 10485760) {
    printf("Big realloc %u!\n", size);
  }
  void *m = realloc(ptr, size);
  if (m == NULL) {
    printf("Out of Memory! @ realloc size %u\n", size);
    exit(1);
  }
  return m;
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1171 <xrealloc+0x31>
mov    -0x10(%rbp),%rdi
call   1040 <xfree@plt>
movq   $0x0,-0x8(%rbp)
jmp    11cd <xrealloc+0x8d>
cmpq   $0xa00000,-0x18(%rbp)
jbe    118f <xrealloc+0x4f>
mov    -0x18(%rbp),%rsi
lea    0xe76(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c5 <xrealloc+0x85>
mov    -0x18(%rbp),%rsi
lea    0xe5b(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
test   %rsi,%rsi
je     1190 <xrealloc+0x50>
mov    %rsi,%rbx
cmp    $0xa00001,%rsi
jb     1167 <xrealloc+0x27>
lea    0xea1(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
jne    119a <xrealloc+0x5a>
lea    0xe93(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    %r14,%rdi
call   1040 <xfree@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Big realloc %u!\0A\00"
@.str.1 = [34 x i8] c"Out of Memory! @ realloc size %u\0A\00"
define i8* @xrealloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = call i32 @xfree(i8* %10)
store i8* null, i8** %3
br label %30
12: ; preds = %2
%13 = load i64, i64* %5
%14 = icmp ugt i64 %13, 10485760
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i64, i64* %5
%17 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %16)
br label %18
18: ; preds = %15, %12
%19 = load i8*, i8** %4
%20 = load i64, i64* %5
%21 = call i8* @realloc(i8* %19, i64 %20)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = load i64, i64* %5
%26 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i64 %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %18
%29 = load i8*, i8** %6
store i8* %29, i8** %3
br label %30
30: ; preds = %28, %9
%31 = load i8*, i8** %3
ret i8* %31
}

LLVM IR (-O2):
@.str = [17 x i8] c"Big realloc %u!\0A\00"
@.str.1 = [34 x i8] c"Out of Memory! @ realloc size %u\0A\00"
define noalias i8* @xrealloc(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @xfree(i8* %0)
br label %16
6: ; preds = %2
%7 = icmp ugt i64 %1, 10485760
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %1)
br label %10
10: ; preds = %8, %6
%11 = tail call i8* @realloc(i8* %0, i64 %1)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i64 %1)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10, %4
%17 = phi i8* [ null, %4 ], [ %11, %10 ]
ret i8* %17
}