#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 CHAR_BIT;
int /*<<< orphan*/ EOVERFLOW;
size_t SIZE_MAX;
int /*<<< orphan*/ errno;
void *realloc(void *, size_t);
void *reallocarray(void *ptr, size_t n, size_t size) {
  if ((n | size) >= (((size_t)1) << (sizeof(size_t) * CHAR_BIT / 2)) &&
      n > SIZE_MAX / size) {
    errno = EOVERFLOW;
    return NULL;
  }
  return realloc(ptr, n * size);
}


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)
mov    -0x18(%rbp),%rax
or     -0x20(%rbp),%rax
mov    0x2ea5(%rip),%rcx
movslq (%rcx),%rcx
shl    $0x3,%rcx
shr    $0x1,%rcx
mov    $0x1,%edx
shl    %cl,%rdx
mov    %rdx,%rcx
cmp    %rcx,%rax
jb     119b <reallocarray+0x8b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jbe    119b <reallocarray+0x8b>
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e44(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11b1 <reallocarray+0xa1>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
imul   -0x20(%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,%r8
mov    %rdx,%rax
or     %rsi,%rax
mov    0x2eb8(%rip),%rcx
mov    (%rcx),%cl
shl    $0x2,%cl
shr    %cl,%rax
test   %rax,%rax
je     116c <reallocarray+0x5c>
mov    0x2e8c(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %r8,%rcx
shr    $0x20,%rcx
je     1162 <reallocarray+0x52>
xor    %edx,%edx
div    %r8
cmp    %rsi,%rax
jae    116c <reallocarray+0x5c>
mov    0x2e9c(%rip),%rax
mov    (%rax),%eax
mov    0x2e73(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
ret
xor    %edx,%edx
div    %r8d
cmp    %rsi,%rax
jb     114d <reallocarray+0x3d>
imul   %rsi,%r8
mov    %r8,%rsi
jmp    1030 <realloc@plt>

LLVM IR (-O0):
@CHAR_BIT = global i32 0
@SIZE_MAX = global i64 0
@EOVERFLOW = 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 = load i64, i64* %7
%10 = or i64 %8, %9
%11 = load i32, i32* @CHAR_BIT
%12 = sext i32 %11 to i64
%13 = mul i64 8, %12
%14 = udiv i64 %13, 2
%15 = shl i64 1, %14
%16 = icmp uge i64 %10, %15
br i1 %16, label %17, label %25
17: ; preds = %3
%18 = load i64, i64* %6
%19 = load i64, i64* @SIZE_MAX
%20 = load i64, i64* %7
%21 = udiv i64 %19, %20
%22 = icmp ugt i64 %18, %21
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i32, i32* @EOVERFLOW
store i32 %24, i32* @errno
store i8* null, i8** %4
br label %31
25: ; preds = %17, %3
%26 = load i8*, i8** %5
%27 = load i64, i64* %6
%28 = load i64, i64* %7
%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):
@CHAR_BIT = global i32 0
@SIZE_MAX = global i64 0
@EOVERFLOW = global i32 0
@errno = global i32 0
define noalias i8* @reallocarray(i8* nocapture %0, i64 %1, i64 %2) {
%4 = or i64 %2, %1
%5 = load i32, i32* @CHAR_BIT
%6 = sext i32 %5 to i64
%7 = shl nsw i64 %6, 2
%8 = and i64 %7, 9223372036854775804
%9 = lshr i64 %4, %8
%10 = icmp eq i64 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %3
%12 = load i64, i64* @SIZE_MAX
%13 = udiv i64 %12, %2
%14 = icmp ult i64 %13, %1
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = load i32, i32* @EOVERFLOW
store i32 %16, i32* @errno
br label %20
17: ; preds = %11, %3
%18 = mul i64 %2, %1
%19 = tail call i8* @realloc(i8* %0, i64 %18)
br label %20
20: ; preds = %17, %15
%21 = phi i8* [ null, %15 ], [ %19, %17 ]
ret i8* %21
}