#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;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *);
void *realloc(void *, size_t);
void *xrealloc(void *oldptr, size_t nmemb, size_t size) {
  size_t newsize = nmemb * size;
  void *newptr;
  if (newsize == 0) {
    errx(EXIT_FAILURE, "zero size");
  }
  if (SIZE_MAX / nmemb < size) {
    errx(EXIT_FAILURE, "nmemb * size > SIZE_MAX");
  }
  if ((newptr = realloc(oldptr, newsize)) == NULL) {
    err(EXIT_FAILURE, "xrealloc failed");
  }
  return (newptr);
}


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,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1171 <xrealloc+0x41>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
lea    0xe94(%rip),%rsi
call   1030 <errx@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
cmp    -0x18(%rbp),%rax
jae    11a2 <xrealloc+0x72>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1030 <errx@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d2 <xrealloc+0xa2>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1040 <err@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,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rdx,%r12
imul   %rsi,%r12
test   %r12,%r12
jne    1162 <xrealloc+0x32>
mov    0x2e8c(%rip),%rax
mov    (%rax),%edi
lea    0xea3(%rip),%rsi
call   1030 <errx@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
je     11ce <xrealloc+0x9e>
xor    %edx,%edx
div    %rbx
cmp    %r15,%rax
jae    1197 <xrealloc+0x67>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
call   1030 <errx@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1050 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11bf <xrealloc+0x8f>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
call   1040 <err@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %edx,%edx
div    %ebx
cmp    %r15,%rax
jb     1182 <xrealloc+0x52>
jmp    1197 <xrealloc+0x67>

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

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 = [16 x i8] c"xrealloc failed\00"
define noalias i8* @xrealloc(i8* nocapture %0, i64 %1, i64 %2) {
%4 = mul i64 %2, %1
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %3
%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 = %6, %3
%10 = load i64, i64* @SIZE_MAX
%11 = udiv i64 %10, %1
%12 = icmp ult i64 %11, %2
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* @realloc(i8* %0, i64 %4)
%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 ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %19, %16
ret i8* %17
}