#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 *);
unsigned int get_ptr_size(void *);
void *malloc(unsigned int);
int /*<<< orphan*/ memcpy(void *, void *, unsigned int);
void *realloc(void *ptr, unsigned int size) {
  void *local_ptr = ((void *)0);
  unsigned int old_size = 0;
  if (ptr == ((void *)0)) {
    return malloc(size);
  }
  if ((size == 0) && (ptr != ((void *)0))) {
    free(ptr);
    return ((void *)0);
  }
  local_ptr = malloc(size);
  old_size = get_ptr_size(ptr);
  memcpy(local_ptr, ptr, old_size > size ? size : old_size);
  free(ptr);
  return local_ptr;
}


x86 assembly (clang, -O0):
<realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117a <realloc+0x3a>
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    120b <realloc+0xcb>
cmpl   $0x0,-0x14(%rbp)
jne    11a5 <realloc+0x65>
cmpq   $0x0,-0x10(%rbp)
je     11a5 <realloc+0x65>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    120b <realloc+0xcb>
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <get_ptr_size@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jbe    11e4 <realloc+0xa4>
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    11ea <realloc+0xaa>
mov    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%edx
call   1040 <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   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
test   %rdi,%rdi
je     1180 <realloc+0x40>
mov    %rdi,%rbx
test   %ebp,%ebp
je     118b <realloc+0x4b>
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <get_ptr_size@plt>
cmp    %ebp,%eax
cmovbe %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1196 <realloc+0x56>
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <malloc@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @realloc(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i8* @malloc(i32 %1)
br label %17
6: ; preds = %2
%7 = icmp eq i32 %1, 0
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @free(i8* %0)
br label %17
10: ; preds = %6
%11 = tail call i8* @malloc(i32 %1)
%12 = tail call i32 @get_ptr_size(i8* %0)
%13 = icmp ugt i32 %12, %1
%14 = select i1 %13, i32 %1, i32 %12
%15 = tail call i32 @memcpy(i8* %11, i8* %0, i32 %14)
%16 = tail call i32 @free(i8* %0)
br label %17
17: ; preds = %10, %8, %4
%18 = phi i8* [ %5, %4 ], [ null, %8 ], [ %11, %10 ]
ret i8* %18
}