#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(size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *mm_realloc(void *oldptr, size_t size) {
  size_t oldsize;
  void *newptr;
  if (size == 0) {
    free(oldptr);
    return 0;
  }
  if (oldptr == NULL) {
    return malloc(size);
  }
  newptr = malloc(size);
  if (!newptr) {
    return 0;
  }
  oldsize = *(
      (size_t *)(((char *)(oldptr)) - ((((sizeof(size_t)) + (8 - 1)) & ~0x7))));
  if (size < oldsize)
    oldsize = size;
  memcpy(newptr, oldptr, oldsize);
  free(oldptr);
  return newptr;
}


x86 assembly (clang, -O0):
<mm_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1161 <mm_realloc+0x31>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e7 <mm_realloc+0xb7>
cmpq   $0x0,-0x10(%rbp)
jne    117e <mm_realloc+0x4e>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11e7 <mm_realloc+0xb7>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a3 <mm_realloc+0x73>
movq   $0x0,-0x8(%rbp)
jmp    11e7 <mm_realloc+0xb7>
mov    -0x10(%rbp),%rax
mov    -0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11c5 <mm_realloc+0x95>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mm_realloc>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
test   %rsi,%rsi
je     1175 <mm_realloc+0x45>
mov    %rsi,%rbx
mov    %rsi,%rdi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %r15,%r15
je     1180 <mm_realloc+0x50>
test   %r14,%r14
je     117d <mm_realloc+0x4d>
mov    -0x8(%r15),%rdx
cmp    %rbx,%rdx
cmova  %rbx,%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <memcpy@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    1180 <mm_realloc+0x50>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @mm_realloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i64, i64* %5
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = call i32 @free(i8* %11)
store i8* null, i8** %3
br label %43
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i64, i64* %5
%18 = call i8* @malloc(i64 %17)
store i8* %18, i8** %3
br label %43
19: ; preds = %13
%20 = load i64, i64* %5
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %7
%22 = load i8*, i8** %7
%23 = icmp ne i8* %22, null
br i1 %23, label %25, label %24
24: ; preds = %19
store i8* null, i8** %3
br label %43
25: ; preds = %19
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i64 -8
%28 = bitcast i8* %27 to i64*
%29 = load i64, i64* %28
store i64 %29, i64* %6
%30 = load i64, i64* %5
%31 = load i64, i64* %6
%32 = icmp ult i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = load i64, i64* %5
store i64 %34, i64* %6
br label %35
35: ; preds = %33, %25
%36 = load i8*, i8** %7
%37 = load i8*, i8** %4
%38 = load i64, i64* %6
%39 = call i32 @memcpy(i8* %36, i8* %37, i64 %38)
%40 = load i8*, i8** %4
%41 = call i32 @free(i8* %40)
%42 = load i8*, i8** %7
store i8* %42, i8** %3
br label %43
43: ; preds = %35, %24, %16, %10
%44 = load i8*, i8** %3
ret i8* %44
}

LLVM IR (-O2):
define i8* @mm_realloc(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 %19
6: ; preds = %2
%7 = icmp eq i8* %0, null
%8 = tail call i8* @malloc(i64 %1)
br i1 %7, label %19, label %9
9: ; preds = %6
%10 = icmp eq i8* %8, null
br i1 %10, label %19, label %11
11: ; preds = %9
%12 = getelementptr inbounds i8, i8* %0, i64 -8
%13 = bitcast i8* %12 to i64*
%14 = load i64, i64* %13
%15 = icmp ugt i64 %14, %1
%16 = select i1 %15, i64 %1, i64 %14
%17 = tail call i32 @memcpy(i8* %8, i8* %0, i64 %16)
%18 = tail call i32 @free(i8* %0)
br label %19
19: ; preds = %6, %9, %11, %4
%20 = phi i8* [ null, %4 ], [ %8, %11 ], [ null, %9 ], [ %8, %6 ]
ret i8* %20
}