#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*/ log_error_format(char *, char const *, char const *,
                                    unsigned long);
int /*<<< orphan*/ log_function_error_message(char *, char *);
void *realloc(void *, size_t);
extern void *safe_realloc(void *ptr, size_t size, const char *modulename,
                          const char *funcname, unsigned long lineno) {
  void *new_ptr = realloc(ptr, size);
  if (new_ptr == NULL) {
    log_function_error_message("stdlib_util.safe_realloc", "out of memory!");
    if (modulename != NULL && funcname != NULL && lineno > 0) {
      log_error_format("[safe_realloc stacktrace] %s:%s:%u\n", modulename,
                       funcname, lineno);
    }
    exit(1);
  }
  return new_ptr;
}


x86 assembly (clang, -O0):
<safe_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ce <safe_realloc+0x8e>
lea    0xe81(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1030 <log_function_error_message@plt>
cmpq   $0x0,-0x18(%rbp)
je     11c4 <safe_realloc+0x84>
cmpq   $0x0,-0x20(%rbp)
je     11c4 <safe_realloc+0x84>
cmpq   $0x0,-0x28(%rbp)
jbe    11c4 <safe_realloc+0x84>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xe69(%rip),%rdi
call   1040 <log_error_format@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_realloc>:
push   %r15
push   %r14
push   %rbx
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%rbx
call   1050 <realloc@plt>
test   %rax,%rax
je     115e <safe_realloc+0x1e>
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe9b(%rip),%rdi
lea    0xead(%rip),%rsi
call   1030 <log_function_error_message@plt>
test   %rbx,%rbx
je     1195 <safe_realloc+0x55>
test   %r15,%r15
je     1195 <safe_realloc+0x55>
test   %r14,%r14
je     1195 <safe_realloc+0x55>
lea    0xea1(%rip),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <log_error_format@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"stdlib_util.safe_realloc\00"
@.str.1 = [15 x i8] c"out of memory!\00"
@.str.2 = [36 x i8] c"[safe_realloc stacktrace] %s:%s:%u\0A\00"
define i8* @safe_realloc(i8* %0, i64 %1, i8* %2, i8* %3, i64 %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i64 %4, i64* %10
%12 = load i8*, i8** %6
%13 = load i64, i64* %7
%14 = call i8* @realloc(i8* %12, i64 %13)
store i8* %14, i8** %11
%15 = load i8*, i8** %11
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %34
17: ; preds = %5
%18 = call i32 @log_function_error_message(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%19 = load i8*, i8** %8
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %32
21: ; preds = %17
%22 = load i8*, i8** %9
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = load i64, i64* %10
%26 = icmp ugt i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i8*, i8** %8
%29 = load i8*, i8** %9
%30 = load i64, i64* %10
%31 = call i32 @log_error_format(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %28, i8* %29, i64 %30)
br label %32
32: ; preds = %27, %24, %21, %17
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %5
%35 = load i8*, i8** %11
ret i8* %35
}

LLVM IR (-O2):
@.str = [25 x i8] c"stdlib_util.safe_realloc\00"
@.str.1 = [15 x i8] c"out of memory!\00"
@.str.2 = [36 x i8] c"[safe_realloc stacktrace] %s:%s:%u\0A\00"
define noalias i8* @safe_realloc(i8* nocapture %0, i64 %1, i8* %2, i8* %3, i64 %4) {
%6 = tail call i8* @realloc(i8* %0, i64 %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %19
8: ; preds = %5
%9 = tail call i32 @log_function_error_message(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp ne i8* %2, null
%11 = icmp ne i8* %3, null
%12 = and i1 %10, %11
%13 = icmp ne i64 %4, 0
%14 = and i1 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = tail call i32 @log_error_format(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %3, i64 %4)
br label %17
17: ; preds = %15, %8
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %5
ret i8* %6
}