#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 */
void *calloc(size_t, size_t);
int /*<<< orphan*/ printf(char *);
void *safe_calloc(size_t nmemb, size_t size) {
  void *mem = NULL;
  if (size == 0 || nmemb == 0)
    printf("Allocating 0 bytes!");
  if ((mem = calloc(nmemb, size)) != NULL)
    return mem;
  else {
    printf("Memory allocation error");
    return NULL;
  }
}


x86 assembly (clang, -O0):
<safe_calloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     114e <safe_calloc+0x2e>
cmpq   $0x0,-0x10(%rbp)
jne    115a <safe_calloc+0x3a>
lea    0xeab(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1182 <safe_calloc+0x62>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1196 <safe_calloc+0x76>
lea    0xe8b(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-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
test   %rdi,%rdi
je     1134 <safe_calloc+0x14>
test   %r14,%r14
jne    1140 <safe_calloc+0x20>
lea    0xec5(%rip),%rdi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    115f <safe_calloc+0x3f>
lea    0xeba(%rip),%rdi
call   1030 <printf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Allocating 0 bytes!\00"
@.str.1 = [24 x i8] c"Memory allocation error\00"
define i8* @safe_calloc(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
store i64 %0, i64* %4
store i64 %1, i64* %5
store i8* null, i8** %6
%7 = load i64, i64* %5
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i64, i64* %4
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9, %2
%13 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = load i64, i64* %4
%16 = load i64, i64* %5
%17 = call i8* @calloc(i64 %15, i64 %16)
store i8* %17, i8** %6
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = load i8*, i8** %6
store i8* %20, i8** %3
br label %23
21: ; preds = %14
%22 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %23
23: ; preds = %21, %19
%24 = load i8*, i8** %3
ret i8* %24
}

LLVM IR (-O2):
@.str = [20 x i8] c"Allocating 0 bytes!\00"
@.str.1 = [24 x i8] c"Memory allocation error\00"
define noalias i8* @safe_calloc(i64 %0, i64 %1) {
%3 = icmp eq i64 %1, 0
%4 = icmp eq i64 %0, 0
%5 = or i1 %4, %3
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %2, %6
%9 = tail call i8* @calloc(i64 %0, i64 %1)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %8, %11
ret i8* %9
}