#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 */
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
void *realloc(void *, size_t);
int /*<<< orphan*/ stderr;
void *erealloc(void *ptr, size_t size) {
  void *retval;
  errno = 0;
  retval = realloc(ptr, size);
  if (errno) {
    perror("CRITICAL ERROR");
    exit(-1);
  }
  if (!retval) {
    fprintf(stderr, "Fatal Error: Unable to allocate memory\n");
    exit(-1);
  }
  return retval;
}


x86 assembly (clang, -O0):
<erealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e81(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e62(%rip),%rax
cmpq   $0x0,(%rax)
je     1196 <erealloc+0x56>
lea    0xe79(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11c0 <erealloc+0x80>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<erealloc>:
push   %rbx
mov    0x2e90(%rip),%rbx
movq   $0x0,(%rbx)
call   1040 <realloc@plt>
cmpq   $0x0,(%rbx)
jne    1161 <erealloc+0x21>
test   %rax,%rax
je     1177 <erealloc+0x37>
pop    %rbx
ret
lea    0xe98(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@.str = [15 x i8] c"CRITICAL ERROR\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"Fatal Error: Unable to allocate memory\0A\00"
define i8* @erealloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
store i64 0, i64* @errno
%6 = load i8*, i8** %3
%7 = load i64, i64* %4
%8 = call i8* @realloc(i8* %6, i64 %7)
store i8* %8, i8** %5
%9 = load i64, i64* @errno
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %14
%22 = load i8*, i8** %5
ret i8* %22
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [15 x i8] c"CRITICAL ERROR\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"Fatal Error: Unable to allocate memory\0A\00"
define noalias i8* @erealloc(i8* nocapture %0, i64 %1) {
store i64 0, i64* @errno
%3 = tail call i8* @realloc(i8* %0, i64 %1)
%4 = load i64, i64* @errno
%5 = icmp eq i64 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %2
%10 = icmp eq i8* %3, null
br i1 %10, label %11, label %15
11: ; preds = %9
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %9
ret i8* %3
}