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


x86 assembly (clang, -O0):
<realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115d <realloc+0x2d>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11c4 <realloc+0x94>
mov    -0x10(%rbp),%rax
add    $0xfffffffffffffff8,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%rax
ja     1192 <realloc+0x62>
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c4 <realloc+0x94>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realloc>:
test   %rdi,%rdi
je     116c <realloc+0x3c>
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    -0x8(%rdi),%r14
cmp    %rsi,%r14
jae    1174 <realloc+0x44>
mov    %rsi,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rbx
jmp    1178 <realloc+0x48>
mov    %rsi,%rdi
jmp    1050 <malloc@plt>
mov    %rsi,-0x8(%rbx)
mov    %rbx,%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 i64*
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i64, i64* %5
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %3
br label %36
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = bitcast i8* %14 to i64*
%16 = getelementptr inbounds i64, i64* %15, i64 -1
store i64* %16, i64** %6
%17 = load i64, i64* %5
%18 = load i64*, i64** %6
%19 = load i64, i64* %18
%20 = icmp ule i64 %17, %19
br i1 %20, label %21, label %25
21: ; preds = %13
%22 = load i64, i64* %5
%23 = load i64*, i64** %6
store i64 %22, i64* %23
%24 = load i8*, i8** %4
store i8* %24, i8** %3
br label %36
25: ; preds = %13
%26 = load i64, i64* %5
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = load i8*, i8** %4
%30 = load i64*, i64** %6
%31 = load i64, i64* %30
%32 = call i32 @memcpy(i8* %28, i8* %29, i64 %31)
%33 = load i8*, i8** %4
%34 = call i32 @free(i8* %33)
%35 = load i8*, i8** %7
store i8* %35, i8** %3
br label %36
36: ; preds = %25, %21, %10
%37 = load i8*, i8** %3
ret i8* %37
}

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 = getelementptr inbounds i8, i8* %0, i64 -8
%8 = bitcast i8* %7 to i64*
%9 = load i64, i64* %8
%10 = icmp ult i64 %9, %1
br i1 %10, label %12, label %11
11: ; preds = %6
store i64 %1, i64* %8
br label %16
12: ; preds = %6
%13 = tail call i8* @malloc(i64 %1)
%14 = tail call i32 @memcpy(i8* %13, i8* %0, i64 %9)
%15 = tail call i32 @free(i8* %0)
br label %16
16: ; preds = %11, %12, %4
%17 = phi i8* [ %5, %4 ], [ %0, %11 ], [ %13, %12 ]
ret i8* %17
}