#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*/ fort_free(void *);
void *fort_malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *custom_fort_realloc(void *ptr, size_t size) {
  if (ptr == NULL)
    return fort_malloc(size);
  if (size == 0) {
    fort_free(ptr);
    return NULL;
  }
  void *new_chunk = fort_malloc(size);
  if (new_chunk == NULL)
    return NULL;
  memcpy(new_chunk, ptr, size);
  fort_free(ptr);
  return new_chunk;
}


x86 assembly (clang, -O0):
<custom_fort_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115d <custom_fort_realloc+0x2d>
mov    -0x18(%rbp),%rdi
call   1050 <fort_malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11c5 <custom_fort_realloc+0x95>
cmpq   $0x0,-0x18(%rbp)
jne    117e <custom_fort_realloc+0x4e>
mov    -0x10(%rbp),%rdi
call   1040 <fort_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c5 <custom_fort_realloc+0x95>
mov    -0x18(%rbp),%rdi
call   1050 <fort_malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a3 <custom_fort_realloc+0x73>
movq   $0x0,-0x8(%rbp)
jmp    11c5 <custom_fort_realloc+0x95>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fort_free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<custom_fort_realloc>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
test   %rdi,%rdi
je     116d <custom_fort_realloc+0x3d>
mov    %rdi,%r15
test   %rbx,%rbx
je     117a <custom_fort_realloc+0x4a>
mov    %rbx,%rdi
call   1050 <fort_malloc@plt>
test   %rax,%rax
je     1182 <custom_fort_realloc+0x52>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
mov    %r15,%rdi
call   1040 <fort_free@plt>
jmp    1185 <custom_fort_realloc+0x55>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <fort_malloc@plt>
mov    %r15,%rdi
call   1040 <fort_free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @custom_fort_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 i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i64, i64* %5
%11 = call i8* @fort_malloc(i64 %10)
store i8* %11, i8** %3
br label %32
12: ; preds = %2
%13 = load i64, i64* %5
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = call i32 @fort_free(i8* %16)
store i8* null, i8** %3
br label %32
18: ; preds = %12
%19 = load i64, i64* %5
%20 = call i8* @fort_malloc(i64 %19)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %18
store i8* null, i8** %3
br label %32
24: ; preds = %18
%25 = load i8*, i8** %6
%26 = load i8*, i8** %4
%27 = load i64, i64* %5
%28 = call i32 @memcpy(i8* %25, i8* %26, i64 %27)
%29 = load i8*, i8** %4
%30 = call i32 @fort_free(i8* %29)
%31 = load i8*, i8** %6
store i8* %31, i8** %3
br label %32
32: ; preds = %24, %23, %15, %9
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
define i8* @custom_fort_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* @fort_malloc(i64 %1)
br label %16
6: ; preds = %2
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @fort_free(i8* %0)
br label %16
10: ; preds = %6
%11 = tail call i8* @fort_malloc(i64 %1)
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i32 @memcpy(i8* %11, i8* %0, i64 %1)
%15 = tail call i32 @fort_free(i8* %0)
br label %16
16: ; preds = %13, %10, %8, %4
%17 = phi i8* [ %5, %4 ], [ null, %8 ], [ %11, %13 ], [ null, %10 ]
ret i8* %17
}