#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 ULONG_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *, ...);
void *realloc(void *, size_t);
void *xrealloc(void *ptr, size_t nmemb, size_t elem_size) {
  if (nmemb && ULONG_MAX / nmemb < elem_size)
    error(EXIT_FAILURE, 0, "realloc: nmemb*size > ULONG_MAX");
  size_t size = nmemb * elem_size;
  void *r = realloc(ptr, size);
  if (!r)
    error(EXIT_FAILURE, errno, "realloc: allocating %lu*%lu bytes",
          (unsigned long)nmemb, (unsigned long)elem_size);
  return r;
}


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)
cmpq   $0x0,-0x10(%rbp)
je     1174 <xrealloc+0x54>
mov    0x2e82(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
cmp    -0x18(%rbp),%rax
jae    1174 <xrealloc+0x54>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    0xe93(%rip),%rdx
mov    $0x0,%al
call   1040 <error@plt>
mov    -0x10(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c5 <xrealloc+0xa5>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe62(%rip),%rdx
mov    $0x0,%al
call   1040 <error@plt>
mov    -0x28(%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,%r14
mov    %rsi,%r12
mov    %rdi,%r15
test   %rsi,%rsi
je     116f <xrealloc+0x4f>
mov    0x2e8b(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %r12,%rcx
shr    $0x20,%rcx
je     11bb <xrealloc+0x9b>
xor    %edx,%edx
div    %r12
cmp    %r14,%rax
jae    116f <xrealloc+0x4f>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    0xe9a(%rip),%rdx
xor    %esi,%esi
xor    %eax,%eax
call   1040 <error@plt>
mov    %r14,%rsi
imul   %r12,%rsi
mov    %r15,%rdi
call   1030 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11ac <xrealloc+0x8c>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdx
mov    %r12,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1040 <error@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %edx,%edx
div    %r12d
cmp    %r14,%rax
jb     1156 <xrealloc+0x36>
jmp    116f <xrealloc+0x4f>

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

LLVM IR (-O2):
@ULONG_MAX = global i64 0
@EXIT_FAILURE = global i32 0
@.str = [32 x i8] c"realloc: nmemb*size > ULONG_MAX\00"
@errno = global i32 0
@.str.1 = [34 x i8] c"realloc: allocating %lu*%lu bytes\00"
define noalias i8* @xrealloc(i8* nocapture %0, i64 %1, i64 %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %12, label %5
5: ; preds = %3
%6 = load i64, i64* @ULONG_MAX
%7 = udiv i64 %6, %1
%8 = icmp ult i64 %7, %2
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 (i32, i32, i8*, ...) @error(i32 %10, i32 0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %9, %5, %3
%13 = mul i64 %2, %1
%14 = tail call i8* @realloc(i8* %0, i64 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* @EXIT_FAILURE
%18 = load i32, i32* @errno
%19 = tail call i32 (i32, i32, i8*, ...) @error(i32 %17, i32 %18, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i64 %1, i64 %2)
br label %20
20: ; preds = %16, %12
ret i8* %14
}