#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 */
void *malloc(size_t);
int /*<<< orphan*/ panic(char *, ...);
void *realloc(void *, size_t);
__attribute__((visibility("hidden"))) void *xrealloc(void *ptr, size_t nmemb,
                                                     size_t size) {
  void *new_ptr;
  size_t new_size = nmemb * size;
  if (new_size == 0)
    panic("xrealloc: zero size\n");
  if (((size_t)~0) / nmemb < size)
    panic("xrealloc: nmemb * size > SIZE_T_MAX\n");
  if (ptr == NULL)
    new_ptr = malloc(new_size);
  else
    new_ptr = realloc(ptr, new_size);
  if (new_ptr == NULL)
    panic("xrealloc: out of memory (size %zu bytes)\n", 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    116a <xrealloc+0x3a>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1030 <panic@plt>
mov    $0xffffffffffffffff,%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
cmp    -0x18(%rbp),%rax
jae    1191 <xrealloc+0x61>
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1030 <panic@plt>
cmpq   $0x0,-0x8(%rbp)
jne    11ae <xrealloc+0x7e>
mov    -0x28(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
jmp    11bf <xrealloc+0x8f>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11dc <xrealloc+0xac>
mov    -0x28(%rbp),%rsi
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1030 <panic@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,%r12
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rdx,%r15
imul   %rsi,%r15
test   %r15,%r15
jne    115b <xrealloc+0x2b>
lea    0xeac(%rip),%rdi
xor    %eax,%eax
call   1030 <panic@plt>
mov    %rbx,%rax
mul    %r12
jno    1171 <xrealloc+0x41>
lea    0xeab(%rip),%rdi
xor    %eax,%eax
call   1030 <panic@plt>
test   %r14,%r14
je     1183 <xrealloc+0x53>
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <realloc@plt>
jmp    118b <xrealloc+0x5b>
mov    %r15,%rdi
call   1040 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a4 <xrealloc+0x74>
lea    0xea0(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <panic@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"xrealloc: zero size\0A\00"
@.str.1 = [37 x i8] c"xrealloc: nmemb * size > SIZE_T_MAX\0A\00"
@.str.2 = [42 x i8] c"xrealloc: out of memory (size %zu bytes)\0A\00"
define hidden 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 (i8*, ...) @panic(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
%17 = load i64, i64* %5
%18 = udiv i64 -1, %17
%19 = load i64, i64* %6
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 (i8*, ...) @panic(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = load i8*, i8** %4
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i64, i64* %8
%28 = call i8* @malloc(i64 %27)
store i8* %28, i8** %7
br label %33
29: ; preds = %23
%30 = load i8*, i8** %4
%31 = load i64, i64* %8
%32 = call i8* @realloc(i8* %30, i64 %31)
store i8* %32, i8** %7
br label %33
33: ; preds = %29, %26
%34 = load i8*, i8** %7
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i64, i64* %8
%38 = call i32 (i8*, ...) @panic(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i64 %37)
br label %39
39: ; preds = %36, %33
%40 = load i8*, i8** %7
ret i8* %40
}

LLVM IR (-O2):
@.str = [21 x i8] c"xrealloc: zero size\0A\00"
@.str.1 = [37 x i8] c"xrealloc: nmemb * size > SIZE_T_MAX\0A\00"
@.str.2 = [42 x i8] c"xrealloc: out of memory (size %zu bytes)\0A\00"
define hidden 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 (i8*, ...) @panic(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %3
%9 = tail call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %1, i64 %2)
%10 = extractvalue { i64, i1 } %9, 1
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 (i8*, ...) @panic(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = icmp eq i8* %0, null
br i1 %14, label %15, label %17
15: ; preds = %13
%16 = tail call i8* @malloc(i64 %4)
br label %19
17: ; preds = %13
%18 = tail call i8* @realloc(i8* %0, i64 %4)
br label %19
19: ; preds = %17, %15
%20 = phi i8* [ %16, %15 ], [ %18, %17 ]
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 (i8*, ...) @panic(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i64 %4)
br label %24
24: ; preds = %22, %19
ret i8* %20
}