#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 */
void *crAlloc(unsigned int);
int /*<<< orphan*/ crError(char *, unsigned int);
void *realloc(void *, unsigned int);
void crRealloc(void **ptr, unsigned int nbytes) {
  if (*ptr == NULL) {
    *ptr = crAlloc(nbytes);
  } else {
    *ptr = realloc(*ptr, nbytes);
    if (*ptr == NULL)
      crError("Couldn't realloc %d bytes!", nbytes);
  }
}


x86 assembly (clang, -O0):
<crRealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1164 <crRealloc+0x34>
mov    -0xc(%rbp),%edi
call   1040 <crAlloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    119f <crRealloc+0x6f>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0xc(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    119a <crRealloc+0x6a>
mov    -0xc(%rbp),%esi
lea    0xe6b(%rip),%rdi
call   1030 <crError@plt>
jmp    119f <crRealloc+0x6f>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crRealloc>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    (%rdi),%rdi
test   %rdi,%rdi
je     1163 <crRealloc+0x33>
mov    %ebp,%esi
call   1050 <realloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
jne    116d <crRealloc+0x3d>
lea    0xeaa(%rip),%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <crError@plt>
mov    %ebp,%edi
call   1040 <crAlloc@plt>
mov    %rax,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Couldn't realloc %d bytes!\00"
define void @crRealloc(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
store i8** %0, i8*** %3
store i32 %1, i32* %4
%5 = load i8**, i8*** %3
%6 = load i8*, i8** %5
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* %4
%10 = call i8* @crAlloc(i32 %9)
%11 = load i8**, i8*** %3
store i8* %10, i8** %11
br label %25
12: ; preds = %2
%13 = load i8**, i8*** %3
%14 = load i8*, i8** %13
%15 = load i32, i32* %4
%16 = call i8* @realloc(i8* %14, i32 %15)
%17 = load i8**, i8*** %3
store i8* %16, i8** %17
%18 = load i8**, i8*** %3
%19 = load i8*, i8** %18
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %12
%22 = load i32, i32* %4
%23 = call i32 @crError(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %21, %12
br label %25
25: ; preds = %24, %8
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"Couldn't realloc %d bytes!\00"
define void @crRealloc(i8** nocapture %0, i32 %1) {
%3 = load i8*, i8** %0
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i8* @crAlloc(i32 %1)
store i8* %6, i8** %0
br label %12
7: ; preds = %2
%8 = tail call i8* @realloc(i8* %3, i32 %1)
store i8* %8, i8** %0
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @crError(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %12
12: ; preds = %7, %10, %5
ret void
}