#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 */
double CSPL_Array_quickselect(unsigned long, double *, int /*<<< orphan*/,
                              long const);
double CSPL_Stats_mean(double *, int);
double CSPL_Stats_median_quickselect(double *inval, const long n) {
  double ans[2];
  unsigned long index;
  double outval;
  index = n / 2;
  if (0 == n % 2) {
    ans[0] = CSPL_Array_quickselect(index, inval, 0, n - 1);
    index--;
    ans[1] = CSPL_Array_quickselect(index, inval, 0, n - 1);
    outval = CSPL_Stats_mean(ans, 2);
  } else {
    outval = CSPL_Array_quickselect(index, inval, 0, n - 1);
  }
  return (outval);
}


x86 assembly (clang, -O0):
<CSPL_Stats_median_quickselect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
xor    %eax,%eax
cmp    %rdx,%rax
jne    11b7 <CSPL_Stats_median_quickselect+0x97>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
xor    %edx,%edx
call   1030 <CSPL_Array_quickselect@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
xor    %edx,%edx
call   1030 <CSPL_Array_quickselect@plt>
movsd  %xmm0,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x2,%esi
call   1040 <CSPL_Stats_mean@plt>
movsd  %xmm0,-0x30(%rbp)
jmp    11d3 <CSPL_Stats_median_quickselect+0xb3>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
xor    %edx,%edx
call   1030 <CSPL_Array_quickselect@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CSPL_Stats_median_quickselect>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsi,%rbx
shr    $0x3f,%rbx
add    %rsi,%rbx
sar    %rbx
mov    %rsi,%r15
add    $0xffffffffffffffff,%r15
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
mov    %r15,%rcx
call   1030 <CSPL_Array_quickselect@plt>
test   $0x1,%r12b
jne    1187 <CSPL_Stats_median_quickselect+0x67>
movsd  %xmm0,(%rsp)
add    $0xffffffffffffffff,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
mov    %r15,%rcx
call   1030 <CSPL_Array_quickselect@plt>
movsd  %xmm0,0x8(%rsp)
mov    %rsp,%rdi
mov    $0x2,%esi
call   1040 <CSPL_Stats_mean@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @CSPL_Stats_median_quickselect(double* %0, i64 %1) {
%3 = alloca double*
%4 = alloca i64
%5 = alloca [2 x double]
%6 = alloca i64
%7 = alloca double
store double* %0, double** %3
store i64 %1, i64* %4
%8 = load i64, i64* %4
%9 = sdiv i64 %8, 2
store i64 %9, i64* %6
%10 = load i64, i64* %4
%11 = srem i64 %10, 2
%12 = icmp eq i64 0, %11
br i1 %12, label %13, label %30
13: ; preds = %2
%14 = load i64, i64* %6
%15 = load double*, double** %3
%16 = load i64, i64* %4
%17 = sub nsw i64 %16, 1
%18 = call double @CSPL_Array_quickselect(i64 %14, double* %15, i32 0, i64 %17)
%19 = getelementptr inbounds [2 x double], [2 x double]* %5, i64 0, i64 0
store double %18, double* %19
%20 = load i64, i64* %6
%21 = add i64 %20, -1
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = load double*, double** %3
%24 = load i64, i64* %4
%25 = sub nsw i64 %24, 1
%26 = call double @CSPL_Array_quickselect(i64 %22, double* %23, i32 0, i64 %25)
%27 = getelementptr inbounds [2 x double], [2 x double]* %5, i64 0, i64 1
store double %26, double* %27
%28 = getelementptr inbounds [2 x double], [2 x double]* %5, i64 0, i64 0
%29 = call double @CSPL_Stats_mean(double* %28, i32 2)
store double %29, double* %7
br label %36
30: ; preds = %2
%31 = load i64, i64* %6
%32 = load double*, double** %3
%33 = load i64, i64* %4
%34 = sub nsw i64 %33, 1
%35 = call double @CSPL_Array_quickselect(i64 %31, double* %32, i32 0, i64 %34)
store double %35, double* %7
br label %36
36: ; preds = %30, %13
%37 = load double, double* %7
ret double %37
}

LLVM IR (-O2):
define double @CSPL_Stats_median_quickselect(double* %0, i64 %1) {
%3 = alloca [2 x double]
%4 = bitcast [2 x double]* %3 to i8*
%5 = sdiv i64 %1, 2
%6 = and i64 %1, 1
%7 = icmp eq i64 %6, 0
%8 = add nsw i64 %1, -1
%9 = tail call double @CSPL_Array_quickselect(i64 %5, double* %0, i32 0, i64 %8)
br i1 %7, label %10, label %16
10: ; preds = %2
%11 = getelementptr inbounds [2 x double], [2 x double]* %3, i64 0, i64 0
store double %9, double* %11
%12 = add nsw i64 %5, -1
%13 = tail call double @CSPL_Array_quickselect(i64 %12, double* %0, i32 0, i64 %8)
%14 = getelementptr inbounds [2 x double], [2 x double]* %3, i64 0, i64 1
store double %13, double* %14
%15 = call double @CSPL_Stats_mean(double* %11, i32 2)
br label %16
16: ; preds = %2, %10
%17 = phi double [ %15, %10 ], [ %9, %2 ]
ret double %17
}