#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*/ free(double *);
double *malloc(size_t);
int /*<<< orphan*/ memcpy(double *, double *, size_t);
int /*<<< orphan*/ quicksort(double *, int /*<<< orphan*/, size_t);
double arr_median(double x[], size_t N) {
  double median;
  double *y = malloc(N * sizeof(double));
  memcpy(y, x, N * sizeof(double));
  quicksort(y, 0, N - 1);
  if (N % 2 == 0) {
    median = (y[N / 2] + y[N / 2 - 1]) / 2.0;
  } else {
    median = y[N / 2];
  }
  free(y);
  return median;
}


x86 assembly (clang, -O0):
<arr_median>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
shl    $0x3,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
shl    $0x3,%rdx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
sub    $0x1,%rdx
xor    %esi,%esi
call   1040 <quicksort@plt>
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
jne    11d7 <arr_median+0x97>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
shr    $0x1,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
shr    $0x1,%rcx
sub    $0x1,%rcx
addsd  (%rax,%rcx,8),%xmm0
movsd  0xe37(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    11ed <arr_median+0xad>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
shr    $0x1,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movsd  -0x18(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<arr_median>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x0(,%rsi,8),%r12
mov    %r12,%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1050 <memcpy@plt>
lea    -0x1(%rbx),%rdx
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <quicksort@plt>
mov    %rbx,%rax
shr    %rax
movsd  (%r14,%rax,8),%xmm0
test   $0x1,%bl
jne    119d <arr_median+0x5d>
addsd  -0x8(%r14,%rax,8),%xmm0
mulsd  0xe63(%rip),%xmm0
movsd  %xmm0,(%rsp)
mov    %r14,%rdi
call   1030 <free@plt>
movsd  (%rsp),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @arr_median(double* %0, i64 %1) {
%3 = alloca double*
%4 = alloca i64
%5 = alloca double
%6 = alloca double*
store double* %0, double** %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
%8 = mul i64 %7, 8
%9 = call double* @malloc(i64 %8)
store double* %9, double** %6
%10 = load double*, double** %6
%11 = load double*, double** %3
%12 = load i64, i64* %4
%13 = mul i64 %12, 8
%14 = call i32 @memcpy(double* %10, double* %11, i64 %13)
%15 = load double*, double** %6
%16 = load i64, i64* %4
%17 = sub i64 %16, 1
%18 = call i32 @quicksort(double* %15, i32 0, i64 %17)
%19 = load i64, i64* %4
%20 = urem i64 %19, 2
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %2
%23 = load double*, double** %6
%24 = load i64, i64* %4
%25 = udiv i64 %24, 2
%26 = getelementptr inbounds double, double* %23, i64 %25
%27 = load double, double* %26
%28 = load double*, double** %6
%29 = load i64, i64* %4
%30 = udiv i64 %29, 2
%31 = sub i64 %30, 1
%32 = getelementptr inbounds double, double* %28, i64 %31
%33 = load double, double* %32
%34 = fadd double %27, %33
%35 = fdiv double %34, 2.000000e+00
store double %35, double* %5
br label %42
36: ; preds = %2
%37 = load double*, double** %6
%38 = load i64, i64* %4
%39 = udiv i64 %38, 2
%40 = getelementptr inbounds double, double* %37, i64 %39
%41 = load double, double* %40
store double %41, double* %5
br label %42
42: ; preds = %36, %22
%43 = load double*, double** %6
%44 = call i32 @free(double* %43)
%45 = load double, double* %5
ret double %45
}

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