#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*/ memcpy(void *, void *, size_t);
int /*<<< orphan*/ mm_free(void *);
void *mm_malloc(size_t);
void *mm_realloc(void *ptr, size_t size) {
  void *oldptr = ptr;
  void *newptr;
  size_t copySize;
  newptr = mm_malloc(size);
  if (newptr == NULL)
    return NULL;
  copySize =
      *(size_t *)((char *)oldptr - ((((sizeof(size_t)) + (8 - 1)) & ~0x7)));
  if (size < copySize)
    copySize = size;
  memcpy(newptr, oldptr, copySize);
  mm_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)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <mm_malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    116d <mm_realloc+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    11b1 <mm_realloc+0x81>
mov    -0x20(%rbp),%rax
mov    -0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    118f <mm_realloc+0x5f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rdi
call   1050 <mm_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    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <mm_malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1169 <mm_realloc+0x39>
mov    -0x8(%r14),%rdx
cmp    %rbx,%rdx
cmova  %rbx,%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <memcpy@plt>
mov    %r14,%rdi
call   1050 <mm_free@plt>
mov    %r15,%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 i8*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = load i8*, i8** %4
store i8* %9, i8** %6
%10 = load i64, i64* %5
%11 = call i8* @mm_malloc(i64 %10)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* null, i8** %3
br label %33
15: ; preds = %2
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i64 -8
%18 = bitcast i8* %17 to i64*
%19 = load i64, i64* %18
store i64 %19, i64* %8
%20 = load i64, i64* %5
%21 = load i64, i64* %8
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = load i64, i64* %5
store i64 %24, i64* %8
br label %25
25: ; preds = %23, %15
%26 = load i8*, i8** %7
%27 = load i8*, i8** %6
%28 = load i64, i64* %8
%29 = call i32 @memcpy(i8* %26, i8* %27, i64 %28)
%30 = load i8*, i8** %6
%31 = call i32 @mm_free(i8* %30)
%32 = load i8*, i8** %7
store i8* %32, i8** %3
br label %33
33: ; preds = %25, %14
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
define i8* @mm_realloc(i8* %0, i64 %1) {
%3 = tail call i8* @mm_malloc(i64 %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %13, label %5
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %0, i64 -8
%7 = bitcast i8* %6 to i64*
%8 = load i64, i64* %7
%9 = icmp ugt i64 %8, %1
%10 = select i1 %9, i64 %1, i64 %8
%11 = tail call i32 @memcpy(i8* %3, i8* %0, i64 %10)
%12 = tail call i32 @mm_free(i8* %0)
br label %13
13: ; preds = %2, %5
ret i8* %3
}