double calcMedian(double *array, int count) {
  if (count & 1)
    return array[count / 2];
  return (array[count / 2 - 1] + array[count / 2]) / 2.0;
}


x86 assembly (clang, -O0):
<calcMedian>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1145 <calcMedian+0x45>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    119b <calcMedian+0x9b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
addsd  (%rax,%rcx,8),%xmm0
movsd  0xe6e(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<calcMedian>:
mov    %esi,%eax
shr    $0x1f,%eax
add    %esi,%eax
sar    %eax
cltq
test   $0x1,%sil
jne    1125 <calcMedian+0x25>
movsd  -0x8(%rdi,%rax,8),%xmm0
addsd  (%rdi,%rax,8),%xmm0
mulsd  0xedc(%rip),%xmm0
ret
movsd  (%rdi,%rax,8),%xmm0
ret

LLVM IR (-O0):
define double @calcMedian(double* %0, i32 %1) {
%3 = alloca double
%4 = alloca double*
%5 = alloca i32
store double* %0, double** %4
store i32 %1, i32* %5
%6 = load i32, i32* %5
%7 = and i32 %6, 1
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %2
%10 = load double*, double** %4
%11 = load i32, i32* %5
%12 = sdiv i32 %11, 2
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds double, double* %10, i64 %13
%15 = load double, double* %14
store double %15, double* %3
br label %32
16: ; preds = %2
%17 = load double*, double** %4
%18 = load i32, i32* %5
%19 = sdiv i32 %18, 2
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds double, double* %17, i64 %21
%23 = load double, double* %22
%24 = load double*, double** %4
%25 = load i32, i32* %5
%26 = sdiv i32 %25, 2
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds double, double* %24, i64 %27
%29 = load double, double* %28
%30 = fadd double %23, %29
%31 = fdiv double %30, 2.000000e+00
store double %31, double* %3
br label %32
32: ; preds = %16, %9
%33 = load double, double* %3
ret double %33
}

LLVM IR (-O2):
define double @calcMedian(double* nocapture %0, i32 %1) {
%3 = and i32 %1, 1
%4 = icmp eq i32 %3, 0
%5 = sdiv i32 %1, 2
br i1 %4, label %10, label %6
6: ; preds = %2
%7 = sext i32 %5 to i64
%8 = getelementptr inbounds double, double* %0, i64 %7
%9 = load double, double* %8
br label %20
10: ; preds = %2
%11 = add nsw i32 %5, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds double, double* %0, i64 %12
%14 = load double, double* %13
%15 = sext i32 %5 to i64
%16 = getelementptr inbounds double, double* %0, i64 %15
%17 = load double, double* %16
%18 = fadd double %14, %17
%19 = fmul double %18, 5.000000e-01
br label %20
20: ; preds = %10, %6
%21 = phi double [ %9, %6 ], [ %19, %10 ]
ret double %21
}