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


x86 assembly (clang, -O0):
<check_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11ab <check_realloc+0x6b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119e <check_realloc+0x5e>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe71(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c7 <check_realloc+0x87>
cmpq   $0x0,-0x10(%rbp)
je     11bf <check_realloc+0x7f>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_realloc>:
push   %rbx
test   %rsi,%rsi
je     1155 <check_realloc+0x15>
mov    %rdx,%rbx
call   1050 <realloc@plt>
test   %rax,%rax
je     1163 <check_realloc+0x23>
pop    %rbx
ret
test   %rdi,%rdi
je     115f <check_realloc+0x1f>
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
lea    0xe8d(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <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 i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%9 = load i64, i64* %6
%10 = icmp ugt i64 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = load i64, i64* %6
%14 = call i8* @realloc(i8* %12, i64 %13)
store i8* %14, i8** %8
%15 = load i8*, i8** %8
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %7
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %11
%23 = load i8*, i8** %8
store i8* %23, i8** %4
br label %31
24: ; preds = %3
%25 = load i8*, i8** %5
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i8*, i8** %5
%29 = call i32 @free(i8* %28)
br label %30
30: ; preds = %27, %24
store i8* null, i8** %4
br label %31
31: ; preds = %30, %22
%32 = load i8*, i8** %4
ret i8* %32
}

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* %0, i64 %1, i8* %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %12, label %5
5: ; preds = %3
%6 = tail call i8* @realloc(i8* %0, i64 %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %16
8: ; preds = %5
%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
%13 = icmp eq i8* %0, null
br i1 %13, label %16, label %14
14: ; preds = %12
%15 = tail call i32 @free(i8* %0)
br label %16
16: ; preds = %12, %14, %5
%17 = phi i8* [ %6, %5 ], [ null, %14 ], [ null, %12 ]
ret i8* %17
}