#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*/ free(void *);
int /*<<< orphan*/ panic(char *);
void *realloc(void *, size_t);
void *xrealloc(void *ptr, size_t size) {
  void *newptr;
  if (size == 0)
    panic("realloc: size 0\n");
  newptr = realloc(ptr, size);
  if (!newptr) {
    free(ptr);
    panic("realloc: out of memory\n");
  }
  return newptr;
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1157 <xrealloc+0x27>
lea    0xeae(%rip),%rdi
call   1040 <panic@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1188 <xrealloc+0x58>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
lea    0xe8e(%rip),%rdi
call   1040 <panic@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
test   %rsi,%rsi
jne    114b <xrealloc+0x1b>
lea    0xeba(%rip),%rdi
call   1040 <panic@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1172 <xrealloc+0x42>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xea4(%rip),%rdi
call   1040 <panic@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [17 x i8] c"realloc: size 0\0A\00"
@.str.1 = [24 x i8] c"realloc: out of memory\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 i64, i64* %4
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @panic(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = load i8*, i8** %3
%12 = load i64, i64* %4
%13 = call i8* @realloc(i8* %11, i64 %12)
store i8* %13, i8** %5
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %20, label %16
16: ; preds = %10
%17 = load i8*, i8** %3
%18 = call i32 @free(i8* %17)
%19 = call i32 @panic(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %16, %10
%21 = load i8*, i8** %5
ret i8* %21
}

LLVM IR (-O2):
@.str = [17 x i8] c"realloc: size 0\0A\00"
@.str.1 = [24 x i8] c"realloc: out of memory\0A\00"
define noalias i8* @xrealloc(i8* nocapture %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @panic(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %2
%7 = tail call i8* @realloc(i8* %0, i64 %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i32 @free(i8* %0)
%11 = tail call i32 @panic(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %9, %6
ret i8* %7
}