#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 */
typedef int /*<<< orphan*/ u_long;

/* Variables and functions */
size_t SIZE_MAX;
void *calloc(size_t, size_t);
int /*<<< orphan*/ errx(int, char *, ...);
void *xcalloc(size_t nmemb, size_t size) {
  void *ptr;
  if (size == 0 || nmemb == 0)
    errx(1, "xcalloc: zero size");
  if (SIZE_MAX / nmemb < size)
    errx(1, "xcalloc: nmemb * size > SIZE_MAX");
  ptr = calloc(nmemb, size);
  if (ptr == NULL)
    errx(1, "xcalloc: out of memory (allocating %lu bytes)",
         (u_long)(size * nmemb));
  return ptr;
}


x86 assembly (clang, -O0):
<xcalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1146 <xcalloc+0x26>
cmpq   $0x0,-0x8(%rbp)
jne    1159 <xcalloc+0x39>
mov    $0x1,%edi
lea    0xeae(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x8(%rbp)
cmp    -0x10(%rbp),%rax
jae    1188 <xcalloc+0x68>
mov    $0x1,%edi
lea    0xe92(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c2 <xcalloc+0xa2>
mov    -0x10(%rbp),%rax
imul   -0x8(%rbp),%rax
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xcalloc>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
test   %rdi,%rdi
je     1135 <xcalloc+0x15>
test   %r14,%r14
jne    1148 <xcalloc+0x28>
lea    0xec4(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1030 <errx@plt>
mov    0x2e89(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %r15,%rcx
shr    $0x20,%rcx
je     11b1 <xcalloc+0x91>
xor    %edx,%edx
div    %r15
cmp    %r14,%rax
jae    117b <xcalloc+0x5b>
lea    0xea4(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1030 <errx@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a8 <xcalloc+0x88>
imul   %r15d,%r14d
lea    0xe9b(%rip),%rsi
mov    $0x1,%edi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <errx@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %edx,%edx
div    %r15d
cmp    %r14,%rax
jb     1168 <xcalloc+0x48>
jmp    117b <xcalloc+0x5b>

LLVM IR (-O0):
@.str = [19 x i8] c"xcalloc: zero size\00"
@SIZE_MAX = global i64 0
@.str.1 = [33 x i8] c"xcalloc: nmemb * size > SIZE_MAX\00"
@.str.2 = [46 x i8] c"xcalloc: out of memory (allocating %lu bytes)\00"
define i8* @xcalloc(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
store i64 %0, i64* %3
store i64 %1, i64* %4
%6 = load i64, i64* %4
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i64, i64* %3
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8, %2
%12 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = load i64, i64* @SIZE_MAX
%15 = load i64, i64* %3
%16 = udiv i64 %14, %15
%17 = load i64, i64* %4
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
%22 = load i64, i64* %3
%23 = load i64, i64* %4
%24 = call i8* @calloc(i64 %22, i64 %23)
store i8* %24, i8** %5
%25 = load i8*, i8** %5
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = load i64, i64* %4
%29 = load i64, i64* %3
%30 = mul i64 %28, %29
%31 = trunc i64 %30 to i32
%32 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %27, %21
%34 = load i8*, i8** %5
ret i8* %34
}

LLVM IR (-O2):
@.str = [19 x i8] c"xcalloc: zero size\00"
@SIZE_MAX = global i64 0
@.str.1 = [33 x i8] c"xcalloc: nmemb * size > SIZE_MAX\00"
@.str.2 = [46 x i8] c"xcalloc: out of memory (allocating %lu bytes)\00"
define noalias i8* @xcalloc(i64 %0, i64 %1) {
%3 = icmp eq i64 %1, 0
%4 = icmp eq i64 %0, 0
%5 = or i1 %4, %3
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %2, %6
%9 = load i64, i64* @SIZE_MAX
%10 = udiv i64 %9, %0
%11 = icmp ult i64 %10, %1
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = tail call i8* @calloc(i64 %0, i64 %1)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = mul i64 %1, %0
%19 = trunc i64 %18 to i32
%20 = tail call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %17, %14
ret i8* %15
}