#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 SIZE_MAX;
void *realloc(void *, size_t);
void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2) {
  if (!size1 || !size2)
    return realloc(ptr, 0);
  if (size1 > SIZE_MAX / size2)
    return 0;
  return realloc(ptr, size1 * size2);
}


x86 assembly (clang, -O0):
<safe_realloc_mul_2op_>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     113a <safe_realloc_mul_2op_+0x2a>
cmpq   $0x0,-0x20(%rbp)
jne    1150 <safe_realloc_mul_2op_+0x40>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <realloc@plt>
mov    %rax,-0x8(%rbp)
jmp    119d <safe_realloc_mul_2op_+0x8d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e79(%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    1187 <safe_realloc_mul_2op_+0x77>
movq   $0x0,-0x8(%rbp)
jmp    119d <safe_realloc_mul_2op_+0x8d>
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):
<safe_realloc_mul_2op_>:
xor    %eax,%eax
test   %rsi,%rsi
je     1152 <safe_realloc_mul_2op_+0x42>
mov    %rdx,%rcx
test   %rdx,%rdx
je     1152 <safe_realloc_mul_2op_+0x42>
mov    0x2eb2(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     1142 <safe_realloc_mul_2op_+0x32>
xor    %edx,%edx
div    %rcx
cmp    %rsi,%rax
jae    114b <safe_realloc_mul_2op_+0x3b>
xor    %eax,%eax
ret
xor    %edx,%edx
div    %ecx
cmp    %rsi,%rax
jb     113f <safe_realloc_mul_2op_+0x2f>
imul   %rsi,%rcx
mov    %rcx,%rax
mov    %rax,%rsi
jmp    1030 <realloc@plt>

LLVM IR (-O0):
@SIZE_MAX = global i64 0
define i8* @safe_realloc_mul_2op_(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 = icmp ne i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i64, i64* %7
%12 = icmp ne i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10, %3
%14 = load i8*, i8** %5
%15 = call i8* @realloc(i8* %14, i64 0)
store i8* %15, i8** %4
br label %29
16: ; preds = %10
%17 = load i64, i64* %6
%18 = load i64, i64* @SIZE_MAX
%19 = load i64, i64* %7
%20 = udiv i64 %18, %19
%21 = icmp ugt i64 %17, %20
br i1 %21, label %22, label %23
22: ; preds = %16
store i8* null, i8** %4
br label %29
23: ; preds = %16
%24 = load i8*, i8** %5
%25 = load i64, i64* %6
%26 = load i64, i64* %7
%27 = mul i64 %25, %26
%28 = call i8* @realloc(i8* %24, i64 %27)
store i8* %28, i8** %4
br label %29
29: ; preds = %23, %22, %13
%30 = load i8*, i8** %4
ret i8* %30
}

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