#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*/ assert(void *);
int /*<<< orphan*/ free(void *);
void *realloc(void *, size_t);
void *twopence_malloc(size_t);
void *twopence_realloc(void *p, size_t size) {
  if (p == NULL)
    return twopence_malloc(size);
  if (size == 0) {
    free(p);
    return NULL;
  }
  p = realloc(p, size);
  do {
    assert(p);
    return p;
  } while (0);
}


x86 assembly (clang, -O0):
<twopence_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116d <twopence_realloc+0x2d>
mov    -0x18(%rbp),%rdi
call   1050 <twopence_malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11b0 <twopence_realloc+0x70>
cmpq   $0x0,-0x18(%rbp)
jne    118e <twopence_realloc+0x4e>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11b0 <twopence_realloc+0x70>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<twopence_realloc>:
test   %rdi,%rdi
je     115d <twopence_realloc+0x1d>
push   %rbx
test   %rsi,%rsi
je     1165 <twopence_realloc+0x25>
call   1060 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <assert@plt>
jmp    116c <twopence_realloc+0x2c>
mov    %rsi,%rdi
jmp    1050 <twopence_malloc@plt>
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
define i8* @twopence_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* @twopence_malloc(i64 %1)
br label %13
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 %13
10: ; preds = %6
%11 = tail call i8* @realloc(i8* %0, i64 %1)
%12 = tail call i32 @assert(i8* %11)
br label %13
13: ; preds = %4, %8, %10
%14 = phi i8* [ %5, %4 ], [ null, %8 ], [ %11, %10 ]
ret i8* %14
}