#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 kth_smallest(double *, int, int);
double wirth_median(double a[], int n) {
  if (n % 2 == 0) {
    return 0.5 * (kth_smallest(a, n, n / 2) + kth_smallest(a, n, n / 2 - 1));
  } else {
    return kth_smallest(a, n, (n - 1) / 2);
  }
}


x86 assembly (clang, -O0):
<wirth_median>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    118c <wirth_median+0x7c>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
call   1030 <kth_smallest@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
sub    $0x1,%edx
call   1030 <kth_smallest@plt>
movsd  -0x20(%rbp),%xmm1
addsd  %xmm0,%xmm1
movsd  0xe82(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11ad <wirth_median+0x9d>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
call   1030 <kth_smallest@plt>
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wirth_median>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
test   $0x1,%bl
jne    1163 <wirth_median+0x53>
mov    %ebx,%ebp
shr    $0x1f,%ebp
add    %ebx,%ebp
sar    %ebp
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1030 <kth_smallest@plt>
movsd  %xmm0,0x8(%rsp)
add    $0xffffffff,%ebp
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1030 <kth_smallest@plt>
addsd  0x8(%rsp),%xmm0
mulsd  0xea6(%rip),%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    -0x1(%rbx),%eax
shr    $0x1f,%eax
lea    (%rbx,%rax,1),%edx
add    $0xffffffff,%edx
sar    %edx
mov    %r14,%rdi
mov    %ebx,%esi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <kth_smallest@plt>

LLVM IR (-O0):
define double @wirth_median(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 = srem i32 %6, 2
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %23
9: ; preds = %2
%10 = load double*, double** %4
%11 = load i32, i32* %5
%12 = load i32, i32* %5
%13 = sdiv i32 %12, 2
%14 = call double @kth_smallest(double* %10, i32 %11, i32 %13)
%15 = load double*, double** %4
%16 = load i32, i32* %5
%17 = load i32, i32* %5
%18 = sdiv i32 %17, 2
%19 = sub nsw i32 %18, 1
%20 = call double @kth_smallest(double* %15, i32 %16, i32 %19)
%21 = fadd double %14, %20
%22 = fmul double 5.000000e-01, %21
store double %22, double* %3
br label %30
23: ; preds = %2
%24 = load double*, double** %4
%25 = load i32, i32* %5
%26 = load i32, i32* %5
%27 = sub nsw i32 %26, 1
%28 = sdiv i32 %27, 2
%29 = call double @kth_smallest(double* %24, i32 %25, i32 %28)
store double %29, double* %3
br label %30
30: ; preds = %23, %9
%31 = load double, double* %3
ret double %31
}

LLVM IR (-O2):
define double @wirth_median(double* %0, i32 %1) {
%3 = and i32 %1, 1
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = sdiv i32 %1, 2
%7 = tail call double @kth_smallest(double* %0, i32 %1, i32 %6)
%8 = add nsw i32 %6, -1
%9 = tail call double @kth_smallest(double* %0, i32 %1, i32 %8)
%10 = fadd double %7, %9
%11 = fmul double %10, 5.000000e-01
br label %16
12: ; preds = %2
%13 = add nsw i32 %1, -1
%14 = sdiv i32 %13, 2
%15 = tail call double @kth_smallest(double* %0, i32 %1, i32 %14)
br label %16
16: ; preds = %12, %5
%17 = phi double [ %11, %5 ], [ %15, %12 ]
ret double %17
}