#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(int /*<<< orphan*/);
int /*<<< orphan*/ free(void *);
void *malloc(size_t);
void *realloc(void *, size_t);
void *xrealloc(void *old_addr, size_t new_size) {
  void *new_addr = NULL;
  if (new_size == 0) {
    free(old_addr);
  } else if (old_addr == NULL) {
    new_addr = malloc(new_size);
    assert(new_addr != NULL);
  } else {
    new_addr = realloc(old_addr, new_size);
    assert(new_addr != NULL);
  }
  return new_addr;
}


x86 assembly (clang, -O0):
<xrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1171 <xrealloc+0x31>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
jmp    11c8 <xrealloc+0x88>
cmpq   $0x0,-0x8(%rbp)
jne    11a0 <xrealloc+0x60>
mov    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
jmp    11c3 <xrealloc+0x83>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
jmp    11c8 <xrealloc+0x88>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %rbx
test   %rsi,%rsi
je     1152 <xrealloc+0x12>
test   %rdi,%rdi
je     115b <xrealloc+0x1b>
call   1060 <realloc@plt>
jmp    1163 <xrealloc+0x23>
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    1174 <xrealloc+0x34>
mov    %rsi,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
define noalias i8* @xrealloc(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @free(i8* %0)
br label %18
6: ; preds = %2
%7 = icmp eq i8* %0, null
br i1 %7, label %8, label %13
8: ; preds = %6
%9 = tail call i8* @malloc(i64 %1)
%10 = icmp ne i8* %9, null
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
br label %18
13: ; preds = %6
%14 = tail call i8* @realloc(i8* %0, i64 %1)
%15 = icmp ne i8* %14, null
%16 = zext i1 %15 to i32
%17 = tail call i32 @assert(i32 %16)
br label %18
18: ; preds = %8, %13, %4
%19 = phi i8* [ null, %4 ], [ %9, %8 ], [ %14, %13 ]
ret i8* %19
}