#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*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
void *safe_calloc(size_t nmemb, size_t size) {
  void *p;
  if (!nmemb || !size)
    return NULL;
  if (!(p = calloc(nmemb, size))) {
    perror("Out of memory");
    sleep(1);
    exit(1);
  }
  return p;
}


x86 assembly (clang, -O0):
<safe_calloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1166 <safe_calloc+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1173 <safe_calloc+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11b6 <safe_calloc+0x76>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ae <safe_calloc+0x6e>
lea    0xe6b(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_calloc>:
push   %rax
xor    %eax,%eax
test   %rdi,%rdi
je     1157 <safe_calloc+0x17>
test   %rsi,%rsi
je     1157 <safe_calloc+0x17>
call   1030 <calloc@plt>
test   %rax,%rax
je     1159 <safe_calloc+0x19>
pop    %rcx
ret
lea    0xea0(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"Out of memory\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
%7 = load i64, i64* %4
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i64, i64* %5
%11 = icmp ne i64 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %9, %2
store i8* null, i8** %3
br label %24
13: ; preds = %9
%14 = load i64, i64* %4
%15 = load i64, i64* %5
%16 = call i8* @calloc(i64 %14, i64 %15)
store i8* %16, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @sleep(i32 1)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = load i8*, i8** %6
store i8* %23, i8** %3
br label %24
24: ; preds = %22, %12
%25 = load i8*, i8** %3
ret i8* %25
}

LLVM IR (-O2):
@.str = [14 x i8] c"Out of memory\00"
define noalias i8* @safe_calloc(i64 %0, i64 %1) {
%3 = icmp ne i64 %0, 0
%4 = icmp ne i64 %1, 0
%5 = and i1 %3, %4
br i1 %5, label %6, label %13
6: ; preds = %2
%7 = tail call i8* @calloc(i64 %0, i64 %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @sleep(i32 1)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %6, %2
%14 = phi i8* [ null, %2 ], [ %7, %6 ]
ret i8* %14
}