#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*/ EINVAL;
int /*<<< orphan*/ ENOMEM;
size_t SIZE_MAX;
int /*<<< orphan*/ errno;
void *realloc(void *, size_t);
void *reallocarray(void *optr, size_t nmemb, size_t size) {
  if (nmemb == 0U) {
    errno = EINVAL;
    return NULL;
  }
  if ((nmemb >= ((size_t)1 << (sizeof(size_t) * 4)) ||
       size >= ((size_t)1 << (sizeof(size_t) * 4))) &&
      nmemb > 0U && SIZE_MAX / nmemb < size) {
    errno = ENOMEM;
    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)
cmpq   $0x0,-0x18(%rbp)
jne    114e <reallocarray+0x3e>
mov    0x2e9a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e99(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11d2 <reallocarray+0xc2>
movabs $0x100000000,%rax
cmp    %rax,-0x18(%rbp)
jae    1176 <reallocarray+0x66>
movabs $0x100000000,%rax
cmp    %rax,-0x20(%rbp)
jb     11bc <reallocarray+0xac>
cmpq   $0x0,-0x18(%rbp)
jbe    11bc <reallocarray+0xac>
mov    0x2e38(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
cmp    -0x20(%rbp),%rax
jae    11bc <reallocarray+0xac>
mov    0x2e3c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2b(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11d2 <reallocarray+0xc2>
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>:
test   %rsi,%rsi
je     114d <reallocarray+0x3d>
mov    %rdx,%rcx
mov    %rdx,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     116b <reallocarray+0x5b>
mov    0x2e95(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rdx
or     %rsi,%rdx
shr    $0x20,%rdx
je     1162 <reallocarray+0x52>
xor    %edx,%edx
div    %rsi
cmp    %rcx,%rax
jae    116b <reallocarray+0x5b>
mov    0x2e95(%rip),%rax
jmp    1154 <reallocarray+0x44>
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    0x2e7b(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
ret
xor    %edx,%edx
div    %esi
cmp    %rcx,%rax
jb     1144 <reallocarray+0x34>
imul   %rsi,%rcx
mov    %rcx,%rsi
jmp    1030 <realloc@plt>

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

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