#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ free(void *);
void *realloc(void *, scalar_t__);
void xrealloc(void **ptr, ssize_t newsize) {
  if (newsize <= 0)
    do {
      void **__ptr = (void **)(ptr);
      free(*__ptr);
      *(void **)__ptr = NULL;
    } while (0);
  else {
    *ptr = realloc(*ptr, newsize);
    if (!*ptr)
      abort();
  }
}


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)
jg     1174 <xrealloc+0x44>
jmp    1150 <xrealloc+0x20>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
jmp    11a8 <xrealloc+0x78>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a3 <xrealloc+0x73>
mov    $0x0,%al
call   1040 <abort@plt>
jmp    11a8 <xrealloc+0x78>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %rbx
mov    %rdi,%rbx
mov    (%rdi),%rdi
test   %rsi,%rsi
jle    114b <xrealloc+0x1b>
call   1050 <realloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     1159 <xrealloc+0x29>
pop    %rbx
ret
call   1030 <free@plt>
movq   $0x0,(%rbx)
pop    %rbx
ret
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
define void @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 sle i64 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %2
br label %9
9: ; preds = %8
%10 = load i8**, i8*** %3
store i8** %10, i8*** %5
%11 = load i8**, i8*** %5
%12 = load i8*, i8** %11
%13 = call i32 @free(i8* %12)
%14 = load i8**, i8*** %5
store i8* null, i8** %14
br label %15
15: ; preds = %9
br label %28
16: ; preds = %2
%17 = load i8**, i8*** %3
%18 = load i8*, i8** %17
%19 = load i64, i64* %4
%20 = call i8* @realloc(i8* %18, i64 %19)
%21 = load i8**, i8*** %3
store i8* %20, i8** %21
%22 = load i8**, i8*** %3
%23 = load i8*, i8** %22
%24 = icmp ne i8* %23, null
br i1 %24, label %27, label %25
25: ; preds = %16
%26 = call i32 (...) @abort()
unreachable
27: ; preds = %16
br label %28
28: ; preds = %27, %15
ret void
}

LLVM IR (-O2):
define void @xrealloc(i8** nocapture %0, i64 %1) {
%3 = icmp slt i64 %1, 1
%4 = load i8*, i8** %0
br i1 %3, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @free(i8* %4)
store i8* null, i8** %0
br label %12
7: ; preds = %2
%8 = tail call i8* @realloc(i8* %4, i64 %1)
store i8* %8, i8** %0
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 (...) @abort()
unreachable
12: ; preds = %7, %5
ret void
}