#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(unsigned short);
void *realloc(void *ptr, unsigned short size) {
  unsigned short i;
  void *mem = malloc(size);
  for (i = 0; i < *((unsigned short *)(ptr - 2)); i++)
    ((unsigned short *)mem)[i] = ((unsigned short *)ptr)[i];
  free(ptr);
  return mem;
}


x86 assembly (clang, -O0):
<realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %rdi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
movzwl -0xa(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
movw   $0x0,-0xc(%rbp)
movzwl -0xc(%rbp),%eax
mov    -0x8(%rbp),%rcx
movzwl -0x2(%rcx),%ecx
cmp    %ecx,%eax
jge    1183 <realloc+0x63>
mov    -0x8(%rbp),%rax
movzwl -0xc(%rbp),%ecx
mov    (%rax,%rcx,2),%dx
mov    -0x18(%rbp),%rax
movzwl -0xc(%rbp),%ecx
mov    %dx,(%rax,%rcx,2)
mov    -0xc(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0xc(%rbp)
jmp    1146 <realloc+0x26>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realloc>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %esi,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
movzwl -0x2(%rbx),%edx
test   %rdx,%rdx
je     1158 <realloc+0x28>
add    %rdx,%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @realloc(i8* %0, i16 zeroext %1) {
%3 = alloca i8*
%4 = alloca i16
%5 = alloca i16
%6 = alloca i8*
store i8* %0, i8** %3
store i16 %1, i16* %4
%7 = load i16, i16* %4
%8 = call i8* @malloc(i16 zeroext %7)
store i8* %8, i8** %6
store i16 0, i16* %5
br label %9
9: ; preds = %30, %2
%10 = load i16, i16* %5
%11 = zext i16 %10 to i32
%12 = load i8*, i8** %3
%13 = getelementptr i8, i8* %12, i64 -2
%14 = bitcast i8* %13 to i16*
%15 = load i16, i16* %14
%16 = zext i16 %15 to i32
%17 = icmp slt i32 %11, %16
br i1 %17, label %18, label %33
18: ; preds = %9
%19 = load i8*, i8** %3
%20 = bitcast i8* %19 to i16*
%21 = load i16, i16* %5
%22 = zext i16 %21 to i64
%23 = getelementptr inbounds i16, i16* %20, i64 %22
%24 = load i16, i16* %23
%25 = load i8*, i8** %6
%26 = bitcast i8* %25 to i16*
%27 = load i16, i16* %5
%28 = zext i16 %27 to i64
%29 = getelementptr inbounds i16, i16* %26, i64 %28
store i16 %24, i16* %29
br label %30
30: ; preds = %18
%31 = load i16, i16* %5
%32 = add i16 %31, 1
store i16 %32, i16* %5
br label %9
33: ; preds = %9
%34 = load i8*, i8** %3
%35 = call i32 @free(i8* %34)
%36 = load i8*, i8** %6
ret i8* %36
}

LLVM IR (-O2):
define noalias i8* @realloc(i8* nocapture %0, i16 zeroext %1) {
%3 = tail call i8* @malloc(i16 zeroext %1)
%4 = getelementptr i8, i8* %0, i64 -2
%5 = bitcast i8* %4 to i16*
%6 = load i16, i16* %5
%7 = icmp eq i16 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = zext i16 %6 to i64
%10 = shl nuw nsw i64 %9, 1
br label %11
11: ; preds = %8, %2
%12 = tail call i32 @free(i8* %0)
ret i8* %3
}