#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 */
typedef int /*<<< orphan*/ __m256;

/* Variables and functions */
int /*<<< orphan*/ bubble_sort_m256(int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ merge_m256(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              size_t, size_t);
void mergesort_m256(__m256 *to_sort, size_t count, __m256 *scratch) {
  size_t part_size = 32;
  size_t remainder = count % part_size;
  bubble_sort_m256(to_sort, remainder);
  for (size_t i = remainder; i < count; i += part_size) {
    bubble_sort_m256(to_sort + i, part_size);
  }
  __m256 *dest = scratch;
  __m256 *source = to_sort;
  for (; part_size < count; part_size *= 2) {
    size_t l_remainder = remainder;
    remainder = count % (2 * part_size);
    size_t r_remainder = remainder - l_remainder;
    merge_m256(dest, source, l_remainder, r_remainder);
    for (size_t i = remainder; i < count; i += 2 * part_size) {
      merge_m256(dest + i, source + i, part_size, part_size);
    }
    scratch = dest;
    dest = source;
    source = scratch;
  }
  if (source != to_sort) {
    memcpy(to_sort, source, count * sizeof(__m256));
  }
}


x86 assembly (clang, -O0):
<mergesort_m256>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x20,-0x20(%rbp)
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
mov    %rdx,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <bubble_sort_m256@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11a8 <mergesort_m256+0x78>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
call   1030 <bubble_sort_m256@plt>
mov    -0x20(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1171 <mergesort_m256+0x41>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1283 <mergesort_m256+0x153>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x1,%rcx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x28(%rbp)
mov    -0x28(%rbp),%rax
sub    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rcx
call   1050 <merge_m256@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    125a <mergesort_m256+0x12a>
mov    -0x38(%rbp),%rdi
mov    -0x58(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x40(%rbp),%rsi
mov    -0x58(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1050 <merge_m256@plt>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
add    -0x58(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    120c <mergesort_m256+0xdc>
mov    -0x38(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11b8 <mergesort_m256+0x88>
mov    -0x40(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     12a6 <mergesort_m256+0x176>
mov    -0x8(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x10(%rbp),%rdx
shl    $0x2,%rdx
call   1040 <memcpy@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergesort_m256>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%r14
mov    %r13d,%r12d
and    $0x1f,%r12d
mov    %r12,%rsi
call   1030 <bubble_sort_m256@plt>
cmp    $0x20,%r13
jb     1267 <mergesort_m256+0x137>
lea    (%r14,%r12,4),%rbx
mov    %r12,%rbp
nopw   0x0(%rax,%rax,1)
mov    $0x20,%esi
mov    %rbx,%rdi
call   1030 <bubble_sort_m256@plt>
add    $0x20,%rbp
sub    $0xffffffffffffff80,%rbx
cmp    %r13,%rbp
jb     1170 <mergesort_m256+0x40>
cmp    $0x21,%r13
jb     1267 <mergesort_m256+0x137>
mov    %r14,%rax
mov    $0x20,%r14d
mov    %rax,0x10(%rsp)
jmp    11c1 <mergesort_m256+0x91>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rax
cmp    %r13,%r14
mov    0x8(%rsp),%r12
jae    1240 <mergesort_m256+0x110>
mov    %r12,%r8
mov    %r15,%rbx
mov    %rax,%r15
mov    %r14,%rbp
add    %r14,%r14
mov    %r13,%rax
or     %r14,%rax
shr    $0x20,%rax
je     11f0 <mergesort_m256+0xc0>
mov    %r13,%rax
xor    %edx,%edx
div    %r14
mov    %rdx,%r12
jmp    11fb <mergesort_m256+0xcb>
nopl   0x0(%rax)
mov    %r13d,%eax
xor    %edx,%edx
div    %r14d
mov    %edx,%r12d
mov    %r12,%rcx
sub    %r8,%rcx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r8,%rdx
call   1050 <merge_m256@plt>
mov    %r12,0x8(%rsp)
cmp    %r13,%r12
jae    11b0 <mergesort_m256+0x80>
mov    0x8(%rsp),%r12
xchg   %ax,%ax
lea    (%rbx,%r12,4),%rdi
lea    (%r15,%r12,4),%rsi
mov    %rbp,%rdx
mov    %rbp,%rcx
call   1050 <merge_m256@plt>
add    %r14,%r12
cmp    %r13,%r12
jb     1220 <mergesort_m256+0xf0>
jmp    11b0 <mergesort_m256+0x80>
mov    0x10(%rsp),%rdi
cmp    %rdi,%rbx
je     1267 <mergesort_m256+0x137>
shl    $0x2,%r13
mov    %rbx,%rsi
mov    %r13,%rdx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <memcpy@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @mergesort_m256(i32* %0, i64 %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
store i32* %0, i32** %4
store i64 %1, i64* %5
store i32* %2, i32** %6
store i64 32, i64* %7
%15 = load i64, i64* %5
%16 = load i64, i64* %7
%17 = urem i64 %15, %16
store i64 %17, i64* %8
%18 = load i32*, i32** %4
%19 = load i64, i64* %8
%20 = call i32 @bubble_sort_m256(i32* %18, i64 %19)
%21 = load i64, i64* %8
store i64 %21, i64* %9
br label %22
22: ; preds = %32, %3
%23 = load i64, i64* %9
%24 = load i64, i64* %5
%25 = icmp ult i64 %23, %24
br i1 %25, label %26, label %36
26: ; preds = %22
%27 = load i32*, i32** %4
%28 = load i64, i64* %9
%29 = getelementptr inbounds i32, i32* %27, i64 %28
%30 = load i64, i64* %7
%31 = call i32 @bubble_sort_m256(i32* %29, i64 %30)
br label %32
32: ; preds = %26
%33 = load i64, i64* %7
%34 = load i64, i64* %9
%35 = add i64 %34, %33
store i64 %35, i64* %9
br label %22
36: ; preds = %22
%37 = load i32*, i32** %6
store i32* %37, i32** %10
%38 = load i32*, i32** %4
store i32* %38, i32** %11
br label %39
39: ; preds = %81, %36
%40 = load i64, i64* %7
%41 = load i64, i64* %5
%42 = icmp ult i64 %40, %41
br i1 %42, label %43, label %84
43: ; preds = %39
%44 = load i64, i64* %8
store i64 %44, i64* %12
%45 = load i64, i64* %5
%46 = load i64, i64* %7
%47 = mul i64 2, %46
%48 = urem i64 %45, %47
store i64 %48, i64* %8
%49 = load i64, i64* %8
%50 = load i64, i64* %12
%51 = sub i64 %49, %50
store i64 %51, i64* %13
%52 = load i32*, i32** %10
%53 = load i32*, i32** %11
%54 = load i64, i64* %12
%55 = load i64, i64* %13
%56 = call i32 @merge_m256(i32* %52, i32* %53, i64 %54, i64 %55)
%57 = load i64, i64* %8
store i64 %57, i64* %14
br label %58
58: ; preds = %72, %43
%59 = load i64, i64* %14
%60 = load i64, i64* %5
%61 = icmp ult i64 %59, %60
br i1 %61, label %62, label %77
62: ; preds = %58
%63 = load i32*, i32** %10
%64 = load i64, i64* %14
%65 = getelementptr inbounds i32, i32* %63, i64 %64
%66 = load i32*, i32** %11
%67 = load i64, i64* %14
%68 = getelementptr inbounds i32, i32* %66, i64 %67
%69 = load i64, i64* %7
%70 = load i64, i64* %7
%71 = call i32 @merge_m256(i32* %65, i32* %68, i64 %69, i64 %70)
br label %72
72: ; preds = %62
%73 = load i64, i64* %7
%74 = mul i64 2, %73
%75 = load i64, i64* %14
%76 = add i64 %75, %74
store i64 %76, i64* %14
br label %58
77: ; preds = %58
%78 = load i32*, i32** %10
store i32* %78, i32** %6
%79 = load i32*, i32** %11
store i32* %79, i32** %10
%80 = load i32*, i32** %6
store i32* %80, i32** %11
br label %81
81: ; preds = %77
%82 = load i64, i64* %7
%83 = mul i64 %82, 2
store i64 %83, i64* %7
br label %39
84: ; preds = %39
%85 = load i32*, i32** %11
%86 = load i32*, i32** %4
%87 = icmp ne i32* %85, %86
br i1 %87, label %88, label %94
88: ; preds = %84
%89 = load i32*, i32** %4
%90 = load i32*, i32** %11
%91 = load i64, i64* %5
%92 = mul i64 %91, 4
%93 = call i32 @memcpy(i32* %89, i32* %90, i64 %92)
br label %94
94: ; preds = %88, %84
ret void
}

LLVM IR (-O2):
define void @mergesort_m256(i32* %0, i64 %1, i32* %2) {
%4 = and i64 %1, 31
%5 = tail call i32 @bubble_sort_m256(i32* %0, i64 %4)
%6 = icmp ugt i64 %1, 31
br i1 %6, label %9, label %39
7: ; preds = %9
%8 = icmp ugt i64 %1, 32
br i1 %8, label %17, label %39
9: ; preds = %3, %9
%10 = phi i64 [ %13, %9 ], [ %4, %3 ]
%11 = getelementptr inbounds i32, i32* %0, i64 %10
%12 = tail call i32 @bubble_sort_m256(i32* %11, i64 32)
%13 = add i64 %10, 32
%14 = icmp ult i64 %13, %1
br i1 %14, label %9, label %7
15: ; preds = %27, %17
%16 = icmp ult i64 %22, %1
br i1 %16, label %17, label %34
17: ; preds = %7, %15
%18 = phi i64 [ %22, %15 ], [ 32, %7 ]
%19 = phi i32* [ %20, %15 ], [ %0, %7 ]
%20 = phi i32* [ %19, %15 ], [ %2, %7 ]
%21 = phi i64 [ %23, %15 ], [ %4, %7 ]
%22 = shl i64 %18, 1
%23 = urem i64 %1, %22
%24 = sub i64 %23, %21
%25 = tail call i32 @merge_m256(i32* %20, i32* %19, i64 %21, i64 %24)
%26 = icmp ult i64 %23, %1
br i1 %26, label %27, label %15
27: ; preds = %17, %27
%28 = phi i64 [ %32, %27 ], [ %23, %17 ]
%29 = getelementptr inbounds i32, i32* %20, i64 %28
%30 = getelementptr inbounds i32, i32* %19, i64 %28
%31 = tail call i32 @merge_m256(i32* %29, i32* %30, i64 %18, i64 %18)
%32 = add i64 %28, %22
%33 = icmp ult i64 %32, %1
br i1 %33, label %27, label %15
34: ; preds = %15
%35 = icmp eq i32* %20, %0
br i1 %35, label %39, label %36
36: ; preds = %34
%37 = shl i64 %1, 2
%38 = tail call i32 @memcpy(i32* %0, i32* %20, i64 %37)
br label %39
39: ; preds = %3, %7, %36, %34
ret void
}