#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 INT32_MAX;
void *calloc(unsigned int, size_t);
int _checked_calloc(void **ptr, unsigned int count, size_t size) {
  if (size > INT32_MAX)
    return -1;
  if ((unsigned long long)count * size > INT32_MAX)
    return -1;
  *ptr = calloc(count, size);
  if (*ptr == NULL)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<_checked_calloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2eb2(%rip),%rcx
cmp    (%rcx),%rax
jbe    1143 <_checked_calloc+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <_checked_calloc+0x8e>
mov    -0x14(%rbp),%eax
imul   -0x20(%rbp),%rax
mov    0x2e8e(%rip),%rcx
cmp    (%rcx),%rax
jbe    1167 <_checked_calloc+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <_checked_calloc+0x8e>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1030 <calloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1197 <_checked_calloc+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <_checked_calloc+0x8e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_checked_calloc>:
push   %rbx
mov    0x2ec8(%rip),%rax
mov    (%rax),%rcx
mov    $0xffffffff,%eax
cmp    %rdx,%rcx
jb     1146 <_checked_calloc+0x36>
mov    %rdi,%rbx
mov    %esi,%edi
imul   %rdx,%rdi
cmp    %rcx,%rdi
ja     1146 <_checked_calloc+0x36>
mov    %esi,%edi
mov    %rdx,%rsi
call   1030 <calloc@plt>
mov    %rax,(%rbx)
cmp    $0x1,%rax
sbb    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@INT32_MAX = global i64 0
define i32 @_checked_calloc(i8** %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i64 %2, i64* %7
%8 = load i64, i64* %7
%9 = load i64, i64* @INT32_MAX
%10 = icmp ugt i64 %8, %9
br i1 %10, label %11, label %12
11: ; preds = %3
store i32 -1, i32* %4
br label %30
12: ; preds = %3
%13 = load i32, i32* %6
%14 = zext i32 %13 to i64
%15 = load i64, i64* %7
%16 = mul i64 %14, %15
%17 = load i64, i64* @INT32_MAX
%18 = icmp ugt i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %12
store i32 -1, i32* %4
br label %30
20: ; preds = %12
%21 = load i32, i32* %6
%22 = load i64, i64* %7
%23 = call i8* @calloc(i32 %21, i64 %22)
%24 = load i8**, i8*** %5
store i8* %23, i8** %24
%25 = load i8**, i8*** %5
%26 = load i8*, i8** %25
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %29
28: ; preds = %20
store i32 -1, i32* %4
br label %30
29: ; preds = %20
store i32 0, i32* %4
br label %30
30: ; preds = %29, %28, %19, %11
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@INT32_MAX = global i64 0
define i32 @_checked_calloc(i8** nocapture writeonly %0, i32 %1, i64 %2) {
%4 = load i64, i64* @INT32_MAX
%5 = icmp ult i64 %4, %2
%6 = zext i32 %1 to i64
%7 = mul i64 %6, %2
%8 = icmp ugt i64 %7, %4
%9 = or i1 %5, %8
br i1 %9, label %14, label %10
10: ; preds = %3
%11 = tail call i8* @calloc(i32 %1, i64 %2)
store i8* %11, i8** %0
%12 = icmp eq i8* %11, null
%13 = sext i1 %12 to i32
br label %14
14: ; preds = %10, %3
%15 = phi i32 [ -1, %3 ], [ %13, %10 ]
ret i32 %15
}