#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 *Rmalloc(size_t, char const *);
size_t SIZE_MAX;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ stderr;
void *Rcalloc(size_t nelem, size_t size, const char *file) {
  void *ret;
  if (size && nelem > SIZE_MAX / size) {
    fprintf(stderr,
            "<MALLOC_DEBUG>\t"
            "WARNING: calloc() overflow! Returning NULL (in %s)\n",
            file);
    return NULL;
  }
  size *= nelem;
  if (size == 0) {
    fprintf(stderr,
            "<MALLOC_DEBUG>\t"
            "WARNING: calloc() demands 0 Bytes (in %s)\n",
            file);
  }
  ret = Rmalloc(size, file);
  if (ret) {
    memset(ret, 0, size);
    return ret;
  } else {
    fprintf(stderr,
            "<MALLOC_DEBUG>\t"
            "WARNING: Out of memory! Returning NULL (in %s)\n",
            file);
    return NULL;
  }
}


x86 assembly (clang, -O0):
<Rcalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119f <Rcalloc+0x6f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jbe    119f <Rcalloc+0x6f>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe73(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1229 <Rcalloc+0xf9>
mov    -0x10(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d0 <Rcalloc+0xa0>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe78(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <Rmalloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1208 <Rcalloc+0xd8>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1229 <Rcalloc+0xf9>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe61(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Rcalloc>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
test   %rsi,%rsi
je     117c <Rcalloc+0x4c>
mov    0x2e89(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %r15,%rcx
shr    $0x20,%rcx
je     1172 <Rcalloc+0x42>
xor    %edx,%edx
div    %r15
cmp    %rdi,%rax
jae    117c <Rcalloc+0x4c>
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
jmp    11cf <Rcalloc+0x9f>
xor    %edx,%edx
div    %r15d
cmp    %rdi,%rax
jb     1160 <Rcalloc+0x30>
imul   %rdi,%r15
test   %r15,%r15
jne    119d <Rcalloc+0x6d>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xeae(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <Rmalloc@plt>
test   %rax,%rax
je     11bf <Rcalloc+0x8f>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1040 <memset@plt>
jmp    11d9 <Rcalloc+0xa9>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xeae(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SIZE_MAX = global i64 0
@stderr = global i32 0
@.str = [67 x i8] c"<MALLOC_DEBUG>\09WARNING: calloc() overflow! Returning NULL (in %s)\0A\00"
@.str.1 = [58 x i8] c"<MALLOC_DEBUG>\09WARNING: calloc() demands 0 Bytes (in %s)\0A\00"
@.str.2 = [63 x i8] c"<MALLOC_DEBUG>\09WARNING: Out of memory! Returning NULL (in %s)\0A\00"
define i8* @Rcalloc(i64 %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
store i64 %0, i64* %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%9 = load i64, i64* %6
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %3
%12 = load i64, i64* %5
%13 = load i64, i64* @SIZE_MAX
%14 = load i64, i64* %6
%15 = udiv i64 %13, %14
%16 = icmp ugt i64 %12, %15
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %7
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0), i8* %19)
store i8* null, i8** %4
br label %46
21: ; preds = %11, %3
%22 = load i64, i64* %5
%23 = load i64, i64* %6
%24 = mul i64 %23, %22
store i64 %24, i64* %6
%25 = load i64, i64* %6
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %7
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %27, %21
%32 = load i64, i64* %6
%33 = load i8*, i8** %7
%34 = call i8* @Rmalloc(i64 %32, i8* %33)
store i8* %34, i8** %8
%35 = load i8*, i8** %8
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %42
37: ; preds = %31
%38 = load i8*, i8** %8
%39 = load i64, i64* %6
%40 = call i32 @memset(i8* %38, i32 0, i64 %39)
%41 = load i8*, i8** %8
store i8* %41, i8** %4
br label %46
42: ; preds = %31
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %7
%45 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0), i8* %44)
store i8* null, i8** %4
br label %46
46: ; preds = %42, %37, %17
%47 = load i8*, i8** %4
ret i8* %47
}

LLVM IR (-O2):
@SIZE_MAX = global i64 0
@stderr = global i32 0
@.str = [67 x i8] c"<MALLOC_DEBUG>\09WARNING: calloc() overflow! Returning NULL (in %s)\0A\00"
@.str.1 = [58 x i8] c"<MALLOC_DEBUG>\09WARNING: calloc() demands 0 Bytes (in %s)\0A\00"
@.str.2 = [63 x i8] c"<MALLOC_DEBUG>\09WARNING: Out of memory! Returning NULL (in %s)\0A\00"
define i8* @Rcalloc(i64 %0, i64 %1, i8* %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %12, label %5
5: ; preds = %3
%6 = load i64, i64* @SIZE_MAX
%7 = udiv i64 %6, %1
%8 = icmp ult i64 %7, %0
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %26
12: ; preds = %5, %3
%13 = mul i64 %1, %0
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %18
18: ; preds = %15, %12
%19 = tail call i8* @Rmalloc(i64 %13, i8* %2)
%20 = icmp eq i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 @memset(i8* %19, i32 0, i64 %13)
br label %26
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %26
26: ; preds = %23, %21, %9
%27 = phi i8* [ null, %9 ], [ %19, %21 ], [ null, %23 ]
ret i8* %27
}