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


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movabs $0x100000000,%rax
cmp    %rax,-0x18(%rbp)
jae    114c <xrealloc+0x3c>
movabs $0x100000000,%rax
cmp    %rax,-0x20(%rbp)
jb     1192 <xrealloc+0x82>
cmpq   $0x0,-0x18(%rbp)
jbe    1192 <xrealloc+0x82>
mov    0x2e6a(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
cmp    -0x20(%rbp),%rax
jae    1192 <xrealloc+0x82>
mov    0x2e66(%rip),%rax
mov    (%rax),%ecx
mov    0x2e55(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11a8 <xrealloc+0x98>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
imul   -0x18(%rbp),%rsi
call   1030 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
test   %rdx,%rdx
je     113b <xrealloc+0x2b>
test   %rsi,%rsi
je     113b <xrealloc+0x2b>
mov    %rdx,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     113b <xrealloc+0x2b>
mov    0x2eb3(%rip),%rax
mov    (%rax),%eax
mov    0x2ea2(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
ret
imul   %rsi,%rdx
mov    %rdx,%rsi
jmp    1030 <realloc@plt>

LLVM IR (-O0):
@SIZE_MAX = constant i64 0
@ENOMEM = 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
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%8 = load i64, i64* %6
%9 = icmp uge i64 %8, 4294967296
br i1 %9, label %13, label %10
10: ; preds = %3
%11 = load i64, i64* %7
%12 = icmp uge i64 %11, 4294967296
br i1 %12, label %13, label %24
13: ; preds = %10, %3
%14 = load i64, i64* %6
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %13
%17 = load i64, i64* @SIZE_MAX
%18 = load i64, i64* %6
%19 = udiv i64 %17, %18
%20 = load i64, i64* %7
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = load i32, i32* @ENOMEM
store i32 %23, i32* @errno
store i8* null, i8** %4
br label %30
24: ; preds = %16, %13, %10
%25 = load i8*, i8** %5
%26 = load i64, i64* %7
%27 = load i64, i64* %6
%28 = mul i64 %26, %27
%29 = call i8* @realloc(i8* %25, i64 %28)
store i8* %29, i8** %4
br label %30
30: ; preds = %24, %22
%31 = load i8*, i8** %4
ret i8* %31
}

LLVM IR (-O2):
@SIZE_MAX = constant i64 0
@ENOMEM = global i32 0
@errno = global i32 0
define noalias i8* @xrealloc(i8* nocapture %0, i64 %1, i64 %2) {
%4 = or i64 %2, %1
%5 = icmp ult i64 %4, 4294967296
%6 = icmp eq i64 %1, 0
%7 = or i1 %6, %5
%8 = icmp eq i64 %2, 0
%9 = or i1 %8, %7
br i1 %9, label %12, label %10
10: ; preds = %3
%11 = load i32, i32* @ENOMEM
store i32 %11, i32* @errno
br label %15
12: ; preds = %3
%13 = mul i64 %2, %1
%14 = tail call i8* @realloc(i8* %0, i64 %13)
br label %15
15: ; preds = %12, %10
%16 = phi i8* [ null, %10 ], [ %14, %12 ]
ret i8* %16
}