#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*/ ENOMEM;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(void *);
void *realloc(void *, size_t);
int aeron_reallocf(void **ptr, size_t size) {
  if ((*ptr = realloc(*ptr, size)) == NULL) {
    if (0 == size) {
      *ptr = NULL;
    } else {
      free(*ptr);
      errno = ENOMEM;
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<aeron_reallocf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <realloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    119c <aeron_reallocf+0x7c>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    116d <aeron_reallocf+0x4d>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
jmp    1197 <aeron_reallocf+0x77>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11a3 <aeron_reallocf+0x83>
jmp    119c <aeron_reallocf+0x7c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aeron_reallocf>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%rdi
call   1040 <realloc@plt>
mov    %rax,%rcx
mov    %rax,(%rbx)
xor    %eax,%eax
test   %rcx,%rcx
jne    116b <aeron_reallocf+0x4b>
test   %r14,%r14
je     1164 <aeron_reallocf+0x44>
xor    %edi,%edi
call   1030 <free@plt>
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    0x2e7d(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    116b <aeron_reallocf+0x4b>
movq   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @aeron_reallocf(i8** %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i64
store i8** %0, i8*** %4
store i64 %1, i64* %5
%6 = load i8**, i8*** %4
%7 = load i8*, i8** %6
%8 = load i64, i64* %5
%9 = call i8* @realloc(i8* %7, i64 %8)
%10 = load i8**, i8*** %4
store i8* %9, i8** %10
%11 = icmp eq i8* %9, null
br i1 %11, label %12, label %23
12: ; preds = %2
%13 = load i64, i64* %5
%14 = icmp eq i64 0, %13
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = load i8**, i8*** %4
store i8* null, i8** %16
br label %22
17: ; preds = %12
%18 = load i8**, i8*** %4
%19 = load i8*, i8** %18
%20 = call i32 @free(i8* %19)
%21 = load i32, i32* @ENOMEM
store i32 %21, i32* @errno
store i32 -1, i32* %3
br label %24
22: ; preds = %15
br label %23
23: ; preds = %22, %2
store i32 0, i32* %3
br label %24
24: ; preds = %23, %17
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @aeron_reallocf(i8** nocapture %0, i64 %1) {
%3 = load i8*, i8** %0
%4 = tail call i8* @realloc(i8* %3, i64 %1)
store i8* %4, i8** %0
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %9
8: ; preds = %6
store i8* null, i8** %0
br label %12
9: ; preds = %6
%10 = tail call i32 @free(i8* null)
%11 = load i32, i32* @ENOMEM
store i32 %11, i32* @errno
br label %12
12: ; preds = %2, %8, %9
%13 = phi i32 [ -1, %9 ], [ 0, %8 ], [ 0, %2 ]
ret i32 %13
}