#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 *_aligned_malloc(size_t, size_t);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *realloc(void *, size_t);
void *_aligned_realloc(void *p, size_t size, size_t alignment) {
  if ((alignment == 0) || ((alignment & (alignment - 1)) != 0) ||
      (alignment < sizeof(void *))) {
    return NULL;
  }
  if (size == 0) {
    return NULL;
  }
  void *rp = realloc(p, size);
  if (((size_t)rp & (alignment - 1)) != 0) {
    void *p1 = _aligned_malloc(size, alignment);
    if (p1 != NULL) {
      memcpy(p1, rp, size);
    }
    free(rp);
    rp = p1;
  }
  return rp;
}


x86 assembly (clang, -O0):
<_aligned_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1183 <_aligned_realloc+0x43>
mov    -0x20(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
jne    1183 <_aligned_realloc+0x43>
cmpq   $0x8,-0x20(%rbp)
jae    1190 <_aligned_realloc+0x50>
movq   $0x0,-0x8(%rbp)
jmp    1218 <_aligned_realloc+0xd8>
cmpq   $0x0,-0x18(%rbp)
jne    11a8 <_aligned_realloc+0x68>
movq   $0x0,-0x8(%rbp)
jmp    1218 <_aligned_realloc+0xd8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
je     1210 <_aligned_realloc+0xd0>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <_aligned_malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11ff <_aligned_realloc+0xbf>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_aligned_realloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     120d <_aligned_realloc+0xcd>
mov    %rsi,%r14
xor    %r15d,%r15d
test   %rsi,%rsi
je     1215 <_aligned_realloc+0xd5>
mov    %rdx,%rbx
cmp    $0x8,%rdx
jb     1215 <_aligned_realloc+0xd5>
mov    %rbx,%rax
shr    %rax
movabs $0x5555555555555555,%rcx
55 55 55
and    %rax,%rcx
mov    %rbx,%rax
sub    %rcx,%rax
movabs $0x3333333333333333,%rcx
33 33 33
mov    %rax,%rdx
and    %rcx,%rdx
shr    $0x2,%rax
and    %rcx,%rax
add    %rdx,%rax
mov    %rax,%rcx
shr    $0x4,%rcx
add    %rax,%rcx
movabs $0xf0f0f0f0f0f0f0f,%rax
0f 0f 0f
and    %rcx,%rax
movabs $0x101010101010101,%rcx
01 01 01
imul   %rax,%rcx
shr    $0x38,%rcx
cmp    $0x1,%ecx
ja     1215 <_aligned_realloc+0xd5>
lea    -0x1(%rbx),%r15
mov    %r14,%rsi
call   1060 <realloc@plt>
mov    %rax,%r12
test   %rax,%r15
je     1212 <_aligned_realloc+0xd2>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <_aligned_malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1203 <_aligned_realloc+0xc3>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1050 <memcpy@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1215 <_aligned_realloc+0xd5>
xor    %r15d,%r15d
jmp    1215 <_aligned_realloc+0xd5>
mov    %r12,%r15
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @_aligned_realloc(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
%11 = icmp eq i64 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %3
%13 = load i64, i64* %7
%14 = load i64, i64* %7
%15 = sub i64 %14, 1
%16 = and i64 %13, %15
%17 = icmp ne i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = load i64, i64* %7
%20 = icmp ult i64 %19, 8
br i1 %20, label %21, label %22
21: ; preds = %18, %12, %3
store i8* null, i8** %4
br label %53
22: ; preds = %18
%23 = load i64, i64* %6
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %22
store i8* null, i8** %4
br label %53
26: ; preds = %22
%27 = load i8*, i8** %5
%28 = load i64, i64* %6
%29 = call i8* @realloc(i8* %27, i64 %28)
store i8* %29, i8** %8
%30 = load i8*, i8** %8
%31 = ptrtoint i8* %30 to i64
%32 = load i64, i64* %7
%33 = sub i64 %32, 1
%34 = and i64 %31, %33
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %51
36: ; preds = %26
%37 = load i64, i64* %6
%38 = load i64, i64* %7
%39 = call i8* @_aligned_malloc(i64 %37, i64 %38)
store i8* %39, i8** %9
%40 = load i8*, i8** %9
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %47
42: ; preds = %36
%43 = load i8*, i8** %9
%44 = load i8*, i8** %8
%45 = load i64, i64* %6
%46 = call i32 @memcpy(i8* %43, i8* %44, i64 %45)
br label %47
47: ; preds = %42, %36
%48 = load i8*, i8** %8
%49 = call i32 @free(i8* %48)
%50 = load i8*, i8** %9
store i8* %50, i8** %8
br label %51
51: ; preds = %47, %26
%52 = load i8*, i8** %8
store i8* %52, i8** %4
br label %53
53: ; preds = %51, %25, %21
%54 = load i8*, i8** %4
ret i8* %54
}

LLVM IR (-O2):
define i8* @_aligned_realloc(i8* nocapture %0, i64 %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %25, label %5
5: ; preds = %3
%6 = tail call i64 @llvm.ctpop.i64(i64 %2)
%7 = icmp ugt i64 %6, 1
%8 = icmp ult i64 %2, 8
%9 = or i1 %8, %7
%10 = icmp eq i64 %1, 0
%11 = or i1 %10, %9
br i1 %11, label %25, label %12
12: ; preds = %5
%13 = add i64 %2, -1
%14 = tail call i8* @realloc(i8* %0, i64 %1)
%15 = ptrtoint i8* %14 to i64
%16 = and i64 %13, %15
%17 = icmp eq i64 %16, 0
br i1 %17, label %25, label %18
18: ; preds = %12
%19 = tail call i8* @_aligned_malloc(i64 %1, i64 %2)
%20 = icmp eq i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 @memcpy(i8* %19, i8* %14, i64 %1)
br label %23
23: ; preds = %21, %18
%24 = tail call i32 @free(i8* %14)
br label %25
25: ; preds = %12, %23, %3, %5
%26 = phi i8* [ null, %5 ], [ null, %3 ], [ %19, %23 ], [ %14, %12 ]
ret i8* %26
}