double median_double_array(const double array[], int laenge) {
  double median = 0.0;
  if (laenge < 1)
    median = 0.0;
  else {
    if (laenge % 2) {
      median = array[(laenge - 1) / 2];
    } else {
      median = (array[(laenge / 2) - 1] + array[(laenge / 2)]) / 2.0;
    }
  }
  return median;
}


x86 assembly (clang, -O0):
<median_double_array>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
cmpl   $0x1,-0xc(%rbp)
jge    112a <median_double_array+0x2a>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    11c7 <median_double_array+0xc7>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     116c <median_double_array+0x6c>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    11c2 <median_double_array+0xc2>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%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    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
addsd  (%rax,%rcx,8),%xmm0
movsd  0xe47(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    11c7 <median_double_array+0xc7>
movsd  -0x18(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<median_double_array>:
test   %esi,%esi
jle    1120 <median_double_array+0x20>
test   $0x1,%sil
jne    1124 <median_double_array+0x24>
shr    %esi
movsd  -0x8(%rdi,%rsi,8),%xmm0
addsd  (%rdi,%rsi,8),%xmm0
mulsd  0xee1(%rip),%xmm0
ret
xorps  %xmm0,%xmm0
ret
add    $0xffffffff,%esi
shr    %esi
movsd  (%rdi,%rsi,8),%xmm0
ret

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

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