#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*/ die_out_of_memory();
void *realloc(void *, size_t);
void *xreallocarray(void *ptr, size_t nmemb, size_t size) {
  size_t bytes = nmemb * size;
  if ((nmemb | size) >= (((size_t)1) << (sizeof(size_t) * 4)) && size &&
      bytes / size != nmemb)
    die_out_of_memory();
  void *p = realloc(ptr, bytes);
  if (!p)
    die_out_of_memory();
  return p;
}


x86 assembly (clang, -O0):
<xreallocarray>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
or     -0x18(%rbp),%rax
movabs $0x100000000,%rcx
cmp    %rcx,%rax
jb     1184 <xreallocarray+0x64>
cmpq   $0x0,-0x18(%rbp)
je     1184 <xreallocarray+0x64>
mov    -0x20(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
cmp    -0x10(%rbp),%rax
je     1184 <xreallocarray+0x64>
mov    $0x0,%al
call   1040 <die_out_of_memory@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a7 <xreallocarray+0x87>
mov    $0x0,%al
call   1040 <die_out_of_memory@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xreallocarray>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rcx
mov    %rdi,%r14
mov    %rdx,%rax
mul    %rsi
mov    %rax,%rbx
seto   %al
test   %rcx,%rcx
je     114f <xreallocarray+0x2f>
or     %rsi,%rcx
shr    $0x20,%rcx
je     114f <xreallocarray+0x2f>
test   %al,%al
je     114f <xreallocarray+0x2f>
xor    %eax,%eax
call   1040 <die_out_of_memory@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1169 <xreallocarray+0x49>
xor    %eax,%eax
call   1040 <die_out_of_memory@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @xreallocarray(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%9 = load i64, i64* %5
%10 = load i64, i64* %6
%11 = mul i64 %9, %10
store i64 %11, i64* %7
%12 = load i64, i64* %5
%13 = load i64, i64* %6
%14 = or i64 %12, %13
%15 = icmp uge i64 %14, 4294967296
br i1 %15, label %16, label %27
16: ; preds = %3
%17 = load i64, i64* %6
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %16
%20 = load i64, i64* %7
%21 = load i64, i64* %6
%22 = udiv i64 %20, %21
%23 = load i64, i64* %5
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 (...) @die_out_of_memory()
br label %27
27: ; preds = %25, %19, %16, %3
%28 = load i8*, i8** %4
%29 = load i64, i64* %7
%30 = call i8* @realloc(i8* %28, i64 %29)
store i8* %30, i8** %8
%31 = load i8*, i8** %8
%32 = icmp ne i8* %31, null
br i1 %32, label %35, label %33
33: ; preds = %27
%34 = call i32 (...) @die_out_of_memory()
br label %35
35: ; preds = %33, %27
%36 = load i8*, i8** %8
ret i8* %36
}

LLVM IR (-O2):
define noalias i8* @xreallocarray(i8* nocapture %0, i64 %1, i64 %2) {
%4 = tail call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %2, i64 %1)
%5 = extractvalue { i64, i1 } %4, 0
%6 = or i64 %2, %1
%7 = icmp ugt i64 %6, 4294967295
%8 = icmp ne i64 %2, 0
%9 = and i1 %8, %7
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = extractvalue { i64, i1 } %4, 1
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = tail call i32 (...) @die_out_of_memory()
br label %14
14: ; preds = %12, %10, %3
%15 = tail call i8* @realloc(i8* %0, i64 %5)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 (...) @die_out_of_memory()
br label %19
19: ; preds = %17, %14
ret i8* %15
}