#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*/ abort();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memset(char *, char, size_t);
void *realloc(void *, size_t);
int /*<<< orphan*/ stderr;
void *zrealloc(void *ptr, size_t size, size_t elt, size_t osize) {
  void *p = realloc(ptr, size * elt);
  if (p == NULL && size) {
    fprintf(stderr, "out of memory\n");
    abort();
  }
  if (osize < size)
    memset((char *)p + (osize * elt), '\0', (size - osize) * elt);
  return p;
}


x86 assembly (clang, -O0):
<zrealloc>:
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    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
imul   -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a0 <zrealloc+0x60>
cmpq   $0x0,-0x10(%rbp)
je     11a0 <zrealloc+0x60>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11d2 <zrealloc+0x92>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
imul   -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
sub    -0x20(%rbp),%rdx
imul   -0x18(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zrealloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdx,%rsi
imul   %rbx,%rsi
call   1060 <realloc@plt>
mov    %rax,%r15
test   %rbx,%rbx
je     116a <zrealloc+0x2a>
test   %r15,%r15
je     1196 <zrealloc+0x56>
sub    %r12,%rbx
jbe    1187 <zrealloc+0x47>
imul   %r14,%r12
mov    %r15,%rdi
add    %r12,%rdi
imul   %r14,%rbx
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <memset@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    0xe5a(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"out of memory\0A\00"
define i8* @zrealloc(i8* %0, i64 %1, i64 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = load i64, i64* %7
%13 = mul i64 %11, %12
%14 = call i8* @realloc(i8* %10, i64 %13)
store i8* %14, i8** %9
%15 = load i8*, i8** %9
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %24
17: ; preds = %4
%18 = load i64, i64* %6
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%23 = call i32 (...) @abort()
unreachable
24: ; preds = %17, %4
%25 = load i64, i64* %8
%26 = load i64, i64* %6
%27 = icmp ult i64 %25, %26
br i1 %27, label %28, label %40
28: ; preds = %24
%29 = load i8*, i8** %9
%30 = load i64, i64* %8
%31 = load i64, i64* %7
%32 = mul i64 %30, %31
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = load i64, i64* %6
%35 = load i64, i64* %8
%36 = sub i64 %34, %35
%37 = load i64, i64* %7
%38 = mul i64 %36, %37
%39 = call i32 @memset(i8* %33, i8 signext 0, i64 %38)
br label %40
40: ; preds = %28, %24
%41 = load i8*, i8** %9
ret i8* %41
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"out of memory\0A\00"
define i8* @zrealloc(i8* nocapture %0, i64 %1, i64 %2, i64 %3) {
%5 = mul i64 %2, %1
%6 = tail call i8* @realloc(i8* %0, i64 %5)
%7 = icmp eq i8* %6, null
%8 = icmp ne i64 %1, 0
%9 = and i1 %8, %7
br i1 %9, label %10, label %14
10: ; preds = %4
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 (...) @abort()
unreachable
14: ; preds = %4
%15 = icmp ugt i64 %1, %3
br i1 %15, label %16, label %22
16: ; preds = %14
%17 = mul i64 %3, %2
%18 = getelementptr inbounds i8, i8* %6, i64 %17
%19 = sub i64 %1, %3
%20 = mul i64 %19, %2
%21 = tail call i32 @memset(i8* %18, i8 signext 0, i64 %20)
br label %22
22: ; preds = %16, %14
ret i8* %6
}