#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*/ sort_dbl(double *, int);
double median(double *x, int n) {
  sort_dbl(x, n);
  if (n % 2 == 0)
    return (x[n / 2] + x[n / 2 - 1]) / 2.;
  else
    return x[n / 2];
}


x86 assembly (clang, -O0):
<median>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <sort_dbl@plt>
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    119a <median+0x8a>
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
movsd  (%rax,%rcx,8),%xmm0
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
sub    $0x1,%ecx
movslq %ecx,%rcx
addsd  (%rax,%rcx,8),%xmm0
movsd  0xe74(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c0 <median+0xb0>
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
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<median>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <sort_dbl@plt>
mov    %ebp,%eax
shr    $0x1f,%eax
add    %ebp,%eax
sar    %eax
cltq
movsd  (%rbx,%rax,8),%xmm0
test   $0x1,%bpl
jne    1141 <median+0x31>
addsd  -0x8(%rbx,%rax,8),%xmm0
mulsd  0xebf(%rip),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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