#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*/ mutt_error(char *);
int /*<<< orphan*/ mutt_exit(int);
int /*<<< orphan*/ sleep(int);
void *safe_calloc(size_t nmemb, size_t size) {
  void *p;
  if (!nmemb || !size)
    return NULL;
  if (((size_t)-1) / nmemb <= size) {
    mutt_error("Integer overflow -- can't allocate memory!");
    sleep(1);
    mutt_exit(1);
  }
  if (!(p = calloc(nmemb, size))) {
    mutt_error("Out of memory!");
    sleep(1);
    mutt_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    11ef <safe_calloc+0xaf>
mov    $0xffffffffffffffff,%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
cmp    -0x18(%rbp),%rax
ja     11ac <safe_calloc+0x6c>
lea    0xe6d(%rip),%rdi
call   1060 <mutt_error@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1030 <mutt_exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11e7 <safe_calloc+0xa7>
lea    0xe5d(%rip),%rdi
call   1060 <mutt_error@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1030 <mutt_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   %r14
push   %rbx
push   %rax
xor    %eax,%eax
test   %rdi,%rdi
je     11b9 <safe_calloc+0x79>
mov    %rsi,%r14
test   %rsi,%rsi
je     11b9 <safe_calloc+0x79>
mov    %rdi,%rbx
mov    $0xffffffffffffffff,%rax
xor    %edx,%edx
div    %rdi
cmp    %r14,%rax
ja     1187 <safe_calloc+0x47>
lea    0xe92(%rip),%rdi
call   1060 <mutt_error@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1030 <mutt_exit@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <calloc@plt>
test   %rax,%rax
jne    11b9 <safe_calloc+0x79>
lea    0xe8d(%rip),%rdi
call   1060 <mutt_error@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1030 <mutt_exit@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [43 x i8] c"Integer overflow -- can't allocate memory!\00"
@.str.1 = [15 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 %33
13: ; preds = %9
%14 = load i64, i64* %4
%15 = udiv i64 -1, %14
%16 = load i64, i64* %5
%17 = icmp ule i64 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = call i32 @mutt_error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @sleep(i32 1)
%21 = call i32 @mutt_exit(i32 1)
br label %22
22: ; preds = %18, %13
%23 = load i64, i64* %4
%24 = load i64, i64* %5
%25 = call i8* @calloc(i64 %23, i64 %24)
store i8* %25, i8** %6
%26 = icmp ne i8* %25, null
br i1 %26, label %31, label %27
27: ; preds = %22
%28 = call i32 @mutt_error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @sleep(i32 1)
%30 = call i32 @mutt_exit(i32 1)
br label %31
31: ; preds = %27, %22
%32 = load i8*, i8** %6
store i8* %32, i8** %3
br label %33
33: ; preds = %31, %12
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
@.str = [43 x i8] c"Integer overflow -- can't allocate memory!\00"
@.str.1 = [15 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 %20
6: ; preds = %2
%7 = udiv i64 -1, %0
%8 = icmp ugt i64 %7, %1
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = tail call i32 @mutt_error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @sleep(i32 1)
%12 = tail call i32 @mutt_exit(i32 1)
br label %13
13: ; preds = %9, %6
%14 = tail call i8* @calloc(i64 %0, i64 %1)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = tail call i32 @mutt_error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @sleep(i32 1)
%19 = tail call i32 @mutt_exit(i32 1)
br label %20
20: ; preds = %13, %16, %2
%21 = phi i8* [ null, %2 ], [ null, %16 ], [ %14, %13 ]
ret i8* %21
}