#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*/ puts(char *);
void *realloc(void *, size_t);
void *safe_realloc(void *old, size_t len, void (*err_callback)(char *)) {
  void *ptr = realloc(old, len);
  if (ptr)
    return ptr;
  else {
    if (err_callback)
      err_callback("FATAL: Memory allocation error!\n");
    else
      puts("FATAL: Memory allocation error!\n");
    exit(0xDEADBEEF);
  }
}


x86 assembly (clang, -O0):
<safe_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)
je     116a <safe_realloc+0x3a>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
cmpq   $0x0,-0x18(%rbp)
je     1184 <safe_realloc+0x54>
lea    0xe84(%rip),%rdi
call   *-0x18(%rbp)
jmp    1190 <safe_realloc+0x60>
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
mov    $0xdeadbeef,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<safe_realloc>:
push   %rbx
mov    %rdx,%rbx
call   1040 <realloc@plt>
test   %rax,%rax
je     1140 <safe_realloc+0x10>
pop    %rbx
ret
lea    0xeb9(%rip),%rdi
test   %rbx,%rbx
jne    115b <safe_realloc+0x2b>
call   1030 <puts@plt>
mov    $0xdeadbeef,%edi
call   1050 <exit@plt>
call   *%rbx
mov    $0xdeadbeef,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [33 x i8] c"FATAL: Memory allocation error!\0A\00"
define i8* @safe_realloc(i8* %0, i64 %1, void (i8*)* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca void (i8*)*
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store void (i8*)* %2, void (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 ne i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i8*, i8** %7
ret i8* %14
15: ; preds = %3
%16 = load void (i8*)*, void (i8*)** %6
%17 = icmp ne void (i8*)* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = load void (i8*)*, void (i8*)** %6
br label %22
20: ; preds = %15
%21 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %18
%23 = call i32 @exit(i32 -559038737)
unreachable
}

LLVM IR (-O2):
@.str = [33 x i8] c"FATAL: Memory allocation error!\0A\00"
define i8* @safe_realloc(i8* nocapture %0, i64 %1, void (i8*)* %2) {
%4 = tail call i8* @realloc(i8* %0, i64 %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %7, label %6
6: ; preds = %3
ret i8* %4
7: ; preds = %3
%8 = icmp eq void (i8*)* %2, null
br i1 %8, label %10, label %9
9: ; preds = %7
tail call void %2(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %12
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %9
%13 = tail call i32 @exit(i32 -559038737)
unreachable
}