#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 *);
void *realloc(void *, size_t);
int /*<<< orphan*/ stderr;
void *check_realloc(void *ptr, size_t size, char *reason) {
  void *res = realloc(ptr, size);
  if ((res == NULL) && (size > 0)) {
    fprintf(stderr, "[Error] Failed to allocate memory (%s)!\n", reason);
    exit(1);
  }
  return res;
}


x86 assembly (clang, -O0):
<check_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118e <check_realloc+0x5e>
cmpq   $0x0,-0x10(%rbp)
jbe    118e <check_realloc+0x5e>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe81(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_realloc>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
call   1040 <realloc@plt>
test   %rbx,%rbx
je     1149 <check_realloc+0x19>
test   %rax,%rax
je     1151 <check_realloc+0x21>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
lea    0xe9f(%rip),%rsi
mov    %r14,%rdx
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [41 x i8] c"[Error] Failed to allocate memory (%s)!\0A\00"
define noalias i8* @check_realloc(i8* nocapture %0, i64 %1, i8* %2) {
%4 = tail call i8* @realloc(i8* %0, i64 %1)
%5 = icmp eq i8* %4, null
%6 = icmp ne i64 %1, 0
%7 = and i1 %6, %5
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %2)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %3
ret i8* %4
}