#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(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
void *malloc(size_t);
void *realloc(void *, size_t);
int /*<<< orphan*/ stderr;
void *xrealloc(void *ptr, size_t size) {
  void *allocated;
  if (ptr == NULL) {
    allocated = malloc(size);
  } else {
    allocated = realloc(ptr, size);
  }
  if (allocated == NULL) {
    fprintf(stderr,
            "Error:  Insufficient memory "
            "(attempt to realloc %zu bytes)\n",
            (unsigned int)size);
    exit(1);
  }
  return allocated;
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    116d <xrealloc+0x2d>
mov    -0x10(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
jmp    117e <xrealloc+0x3e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ae <xrealloc+0x6e>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    %eax,%edx
lea    0xe61(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %rbx
mov    %rsi,%rbx
test   %rdi,%rdi
je     1158 <xrealloc+0x18>
mov    %rbx,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     1165 <xrealloc+0x25>
pop    %rbx
ret
mov    %rbx,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
jne    1156 <xrealloc+0x16>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
lea    0xe8b(%rip),%rsi
mov    %ebx,%edx
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [60 x i8] c"Error: Insufficient memory (attempt to realloc %zu bytes)\0A\00"
define i8* @xrealloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i64, i64* %4
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %5
br label %15
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = load i64, i64* %4
%14 = call i8* @realloc(i8* %12, i64 %13)
store i8* %14, i8** %5
br label %15
15: ; preds = %11, %8
%16 = load i8*, i8** %5
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %24
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = load i64, i64* %4
%21 = trunc i64 %20 to i32
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = load i8*, i8** %5
ret i8* %25
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [60 x i8] c"Error: Insufficient memory (attempt to realloc %zu bytes)\0A\00"
define i8* @xrealloc(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i8* @malloc(i64 %1)
br label %8
6: ; preds = %2
%7 = tail call i8* @realloc(i8* %0, i64 %1)
br label %8
8: ; preds = %6, %4
%9 = phi i8* [ %5, %4 ], [ %7, %6 ]
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = trunc i64 %1 to i32
%14 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %8
ret i8* %9
}