#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 n;

/* Variables and functions */
int /*<<< orphan*/ CSPL_Sort_mergesort(double *, long);
double CSPL_Stats_mean(double *, int);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ memcpy(double *, double *, int);
double CSPL_Stats_median_sort(double *inval, long n) {
  double *incopy;
  double median;
  long index;
  double tmpvals[2];
  {
    incopy = (double *)calloc((n) - (0) + 1, sizeof(double));
    memcpy((incopy), &(inval)[(0)], sizeof(double) * ((n) - (0) + 1));
  };
  CSPL_Sort_mergesort(incopy, n);
  if (0 == n % 2) {
    index = (n / 2) - 1;
    tmpvals[0] = incopy[index];
    tmpvals[1] = incopy[index + 1];
    median = CSPL_Stats_mean(tmpvals, 2);
  } else {
    median = incopy[((n + 1) / 2) - 1];
  }
  free(incopy);
  return (median);
}


x86 assembly (clang, -O0):
<CSPL_Stats_median_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
sub    $0x0,%rax
add    $0x1,%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <CSPL_Sort_mergesort@plt>
mov    -0x10(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
xor    %eax,%eax
cmp    %rdx,%rax
jne    1214 <CSPL_Stats_median_sort+0xc4>
mov    -0x10(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsd  0x8(%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x38(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x2,%esi
call   1040 <CSPL_Stats_mean@plt>
movsd  %xmm0,-0x20(%rbp)
jmp    1243 <CSPL_Stats_median_sort+0xf3>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
sub    $0x1,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movsd  -0x20(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CSPL_Stats_median_sort>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %rdi,%r12
lea    0x1(%rsi),%r15
mov    %r15d,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
lea    0x0(,%r15,8),%edx
mov    %rax,%rdi
mov    %r12,%rsi
call   1070 <memcpy@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <CSPL_Sort_mergesort@plt>
test   $0x1,%bl
jne    11d0 <CSPL_Stats_median_sort+0x80>
mov    %rbx,%rax
shr    $0x3f,%rax
add    %rbx,%rax
and    $0xfffffffffffffffe,%rax
movsd  -0x8(%r14,%rax,4),%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  (%r14,%rax,4),%xmm0
movsd  %xmm0,0x18(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x2,%esi
call   1040 <CSPL_Stats_mean@plt>
jmp    11e5 <CSPL_Stats_median_sort+0x95>
mov    %r15,%rax
shr    $0x3f,%rax
add    %r15,%rax
and    $0xfffffffffffffffe,%rax
movsd  -0x8(%r14,%rax,4),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %r14,%rdi
call   1030 <free@plt>
movsd  0x8(%rsp),%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define double @CSPL_Stats_median_sort(double* %0, i64 %1) {
%3 = alloca [2 x double]
%4 = bitcast [2 x double]* %3 to i8*
%5 = add nsw i64 %1, 1
%6 = trunc i64 %5 to i32
%7 = tail call i64 @calloc(i32 %6, i32 8)
%8 = inttoptr i64 %7 to double*
%9 = shl i32 %6, 3
%10 = tail call i32 @memcpy(double* %8, double* %0, i32 %9)
%11 = tail call i32 @CSPL_Sort_mergesort(double* %8, i64 %1)
%12 = and i64 %1, 1
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %2
%15 = sdiv i64 %1, 2
%16 = add nsw i64 %15, -1
%17 = getelementptr inbounds double, double* %8, i64 %16
%18 = load double, double* %17
%19 = getelementptr inbounds [2 x double], [2 x double]* %3, i64 0, i64 0
store double %18, double* %19
%20 = getelementptr inbounds double, double* %8, i64 %15
%21 = load double, double* %20
%22 = getelementptr inbounds [2 x double], [2 x double]* %3, i64 0, i64 1
store double %21, double* %22
%23 = call double @CSPL_Stats_mean(double* %19, i32 2)
br label %29
24: ; preds = %2
%25 = sdiv i64 %5, 2
%26 = add nsw i64 %25, -1
%27 = getelementptr inbounds double, double* %8, i64 %26
%28 = load double, double* %27
br label %29
29: ; preds = %24, %14
%30 = phi double [ %23, %14 ], [ %28, %24 ]
%31 = call i32 @free(double* %8)
ret double %30
}