#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*/ EOVERFLOW;
size_t const SIZE_MAX;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
void *realloc(void *, size_t const);
void *xrealloc(void **p, const size_t n, const size_t size) {
  assert(p != NULL);
  assert(n > 0);
  assert(size > 0);
  void *new_p;
  if ((n >= ((size_t)1 << (sizeof(size_t) * 4)) ||
       size >= ((size_t)1 << (sizeof(size_t) * 4))) &&
      n > 0 && SIZE_MAX / n < size) {
    errno = EOVERFLOW;
    return NULL;
  }
  if (!(new_p = realloc(*p, n * size)))
    return NULL;
  return (*p = new_p);
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
movabs $0x100000000,%rax
cmp    %rax,-0x18(%rbp)
jae    1192 <xrealloc+0x72>
movabs $0x100000000,%rax
cmp    %rax,-0x20(%rbp)
jb     11d8 <xrealloc+0xb8>
cmpq   $0x0,-0x18(%rbp)
jbe    11d8 <xrealloc+0xb8>
mov    0x2e24(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
cmp    -0x20(%rbp),%rax
jae    11d8 <xrealloc+0xb8>
mov    0x2e30(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0f(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1217 <xrealloc+0xf7>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
imul   -0x20(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1208 <xrealloc+0xe8>
movq   $0x0,-0x8(%rbp)
jmp    1217 <xrealloc+0xf7>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1030 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1030 <assert@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1030 <assert@plt>
test   %rbx,%rbx
je     1182 <xrealloc+0x62>
test   %r15,%r15
je     1182 <xrealloc+0x62>
mov    %rbx,%rax
or     %r15,%rax
shr    $0x20,%rax
je     1182 <xrealloc+0x62>
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    0x2e5a(%rip),%rcx
mov    %eax,(%rcx)
jmp    119b <xrealloc+0x7b>
mov    (%r14),%rdi
imul   %r15,%rbx
mov    %rbx,%rsi
call   1040 <realloc@plt>
test   %rax,%rax
je     119b <xrealloc+0x7b>
mov    %rax,(%r14)
jmp    119d <xrealloc+0x7d>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SIZE_MAX = constant i64 0
@EOVERFLOW = global i32 0
@errno = global i32 0
define i8* @xrealloc(i8** %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8** %0, i8*** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%9 = load i8**, i8*** %5
%10 = icmp ne i8** %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i64, i64* %6
%14 = icmp ugt i64 %13, 0
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i64, i64* %7
%18 = icmp ugt i64 %17, 0
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i64, i64* %6
%22 = icmp uge i64 %21, 4294967296
br i1 %22, label %26, label %23
23: ; preds = %3
%24 = load i64, i64* %7
%25 = icmp uge i64 %24, 4294967296
br i1 %25, label %26, label %37
26: ; preds = %23, %3
%27 = load i64, i64* %6
%28 = icmp ugt i64 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %26
%30 = load i64, i64* @SIZE_MAX
%31 = load i64, i64* %6
%32 = udiv i64 %30, %31
%33 = load i64, i64* %7
%34 = icmp ult i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = load i32, i32* @EOVERFLOW
store i32 %36, i32* @errno
store i8* null, i8** %4
br label %49
37: ; preds = %29, %26, %23
%38 = load i8**, i8*** %5
%39 = load i8*, i8** %38
%40 = load i64, i64* %6
%41 = load i64, i64* %7
%42 = mul i64 %40, %41
%43 = call i8* @realloc(i8* %39, i64 %42)
store i8* %43, i8** %8
%44 = icmp ne i8* %43, null
br i1 %44, label %46, label %45
45: ; preds = %37
store i8* null, i8** %4
br label %49
46: ; preds = %37
%47 = load i8*, i8** %8
%48 = load i8**, i8*** %5
store i8* %47, i8** %48
store i8* %47, i8** %4
br label %49
49: ; preds = %46, %45, %35
%50 = load i8*, i8** %4
ret i8* %50
}

LLVM IR (-O2):
@SIZE_MAX = constant i64 0
@EOVERFLOW = global i32 0
@errno = global i32 0
define i8* @xrealloc(i8** %0, i64 %1, i64 %2) {
%4 = icmp ne i8** %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ne i64 %1, 0
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp ne i64 %2, 0
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = or i64 %2, %1
%14 = icmp ugt i64 %13, 4294967295
%15 = and i1 %7, %14
%16 = and i1 %10, %15
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i32, i32* @EOVERFLOW
store i32 %18, i32* @errno
br label %25
19: ; preds = %3
%20 = load i8*, i8** %0
%21 = mul i64 %2, %1
%22 = tail call i8* @realloc(i8* %20, i64 %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %25, label %24
24: ; preds = %19
store i8* %22, i8** %0
br label %25
25: ; preds = %19, %24, %17
%26 = phi i8* [ null, %17 ], [ %22, %24 ], [ null, %19 ]
ret i8* %26
}