#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);
size_t malloc_size(void *);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *realloc(void *ptr, size_t size) {
  void *new;
  if (!ptr)
    return malloc(size);
  if (size <= malloc_size(ptr))
    return ptr;
  new = malloc(size);
  if (!new)
    return NULL;
  memcpy(new, ptr, malloc_size(ptr));
  free(ptr);
  return new;
}


x86 assembly (clang, -O0):
<realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116d <realloc+0x2d>
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11fa <realloc+0xba>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <malloc_size@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
ja     119b <realloc+0x5b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11fa <realloc+0xba>
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c0 <realloc+0x80>
movq   $0x0,-0x8(%rbp)
jmp    11fa <realloc+0xba>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <malloc_size@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %rax,%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   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
test   %rdi,%rdi
je     118c <realloc+0x4c>
mov    %rdi,%rbx
call   1040 <malloc_size@plt>
cmp    %r14,%rax
jae    119d <realloc+0x5d>
mov    %r14,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     119b <realloc+0x5b>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <malloc_size@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rbx
jmp    119d <realloc+0x5d>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <malloc@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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