#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 *);
void *malloc(size_t);
void *realloc(void *, size_t);
void *safe_realloc(void *ptr, size_t size) {
  if (ptr == NULL)
    return malloc(size);
  if (size == 0) {
    free(ptr);
    return NULL;
  } else {
    void *new_block = realloc(ptr, size);
    if (!new_block)
      free(ptr);
    return new_block;
  }
}


x86 assembly (clang, -O0):
<safe_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115d <safe_realloc+0x2d>
mov    -0x18(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11ab <safe_realloc+0x7b>
cmpq   $0x0,-0x18(%rbp)
jne    117e <safe_realloc+0x4e>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ab <safe_realloc+0x7b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a3 <safe_realloc+0x73>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_realloc>:
test   %rdi,%rdi
je     114d <safe_realloc+0x1d>
push   %rbx
mov    %rdi,%rbx
test   %rsi,%rsi
je     1155 <safe_realloc+0x25>
mov    %rbx,%rdi
call   1050 <realloc@plt>
test   %rax,%rax
je     1155 <safe_realloc+0x25>
pop    %rbx
ret
mov    %rsi,%rdi
jmp    1040 <malloc@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
define noalias i8* @safe_realloc(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 %15
6: ; preds = %2
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @free(i8* %0)
br label %15
10: ; preds = %6
%11 = tail call i8* @realloc(i8* %0, i64 %1)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @free(i8* %0)
br label %15
15: ; preds = %10, %13, %8, %4
%16 = phi i8* [ %5, %4 ], [ null, %8 ], [ null, %13 ], [ %11, %10 ]
ret i8* %16
}