#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 */
size_t SIZE_MAX;
int /*<<< orphan*/ errx(int, int /*<<< orphan*/ *);
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(2, NULL);
  if (SIZE_MAX / nmemb < size)
    errx(2, NULL);
  if (ptr == NULL)
    new_ptr = malloc(new_size);
  else
    new_ptr = realloc(ptr, new_size);
  if (new_ptr == NULL)
    errx(2, NULL);
  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    116a <xrealloc+0x3a>
mov    $0x2,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <errx@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
cmp    -0x18(%rbp),%rax
jae    1194 <xrealloc+0x64>
mov    $0x2,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <errx@plt>
cmpq   $0x0,-0x8(%rbp)
jne    11b1 <xrealloc+0x81>
mov    -0x28(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
jmp    11c2 <xrealloc+0x92>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11db <xrealloc+0xab>
mov    $0x2,%edi
xor    %eax,%eax
mov    %eax,%esi
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    1159 <xrealloc+0x29>
mov    $0x2,%edi
xor    %esi,%esi
call   1030 <errx@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
je     1197 <xrealloc+0x67>
xor    %edx,%edx
div    %rbx
cmp    %r15,%rax
jae    1185 <xrealloc+0x55>
mov    $0x2,%edi
xor    %esi,%esi
call   1030 <errx@plt>
test   %r14,%r14
je     11a2 <xrealloc+0x72>
mov    %r14,%rdi
mov    %r12,%rsi
call   1050 <realloc@plt>
jmp    11aa <xrealloc+0x7a>
xor    %edx,%edx
div    %ebx
cmp    %r15,%rax
jb     1179 <xrealloc+0x49>
jmp    1185 <xrealloc+0x55>
mov    %r12,%rdi
call   1040 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11be <xrealloc+0x8e>
mov    $0x2,%edi
xor    %esi,%esi
call   1030 <errx@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SIZE_MAX = global i64 0
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 @errx(i32 2, i32* null)
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 @errx(i32 2, i32* null)
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 %39
37: ; preds = %34
%38 = call i32 @errx(i32 2, i32* null)
br label %39
39: ; preds = %37, %34
%40 = load i8*, i8** %7
ret i8* %40
}

LLVM IR (-O2):
@SIZE_MAX = global i64 0
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 @errx(i32 2, i32* null)
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 @errx(i32 2, i32* null)
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 %25
23: ; preds = %20
%24 = tail call i32 @errx(i32 2, i32* null)
br label %25
25: ; preds = %23, %20
ret i8* %21
}