#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 *safe_malloc_(size_t);
void *safe_malloc_add_2op_(size_t, size_t);
void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3) {
  if (!size1 || !size2)
    return safe_malloc_(size3);
  if (size1 > SIZE_MAX / size2)
    return 0;
  return safe_malloc_add_2op_(size1 * size2, size3);
}


x86 assembly (clang, -O0):
<safe_malloc_mul2add_>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     114a <safe_malloc_mul2add_+0x2a>
cmpq   $0x0,-0x18(%rbp)
jne    115c <safe_malloc_mul2add_+0x3c>
mov    -0x20(%rbp),%rdi
call   1040 <safe_malloc_@plt>
mov    %rax,-0x8(%rbp)
jmp    11a9 <safe_malloc_mul2add_+0x89>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jbe    1193 <safe_malloc_mul2add_+0x73>
movq   $0x0,-0x8(%rbp)
jmp    11a9 <safe_malloc_mul2add_+0x89>
mov    -0x10(%rbp),%rdi
imul   -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <safe_malloc_add_2op_@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_malloc_mul2add_>:
mov    %rdx,%rcx
test   %rdi,%rdi
je     1150 <safe_malloc_mul2add_+0x30>
test   %rsi,%rsi
je     1150 <safe_malloc_mul2add_+0x30>
mov    0x2ea4(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rdx
or     %rsi,%rdx
shr    $0x20,%rdx
je     1158 <safe_malloc_mul2add_+0x38>
xor    %edx,%edx
div    %rsi
cmp    %rdi,%rax
jae    1161 <safe_malloc_mul2add_+0x41>
xor    %eax,%eax
ret
mov    %rcx,%rdi
jmp    1040 <safe_malloc_@plt>
xor    %edx,%edx
div    %esi
cmp    %rdi,%rax
jb     114d <safe_malloc_mul2add_+0x2d>
imul   %rdi,%rsi
mov    %rsi,%rdi
mov    %rcx,%rsi
jmp    1030 <safe_malloc_add_2op_@plt>

LLVM IR (-O0):
@SIZE_MAX = global i64 0
define i8* @safe_malloc_mul2add_(i64 %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i64 %0, i64* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%8 = load i64, i64* %5
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i64, i64* %6
%12 = icmp ne i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10, %3
%14 = load i64, i64* %7
%15 = call i8* @safe_malloc_(i64 %14)
store i8* %15, i8** %4
br label %29
16: ; preds = %10
%17 = load i64, i64* %5
%18 = load i64, i64* @SIZE_MAX
%19 = load i64, i64* %6
%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 i64, i64* %5
%25 = load i64, i64* %6
%26 = mul i64 %24, %25
%27 = load i64, i64* %7
%28 = call i8* @safe_malloc_add_2op_(i64 %26, 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 i8* @safe_malloc_mul2add_(i64 %0, i64 %1, i64 %2) {
%4 = icmp ne i64 %0, 0
%5 = icmp ne i64 %1, 0
%6 = and i1 %4, %5
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = tail call i8* @safe_malloc_(i64 %2)
br label %16
9: ; preds = %3
%10 = load i64, i64* @SIZE_MAX
%11 = udiv i64 %10, %1
%12 = icmp ult i64 %11, %0
br i1 %12, label %16, label %13
13: ; preds = %9
%14 = mul i64 %1, %0
%15 = tail call i8* @safe_malloc_add_2op_(i64 %14, i64 %2)
br label %16
16: ; preds = %9, %13, %7
%17 = phi i8* [ %15, %13 ], [ %8, %7 ], [ null, %9 ]
ret i8* %17
}