#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 *malloc(size_t);
void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3) {
  if (!size1 || !size2 || !size3)
    return malloc(1);
  if (size1 > SIZE_MAX / size2)
    return 0;
  size1 *= size2;
  if (size1 > SIZE_MAX / size3)
    return 0;
  return malloc(size1 * size3);
}


x86 assembly (clang, -O0):
<safe_malloc_mul_3op_>:
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     1145 <safe_malloc_mul_3op_+0x35>
cmpq   $0x0,-0x18(%rbp)
je     1145 <safe_malloc_mul_3op_+0x35>
cmpq   $0x0,-0x20(%rbp)
jne    1158 <safe_malloc_mul_3op_+0x48>
mov    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11e5 <safe_malloc_mul_3op_+0xd5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e71(%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    118f <safe_malloc_mul_3op_+0x7f>
movq   $0x0,-0x8(%rbp)
jmp    11e5 <safe_malloc_mul_3op_+0xd5>
mov    -0x18(%rbp),%rax
imul   -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jbe    11d3 <safe_malloc_mul_3op_+0xc3>
movq   $0x0,-0x8(%rbp)
jmp    11e5 <safe_malloc_mul_3op_+0xd5>
mov    -0x10(%rbp),%rdi
imul   -0x20(%rbp),%rdi
call   1030 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_malloc_mul_3op_>:
test   %rdi,%rdi
je     1147 <safe_malloc_mul_3op_+0x37>
test   %rsi,%rsi
je     1147 <safe_malloc_mul_3op_+0x37>
mov    %rdx,%r8
test   %rdx,%rdx
je     1147 <safe_malloc_mul_3op_+0x37>
mov    0x2eaf(%rip),%rax
mov    (%rax),%rcx
mov    %rcx,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     1151 <safe_malloc_mul_3op_+0x41>
mov    %rcx,%rax
xor    %edx,%edx
div    %rsi
cmp    %rdi,%rax
jae    115c <safe_malloc_mul_3op_+0x4c>
jmp    1179 <safe_malloc_mul_3op_+0x69>
mov    $0x1,%edi
jmp    1030 <malloc@plt>
mov    %ecx,%eax
xor    %edx,%edx
div    %esi
cmp    %rdi,%rax
jb     1179 <safe_malloc_mul_3op_+0x69>
imul   %rdi,%rsi
mov    %rcx,%rax
or     %r8,%rax
shr    $0x20,%rax
je     117c <safe_malloc_mul_3op_+0x6c>
mov    %rcx,%rax
xor    %edx,%edx
div    %r8
cmp    %rax,%rsi
jbe    1188 <safe_malloc_mul_3op_+0x78>
xor    %eax,%eax
ret
mov    %ecx,%eax
xor    %edx,%edx
div    %r8d
cmp    %rax,%rsi
ja     1179 <safe_malloc_mul_3op_+0x69>
imul   %r8,%rsi
mov    %rsi,%rdi
jmp    1030 <malloc@plt>

LLVM IR (-O0):
@SIZE_MAX = global i64 0
define i8* @safe_malloc_mul_3op_(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 %16
10: ; preds = %3
%11 = load i64, i64* %6
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i64, i64* %7
%15 = icmp ne i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13, %10, %3
%17 = call i8* @malloc(i64 1)
store i8* %17, i8** %4
br label %40
18: ; preds = %13
%19 = load i64, i64* %5
%20 = load i64, i64* @SIZE_MAX
%21 = load i64, i64* %6
%22 = udiv i64 %20, %21
%23 = icmp ugt i64 %19, %22
br i1 %23, label %24, label %25
24: ; preds = %18
store i8* null, i8** %4
br label %40
25: ; preds = %18
%26 = load i64, i64* %6
%27 = load i64, i64* %5
%28 = mul i64 %27, %26
store i64 %28, i64* %5
%29 = load i64, i64* %5
%30 = load i64, i64* @SIZE_MAX
%31 = load i64, i64* %7
%32 = udiv i64 %30, %31
%33 = icmp ugt i64 %29, %32
br i1 %33, label %34, label %35
34: ; preds = %25
store i8* null, i8** %4
br label %40
35: ; preds = %25
%36 = load i64, i64* %5
%37 = load i64, i64* %7
%38 = mul i64 %36, %37
%39 = call i8* @malloc(i64 %38)
store i8* %39, i8** %4
br label %40
40: ; preds = %35, %34, %24, %16
%41 = load i8*, i8** %4
ret i8* %41
}

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