#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 *blt_reallocarray(void *p, size_t n, size_t m) {
  if (m && n > SIZE_MAX / m) {
    errno = ENOMEM;
    return NULL;
  }
  return realloc(p, n * m);
}


x86 assembly (clang, -O0):
<blt_reallocarray>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1178 <blt_reallocarray+0x68>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e8a(%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    1178 <blt_reallocarray+0x68>
mov    0x2e80(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6f(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    118e <blt_reallocarray+0x7e>
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):
<blt_reallocarray>:
mov    %rdx,%rcx
test   %rdx,%rdx
je     1156 <blt_reallocarray+0x46>
mov    0x2ea9(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     114d <blt_reallocarray+0x3d>
xor    %edx,%edx
div    %rcx
cmp    %rsi,%rax
jae    1156 <blt_reallocarray+0x46>
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
mov    0x2e90(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
ret
xor    %edx,%edx
div    %ecx
cmp    %rsi,%rax
jb     1138 <blt_reallocarray+0x28>
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* @blt_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* %7
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %3
%11 = load i64, i64* %6
%12 = load i64, i64* @SIZE_MAX
%13 = load i64, i64* %7
%14 = udiv i64 %12, %13
%15 = icmp ugt i64 %11, %14
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = load i32, i32* @ENOMEM
store i32 %17, i32* @errno
store i8* null, i8** %4
br label %24
18: ; preds = %10, %3
%19 = load i8*, i8** %5
%20 = load i64, i64* %6
%21 = load i64, i64* %7
%22 = mul i64 %20, %21
%23 = call i8* @realloc(i8* %19, i64 %22)
store i8* %23, i8** %4
br label %24
24: ; preds = %18, %16
%25 = load i8*, i8** %4
ret i8* %25
}

LLVM IR (-O2):
@SIZE_MAX = global i64 0
@ENOMEM = global i32 0
@errno = global i32 0
define noalias i8* @blt_reallocarray(i8* nocapture %0, i64 %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %11, label %5
5: ; preds = %3
%6 = load i64, i64* @SIZE_MAX
%7 = udiv i64 %6, %2
%8 = icmp ult i64 %7, %1
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = load i32, i32* @ENOMEM
store i32 %10, i32* @errno
br label %14
11: ; preds = %5, %3
%12 = mul i64 %2, %1
%13 = tail call i8* @realloc(i8* %0, i64 %12)
br label %14
14: ; preds = %11, %9
%15 = phi i8* [ null, %9 ], [ %13, %11 ]
ret i8* %15
}