#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 */
size_t SIZE_MAX;
void *malloc(size_t);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
void *calloc(size_t nmemb, size_t size) {
  void *ptr;
  int nb;
  nb = sizeof(size_t) * 4;
  if (size >= SIZE_MAX >> nb || nmemb >= SIZE_MAX >> nb) {
    return NULL;
  }
  size *= nmemb;
  ptr = malloc(size);
  if (ptr)
    memset(ptr, 0, size);
  return ptr;
}


x86 assembly (clang, -O0):
<calloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x20,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e96(%rip),%rcx
mov    (%rcx),%rdx
mov    -0x24(%rbp),%ecx
shr    %cl,%rdx
mov    %rdx,%rcx
cmp    %rcx,%rax
jae    1177 <calloc+0x57>
mov    -0x10(%rbp),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%rdx
mov    -0x24(%rbp),%ecx
shr    %cl,%rdx
mov    %rdx,%rcx
cmp    %rcx,%rax
jb     1184 <calloc+0x64>
movq   $0x0,-0x8(%rbp)
jmp    11c0 <calloc+0xa0>
mov    -0x10(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b8 <calloc+0x98>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calloc>:
push   %r14
push   %rbx
push   %rax
mov    0x2ead(%rip),%rax
mov    (%rax),%rax
shr    $0x20,%rax
xor    %r14d,%r14d
cmp    %rsi,%rax
jbe    1168 <calloc+0x48>
cmp    %rdi,%rax
jbe    1168 <calloc+0x48>
mov    %rsi,%rbx
imul   %rdi,%rbx
mov    %rbx,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     1165 <calloc+0x45>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1030 <memset@plt>
jmp    1168 <calloc+0x48>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@SIZE_MAX = global i64 0
define i8* @calloc(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i64 %0, i64* %4
store i64 %1, i64* %5
store i32 32, i32* %7
%8 = load i64, i64* %5
%9 = load i64, i64* @SIZE_MAX
%10 = load i32, i32* %7
%11 = zext i32 %10 to i64
%12 = lshr i64 %9, %11
%13 = icmp uge i64 %8, %12
br i1 %13, label %21, label %14
14: ; preds = %2
%15 = load i64, i64* %4
%16 = load i64, i64* @SIZE_MAX
%17 = load i32, i32* %7
%18 = zext i32 %17 to i64
%19 = lshr i64 %16, %18
%20 = icmp uge i64 %15, %19
br i1 %20, label %21, label %22
21: ; preds = %14, %2
store i8* null, i8** %3
br label %36
22: ; preds = %14
%23 = load i64, i64* %4
%24 = load i64, i64* %5
%25 = mul i64 %24, %23
store i64 %25, i64* %5
%26 = load i64, i64* %5
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %34
30: ; preds = %22
%31 = load i8*, i8** %6
%32 = load i64, i64* %5
%33 = call i32 @memset(i8* %31, i32 0, i64 %32)
br label %34
34: ; preds = %30, %22
%35 = load i8*, i8** %6
store i8* %35, i8** %3
br label %36
36: ; preds = %34, %21
%37 = load i8*, i8** %3
ret i8* %37
}

LLVM IR (-O2):
@SIZE_MAX = global i64 0
define i8* @calloc(i64 %0, i64 %1) {
%3 = load i64, i64* @SIZE_MAX
%4 = lshr i64 %3, 32
%5 = icmp ugt i64 %4, %1
%6 = icmp ugt i64 %4, %0
%7 = and i1 %5, %6
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = mul i64 %1, %0
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @memset(i8* %10, i32 0, i64 %9)
br label %14
14: ; preds = %8, %12, %2
%15 = phi i8* [ null, %2 ], [ %10, %12 ], [ null, %8 ]
ret i8* %15
}