#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 */
size_t allocsize(void *);
int /*<<< orphan*/ free(void *);
void *malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *realloc(void *ptr, size_t size) {
  void *p;
  size_t sz;
  p = malloc(size);
  if (!p)
    return (p);
  sz = allocsize(ptr);
  memcpy(p, ptr, (sz > size) ? size : sz);
  free(ptr);
  return (p);
}


x86 assembly (clang, -O0):
<realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1175 <realloc+0x35>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d7 <realloc+0x97>
mov    -0x10(%rbp),%rdi
call   1040 <allocsize@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    11ad <realloc+0x6d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11b5 <realloc+0x75>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realloc>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1180 <realloc+0x40>
mov    %r14,%rdi
call   1040 <allocsize@plt>
cmp    %rbx,%rax
cmovbe %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @realloc(i8* %0, i64 %1) {
%3 = tail call i8* @malloc(i64 %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %11, label %5
5: ; preds = %2
%6 = tail call i64 @allocsize(i8* %0)
%7 = icmp ugt i64 %6, %1
%8 = select i1 %7, i64 %1, i64 %6
%9 = tail call i32 @memcpy(i8* %3, i8* %0, i64 %8)
%10 = tail call i32 @free(i8* %0)
br label %11
11: ; preds = %2, %5
ret i8* %3
}