#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 */
void *_memory_reallocate(void *, size_t, size_t, unsigned int);
size_t _memory_usable_size(void *);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *rpaligned_alloc(size_t, size_t);
int /*<<< orphan*/ rpfree(void *);
void *rpaligned_realloc(void *ptr, size_t alignment, size_t size,
                        size_t oldsize, unsigned int flags) {
  void *block;
  if (alignment > 32) {
    size_t usablesize = _memory_usable_size(ptr);
    if ((usablesize >= size) && (size >= (usablesize / 2)) &&
        !((uintptr_t)ptr & (alignment - 1)))
      return ptr;
    block = rpaligned_alloc(alignment, size);
    if (ptr) {
      if (!oldsize)
        oldsize = usablesize;
      if (!(flags & 1))
        memcpy(block, ptr, oldsize < size ? oldsize : size);
      rpfree(ptr);
    }
  } else {
    block = _memory_reallocate(ptr, size, oldsize, flags);
  }
  return block;
}


x86 assembly (clang, -O0):
<rpaligned_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
cmpq   $0x20,-0x18(%rbp)
jbe    125d <rpaligned_realloc+0x10d>
mov    -0x10(%rbp),%rdi
call   1050 <_memory_usable_size@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     11cd <rpaligned_realloc+0x7d>
mov    -0x20(%rbp),%rax
mov    -0x40(%rbp),%rcx
shr    $0x1,%rcx
cmp    %rcx,%rax
jb     11cd <rpaligned_realloc+0x7d>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
jne    11cd <rpaligned_realloc+0x7d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    127d <rpaligned_realloc+0x12d>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <rpaligned_alloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1258 <rpaligned_realloc+0x108>
cmpq   $0x0,-0x28(%rbp)
jne    11fc <rpaligned_realloc+0xac>
mov    -0x40(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    124f <rpaligned_realloc+0xff>
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1236 <rpaligned_realloc+0xe6>
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    123e <rpaligned_realloc+0xee>
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1060 <rpfree@plt>
jmp    1275 <rpaligned_realloc+0x125>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1070 <_memory_reallocate@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rpaligned_realloc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r15d
mov    %rcx,%r13
mov    %rdx,%rbx
mov    %rdi,%r14
cmp    $0x21,%rsi
jb     11d3 <rpaligned_realloc+0x83>
mov    %rsi,%rbp
mov    %r14,%rdi
call   1050 <_memory_usable_size@plt>
mov    %rax,%r12
cmp    %rbx,%rax
jb     1194 <rpaligned_realloc+0x44>
mov    %r12,%rax
shr    %rax
cmp    %rbx,%rax
ja     1194 <rpaligned_realloc+0x44>
lea    -0x1(%rbp),%rax
test   %r14,%rax
je     11f2 <rpaligned_realloc+0xa2>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1030 <rpaligned_alloc@plt>
mov    %rax,%rbp
test   %r14,%r14
je     11f5 <rpaligned_realloc+0xa5>
test   %r13,%r13
cmovne %r13,%r12
test   $0x1,%r15b
jne    11c9 <rpaligned_realloc+0x79>
cmp    %rbx,%r12
cmovb  %r12,%rbx
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %r14,%rdi
call   1060 <rpfree@plt>
jmp    11f5 <rpaligned_realloc+0xa5>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
mov    %r15d,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <_memory_reallocate@plt>
mov    %r14,%rbp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @rpaligned_realloc(i8* %0, i64 %1, i64 %2, i64 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
store i8* %0, i8** %7
store i64 %1, i64* %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i32 %4, i32* %11
%14 = load i64, i64* %8
%15 = icmp ugt i64 %14, 32
br i1 %15, label %16, label %68
16: ; preds = %5
%17 = load i8*, i8** %7
%18 = call i64 @_memory_usable_size(i8* %17)
store i64 %18, i64* %13
%19 = load i64, i64* %13
%20 = load i64, i64* %9
%21 = icmp uge i64 %19, %20
br i1 %21, label %22, label %36
22: ; preds = %16
%23 = load i64, i64* %9
%24 = load i64, i64* %13
%25 = udiv i64 %24, 2
%26 = icmp uge i64 %23, %25
br i1 %26, label %27, label %36
27: ; preds = %22
%28 = load i8*, i8** %7
%29 = ptrtoint i8* %28 to i64
%30 = load i64, i64* %8
%31 = sub i64 %30, 1
%32 = and i64 %29, %31
%33 = icmp ne i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %27
%35 = load i8*, i8** %7
store i8* %35, i8** %6
br label %76
36: ; preds = %27, %22, %16
%37 = load i64, i64* %8
%38 = load i64, i64* %9
%39 = call i8* @rpaligned_alloc(i64 %37, i64 %38)
store i8* %39, i8** %12
%40 = load i8*, i8** %7
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %67
42: ; preds = %36
%43 = load i64, i64* %10
%44 = icmp ne i64 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %42
%46 = load i64, i64* %13
store i64 %46, i64* %10
br label %47
47: ; preds = %45, %42
%48 = load i32, i32* %11
%49 = and i32 %48, 1
%50 = icmp ne i32 %49, 0
br i1 %50, label %64, label %51
51: ; preds = %47
%52 = load i8*, i8** %12
%53 = load i8*, i8** %7
%54 = load i64, i64* %10
%55 = load i64, i64* %9
%56 = icmp ult i64 %54, %55
br i1 %56, label %57, label %59
57: ; preds = %51
%58 = load i64, i64* %10
br label %61
59: ; preds = %51
%60 = load i64, i64* %9
br label %61
61: ; preds = %59, %57
%62 = phi i64 [ %58, %57 ], [ %60, %59 ]
%63 = call i32 @memcpy(i8* %52, i8* %53, i64 %62)
br label %64
64: ; preds = %61, %47
%65 = load i8*, i8** %7
%66 = call i32 @rpfree(i8* %65)
br label %67
67: ; preds = %64, %36
br label %74
68: ; preds = %5
%69 = load i8*, i8** %7
%70 = load i64, i64* %9
%71 = load i64, i64* %10
%72 = load i32, i32* %11
%73 = call i8* @_memory_reallocate(i8* %69, i64 %70, i64 %71, i32 %72)
store i8* %73, i8** %12
br label %74
74: ; preds = %68, %67
%75 = load i8*, i8** %12
store i8* %75, i8** %6
br label %76
76: ; preds = %74, %34
%77 = load i8*, i8** %6
ret i8* %77
}

LLVM IR (-O2):
define i8* @rpaligned_realloc(i8* %0, i64 %1, i64 %2, i64 %3, i32 %4) {
%6 = icmp ugt i64 %1, 32
br i1 %6, label %7, label %32
7: ; preds = %5
%8 = tail call i64 @_memory_usable_size(i8* %0)
%9 = icmp ult i64 %8, %2
%10 = lshr i64 %8, 1
%11 = icmp ugt i64 %10, %2
%12 = or i1 %9, %11
br i1 %12, label %18, label %13
13: ; preds = %7
%14 = ptrtoint i8* %0 to i64
%15 = add i64 %1, -1
%16 = and i64 %15, %14
%17 = icmp eq i64 %16, 0
br i1 %17, label %34, label %18
18: ; preds = %13, %7
%19 = tail call i8* @rpaligned_alloc(i64 %1, i64 %2)
%20 = icmp eq i8* %0, null
br i1 %20, label %34, label %21
21: ; preds = %18
%22 = icmp eq i64 %3, 0
%23 = select i1 %22, i64 %8, i64 %3
%24 = and i32 %4, 1
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = icmp ult i64 %23, %2
%28 = select i1 %27, i64 %23, i64 %2
%29 = tail call i32 @memcpy(i8* %19, i8* %0, i64 %28)
br label %30
30: ; preds = %26, %21
%31 = tail call i32 @rpfree(i8* %0)
br label %34
32: ; preds = %5
%33 = tail call i8* @_memory_reallocate(i8* %0, i64 %2, i64 %3, i32 %4)
br label %34
34: ; preds = %18, %30, %32, %13
%35 = phi i8* [ %0, %13 ], [ %33, %32 ], [ %19, %30 ], [ %19, %18 ]
ret i8* %35
}