#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*/ vector_bzero(void *, size_t);
scalar_t__ vector_memalloc(size_t);
int /*<<< orphan*/ vector_memcpy(void *, void *, size_t);
int /*<<< orphan*/ vector_memdel(void *);
void *vector_realloc(void *ptr, size_t old, size_t size) {
  void *new;
  if (ptr == NULL)
    return (NULL);
  if (size == 0)
    new = (void *)vector_memalloc(sizeof(char));
  else
    new = (void *)vector_memalloc(size);
  if (new == NULL)
    return (NULL);
  vector_bzero(new, (size) ? size : sizeof(char));
  if (old > size)
    vector_memcpy(new, ptr, (size) ? size : sizeof(char));
  else
    vector_memcpy(new, ptr, old);
  vector_memdel((void *)&ptr);
  return (new);
}


x86 assembly (clang, -O0):
<vector_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116c <vector_realloc+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    1266 <vector_realloc+0x126>
cmpq   $0x0,-0x20(%rbp)
jne    118a <vector_realloc+0x4a>
mov    $0x1,%edi
call   1050 <vector_memalloc@plt>
mov    %rax,-0x28(%rbp)
jmp    1197 <vector_realloc+0x57>
mov    -0x20(%rbp),%rdi
call   1050 <vector_memalloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11af <vector_realloc+0x6f>
movq   $0x0,-0x8(%rbp)
jmp    1266 <vector_realloc+0x126>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11cf <vector_realloc+0x8f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11dd <vector_realloc+0x9d>
mov    $0x1,%eax
mov    %rax,-0x38(%rbp)
jmp    11dd <vector_realloc+0x9d>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1030 <vector_bzero@plt>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    1244 <vector_realloc+0x104>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1220 <vector_realloc+0xe0>
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    122e <vector_realloc+0xee>
mov    $0x1,%eax
mov    %rax,-0x50(%rbp)
jmp    122e <vector_realloc+0xee>
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rdx
call   1060 <vector_memcpy@plt>
jmp    1255 <vector_realloc+0x115>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <vector_memcpy@plt>
lea    -0x10(%rbp),%rdi
call   1040 <vector_memdel@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vector_realloc>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,0x8(%rsp)
test   %rdi,%rdi
je     11af <vector_realloc+0x6f>
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r12
test   %rdx,%rdx
mov    $0x1,%ebx
mov    %rdx,%rdi
cmove  %rbx,%rdi
call   1050 <vector_memalloc@plt>
test   %rax,%rax
je     11af <vector_realloc+0x6f>
mov    %rax,%r14
test   %r13,%r13
cmovne %r13,%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <vector_bzero@plt>
cmp    %r13,%r15
cmovbe %r15,%rbx
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <vector_memcpy@plt>
lea    0x8(%rsp),%rdi
call   1040 <vector_memdel@plt>
jmp    11b2 <vector_realloc+0x72>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @vector_realloc(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %3
store i8* null, i8** %4
br label %59
12: ; preds = %3
%13 = load i64, i64* %7
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i64 @vector_memalloc(i64 1)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %8
br label %22
18: ; preds = %12
%19 = load i64, i64* %7
%20 = call i64 @vector_memalloc(i64 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %8
br label %22
22: ; preds = %18, %15
%23 = load i8*, i8** %8
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %22
store i8* null, i8** %4
br label %59
26: ; preds = %22
%27 = load i8*, i8** %8
%28 = load i64, i64* %7
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i64, i64* %7
br label %33
32: ; preds = %26
br label %33
33: ; preds = %32, %30
%34 = phi i64 [ %31, %30 ], [ 1, %32 ]
%35 = call i32 @vector_bzero(i8* %27, i64 %34)
%36 = load i64, i64* %6
%37 = load i64, i64* %7
%38 = icmp ugt i64 %36, %37
br i1 %38, label %39, label %50
39: ; preds = %33
%40 = load i8*, i8** %8
%41 = load i8*, i8** %5
%42 = load i64, i64* %7
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = load i64, i64* %7
br label %47
46: ; preds = %39
br label %47
47: ; preds = %46, %44
%48 = phi i64 [ %45, %44 ], [ 1, %46 ]
%49 = call i32 @vector_memcpy(i8* %40, i8* %41, i64 %48)
br label %55
50: ; preds = %33
%51 = load i8*, i8** %8
%52 = load i8*, i8** %5
%53 = load i64, i64* %6
%54 = call i32 @vector_memcpy(i8* %51, i8* %52, i64 %53)
br label %55
55: ; preds = %50, %47
%56 = bitcast i8** %5 to i8*
%57 = call i32 @vector_memdel(i8* %56)
%58 = load i8*, i8** %8
store i8* %58, i8** %4
br label %59
59: ; preds = %55, %25, %11
%60 = load i8*, i8** %4
ret i8* %60
}

LLVM IR (-O2):
define i8* @vector_realloc(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
store i8* %0, i8** %4
%5 = icmp eq i8* %0, null
br i1 %5, label %20, label %6
6: ; preds = %3
%7 = icmp eq i64 %2, 0
%8 = select i1 %7, i64 1, i64 %2
%9 = tail call i64 @vector_memalloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
%11 = icmp eq i64 %9, 0
br i1 %11, label %20, label %12
12: ; preds = %6
%13 = select i1 %7, i64 1, i64 %2
%14 = tail call i32 @vector_bzero(i8* %10, i64 %13)
%15 = icmp ugt i64 %1, %2
%16 = select i1 %15, i64 %13, i64 %1
%17 = tail call i32 @vector_memcpy(i8* %10, i8* %0, i64 %16)
%18 = bitcast i8** %4 to i8*
%19 = call i32 @vector_memdel(i8* %18)
br label %20
20: ; preds = %6, %3, %12
%21 = phi i8* [ %10, %12 ], [ null, %3 ], [ null, %6 ]
ret i8* %21
}