#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;
int /*<<< orphan*/ errx(int, char *, ...);
void *malloc(size_t);
void *realloc(void *, size_t);
void *xrealloc(void *ptr, size_t nmemb, size_t size) {
  void *new_ptr;
  size_t new_size = nmemb * size;
  if (new_size == 0)
    errx(1, "xrealloc: zero size");
  if (SIZE_MAX / nmemb < size)
    errx(1, "xrealloc: nmemb * size > SIZE_MAX");
  if (ptr == NULL)
    new_ptr = malloc(new_size);
  else
    new_ptr = realloc(ptr, new_size);
  if (new_ptr == NULL)
    errx(1, "xrealloc: out of memory (new_size %lu bytes)", (u_long)new_size);
  return new_ptr;
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    116f <xrealloc+0x3f>
mov    $0x1,%edi
lea    0xe98(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
cmp    -0x18(%rbp),%rax
jae    119e <xrealloc+0x6e>
mov    $0x1,%edi
lea    0xe7d(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
cmpq   $0x0,-0x8(%rbp)
jne    11bb <xrealloc+0x8b>
mov    -0x28(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
jmp    11cc <xrealloc+0x9c>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f0 <xrealloc+0xc0>
mov    -0x28(%rbp),%rax
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rdx,%r12
imul   %rsi,%r12
test   %r12,%r12
jne    1160 <xrealloc+0x30>
lea    0xeac(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1030 <errx@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
je     11a5 <xrealloc+0x75>
xor    %edx,%edx
div    %rbx
cmp    %r15,%rax
jae    1193 <xrealloc+0x63>
lea    0xe8d(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1030 <errx@plt>
test   %r14,%r14
je     11b0 <xrealloc+0x80>
mov    %r14,%rdi
mov    %r12,%rsi
call   1050 <realloc@plt>
jmp    11b8 <xrealloc+0x88>
xor    %edx,%edx
div    %ebx
cmp    %r15,%rax
jb     1180 <xrealloc+0x50>
jmp    1193 <xrealloc+0x63>
mov    %r12,%rdi
call   1040 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d6 <xrealloc+0xa6>
lea    0xe6f(%rip),%rsi
mov    $0x1,%edi
mov    %r12d,%edx
xor    %eax,%eax
call   1030 <errx@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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