#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 *checked_malloc2(size_t, size_t);
int /*<<< orphan*/ fail(char *);
void *checked_malloc3(size_t a, size_t b, size_t c) {
  if (a != 0 && b > SIZE_MAX / a) {
    fail("Could not allocate memory: size does not fit in size_t.");
  }
  return checked_malloc2(a * b, c);
}


x86 assembly (clang, -O0):
<checked_malloc3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1175 <checked_malloc3+0x55>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x8(%rbp)
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jbe    1175 <checked_malloc3+0x55>
lea    0xe90(%rip),%rdi
call   1030 <fail@plt>
mov    -0x8(%rbp),%rdi
imul   -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <checked_malloc2@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checked_malloc3>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
test   %rdi,%rdi
je     115f <checked_malloc3+0x3f>
mov    0x2e9e(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %r15,%rcx
shr    $0x20,%rcx
je     1173 <checked_malloc3+0x53>
xor    %edx,%edx
div    %r15
cmp    %rbx,%rax
jae    115f <checked_malloc3+0x3f>
lea    0xea6(%rip),%rdi
call   1030 <fail@plt>
imul   %r15,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <checked_malloc2@plt>
xor    %edx,%edx
div    %r15d
cmp    %rbx,%rax
jb     1153 <checked_malloc3+0x33>
jmp    115f <checked_malloc3+0x3f>

LLVM IR (-O0):
@SIZE_MAX = global i64 0
@.str = [56 x i8] c"Could not allocate memory: size does not fit in size_t.\00"
define i8* @checked_malloc3(i64 %0, i64 %1, i64 %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i64 %0, i64* %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%7 = load i64, i64* %4
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %3
%10 = load i64, i64* %5
%11 = load i64, i64* @SIZE_MAX
%12 = load i64, i64* %4
%13 = udiv i64 %11, %12
%14 = icmp ugt i64 %10, %13
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 @fail(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %9, %3
%18 = load i64, i64* %4
%19 = load i64, i64* %5
%20 = mul i64 %18, %19
%21 = load i64, i64* %6
%22 = call i8* @checked_malloc2(i64 %20, i64 %21)
ret i8* %22
}

LLVM IR (-O2):
@SIZE_MAX = global i64 0
@.str = [56 x i8] c"Could not allocate memory: size does not fit in size_t.\00"
define i8* @checked_malloc3(i64 %0, i64 %1, i64 %2) {
%4 = icmp eq i64 %0, 0
br i1 %4, label %11, label %5
5: ; preds = %3
%6 = load i64, i64* @SIZE_MAX
%7 = udiv i64 %6, %0
%8 = icmp ult i64 %7, %1
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @fail(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %5, %3
%12 = mul i64 %1, %0
%13 = tail call i8* @checked_malloc2(i64 %12, i64 %2)
ret i8* %13
}