#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 */
typedef scalar_t__ ptr;

/* 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) {
  size_t oldsize;
  void *newptr;
  if (size == 0) {
    mm_free(ptr);
    return 0;
  }
  if (ptr == NULL) {
    return mm_malloc(size);
  }
  newptr = mm_malloc(size);
  if (!newptr) {
    return 0;
  }
  oldsize = ((*(unsigned int *)(((char *)(ptr)-4))) & ~0x7);
  if (size < oldsize)
    oldsize = size;
  memcpy(newptr, ptr, oldsize);
  mm_free(ptr);
  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   1050 <mm_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11eb <mm_realloc+0xbb>
cmpq   $0x0,-0x10(%rbp)
jne    117e <mm_realloc+0x4e>
mov    -0x18(%rbp),%rdi
call   1030 <mm_malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11eb <mm_realloc+0xbb>
mov    -0x18(%rbp),%rdi
call   1030 <mm_malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a3 <mm_realloc+0x73>
movq   $0x0,-0x8(%rbp)
jmp    11eb <mm_realloc+0xbb>
mov    -0x10(%rbp),%rax
mov    -0x4(%rax),%eax
and    $0xfffffff8,%eax
mov    %eax,%eax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11c9 <mm_realloc+0x99>
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   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    %rdi,%r15
test   %rsi,%rsi
je     1178 <mm_realloc+0x48>
mov    %rsi,%rbx
mov    %rsi,%rdi
call   1030 <mm_malloc@plt>
mov    %rax,%r14
test   %r15,%r15
je     1183 <mm_realloc+0x53>
test   %r14,%r14
je     1180 <mm_realloc+0x50>
mov    -0x4(%r15),%edx
and    $0xfffffff8,%edx
cmp    %rbx,%rdx
cmova  %rbx,%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <memcpy@plt>
mov    %r15,%rdi
call   1050 <mm_free@plt>
jmp    1183 <mm_realloc+0x53>
mov    %r15,%rdi
call   1050 <mm_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 @mm_free(i8* %11)
store i8* null, i8** %3
br label %45
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* @mm_malloc(i64 %17)
store i8* %18, i8** %3
br label %45
19: ; preds = %13
%20 = load i64, i64* %5
%21 = call i8* @mm_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 %45
25: ; preds = %19
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i64 -4
%28 = bitcast i8* %27 to i32*
%29 = load i32, i32* %28
%30 = and i32 %29, -8
%31 = zext i32 %30 to i64
store i64 %31, i64* %6
%32 = load i64, i64* %5
%33 = load i64, i64* %6
%34 = icmp ult i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %25
%36 = load i64, i64* %5
store i64 %36, i64* %6
br label %37
37: ; preds = %35, %25
%38 = load i8*, i8** %7
%39 = load i8*, i8** %4
%40 = load i64, i64* %6
%41 = call i32 @memcpy(i8* %38, i8* %39, i64 %40)
%42 = load i8*, i8** %4
%43 = call i32 @mm_free(i8* %42)
%44 = load i8*, i8** %7
store i8* %44, i8** %3
br label %45
45: ; preds = %37, %24, %16, %10
%46 = load i8*, i8** %3
ret i8* %46
}

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