#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 *);
scalar_t__ malloc(int const);
int /*<<< orphan*/ sortDoubleArray(double *, int const);
double medianDouble(const double *anArray, const int length) {
  int i;
  double *copyArray = (double *)malloc(length * sizeof(double));
  double median;
  for (i = 0; i < length; i++) {
    copyArray[i] = anArray[i];
  }
  sortDoubleArray(copyArray, length);
  if (length % 2 == 0) {
    median = (copyArray[(length / 2)] + copyArray[(length / 2) - 1]) / 2;
  } else {
    median = copyArray[(length - 1) / 2];
  }
  free(copyArray);
  return median;
}


x86 assembly (clang, -O0):
<medianDouble>:
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   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    118d <medianDouble+0x5d>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1159 <medianDouble+0x29>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <sortDoubleArray@plt>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1208 <medianDouble+0xd8>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
addsd  (%rax,%rcx,8),%xmm0
movsd  0xe06(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    1231 <medianDouble+0x101>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%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):
<medianDouble>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0x0(,%r14,8),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
test   %r14d,%r14d
jle    12c4 <medianDouble+0x194>
mov    %r14d,%eax
cmp    $0x4,%r14d
jb     1177 <medianDouble+0x47>
lea    (%rbx,%rax,8),%rcx
cmp    %rcx,%r15
jae    11f0 <medianDouble+0xc0>
lea    (%r15,%rax,8),%rcx
cmp    %rbx,%rcx
jbe    11f0 <medianDouble+0xc0>
xor    %ecx,%ecx
mov    %rcx,%rdx
not    %rdx
add    %rax,%rdx
mov    %rax,%rsi
and    $0x3,%rsi
je     11a5 <medianDouble+0x75>
nopl   0x0(%rax,%rax,1)
movsd  (%rbx,%rcx,8),%xmm0
movsd  %xmm0,(%r15,%rcx,8)
add    $0x1,%rcx
add    $0xffffffffffffffff,%rsi
jne    1190 <medianDouble+0x60>
cmp    $0x3,%rdx
jb     12c4 <medianDouble+0x194>
nop
movsd  (%rbx,%rcx,8),%xmm0
movsd  %xmm0,(%r15,%rcx,8)
movsd  0x8(%rbx,%rcx,8),%xmm0
movsd  %xmm0,0x8(%r15,%rcx,8)
movsd  0x10(%rbx,%rcx,8),%xmm0
movsd  %xmm0,0x10(%r15,%rcx,8)
movsd  0x18(%rbx,%rcx,8),%xmm0
movsd  %xmm0,0x18(%r15,%rcx,8)
add    $0x4,%rcx
cmp    %rcx,%rax
jne    11b0 <medianDouble+0x80>
jmp    12c4 <medianDouble+0x194>
mov    %eax,%ecx
and    $0xfffffffc,%ecx
lea    -0x4(%rcx),%rdx
mov    %rdx,%rsi
shr    $0x2,%rsi
add    $0x1,%rsi
mov    %esi,%r8d
and    $0x3,%r8d
cmp    $0xc,%rdx
jae    1215 <medianDouble+0xe5>
xor    %edi,%edi
jmp    1280 <medianDouble+0x150>
and    $0xfffffffffffffffc,%rsi
xor    %edi,%edi
nopl   0x0(%rax,%rax,1)
movups (%rbx,%rdi,8),%xmm0
movups 0x10(%rbx,%rdi,8),%xmm1
movups %xmm0,(%r15,%rdi,8)
movups %xmm1,0x10(%r15,%rdi,8)
movups 0x20(%rbx,%rdi,8),%xmm0
movups 0x30(%rbx,%rdi,8),%xmm1
movups %xmm0,0x20(%r15,%rdi,8)
movups %xmm1,0x30(%r15,%rdi,8)
movups 0x40(%rbx,%rdi,8),%xmm0
movups 0x50(%rbx,%rdi,8),%xmm1
movups %xmm0,0x40(%r15,%rdi,8)
movups %xmm1,0x50(%r15,%rdi,8)
movups 0x60(%rbx,%rdi,8),%xmm0
movups 0x70(%rbx,%rdi,8),%xmm1
movups %xmm0,0x60(%r15,%rdi,8)
movups %xmm1,0x70(%r15,%rdi,8)
add    $0x10,%rdi
add    $0xfffffffffffffffc,%rsi
jne    1220 <medianDouble+0xf0>
test   %r8,%r8
je     12bb <medianDouble+0x18b>
lea    (%r15,%rdi,8),%rsi
add    $0x10,%rsi
lea    (%rbx,%rdi,8),%rdi
add    $0x10,%rdi
shl    $0x5,%r8
xor    %edx,%edx
nopl   0x0(%rax,%rax,1)
movups -0x10(%rdi,%rdx,1),%xmm0
movups (%rdi,%rdx,1),%xmm1
movups %xmm0,-0x10(%rsi,%rdx,1)
movups %xmm1,(%rsi,%rdx,1)
add    $0x20,%rdx
cmp    %rdx,%r8
jne    12a0 <medianDouble+0x170>
cmp    %rax,%rcx
jne    1179 <medianDouble+0x49>
mov    %r15,%rdi
mov    %r14d,%esi
call   1050 <sortDoubleArray@plt>
test   $0x1,%r14b
jne    12f9 <medianDouble+0x1c9>
mov    %r14d,%eax
shr    $0x1f,%eax
add    %r14d,%eax
sar    %eax
cltq
movsd  (%r15,%rax,8),%xmm0
addsd  -0x8(%r15,%rax,8),%xmm0
mulsd  0xd09(%rip),%xmm0
jmp    1312 <medianDouble+0x1e2>
mov    %r14d,%eax
add    $0xffffffff,%eax
shr    $0x1f,%eax
add    %r14d,%eax
add    $0xffffffff,%eax
sar    %eax
cltq
movsd  (%r15,%rax,8),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %r15,%rdi
call   1030 <free@plt>
movsd  0x8(%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define double @medianDouble(double* nocapture %0, i32 %1) {
%3 = shl i32 %1, 3
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to double*
%6 = inttoptr i64 %4 to i8*
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %134
8: ; preds = %2
%9 = zext i32 %1 to i64
%10 = icmp ult i32 %1, 4
br i1 %10, label %97, label %11
11: ; preds = %8
%12 = getelementptr double, double* %5, i64 %9
%13 = getelementptr double, double* %0, i64 %9
%14 = bitcast double* %13 to i8*
%15 = icmp ult i8* %6, %14
%16 = icmp ugt double* %12, %0
%17 = and i1 %15, %16
br i1 %17, label %97, label %18
18: ; preds = %11
%19 = and i64 %9, 4294967292
%20 = add nsw i64 %19, -4
%21 = lshr exact i64 %20, 2
%22 = add nuw nsw i64 %21, 1
%23 = and i64 %22, 3
%24 = icmp ult i64 %20, 12
br i1 %24, label %76, label %25
25: ; preds = %18
%26 = and i64 %22, 9223372036854775804
br label %27
27: ; preds = %27, %25
%28 = phi i64 [ 0, %25 ], [ %73, %27 ]
%29 = phi i64 [ 0, %25 ], [ %74, %27 ]
%30 = getelementptr inbounds double, double* %0, i64 %28
%31 = bitcast double* %30 to <2 x double>*
%32 = load <2 x double>, <2 x double>* %31
%33 = getelementptr inbounds double, double* %30, i64 2
%34 = bitcast double* %33 to <2 x double>*
%35 = load <2 x double>, <2 x double>* %34
%36 = getelementptr inbounds double, double* %5, i64 %28
%37 = bitcast double* %36 to <2 x double>*
store <2 x double> %32, <2 x double>* %37
%38 = getelementptr inbounds double, double* %36, i64 2
%39 = bitcast double* %38 to <2 x double>*
store <2 x double> %35, <2 x double>* %39
%40 = or i64 %28, 4
%41 = getelementptr inbounds double, double* %0, i64 %40
%42 = bitcast double* %41 to <2 x double>*
%43 = load <2 x double>, <2 x double>* %42
%44 = getelementptr inbounds double, double* %41, i64 2
%45 = bitcast double* %44 to <2 x double>*
%46 = load <2 x double>, <2 x double>* %45
%47 = getelementptr inbounds double, double* %5, i64 %40
%48 = bitcast double* %47 to <2 x double>*
store <2 x double> %43, <2 x double>* %48
%49 = getelementptr inbounds double, double* %47, i64 2
%50 = bitcast double* %49 to <2 x double>*
store <2 x double> %46, <2 x double>* %50
%51 = or i64 %28, 8
%52 = getelementptr inbounds double, double* %0, i64 %51
%53 = bitcast double* %52 to <2 x double>*
%54 = load <2 x double>, <2 x double>* %53
%55 = getelementptr inbounds double, double* %52, i64 2
%56 = bitcast double* %55 to <2 x double>*
%57 = load <2 x double>, <2 x double>* %56
%58 = getelementptr inbounds double, double* %5, i64 %51
%59 = bitcast double* %58 to <2 x double>*
store <2 x double> %54, <2 x double>* %59
%60 = getelementptr inbounds double, double* %58, i64 2
%61 = bitcast double* %60 to <2 x double>*
store <2 x double> %57, <2 x double>* %61
%62 = or i64 %28, 12
%63 = getelementptr inbounds double, double* %0, i64 %62
%64 = bitcast double* %63 to <2 x double>*
%65 = load <2 x double>, <2 x double>* %64
%66 = getelementptr inbounds double, double* %63, i64 2
%67 = bitcast double* %66 to <2 x double>*
%68 = load <2 x double>, <2 x double>* %67
%69 = getelementptr inbounds double, double* %5, i64 %62
%70 = bitcast double* %69 to <2 x double>*
store <2 x double> %65, <2 x double>* %70
%71 = getelementptr inbounds double, double* %69, i64 2
%72 = bitcast double* %71 to <2 x double>*
store <2 x double> %68, <2 x double>* %72
%73 = add nuw i64 %28, 16
%74 = add nuw i64 %29, 4
%75 = icmp eq i64 %74, %26
br i1 %75, label %76, label %27
76: ; preds = %27, %18
%77 = phi i64 [ 0, %18 ], [ %73, %27 ]
%78 = icmp eq i64 %23, 0
br i1 %78, label %95, label %79
79: ; preds = %76, %79
%80 = phi i64 [ %92, %79 ], [ %77, %76 ]
%81 = phi i64 [ %93, %79 ], [ 0, %76 ]
%82 = getelementptr inbounds double, double* %0, i64 %80
%83 = bitcast double* %82 to <2 x double>*
%84 = load <2 x double>, <2 x double>* %83
%85 = getelementptr inbounds double, double* %82, i64 2
%86 = bitcast double* %85 to <2 x double>*
%87 = load <2 x double>, <2 x double>* %86
%88 = getelementptr inbounds double, double* %5, i64 %80
%89 = bitcast double* %88 to <2 x double>*
store <2 x double> %84, <2 x double>* %89
%90 = getelementptr inbounds double, double* %88, i64 2
%91 = bitcast double* %90 to <2 x double>*
store <2 x double> %87, <2 x double>* %91
%92 = add nuw i64 %80, 4
%93 = add i64 %81, 1
%94 = icmp eq i64 %93, %23
br i1 %94, label %95, label %79
95: ; preds = %79, %76
%96 = icmp eq i64 %19, %9
br i1 %96, label %134, label %97
97: ; preds = %11, %8, %95
%98 = phi i64 [ 0, %11 ], [ 0, %8 ], [ %19, %95 ]
%99 = xor i64 %98, -1
%100 = add nsw i64 %99, %9
%101 = and i64 %9, 3
%102 = icmp eq i64 %101, 0
br i1 %102, label %112, label %103
103: ; preds = %97, %103
%104 = phi i64 [ %109, %103 ], [ %98, %97 ]
%105 = phi i64 [ %110, %103 ], [ 0, %97 ]
%106 = getelementptr inbounds double, double* %0, i64 %104
%107 = load double, double* %106
%108 = getelementptr inbounds double, double* %5, i64 %104
store double %107, double* %108
%109 = add nuw nsw i64 %104, 1
%110 = add i64 %105, 1
%111 = icmp eq i64 %110, %101
br i1 %111, label %112, label %103
112: ; preds = %103, %97
%113 = phi i64 [ %98, %97 ], [ %109, %103 ]
%114 = icmp ult i64 %100, 3
br i1 %114, label %134, label %115
115: ; preds = %112, %115
%116 = phi i64 [ %132, %115 ], [ %113, %112 ]
%117 = getelementptr inbounds double, double* %0, i64 %116
%118 = load double, double* %117
%119 = getelementptr inbounds double, double* %5, i64 %116
store double %118, double* %119
%120 = add nuw nsw i64 %116, 1
%121 = getelementptr inbounds double, double* %0, i64 %120
%122 = load double, double* %121
%123 = getelementptr inbounds double, double* %5, i64 %120
store double %122, double* %123
%124 = add nuw nsw i64 %116, 2
%125 = getelementptr inbounds double, double* %0, i64 %124
%126 = load double, double* %125
%127 = getelementptr inbounds double, double* %5, i64 %124
store double %126, double* %127
%128 = add nuw nsw i64 %116, 3
%129 = getelementptr inbounds double, double* %0, i64 %128
%130 = load double, double* %129
%131 = getelementptr inbounds double, double* %5, i64 %128
store double %130, double* %131
%132 = add nuw nsw i64 %116, 4
%133 = icmp eq i64 %132, %9
br i1 %133, label %134, label %115
134: ; preds = %112, %115, %95, %2
%135 = tail call i32 @sortDoubleArray(double* %5, i32 %1)
%136 = and i32 %1, 1
%137 = icmp eq i32 %136, 0
br i1 %137, label %138, label %149
138: ; preds = %134
%139 = sdiv i32 %1, 2
%140 = sext i32 %139 to i64
%141 = getelementptr inbounds double, double* %5, i64 %140
%142 = load double, double* %141
%143 = add nsw i32 %139, -1
%144 = sext i32 %143 to i64
%145 = getelementptr inbounds double, double* %5, i64 %144
%146 = load double, double* %145
%147 = fadd double %142, %146
%148 = fmul double %147, 5.000000e-01
br label %155
149: ; preds = %134
%150 = add nsw i32 %1, -1
%151 = sdiv i32 %150, 2
%152 = sext i32 %151 to i64
%153 = getelementptr inbounds double, double* %5, i64 %152
%154 = load double, double* %153
br label %155
155: ; preds = %149, %138
%156 = phi double [ %148, %138 ], [ %154, %149 ]
%157 = tail call i32 @free(double* %5)
ret double %156
}