#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) {
  const size_t mul_no_overflow = (size_t)1 << (sizeof(size_t) * 4);
  if ((nmemb >= mul_no_overflow || size >= mul_no_overflow) && 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    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movabs $0x100000000,%rax
mov    %rax,-0x28(%rbp)
movabs $0x100000000,%rax
cmp    %rax,-0x18(%rbp)
jae    115a <reallocarray+0x4a>
movabs $0x100000000,%rax
cmp    %rax,-0x20(%rbp)
jb     11a0 <reallocarray+0x90>
cmpq   $0x0,-0x18(%rbp)
jbe    11a0 <reallocarray+0x90>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
cmp    -0x20(%rbp),%rax
jae    11a0 <reallocarray+0x90>
mov    0x2e58(%rip),%rax
mov    (%rax),%ecx
mov    0x2e47(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11b6 <reallocarray+0xa6>
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    $0x30,%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
%8 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i64 4294967296, i64* %8
%9 = load i64, i64* %6
%10 = icmp uge i64 %9, 4294967296
br i1 %10, label %14, label %11
11: ; preds = %3
%12 = load i64, i64* %7
%13 = icmp uge i64 %12, 4294967296
br i1 %13, label %14, label %25
14: ; preds = %11, %3
%15 = load i64, i64* %6
%16 = icmp ugt i64 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = load i64, i64* @SIZE_MAX
%19 = load i64, i64* %6
%20 = udiv i64 %18, %19
%21 = load i64, i64* %7
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i32, i32* @ENOMEM
store i32 %24, i32* @errno
store i8* null, i8** %4
br label %31
25: ; preds = %17, %14, %11
%26 = load i8*, i8** %5
%27 = load i64, i64* %7
%28 = load i64, i64* %6
%29 = mul i64 %27, %28
%30 = call i8* @realloc(i8* %26, i64 %29)
store i8* %30, i8** %4
br label %31
31: ; preds = %25, %23
%32 = load i8*, i8** %4
ret i8* %32
}

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
}