#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 *ar_malloc(size_t);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
void *ar_calloc(size_t count, size_t size) {
  void *ptr = NULL;
  if (size <= SIZE_MAX / count)
    ptr = ar_malloc(count * size);
  if (ptr)
    memset(ptr, 0, count * size);
  return ptr;
}


x86 assembly (clang, -O0):
<ar_calloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x8(%rbp)
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
ja     1174 <ar_calloc+0x54>
mov    -0x8(%rbp),%rdi
imul   -0x10(%rbp),%rdi
call   1040 <ar_malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1193 <ar_calloc+0x73>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
imul   -0x10(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ar_calloc>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2eaa(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %rdi,%rcx
shr    $0x20,%rcx
je     1149 <ar_calloc+0x29>
xor    %edx,%edx
div    %rdi
cmp    %r14,%rax
jae    1152 <ar_calloc+0x32>
jmp    1175 <ar_calloc+0x55>
xor    %edx,%edx
div    %edi
cmp    %r14,%rax
jb     1175 <ar_calloc+0x55>
imul   %rdi,%r14
mov    %r14,%rdi
call   1040 <ar_malloc@plt>
test   %rax,%rax
je     1175 <ar_calloc+0x55>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
jmp    1177 <ar_calloc+0x57>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@SIZE_MAX = global i64 0
define i8* @ar_calloc(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
store i64 %0, i64* %3
store i64 %1, i64* %4
store i8* null, i8** %5
%6 = load i64, i64* %4
%7 = load i64, i64* @SIZE_MAX
%8 = load i64, i64* %3
%9 = udiv i64 %7, %8
%10 = icmp ule i64 %6, %9
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i64, i64* %3
%13 = load i64, i64* %4
%14 = mul i64 %12, %13
%15 = call i8* @ar_malloc(i64 %14)
store i8* %15, i8** %5
br label %16
16: ; preds = %11, %2
%17 = load i8*, i8** %5
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = load i8*, i8** %5
%21 = load i64, i64* %3
%22 = load i64, i64* %4
%23 = mul i64 %21, %22
%24 = call i32 @memset(i8* %20, i32 0, i64 %23)
br label %25
25: ; preds = %19, %16
%26 = load i8*, i8** %5
ret i8* %26
}

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