#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*/ free(void *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ perror(char *);
scalar_t__ realloc(void *, size_t);
int /*<<< orphan*/ sleep(int);
void safe_realloc(void **p, size_t siz) {
  void *r;
  if (siz == 0) {
    if (*p) {
      free(*p);
      *p = NULL;
    }
    return;
  }
  if (*p)
    r = (void *)realloc(*p, siz);
  else {
    r = (void *)malloc(siz);
  }
  if (!r) {
    perror("Out of memory!");
    sleep(1);
    exit(1);
  }
  *p = r;
}


x86 assembly (clang, -O0):
<safe_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a5 <safe_realloc+0x45>
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     11a0 <safe_realloc+0x40>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
jmp    120f <safe_realloc+0xaf>
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     11cc <safe_realloc+0x6c>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
jmp    11d9 <safe_realloc+0x79>
mov    -0x10(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1204 <safe_realloc+0xa4>
lea    0xe15(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_realloc>:
push   %rbx
mov    %rdi,%rbx
mov    (%rdi),%rdi
test   %rsi,%rsi
je     117d <safe_realloc+0x1d>
test   %rdi,%rdi
je     118b <safe_realloc+0x2b>
call   1050 <realloc@plt>
test   %rax,%rax
jne    1198 <safe_realloc+0x38>
jmp    119d <safe_realloc+0x3d>
test   %rdi,%rdi
je     119b <safe_realloc+0x3b>
call   1030 <free@plt>
xor    %eax,%eax
jmp    1198 <safe_realloc+0x38>
mov    %rsi,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     119d <safe_realloc+0x3d>
mov    %rax,(%rbx)
pop    %rbx
ret
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Out of memory!\00"
define void @safe_realloc(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 %18
8: ; preds = %2
%9 = load i8**, i8*** %3
%10 = load i8*, i8** %9
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = load i8**, i8*** %3
%14 = load i8*, i8** %13
%15 = call i32 @free(i8* %14)
%16 = load i8**, i8*** %3
store i8* null, i8** %16
br label %17
17: ; preds = %12, %8
br label %42
18: ; preds = %2
%19 = load i8**, i8*** %3
%20 = load i8*, i8** %19
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = load i8**, i8*** %3
%24 = load i8*, i8** %23
%25 = load i64, i64* %4
%26 = call i64 @realloc(i8* %24, i64 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %5
br label %32
28: ; preds = %18
%29 = load i64, i64* %4
%30 = call i64 @malloc(i64 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %5
br label %32
32: ; preds = %28, %22
%33 = load i8*, i8** %5
%34 = icmp ne i8* %33, null
br i1 %34, label %39, label %35
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @sleep(i32 1)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %32
%40 = load i8*, i8** %5
%41 = load i8**, i8*** %3
store i8* %40, i8** %41
br label %42
42: ; preds = %39, %17
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"Out of memory!\00"
define void @safe_realloc(i8** nocapture %0, i64 %1) {
%3 = icmp eq i64 %1, 0
%4 = load i8*, i8** %0
%5 = icmp eq i8* %4, null
br i1 %3, label %6, label %9
6: ; preds = %2
br i1 %5, label %25, label %7
7: ; preds = %6
%8 = tail call i32 @free(i8* %4)
br label %23
9: ; preds = %2
br i1 %5, label %12, label %10
10: ; preds = %9
%11 = tail call i64 @realloc(i8* %4, i64 %1)
br label %14
12: ; preds = %9
%13 = tail call i64 @malloc(i64 %1)
br label %14
14: ; preds = %12, %10
%15 = phi i64 [ %11, %10 ], [ %13, %12 ]
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @sleep(i32 1)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = inttoptr i64 %15 to i8*
br label %23
23: ; preds = %21, %7
%24 = phi i8* [ null, %7 ], [ %22, %21 ]
store i8* %24, i8** %0
br label %25
25: ; preds = %23, %6
ret void
}