#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 */
int /*<<< orphan*/ fatal(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)
    fatal("xrealloc: zero size");
  if (ptr == NULL)
    new_ptr = malloc(new_size);
  else
    new_ptr = realloc(ptr, new_size);
  if (new_ptr == NULL)
    fatal("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    116a <xrealloc+0x3a>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1050 <fatal@plt>
cmpq   $0x0,-0x8(%rbp)
jne    1187 <xrealloc+0x57>
mov    -0x28(%rbp),%rdi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
jmp    1198 <xrealloc+0x68>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b7 <xrealloc+0x87>
mov    -0x28(%rbp),%rax
mov    %eax,%esi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1050 <fatal@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
imul   %rdx,%rbx
test   %rbx,%rbx
je     1155 <xrealloc+0x25>
test   %r14,%r14
je     1168 <xrealloc+0x38>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <realloc@plt>
jmp    1170 <xrealloc+0x40>
lea    0xea4(%rip),%rdi
xor    %eax,%eax
call   1050 <fatal@plt>
test   %r14,%r14
jne    1148 <xrealloc+0x18>
mov    %rbx,%rdi
call   1030 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1188 <xrealloc+0x58>
lea    0xe95(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <fatal@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [20 x i8] c"xrealloc: zero size\00"
@.str.1 = [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 (i8*, ...) @fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
%17 = load i8*, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i64, i64* %8
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %7
br label %26
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = load i64, i64* %8
%25 = call i8* @realloc(i8* %23, i64 %24)
store i8* %25, i8** %7
br label %26
26: ; preds = %22, %19
%27 = load i8*, i8** %7
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i64, i64* %8
%31 = trunc i64 %30 to i32
%32 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %29, %26
%34 = load i8*, i8** %7
ret i8* %34
}

LLVM IR (-O2):
@.str = [20 x i8] c"xrealloc: zero size\00"
@.str.1 = [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 (i8*, ...) @fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %3
%9 = icmp eq i8* %0, null
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = tail call i8* @malloc(i64 %4)
br label %14
12: ; preds = %8
%13 = tail call i8* @realloc(i8* %0, i64 %4)
br label %14
14: ; preds = %12, %10
%15 = phi i8* [ %11, %10 ], [ %13, %12 ]
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = trunc i64 %4 to i32
%19 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %17, %14
ret i8* %15
}