#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, int const);
void *realloc(void *, size_t);
int /*<<< orphan*/ stderr;
void *realloc_safe_f(void *orig_ptr, size_t size, const char *file,
                     const int line) {
  void *ptr = NULL;
  ptr = realloc(orig_ptr, size);
  if (ptr == NULL && size != 0) {
    fprintf(stderr, "[%s, %i] Memory reallocation failed!\n", file, line);
    exit(EXIT_FAILURE);
  }
  return ptr;
}


x86 assembly (clang, -O0):
<realloc_safe_f>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a0 <realloc_safe_f+0x70>
cmpq   $0x0,-0x10(%rbp)
je     11a0 <realloc_safe_f+0x70>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xe73(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realloc_safe_f>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%r14d
mov    %rdx,%rbx
mov    %rsi,%rbp
call   1040 <realloc@plt>
test   %rbp,%rbp
je     114c <realloc_safe_f+0x1c>
test   %rax,%rax
je     1151 <realloc_safe_f+0x21>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
lea    0xe9f(%rip),%rsi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1030 <fprintf@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"[%s, %i] Memory reallocation failed!\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @realloc_safe_f(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i8* null, i8** %9
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = call i8* @realloc(i8* %10, i64 %11)
store i8* %12, i8** %9
%13 = load i8*, i8** %9
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %25
15: ; preds = %4
%16 = load i64, i64* %6
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %7
%21 = load i32, i32* %8
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %15, %4
%26 = load i8*, i8** %9
ret i8* %26
}

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