#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 */
int /*<<< orphan*/ bsort(double *, int);
int floor(int);
double median(double *x, int n) {
  int i;
  double ret;
  bsort(x, n);
  i = (int)floor(n / 2.0);
  if (n / 2.0 == floor(n / 2.0)) {
    ret = (x[i] + x[i - 1]) / 2.0;
  } else {
    ret = x[i];
  }
  return (ret);
}


x86 assembly (clang, -O0):
<median>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <bsort@plt>
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  0xeb8(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1040 <floor@plt>
mov    %eax,-0x10(%rbp)
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  0xe9b(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  0xe85(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1040 <floor@plt>
movsd  -0x20(%rbp),%xmm0
cvtsi2sd %eax,%xmm1
ucomisd %xmm1,%xmm0
jne    11d6 <median+0xb6>
jp     11d6 <median+0xb6>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
addsd  (%rax,%rcx,8),%xmm0
movsd  0xe38(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    11e8 <median+0xc8>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<median>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <bsort@plt>
cvtsi2sd %ebp,%xmm0
mulsd  0xec2(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
cvttsd2si %xmm0,%ebp
mov    %ebp,%edi
call   1040 <floor@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1040 <floor@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  0x8(%rsp),%xmm1
ucomisd %xmm0,%xmm1
movslq %r14d,%rax
movsd  (%rbx,%rax,8),%xmm0
jne    1184 <median+0x64>
jp     1184 <median+0x64>
addsd  -0x8(%rbx,%rax,8),%xmm0
mulsd  0xe7c(%rip),%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define double @median(double* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
store double* %0, double** %3
store i32 %1, i32* %4
%7 = load double*, double** %3
%8 = load i32, i32* %4
%9 = call i32 @bsort(double* %7, i32 %8)
%10 = load i32, i32* %4
%11 = sitofp i32 %10 to double
%12 = fdiv double %11, 2.000000e+00
%13 = fptosi double %12 to i32
%14 = call i32 @floor(i32 %13)
store i32 %14, i32* %5
%15 = load i32, i32* %4
%16 = sitofp i32 %15 to double
%17 = fdiv double %16, 2.000000e+00
%18 = load i32, i32* %4
%19 = sitofp i32 %18 to double
%20 = fdiv double %19, 2.000000e+00
%21 = fptosi double %20 to i32
%22 = call i32 @floor(i32 %21)
%23 = sitofp i32 %22 to double
%24 = fcmp oeq double %17, %23
br i1 %24, label %25, label %39
25: ; preds = %2
%26 = load double*, double** %3
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds double, double* %26, i64 %28
%30 = load double, double* %29
%31 = load double*, double** %3
%32 = load i32, i32* %5
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds double, double* %31, i64 %34
%36 = load double, double* %35
%37 = fadd double %30, %36
%38 = fdiv double %37, 2.000000e+00
store double %38, double* %6
br label %45
39: ; preds = %2
%40 = load double*, double** %3
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds double, double* %40, i64 %42
%44 = load double, double* %43
store double %44, double* %6
br label %45
45: ; preds = %39, %25
%46 = load double, double* %6
ret double %46
}

LLVM IR (-O2):
define double @median(double* %0, i32 %1) {
%3 = tail call i32 @bsort(double* %0, i32 %1)
%4 = sitofp i32 %1 to double
%5 = fmul double %4, 5.000000e-01
%6 = fptosi double %5 to i32
%7 = tail call i32 @floor(i32 %6)
%8 = tail call i32 @floor(i32 %6)
%9 = sitofp i32 %8 to double
%10 = fcmp oeq double %5, %9
%11 = sext i32 %7 to i64
%12 = getelementptr inbounds double, double* %0, i64 %11
%13 = load double, double* %12
br i1 %10, label %14, label %21
14: ; preds = %2
%15 = add nsw i32 %7, -1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds double, double* %0, i64 %16
%18 = load double, double* %17
%19 = fadd double %13, %18
%20 = fmul double %19, 5.000000e-01
br label %21
21: ; preds = %2, %14
%22 = phi double [ %20, %14 ], [ %13, %2 ]
ret double %22
}