#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 scalar_t__ uint64_t;

/* Variables and functions */
scalar_t__ insertionSort(double *, size_t);
int /*<<< orphan*/ memcpy(double *, double *, size_t);
scalar_t__ merge(double *, double *, size_t, size_t);
uint64_t mergeSort(double *x, double *buf, size_t len) {
  uint64_t swaps;
  size_t half;
  if (len < 10) {
    return insertionSort(x, len);
  }
  swaps = 0;
  if (len < 2) {
    return 0;
  }
  half = len / 2;
  swaps += mergeSort(x, buf, half);
  swaps += mergeSort(x + half, buf + half, len - half);
  swaps += merge(x, buf, half, len);
  memcpy(x, buf, len * sizeof(double));
  return swaps;
}


x86 assembly (clang, -O0):
<mergeSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0xa,-0x20(%rbp)
jae    1175 <mergeSort+0x35>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <insertionSort@plt>
mov    %rax,-0x8(%rbp)
jmp    1227 <mergeSort+0xe7>
movq   $0x0,-0x28(%rbp)
cmpq   $0x2,-0x20(%rbp)
jae    1195 <mergeSort+0x55>
movq   $0x0,-0x8(%rbp)
jmp    1227 <mergeSort+0xe7>
mov    -0x20(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <mergeSort@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1040 <mergeSort@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1050 <merge@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
shl    $0x3,%rdx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergeSort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rdi,%r15
cmp    $0x9,%rdx
ja     1170 <mergeSort+0x30>
mov    %r15,%rdi
mov    %r12,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <insertionSort@plt>
mov    %rsi,%r14
mov    %r12,%rbx
shr    %rbx
mov    %r15,%rdi
mov    %rbx,%rdx
call   1040 <mergeSort@plt>
mov    %rax,%r13
lea    (%r15,%rbx,8),%rdi
lea    (%r14,%rbx,8),%rsi
mov    %r12,%rdx
sub    %rbx,%rdx
call   1040 <mergeSort@plt>
mov    %rax,%rbp
add    %r13,%rbp
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1050 <merge@plt>
mov    %rax,%rbx
add    %rbp,%rbx
shl    $0x3,%r12
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1030 <memcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @mergeSort(double* %0, double* %1, i64 %2) {
%4 = alloca i64
%5 = alloca double*
%6 = alloca double*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store double* %0, double** %5
store double* %1, double** %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
%11 = icmp ult i64 %10, 10
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load double*, double** %5
%14 = load i64, i64* %7
%15 = call i64 @insertionSort(double* %13, i64 %14)
store i64 %15, i64* %4
br label %54
16: ; preds = %3
store i64 0, i64* %8
%17 = load i64, i64* %7
%18 = icmp ult i64 %17, 2
br i1 %18, label %19, label %20
19: ; preds = %16
store i64 0, i64* %4
br label %54
20: ; preds = %16
%21 = load i64, i64* %7
%22 = udiv i64 %21, 2
store i64 %22, i64* %9
%23 = load double*, double** %5
%24 = load double*, double** %6
%25 = load i64, i64* %9
%26 = call i64 @mergeSort(double* %23, double* %24, i64 %25)
%27 = load i64, i64* %8
%28 = add nsw i64 %27, %26
store i64 %28, i64* %8
%29 = load double*, double** %5
%30 = load i64, i64* %9
%31 = getelementptr inbounds double, double* %29, i64 %30
%32 = load double*, double** %6
%33 = load i64, i64* %9
%34 = getelementptr inbounds double, double* %32, i64 %33
%35 = load i64, i64* %7
%36 = load i64, i64* %9
%37 = sub i64 %35, %36
%38 = call i64 @mergeSort(double* %31, double* %34, i64 %37)
%39 = load i64, i64* %8
%40 = add nsw i64 %39, %38
store i64 %40, i64* %8
%41 = load double*, double** %5
%42 = load double*, double** %6
%43 = load i64, i64* %9
%44 = load i64, i64* %7
%45 = call i64 @merge(double* %41, double* %42, i64 %43, i64 %44)
%46 = load i64, i64* %8
%47 = add nsw i64 %46, %45
store i64 %47, i64* %8
%48 = load double*, double** %5
%49 = load double*, double** %6
%50 = load i64, i64* %7
%51 = mul i64 %50, 8
%52 = call i32 @memcpy(double* %48, double* %49, i64 %51)
%53 = load i64, i64* %8
store i64 %53, i64* %4
br label %54
54: ; preds = %20, %19, %12
%55 = load i64, i64* %4
ret i64 %55
}

LLVM IR (-O2):
define i64 @mergeSort(double* %0, double* %1, i64 %2) {
%4 = icmp ult i64 %2, 10
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = tail call i64 @insertionSort(double* %0, i64 %2)
br label %7
7: ; preds = %5, %9
%8 = phi i64 [ %18, %9 ], [ %6, %5 ]
ret i64 %8
9: ; preds = %3
%10 = lshr i64 %2, 1
%11 = tail call i64 @mergeSort(double* %0, double* %1, i64 %10)
%12 = getelementptr inbounds double, double* %0, i64 %10
%13 = getelementptr inbounds double, double* %1, i64 %10
%14 = sub i64 %2, %10
%15 = tail call i64 @mergeSort(double* %12, double* %13, i64 %14)
%16 = add nsw i64 %15, %11
%17 = tail call i64 @merge(double* %0, double* %1, i64 %10, i64 %2)
%18 = add nsw i64 %16, %17
%19 = shl i64 %2, 3
%20 = tail call i32 @memcpy(double* %0, double* %1, i64 %19)
br label %7
}