#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 */
int /*<<< orphan*/ ENOMEM;
size_t SIZE_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ lock();
void *malloc_internal(size_t);
int /*<<< orphan*/ unlock();
void *calloc(size_t a, size_t b) {
  if (b > 0 && a > SIZE_MAX / b) {
    errno = ENOMEM;
    return NULL;
  }
  lock();
  void *result = malloc_internal(a * b);
  unlock();
  return result;
}


x86 assembly (clang, -O0):
<calloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    1194 <calloc+0x64>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jbe    1194 <calloc+0x64>
mov    0x2e64(%rip),%rax
mov    (%rax),%ecx
mov    0x2e53(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11bc <calloc+0x8c>
mov    $0x0,%al
call   1040 <lock@plt>
mov    -0x10(%rbp),%rdi
imul   -0x18(%rbp),%rdi
call   1050 <malloc_internal@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <unlock@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    %rsi,%rbx
mov    %rdi,%r14
test   %rsi,%rsi
je     117e <calloc+0x4e>
mov    0x2e82(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
je     1175 <calloc+0x45>
xor    %edx,%edx
div    %rbx
cmp    %r14,%rax
jae    117e <calloc+0x4e>
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    0x2e69(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
jmp    119e <calloc+0x6e>
xor    %edx,%edx
div    %ebx
cmp    %r14,%rax
jb     115f <calloc+0x2f>
xor    %eax,%eax
call   1040 <lock@plt>
imul   %r14,%rbx
mov    %rbx,%rdi
call   1050 <malloc_internal@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <unlock@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@SIZE_MAX = global i64 0
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @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* %5
%8 = icmp ugt i64 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %2
%10 = load i64, i64* %4
%11 = load i64, i64* @SIZE_MAX
%12 = load i64, i64* %5
%13 = udiv i64 %11, %12
%14 = icmp ugt i64 %10, %13
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = load i32, i32* @ENOMEM
store i32 %16, i32* @errno
store i8* null, i8** %3
br label %25
17: ; preds = %9, %2
%18 = call i32 (...) @lock()
%19 = load i64, i64* %4
%20 = load i64, i64* %5
%21 = mul i64 %19, %20
%22 = call i8* @malloc_internal(i64 %21)
store i8* %22, i8** %6
%23 = call i32 (...) @unlock()
%24 = load i8*, i8** %6
store i8* %24, i8** %3
br label %25
25: ; preds = %17, %15
%26 = load i8*, i8** %3
ret i8* %26
}

LLVM IR (-O2):
@SIZE_MAX = global i64 0
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @calloc(i64 %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = load i64, i64* @SIZE_MAX
%6 = udiv i64 %5, %1
%7 = icmp ult i64 %6, %0
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = load i32, i32* @ENOMEM
store i32 %9, i32* @errno
br label %15
10: ; preds = %4, %2
%11 = tail call i32 (...) @lock()
%12 = mul i64 %1, %0
%13 = tail call i8* @malloc_internal(i64 %12)
%14 = tail call i32 (...) @unlock()
br label %15
15: ; preds = %10, %8
%16 = phi i8* [ null, %8 ], [ %13, %10 ]
ret i8* %16
}