#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 */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ _safe_calloc_check_mul(size_t, size_t, int /*<<< orphan*/ *);
void *calloc(size_t, size_t);
int /*<<< orphan*/ errno;
void *safe_calloc(size_t num, size_t sz) {
  int have_safe_calloc;
  have_safe_calloc = 0;
  if (!have_safe_calloc && !_safe_calloc_check_mul(num, sz, NULL)) {
    errno = ENOMEM;
    return NULL;
  }
  return calloc(num, sz);
}


x86 assembly (clang, -O0):
<safe_calloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    117a <safe_calloc+0x5a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <_safe_calloc_check_mul@plt>
cmp    $0x0,%eax
jne    117a <safe_calloc+0x5a>
mov    0x2e7e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6d(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    118b <safe_calloc+0x6b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <calloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_calloc>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edx,%edx
call   1030 <_safe_calloc_check_mul@plt>
test   %eax,%eax
je     1147 <safe_calloc+0x27>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <calloc@plt>
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    0x2e81(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @safe_calloc(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i64 %0, i64* %4
store i64 %1, i64* %5
store i32 0, i32* %6
%7 = load i32, i32* %6
%8 = icmp ne i32 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %2
%10 = load i64, i64* %4
%11 = load i64, i64* %5
%12 = call i32 @_safe_calloc_check_mul(i64 %10, i64 %11, i32* null)
%13 = icmp ne i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %9
%15 = load i32, i32* @ENOMEM
store i32 %15, i32* @errno
store i8* null, i8** %3
br label %20
16: ; preds = %9, %2
%17 = load i64, i64* %4
%18 = load i64, i64* %5
%19 = call i8* @calloc(i64 %17, i64 %18)
store i8* %19, i8** %3
br label %20
20: ; preds = %16, %14
%21 = load i8*, i8** %3
ret i8* %21
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
define noalias i8* @safe_calloc(i64 %0, i64 %1) {
%3 = tail call i32 @_safe_calloc_check_mul(i64 %0, i64 %1, i32* null)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = load i32, i32* @ENOMEM
store i32 %6, i32* @errno
br label %9
7: ; preds = %2
%8 = tail call i8* @calloc(i64 %0, i64 %1)
br label %9
9: ; preds = %7, %5
%10 = phi i8* [ %8, %7 ], [ null, %5 ]
ret i8* %10
}