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


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

x86 assembly (clang, -O2):
<checked_malloc2>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
test   %rdi,%rdi
je     115b <checked_malloc2+0x3b>
mov    0x2ea2(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %r14,%rcx
shr    $0x20,%rcx
je     116e <checked_malloc2+0x4e>
xor    %edx,%edx
div    %r14
cmp    %rbx,%rax
jae    115b <checked_malloc2+0x3b>
lea    0xeaa(%rip),%rdi
call   1030 <fail@plt>
imul   %r14,%rbx
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <checked_malloc@plt>
xor    %edx,%edx
div    %r14d
cmp    %rbx,%rax
jb     114f <checked_malloc2+0x2f>
jmp    115b <checked_malloc2+0x3b>

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_malloc2(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
store i64 %0, i64* %3
store i64 %1, i64* %4
%5 = load i64, i64* %3
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %2
%8 = load i64, i64* %4
%9 = load i64, i64* @SIZE_MAX
%10 = load i64, i64* %3
%11 = udiv i64 %9, %10
%12 = icmp ugt i64 %8, %11
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = call i32 @fail(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %7, %2
%16 = load i64, i64* %3
%17 = load i64, i64* %4
%18 = mul i64 %16, %17
%19 = call i8* @checked_malloc(i64 %18)
ret i8* %19
}

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_malloc2(i64 %0, i64 %1) {
%3 = icmp eq i64 %0, 0
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = load i64, i64* @SIZE_MAX
%6 = udiv i64 %5, %0
%7 = icmp ult i64 %6, %1
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 @fail(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %4, %2
%11 = mul i64 %1, %0
%12 = tail call i8* @checked_malloc(i64 %11)
ret i8* %12
}