#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 *, size_t);
void *realloc(void *, size_t);
int /*<<< orphan*/ stderr;
void *erealloc(void *ptr, size_t num, size_t size) {
  void *mem = realloc(ptr, num * size);
  if (!mem && size) {
    fprintf(stderr, "Failed to reallocate %zu bytes\n", num * size);
    exit(EXIT_FAILURE);
  }
  return mem;
}


x86 assembly (clang, -O0):
<erealloc>:
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
imul   -0x18(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119c <erealloc+0x6c>
cmpq   $0x0,-0x18(%rbp)
je     119c <erealloc+0x6c>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
imul   -0x18(%rbp),%rdx
lea    0xe77(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<erealloc>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
imul   %rdx,%r14
mov    %r14,%rsi
call   1040 <realloc@plt>
test   %rbx,%rbx
je     1150 <erealloc+0x20>
test   %rax,%rax
je     1158 <erealloc+0x28>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
lea    0xe98(%rip),%rsi
mov    %r14,%rdx
call   1030 <fprintf@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

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

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