#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 *brp_malloc_internal(void *, int, int /*<<< orphan*/);
int /*<<< orphan*/ brp_zero_mem(void *, int);
int /*<<< orphan*/ lock();
int /*<<< orphan*/ unlock();
void *brp_calloc_internal(void *storage, int elem_numbers, int elem_size,
                          int need_lock) {
  if (need_lock) {
    lock();
  }
  void *new_ptr = brp_malloc_internal(storage, elem_numbers * elem_size, 0);
  if (new_ptr) {
    brp_zero_mem(new_ptr, elem_numbers * elem_size);
  }
  if (need_lock) {
    unlock();
  }
  return new_ptr;
}


x86 assembly (clang, -O0):
<brp_calloc_internal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1166 <brp_calloc_internal+0x26>
mov    $0x0,%al
call   1040 <lock@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
imul   -0x10(%rbp),%esi
xor    %edx,%edx
call   1050 <brp_malloc_internal@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1197 <brp_calloc_internal+0x57>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
imul   -0x10(%rbp),%esi
call   1060 <brp_zero_mem@plt>
cmpl   $0x0,-0x14(%rbp)
je     11a8 <brp_calloc_internal+0x68>
mov    $0x0,%al
call   1030 <unlock@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<brp_calloc_internal>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r15
test   %ecx,%ecx
je     115c <brp_calloc_internal+0x1c>
xor    %eax,%eax
call   1040 <lock@plt>
imul   %ebx,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1050 <brp_malloc_internal@plt>
mov    %rax,%rbx
test   %rax,%rax
je     117d <brp_calloc_internal+0x3d>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <brp_zero_mem@plt>
test   %r14d,%r14d
je     1189 <brp_calloc_internal+0x49>
xor    %eax,%eax
call   1030 <unlock@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @brp_calloc_internal(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%10 = load i32, i32* %8
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = call i32 (...) @lock()
br label %14
14: ; preds = %12, %4
%15 = load i8*, i8** %5
%16 = load i32, i32* %6
%17 = load i32, i32* %7
%18 = mul nsw i32 %16, %17
%19 = call i8* @brp_malloc_internal(i8* %15, i32 %18, i32 0)
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %28
22: ; preds = %14
%23 = load i8*, i8** %9
%24 = load i32, i32* %6
%25 = load i32, i32* %7
%26 = mul nsw i32 %24, %25
%27 = call i32 @brp_zero_mem(i8* %23, i32 %26)
br label %28
28: ; preds = %22, %14
%29 = load i32, i32* %8
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 (...) @unlock()
br label %33
33: ; preds = %31, %28
%34 = load i8*, i8** %9
ret i8* %34
}

LLVM IR (-O2):
define i8* @brp_calloc_internal(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %3, 0
br i1 %5, label %8, label %6
6: ; preds = %4
%7 = tail call i32 (...) @lock()
br label %8
8: ; preds = %6, %4
%9 = mul nsw i32 %2, %1
%10 = tail call i8* @brp_malloc_internal(i8* %0, i32 %9, i32 0)
%11 = icmp eq i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @brp_zero_mem(i8* %10, i32 %9)
br label %14
14: ; preds = %12, %8
br i1 %5, label %17, label %15
15: ; preds = %14
%16 = tail call i32 (...) @unlock()
br label %17
17: ; preds = %15, %14
ret i8* %10
}