#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 */
scalar_t__ floor(int);
int /*<<< orphan*/ free(double *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(double *, double *, int);
int /*<<< orphan*/ qsort(void *, int, int, int (*)(void const *, void const *));
double findmediandouble(double *arr, int n) {
  double *tarr = (double *)malloc(n * sizeof(double));
  double median = 0;
  int compd(const void *a, const void *b);
  memcpy(tarr, arr, n * sizeof(double));
  qsort((void *)tarr, n, sizeof(double), compd);
  median = (n % 2 == 1) ? tarr[(int)floor(n / 2)]
                        : (tarr[n / 2 - 1] + tarr[n / 2]) / 2.0;
  free(tarr);
  return median;
}


x86 assembly (clang, -O0):
<findmediandouble>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x8,%edx
mov    0x2e3c(%rip),%rcx
call   1040 <qsort@plt>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x1,%edx
jne    11f0 <findmediandouble+0xa0>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1070 <floor@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    1246 <findmediandouble+0xf6>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
addsd  (%rax,%rcx,8),%xmm0
movsd  0xdc3(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movsd  -0x20(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findmediandouble>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r15
lea    0x0(,%rbx,8),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    0x2e5f(%rip),%rcx
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x8,%edx
call   1040 <qsort@plt>
mov    %ebx,%eax
shr    $0x1f,%eax
add    %ebx,%eax
mov    %eax,%edi
sar    %edi
and    $0xfffffffe,%eax
sub    %eax,%ebx
cmp    $0x1,%ebx
jne    11b4 <findmediandouble+0x64>
call   1070 <floor@plt>
cltq
movsd  (%r14,%rax,8),%xmm0
jmp    11cc <findmediandouble+0x7c>
movslq %edi,%rax
movsd  -0x8(%r14,%rax,8),%xmm0
addsd  (%r14,%rax,8),%xmm0
mulsd  0xe34(%rip),%xmm0
movsd  %xmm0,(%rsp)
mov    %r14,%rdi
call   1030 <free@plt>
movsd  (%rsp),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @findmediandouble(double* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca double*
%6 = alloca double
store double* %0, double** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = sext i32 %7 to i64
%9 = mul i64 %8, 8
%10 = trunc i64 %9 to i32
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to double*
store double* %12, double** %5
store double 0.000000e+00, double* %6
%13 = load double*, double** %5
%14 = load double*, double** %3
%15 = load i32, i32* %4
%16 = sext i32 %15 to i64
%17 = mul i64 %16, 8
%18 = trunc i64 %17 to i32
%19 = call i32 @memcpy(double* %13, double* %14, i32 %18)
%20 = load double*, double** %5
%21 = bitcast double* %20 to i8*
%22 = load i32, i32* %4
%23 = call i32 @qsort(i8* %21, i32 %22, i32 8, i32 (i8*, i8*)* @compd)
%24 = load i32, i32* %4
%25 = srem i32 %24, 2
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %36
27: ; preds = %2
%28 = load double*, double** %5
%29 = load i32, i32* %4
%30 = sdiv i32 %29, 2
%31 = call i64 @floor(i32 %30)
%32 = trunc i64 %31 to i32
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds double, double* %28, i64 %33
%35 = load double, double* %34
br label %52
36: ; preds = %2
%37 = load double*, double** %5
%38 = load i32, i32* %4
%39 = sdiv i32 %38, 2
%40 = sub nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds double, double* %37, i64 %41
%43 = load double, double* %42
%44 = load double*, double** %5
%45 = load i32, i32* %4
%46 = sdiv i32 %45, 2
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds double, double* %44, i64 %47
%49 = load double, double* %48
%50 = fadd double %43, %49
%51 = fdiv double %50, 2.000000e+00
br label %52
52: ; preds = %36, %27
%53 = phi double [ %35, %27 ], [ %51, %36 ]
store double %53, double* %6
%54 = load double*, double** %5
%55 = call i32 @free(double* %54)
%56 = load double, double* %6
ret double %56
}

LLVM IR (-O2):
define double @findmediandouble(double* %0, i32 %1) {
%3 = shl i32 %1, 3
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to double*
%6 = tail call i32 @memcpy(double* %5, double* %0, i32 %3)
%7 = inttoptr i64 %4 to i8*
%8 = tail call i32 @qsort(i8* %7, i32 %1, i32 8, i32 (i8*, i8*)* @compd)
%9 = srem i32 %1, 2
%10 = icmp eq i32 %9, 1
%11 = sdiv i32 %1, 2
br i1 %10, label %12, label %18
12: ; preds = %2
%13 = tail call i64 @floor(i32 %11)
%14 = shl i64 %13, 32
%15 = ashr exact i64 %14, 32
%16 = getelementptr inbounds double, double* %5, i64 %15
%17 = load double, double* %16
br label %28
18: ; preds = %2
%19 = add nsw i32 %11, -1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds double, double* %5, i64 %20
%22 = load double, double* %21
%23 = sext i32 %11 to i64
%24 = getelementptr inbounds double, double* %5, i64 %23
%25 = load double, double* %24
%26 = fadd double %22, %25
%27 = fmul double %26, 5.000000e-01
br label %28
28: ; preds = %18, %12
%29 = phi double [ %17, %12 ], [ %27, %18 ]
%30 = tail call i32 @free(double* %5)
ret double %29
}