#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*/ EXIT_FAILURE;
size_t SIZE_MAX;
void *calloc(size_t, size_t);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *);
void *xcalloc(size_t nmemb, size_t size) {
  void *ptr;
  if (size == 0 || nmemb == 0) {
    errx(EXIT_FAILURE, "zero size");
  }
  if (SIZE_MAX / nmemb < size) {
    errx(EXIT_FAILURE, "nmemb * size > SIZE_MAX");
  }
  if ((ptr = calloc(nmemb, size)) == NULL) {
    err(EXIT_FAILURE, "xcalloc failed");
  }
  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     1156 <xcalloc+0x26>
cmpq   $0x0,-0x8(%rbp)
jne    116b <xcalloc+0x3b>
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
lea    0xe9a(%rip),%rsi
call   1030 <errx@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x8(%rbp)
cmp    -0x10(%rbp),%rax
jae    119c <xcalloc+0x6c>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
call   1030 <errx@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11cc <xcalloc+0x9c>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1050 <err@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xcalloc>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
je     1144 <xcalloc+0x14>
test   %r14,%r14
jne    1159 <xcalloc+0x29>
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
lea    0xeac(%rip),%rsi
call   1030 <errx@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
je     11c1 <xcalloc+0x91>
xor    %edx,%edx
div    %rbx
cmp    %r14,%rax
jae    118e <xcalloc+0x5e>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xe81(%rip),%rsi
call   1030 <errx@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b6 <xcalloc+0x86>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
call   1050 <err@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %edx,%edx
div    %ebx
cmp    %r14,%rax
jb     1179 <xcalloc+0x49>
jmp    118e <xcalloc+0x5e>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [10 x i8] c"zero size\00"
@SIZE_MAX = global i64 0
@.str.1 = [24 x i8] c"nmemb * size > SIZE_MAX\00"
@.str.2 = [15 x i8] c"xcalloc failed\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 %14
11: ; preds = %8, %2
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @errx(i32 %12, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %11, %8
%15 = load i64, i64* @SIZE_MAX
%16 = load i64, i64* %3
%17 = udiv i64 %15, %16
%18 = load i64, i64* %4
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @errx(i32 %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %20, %14
%24 = load i64, i64* %3
%25 = load i64, i64* %4
%26 = call i8* @calloc(i64 %24, i64 %25)
store i8* %26, i8** %5
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @err(i32 %29, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %28, %23
%32 = load i8*, i8** %5
ret i8* %32
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [10 x i8] c"zero size\00"
@SIZE_MAX = global i64 0
@.str.1 = [24 x i8] c"nmemb * size > SIZE_MAX\00"
@.str.2 = [15 x i8] c"xcalloc failed\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 %9
6: ; preds = %2
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @errx(i32 %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %2, %6
%10 = load i64, i64* @SIZE_MAX
%11 = udiv i64 %10, %0
%12 = icmp ult i64 %11, %1
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @errx(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %13, %9
%17 = tail call i8* @calloc(i64 %0, i64 %1)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @err(i32 %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %19, %16
ret i8* %17
}