#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 SIZE_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 && 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)
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    0x2e6a(%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    11a8 <reallocarray+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):
<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    0x2e9d(%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>
imul   %rsi,%rcx
mov    %rcx,%rsi
jmp    1030 <realloc@plt>

LLVM IR (-O0):
@SIZE_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* @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 = 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* @SIZE_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 %17
14: ; preds = %3, %8
%15 = mul i64 %2, %1
%16 = tail call i8* @realloc(i8* %0, i64 %15)
br label %17
17: ; preds = %14, %12
%18 = phi i8* [ null, %12 ], [ %16, %14 ]
ret i8* %18
}