#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*/ fatal(char *);
void *realloc(void *, size_t);
void *safe_realloc(void *ptr, size_t size) {
  void *mymem;
  if ((int)size < 0)
    fatal("Tried to realloc negative amount of memory!!!");
  mymem = realloc(ptr, size);
  if (mymem == NULL)
    fatal("Realloc Failed! Probably out of space.");
  return mymem;
}


x86 assembly (clang, -O0):
<safe_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    $0x0,%eax
jge    1149 <safe_realloc+0x29>
lea    0xebc(%rip),%rdi
call   1040 <fatal@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1171 <safe_realloc+0x51>
lea    0xec2(%rip),%rdi
call   1040 <fatal@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_realloc>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
test   $0x80000000,%ebx
je     113e <safe_realloc+0x1e>
lea    0xec7(%rip),%rdi
call   1040 <fatal@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    115d <safe_realloc+0x3d>
lea    0xed6(%rip),%rdi
call   1040 <fatal@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [46 x i8] c"Tried to realloc negative amount of memory!!!\00"
@.str.1 = [39 x i8] c"Realloc Failed! Probably out of space.\00"
define i8* @safe_realloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%6 = load i64, i64* %4
%7 = trunc i64 %6 to i32
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @fatal(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = load i8*, i8** %3
%13 = load i64, i64* %4
%14 = call i8* @realloc(i8* %12, i64 %13)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @fatal(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = load i8*, i8** %5
ret i8* %20
}

LLVM IR (-O2):
@.str = [46 x i8] c"Tried to realloc negative amount of memory!!!\00"
@.str.1 = [39 x i8] c"Realloc Failed! Probably out of space.\00"
define noalias i8* @safe_realloc(i8* nocapture %0, i64 %1) {
%3 = and i64 %1, 2147483648
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @fatal(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = tail call i8* @realloc(i8* %0, i64 %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @fatal(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
ret i8* %8
}