#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*/ errno;
int /*<<< orphan*/ kmr_error2(int /*<<< orphan*/, char *, char const *,
                              int const, char const *);
void *realloc(void *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, size_t, char *);
char *strerror(int /*<<< orphan*/);
void *kmr_safe_realloc(void *q, size_t s, char *szexpr, const char *file,
                       const int line, const char *func) {
  void *p = realloc(q, s);
  if (p == 0) {
    char ee[80];
    char *m = strerror(errno);
    snprintf(ee, 80, "realloc((%s)=%zd): %s", szexpr, s, m);
    kmr_error2(0, ee, file, line, func);
  }
  return p;
}


x86 assembly (clang, -O0):
<kmr_safe_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11d4 <kmr_safe_realloc+0x94>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,-0x98(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    -0x98(%rbp),%r9
mov    $0x50,%esi
lea    0xe4a(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x90(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%r8
xor    %edi,%edi
call   1040 <kmr_error2@plt>
mov    -0x38(%rbp),%rax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kmr_safe_realloc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r9,%r14
mov    %r8d,%r15d
mov    %rcx,%r12
mov    %rdx,%r13
mov    %rsi,%rbx
call   1050 <realloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11a8 <kmr_safe_realloc+0x68>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe81(%rip),%rdx
mov    %rsp,%rdi
mov    $0x50,%esi
mov    %r13,%rcx
mov    %rbx,%r8
mov    %rax,%r9
call   1030 <snprintf@plt>
xor    %edi,%edi
mov    %rsp,%rsi
mov    %r12,%rdx
mov    %r15d,%ecx
mov    %r14,%r8
call   1040 <kmr_error2@plt>
mov    %rbp,%rax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [22 x i8] c"realloc((%s)=%zd): %s\00"
define i8* @kmr_safe_realloc(i8* %0, i64 %1, i8* %2, i8* %3, i32 %4, i8* %5) {
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca [80 x i8]
%15 = alloca i8*
store i8* %0, i8** %7
store i64 %1, i64* %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
store i8* %5, i8** %12
%16 = load i8*, i8** %7
%17 = load i64, i64* %8
%18 = call i8* @realloc(i8* %16, i64 %17)
store i8* %18, i8** %13
%19 = load i8*, i8** %13
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %34
21: ; preds = %6
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
store i8* %23, i8** %15
%24 = getelementptr inbounds [80 x i8], [80 x i8]* %14, i64 0, i64 0
%25 = load i8*, i8** %9
%26 = load i64, i64* %8
%27 = load i8*, i8** %15
%28 = call i32 @snprintf(i8* %24, i32 80, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %25, i64 %26, i8* %27)
%29 = getelementptr inbounds [80 x i8], [80 x i8]* %14, i64 0, i64 0
%30 = load i8*, i8** %10
%31 = load i32, i32* %11
%32 = load i8*, i8** %12
%33 = call i32 @kmr_error2(i32 0, i8* %29, i8* %30, i32 %31, i8* %32)
br label %34
34: ; preds = %21, %6
%35 = load i8*, i8** %13
ret i8* %35
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [22 x i8] c"realloc((%s)=%zd): %s\00"
define noalias i8* @kmr_safe_realloc(i8* nocapture %0, i64 %1, i8* %2, i8* %3, i32 %4, i8* %5) {
%7 = alloca [80 x i8]
%8 = tail call i8* @realloc(i8* %0, i64 %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %16
10: ; preds = %6
%11 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = call i32 @snprintf(i8* %11, i32 80, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %2, i64 %1, i8* %13)
%15 = call i32 @kmr_error2(i32 0, i8* %11, i8* %3, i32 %4, i8* %5)
br label %16
16: ; preds = %10, %6
ret i8* %8
}