#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*/ free(void *);
int /*<<< orphan*/ hz_check_alloc(void *);
int /*<<< orphan*/ hz_check_size(size_t, size_t);
void *realloc(void *, size_t);
void *hz_realloc(void *ptr, size_t num, size_t size) {
  hz_check_size(num, size);
  if (num == 0) {
    free(ptr);
    return NULL;
  }
  ptr = realloc(ptr, num * size);
  hz_check_alloc(ptr);
  return ptr;
}


x86 assembly (clang, -O0):
<hz_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <hz_check_size@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1182 <hz_realloc+0x42>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11a9 <hz_realloc+0x69>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
imul   -0x20(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <hz_check_alloc@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hz_realloc>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1040 <hz_check_size@plt>
test   %rbx,%rbx
je     117a <hz_realloc+0x3a>
imul   %rbx,%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <hz_check_alloc@plt>
jmp    1184 <hz_realloc+0x44>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @hz_realloc(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%8 = load i64, i64* %6
%9 = load i64, i64* %7
%10 = call i32 @hz_check_size(i64 %8, i64 %9)
%11 = load i64, i64* %6
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i32 @free(i8* %14)
store i8* null, i8** %4
br label %25
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = load i64, i64* %6
%19 = load i64, i64* %7
%20 = mul i64 %18, %19
%21 = call i8* @realloc(i8* %17, i64 %20)
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = call i32 @hz_check_alloc(i8* %22)
%24 = load i8*, i8** %5
store i8* %24, i8** %4
br label %25
25: ; preds = %16, %13
%26 = load i8*, i8** %4
ret i8* %26
}

LLVM IR (-O2):
define i8* @hz_realloc(i8* nocapture %0, i64 %1, i64 %2) {
%4 = tail call i32 @hz_check_size(i64 %1, i64 %2)
%5 = icmp eq i64 %1, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @free(i8* %0)
br label %12
8: ; preds = %3
%9 = mul i64 %2, %1
%10 = tail call i8* @realloc(i8* %0, i64 %9)
%11 = tail call i32 @hz_check_alloc(i8* %10)
br label %12
12: ; preds = %8, %6
%13 = phi i8* [ null, %6 ], [ %10, %8 ]
ret i8* %13
}