#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(unsigned long);
void *realloc(void *, unsigned long);
int /*<<< orphan*/ rs_error(int /*<<< orphan*/ *, int /*<<< orphan*/ *, char *,
                            unsigned long);
void *rs_realloc(void *ptr, unsigned long count) {
  void *p;
  if (!count) {
    if (ptr) {
      free(ptr);
    }
    return NULL;
  }
  if (ptr)
    p = realloc(ptr, count);
  else
    p = malloc(count);
  if (!p)
    rs_error(NULL, NULL, "out of memory (need %lu bytes)", count);
  return p;
}


x86 assembly (clang, -O0):
<rs_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117c <rs_realloc+0x3c>
cmpq   $0x0,-0x10(%rbp)
je     116f <rs_realloc+0x2f>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d4 <rs_realloc+0x94>
cmpq   $0x0,-0x10(%rbp)
je     119d <rs_realloc+0x5d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    11aa <rs_realloc+0x6a>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cc <rs_realloc+0x8c>
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
lea    0xe3c(%rip),%rdx
mov    %rsi,%rdi
call   1040 <rs_error@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rs_realloc>:
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     115b <rs_realloc+0x1b>
mov    %rsi,%r14
test   %rdi,%rdi
je     1169 <rs_realloc+0x29>
mov    %r14,%rsi
call   1060 <realloc@plt>
jmp    1171 <rs_realloc+0x31>
test   %rdi,%rdi
je     1165 <rs_realloc+0x25>
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    118e <rs_realloc+0x4e>
mov    %r14,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    118e <rs_realloc+0x4e>
lea    0xe80(%rip),%rdx
xor    %ebx,%ebx
xor    %edi,%edi
xor    %esi,%esi
mov    %r14,%rcx
call   1040 <rs_error@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [31 x i8] c"out of memory (need %lu bytes)\00"
define i8* @rs_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 i64, i64* %5
%8 = icmp ne i64 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i8*, i8** %4
%14 = call i32 @free(i8* %13)
br label %15
15: ; preds = %12, %9
store i8* null, i8** %3
br label %34
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i8*, i8** %4
%21 = load i64, i64* %5
%22 = call i8* @realloc(i8* %20, i64 %21)
store i8* %22, i8** %6
br label %26
23: ; preds = %16
%24 = load i64, i64* %5
%25 = call i8* @malloc(i64 %24)
store i8* %25, i8** %6
br label %26
26: ; preds = %23, %19
%27 = load i8*, i8** %6
%28 = icmp ne i8* %27, null
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i64, i64* %5
%31 = call i32 @rs_error(i32* null, i32* null, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 %30)
br label %32
32: ; preds = %29, %26
%33 = load i8*, i8** %6
store i8* %33, i8** %3
br label %34
34: ; preds = %32, %15
%35 = load i8*, i8** %3
ret i8* %35
}

LLVM IR (-O2):
@.str = [31 x i8] c"out of memory (need %lu bytes)\00"
define i8* @rs_realloc(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
%4 = icmp eq i8* %0, null
br i1 %3, label %5, label %8
5: ; preds = %2
br i1 %4, label %18, label %6
6: ; preds = %5
%7 = tail call i32 @free(i8* %0)
br label %18
8: ; preds = %2
br i1 %4, label %11, label %9
9: ; preds = %8
%10 = tail call i8* @realloc(i8* %0, i64 %1)
br label %13
11: ; preds = %8
%12 = tail call i8* @malloc(i64 %1)
br label %13
13: ; preds = %11, %9
%14 = phi i8* [ %10, %9 ], [ %12, %11 ]
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @rs_error(i32* null, i32* null, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 %1)
br label %18
18: ; preds = %13, %16, %5, %6
%19 = phi i8* [ null, %6 ], [ null, %5 ], [ null, %16 ], [ %14, %13 ]
ret i8* %19
}