#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 *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ mutt_error(char *);
int /*<<< orphan*/ mutt_exit(int);
scalar_t__ realloc(void *, size_t);
int /*<<< orphan*/ sleep(int);
void safe_realloc(void *ptr, size_t siz) {
  void *r;
  void **p = (void **)ptr;
  if (siz == 0) {
    if (*p) {
      free(*p);
      *p = NULL;
    }
    return;
  }
  if (*p)
    r = (void *)realloc(*p, siz);
  else {
    r = (void *)malloc(siz);
  }
  if (!r) {
    mutt_error("Out of memory!");
    sleep(1);
    mutt_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)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ad <safe_realloc+0x4d>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11a8 <safe_realloc+0x48>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
jmp    1217 <safe_realloc+0xb7>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11d4 <safe_realloc+0x74>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
jmp    11e1 <safe_realloc+0x81>
mov    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120c <safe_realloc+0xac>
lea    0xe0d(%rip),%rdi
call   1080 <mutt_error@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    $0x1,%edi
call   1040 <mutt_exit@plt>
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_realloc>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    (%rdi),%rdi
test   %rsi,%rsi
je     117b <safe_realloc+0x1b>
test   %rdi,%rdi
je     1189 <safe_realloc+0x29>
call   1060 <realloc@plt>
jmp    1191 <safe_realloc+0x31>
test   %rdi,%rdi
je     11bc <safe_realloc+0x5c>
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    11b9 <safe_realloc+0x59>
mov    %rsi,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b9 <safe_realloc+0x59>
lea    0xe60(%rip),%rdi
call   1080 <mutt_error@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    $0x1,%edi
call   1040 <mutt_exit@plt>
mov    %rbx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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*
%6 = alloca i8**
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i8*, i8** %3
%8 = bitcast i8* %7 to i8**
store i8** %8, i8*** %6
%9 = load i64, i64* %4
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %2
%12 = load i8**, i8*** %6
%13 = load i8*, i8** %12
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i8**, i8*** %6
%17 = load i8*, i8** %16
%18 = call i32 @free(i8* %17)
%19 = load i8**, i8*** %6
store i8* null, i8** %19
br label %20
20: ; preds = %15, %11
br label %45
21: ; preds = %2
%22 = load i8**, i8*** %6
%23 = load i8*, i8** %22
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = load i8**, i8*** %6
%27 = load i8*, i8** %26
%28 = load i64, i64* %4
%29 = call i64 @realloc(i8* %27, i64 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %5
br label %35
31: ; preds = %21
%32 = load i64, i64* %4
%33 = call i64 @malloc(i64 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %5
br label %35
35: ; preds = %31, %25
%36 = load i8*, i8** %5
%37 = icmp ne i8* %36, null
br i1 %37, label %42, label %38
38: ; preds = %35
%39 = call i32 @mutt_error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%40 = call i32 @sleep(i32 1)
%41 = call i32 @mutt_exit(i32 1)
br label %42
42: ; preds = %38, %35
%43 = load i8*, i8** %5
%44 = load i8**, i8*** %6
store i8* %43, i8** %44
br label %45
45: ; preds = %42, %20
ret void
}

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