#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 */
void *realloc(void *, int);
scalar_t__ size_multiply_overflow(size_t, size_t);
__attribute__((__alloc_size__(2, 3))) void *reallocarray(void *p, size_t need,
                                                         size_t size) {
  if (size_multiply_overflow(size, need))
    return NULL;
  return realloc(p, size * need ?: 1);
}


x86 assembly (clang, -O0):
<reallocarray>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <size_multiply_overflow@plt>
cmp    $0x0,%rax
je     1158 <reallocarray+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11a5 <reallocarray+0x85>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1184 <reallocarray+0x64>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1192 <reallocarray+0x72>
mov    $0x1,%eax
mov    %rax,-0x38(%rbp)
jmp    1192 <reallocarray+0x72>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reallocarray>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rdx,%rdi
call   1030 <size_multiply_overflow@plt>
test   %rax,%rax
je     1143 <reallocarray+0x23>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
imul   %r15,%rbx
test   %rbx,%rbx
mov    $0x1,%esi
cmovne %ebx,%esi
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <realloc@plt>

LLVM IR (-O0):
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* %7
%9 = load i64, i64* %6
%10 = call i64 @size_multiply_overflow(i64 %8, i64 %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %3
store i8* null, i8** %4
br label %25
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = load i64, i64* %7
%16 = load i64, i64* %6
%17 = mul i64 %15, %16
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %13
br label %21
20: ; preds = %13
br label %21
21: ; preds = %20, %19
%22 = phi i64 [ %17, %19 ], [ 1, %20 ]
%23 = trunc i64 %22 to i32
%24 = call i8* @realloc(i8* %14, i32 %23)
store i8* %24, i8** %4
br label %25
25: ; preds = %21, %12
%26 = load i8*, i8** %4
ret i8* %26
}

LLVM IR (-O2):
define i8* @reallocarray(i8* %0, i64 %1, i64 %2) {
%4 = tail call i64 @size_multiply_overflow(i64 %2, i64 %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %3
%7 = mul i64 %2, %1
%8 = icmp eq i64 %7, 0
%9 = trunc i64 %7 to i32
%10 = select i1 %8, i32 1, i32 %9
%11 = tail call i8* @realloc(i8* %0, i32 %10)
br label %12
12: ; preds = %3, %6
%13 = phi i8* [ %11, %6 ], [ null, %3 ]
ret i8* %13
}