#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 *malloc(int /*<<< orphan*/);
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 ((void *)0);
  }
  if (nmemb == 0 || size == 0)
    return malloc(0);
  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    115c <reallocarray+0x3c>
movabs $0x100000000,%rax
cmp    %rax,-0x20(%rbp)
jb     11a2 <reallocarray+0x82>
cmpq   $0x0,-0x18(%rbp)
jbe    11a2 <reallocarray+0x82>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
cmp    -0x20(%rbp),%rax
jae    11a2 <reallocarray+0x82>
mov    0x2e56(%rip),%rax
mov    (%rax),%ecx
mov    0x2e45(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11de <reallocarray+0xbe>
cmpq   $0x0,-0x18(%rbp)
je     11b8 <reallocarray+0x98>
cmpq   $0x0,-0x20(%rbp)
jne    11c8 <reallocarray+0xa8>
xor    %edi,%edi
call   1030 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11de <reallocarray+0xbe>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
imul   -0x18(%rbp),%rsi
call   1040 <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     1172 <reallocarray+0x52>
mov    %rcx,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     1172 <reallocarray+0x52>
mov    0x2e8d(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rdx
or     %rsi,%rdx
shr    $0x20,%rdx
je     1169 <reallocarray+0x49>
xor    %edx,%edx
div    %rsi
cmp    %rcx,%rax
jae    1172 <reallocarray+0x52>
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    0x2e74(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
ret
xor    %edx,%edx
div    %esi
cmp    %rcx,%rax
jb     1154 <reallocarray+0x34>
test   %rsi,%rsi
je     1188 <reallocarray+0x68>
test   %rcx,%rcx
je     1188 <reallocarray+0x68>
imul   %rsi,%rcx
mov    %rcx,%rsi
jmp    1040 <realloc@plt>
xor    %edi,%edi
jmp    1030 <malloc@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 %38
24: ; preds = %16, %13, %10
%25 = load i64, i64* %6
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i64, i64* %7
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27, %24
%31 = call i8* @malloc(i32 0)
store i8* %31, i8** %4
br label %38
32: ; preds = %27
%33 = load i8*, i8** %5
%34 = load i64, i64* %7
%35 = load i64, i64* %6
%36 = mul i64 %34, %35
%37 = call i8* @realloc(i8* %33, i64 %36)
store i8* %37, i8** %4
br label %38
38: ; preds = %32, %30, %22
%39 = load i8*, i8** %4
ret i8* %39
}

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 %23
14: ; preds = %3, %8
%15 = icmp eq i64 %1, 0
%16 = icmp eq i64 %2, 0
%17 = or i1 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i8* @malloc(i32 0)
br label %23
20: ; preds = %14
%21 = mul i64 %2, %1
%22 = tail call i8* @realloc(i8* %0, i64 %21)
br label %23
23: ; preds = %20, %18, %12
%24 = phi i8* [ null, %12 ], [ %19, %18 ], [ %22, %20 ]
ret i8* %24
}