#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 */
size_t UINT_MAX;
void *realloc(void *, size_t);
int /*<<< orphan*/ sysdie(char *, ...);
void *x_reallocarray(void *p, size_t n, size_t size, const char *file,
                     int line) {
  if (n > 0 && UINT_MAX / n <= size)
    sysdie("realloc too large at %s line %d", file, line);
  p = realloc(p, n * size);
  if (p == NULL)
    sysdie("failed to realloc %lu bytes at %s line %d",
           (unsigned long)(n * size), file, line);
  return p;
}


x86 assembly (clang, -O0):
<x_reallocarray>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jbe    1178 <x_reallocarray+0x58>
mov    0x2e9a(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
cmp    -0x18(%rbp),%rax
ja     1178 <x_reallocarray+0x58>
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1030 <sysdie@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
imul   -0x18(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11b7 <x_reallocarray+0x97>
mov    -0x10(%rbp),%rsi
imul   -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1030 <sysdie@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<x_reallocarray>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r12
test   %rsi,%rsi
je     1170 <x_reallocarray+0x50>
mov    0x2ea5(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
je     11aa <x_reallocarray+0x8a>
xor    %edx,%edx
div    %rbx
cmp    %rbp,%rax
ja     1170 <x_reallocarray+0x50>
lea    0xe9d(%rip),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <sysdie@plt>
imul   %rbx,%rbp
mov    %r12,%rdi
mov    %rbp,%rsi
call   1040 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    119e <x_reallocarray+0x7e>
lea    0xe92(%rip),%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   1030 <sysdie@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edx,%edx
div    %ebx
cmp    %rbp,%rax
jbe    115c <x_reallocarray+0x3c>
jmp    1170 <x_reallocarray+0x50>

LLVM IR (-O0):
@UINT_MAX = global i64 0
@.str = [32 x i8] c"realloc too large at %s line %d\00"
@.str.1 = [42 x i8] c"failed to realloc %lu bytes at %s line %d\00"
define i8* @x_reallocarray(i8* %0, i64 %1, i64 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%11 = load i64, i64* %7
%12 = icmp ugt i64 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %5
%14 = load i64, i64* @UINT_MAX
%15 = load i64, i64* %7
%16 = udiv i64 %14, %15
%17 = load i64, i64* %8
%18 = icmp ule i64 %16, %17
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = load i8*, i8** %9
%21 = load i32, i32* %10
%22 = call i32 (i8*, ...) @sysdie(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %21)
br label %23
23: ; preds = %19, %13, %5
%24 = load i8*, i8** %6
%25 = load i64, i64* %7
%26 = load i64, i64* %8
%27 = mul i64 %25, %26
%28 = call i8* @realloc(i8* %24, i64 %27)
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %38
31: ; preds = %23
%32 = load i64, i64* %7
%33 = load i64, i64* %8
%34 = mul i64 %32, %33
%35 = load i8*, i8** %9
%36 = load i32, i32* %10
%37 = call i32 (i8*, ...) @sysdie(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i64 %34, i8* %35, i32 %36)
br label %38
38: ; preds = %31, %23
%39 = load i8*, i8** %6
ret i8* %39
}

LLVM IR (-O2):
@UINT_MAX = global i64 0
@.str = [32 x i8] c"realloc too large at %s line %d\00"
@.str.1 = [42 x i8] c"failed to realloc %lu bytes at %s line %d\00"
define noalias i8* @x_reallocarray(i8* nocapture %0, i64 %1, i64 %2, i8* %3, i32 %4) {
%6 = icmp eq i64 %1, 0
br i1 %6, label %13, label %7
7: ; preds = %5
%8 = load i64, i64* @UINT_MAX
%9 = udiv i64 %8, %1
%10 = icmp ugt i64 %9, %2
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i32 (i8*, ...) @sysdie(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %3, i32 %4)
br label %13
13: ; preds = %11, %7, %5
%14 = mul i64 %2, %1
%15 = tail call i8* @realloc(i8* %0, i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 (i8*, ...) @sysdie(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i64 %14, i8* %3, i32 %4)
br label %19
19: ; preds = %17, %13
ret i8* %15
}