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


x86 assembly (clang, -O0):
<reallocarray>:
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 <reallocarray+0x3c>
movabs $0x100000000,%rax
cmp    %rax,-0x20(%rbp)
jb     1192 <reallocarray+0x82>
cmpq   $0x0,-0x18(%rbp)
jbe    1192 <reallocarray+0x82>
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
cmp    -0x20(%rbp),%rax
jae    1192 <reallocarray+0x82>
mov    0x2e66(%rip),%rax
mov    (%rax),%ecx
mov    0x2e55(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11cb <reallocarray+0xbb>
cmpq   $0x0,-0x20(%rbp)
je     11a8 <reallocarray+0x98>
cmpq   $0x0,-0x18(%rbp)
jne    11b5 <reallocarray+0xa5>
movq   $0x0,-0x8(%rbp)
jmp    11cb <reallocarray+0xbb>
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):
<reallocarray>:
mov    %rdx,%rcx
test   %rsi,%rsi
je     1162 <reallocarray+0x52>
mov    %rcx,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     1162 <reallocarray+0x52>
mov    0x2ea5(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rdx
or     %rsi,%rdx
shr    $0x20,%rdx
je     1159 <reallocarray+0x49>
xor    %edx,%edx
div    %rsi
cmp    %rcx,%rax
jae    1162 <reallocarray+0x52>
mov    0x2e95(%rip),%rax
mov    (%rax),%eax
mov    0x2e84(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
ret
xor    %edx,%edx
div    %esi
cmp    %rcx,%rax
jb     1144 <reallocarray+0x34>
test   %rsi,%rsi
je     1178 <reallocarray+0x68>
test   %rcx,%rcx
je     1178 <reallocarray+0x68>
imul   %rsi,%rcx
mov    %rcx,%rsi
jmp    1030 <realloc@plt>
xor    %eax,%eax
ret

LLVM IR (-O0):
@UINTPTR_MAX = global i64 0
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @reallocarray(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* @UINTPTR_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 %37
24: ; preds = %16, %13, %10
%25 = load i64, i64* %7
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i64, i64* %6
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %27, %24
store i8* null, i8** %4
br label %37
31: ; preds = %27
%32 = load i8*, i8** %5
%33 = load i64, i64* %7
%34 = load i64, i64* %6
%35 = mul i64 %33, %34
%36 = call i8* @realloc(i8* %32, i64 %35)
store i8* %36, i8** %4
br label %37
37: ; preds = %31, %30, %22
%38 = load i8*, i8** %4
ret i8* %38
}

LLVM IR (-O2):
@UINTPTR_MAX = global i64 0
@ENOMEM = global i32 0
@errno = global i32 0
define noalias i8* @reallocarray(i8* nocapture %0, i64 %1, i64 %2) {
%4 = or i64 %2, %1
%5 = icmp ugt i64 %4, 4294967295
%6 = icmp ne i64 %1, 0
%7 = and i1 %6, %5
br i1 %7, label %8, label %14
8: ; preds = %3
%9 = load i64, i64* @UINTPTR_MAX
%10 = udiv i64 %9, %1
%11 = icmp ult i64 %10, %2
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = load i32, i32* @ENOMEM
store i32 %13, i32* @errno
br label %21
14: ; preds = %3, %8
%15 = icmp eq i64 %2, 0
%16 = icmp eq i64 %1, 0
%17 = or i1 %16, %15
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = mul i64 %2, %1
%20 = tail call i8* @realloc(i8* %0, i64 %19)
br label %21
21: ; preds = %14, %18, %12
%22 = phi i8* [ null, %12 ], [ %20, %18 ], [ null, %14 ]
ret i8* %22
}