#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 *, int);
void *malloc(int);
void *realloc(void *, int);
int /*<<< orphan*/ rfx_free(void *);
int /*<<< orphan*/ start_debugger();
int /*<<< orphan*/ stderr;
void *rfx_realloc(void *data, int size) {
  void *ptr;
  if (size == 0) {
    rfx_free(data);
    return 0;
  }
  if (!data) {
    ptr = malloc(size);
  } else {
    ptr = realloc(data, size);
  }
  if (!ptr) {
    fprintf(stderr, "FATAL: Out of memory (while trying to claim %d bytes)\n",
            size);
    start_debugger();
    exit(1);
  }
  return ptr;
}


x86 assembly (clang, -O0):
<rfx_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    118f <rfx_realloc+0x2f>
mov    -0x10(%rbp),%rdi
call   1030 <rfx_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f7 <rfx_realloc+0x97>
cmpq   $0x0,-0x10(%rbp)
jne    11ab <rfx_realloc+0x4b>
mov    -0x14(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
jmp    11bb <rfx_realloc+0x5b>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ef <rfx_realloc+0x8f>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe27(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1040 <start_debugger@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rfx_realloc>:
push   %rbx
test   %esi,%esi
je     117a <rfx_realloc+0x1a>
mov    %esi,%ebx
test   %rdi,%rdi
je     1183 <rfx_realloc+0x23>
mov    %ebx,%esi
call   1070 <realloc@plt>
test   %rax,%rax
je     118f <rfx_realloc+0x2f>
pop    %rbx
ret
call   1030 <rfx_free@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    %ebx,%edi
call   1060 <malloc@plt>
test   %rax,%rax
jne    1178 <rfx_realloc+0x18>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
mov    %ebx,%edx
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1040 <start_debugger@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [55 x i8] c"FATAL: Out of memory (while trying to claim %d bytes)\0A\00"
define i8* @rfx_realloc(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = call i32 @rfx_free(i8* %10)
store i8* null, i8** %3
br label %33
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i32, i32* %5
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %6
br label %22
18: ; preds = %12
%19 = load i8*, i8** %4
%20 = load i32, i32* %5
%21 = call i8* @realloc(i8* %19, i32 %20)
store i8* %21, i8** %6
br label %22
22: ; preds = %18, %15
%23 = load i8*, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %31, label %25
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = load i32, i32* %5
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = call i32 (...) @start_debugger()
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %22
%32 = load i8*, i8** %6
store i8* %32, i8** %3
br label %33
33: ; preds = %31, %9
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [55 x i8] c"FATAL: Out of memory (while trying to claim %d bytes)\0A\00"
define i8* @rfx_realloc(i8* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @rfx_free(i8* %0)
br label %20
6: ; preds = %2
%7 = icmp eq i8* %0, null
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i8* @malloc(i32 %1)
br label %12
10: ; preds = %6
%11 = tail call i8* @realloc(i8* %0, i32 %1)
br label %12
12: ; preds = %10, %8
%13 = phi i8* [ %11, %10 ], [ %9, %8 ]
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %1)
%18 = tail call i32 (...) @start_debugger()
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %12, %4
%21 = phi i8* [ null, %4 ], [ %13, %12 ]
ret i8* %21
}