#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 */
typedef int n;

/* Variables and functions */
int /*<<< orphan*/ CSPL_Sort_mergesort(double *, long);
double CSPL_Stats_percent_rank(long, long);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ memcpy(double *, double *, int);
double CSPL_Stats_percentile(double *inval, double percentile, long n) {
  long i;
  double *incopy;
  double *rank;
  double answer;
  rank = (double *)calloc(n, sizeof(double));
  {
    incopy = (double *)calloc((n) - (0) + 1, sizeof(double));
    memcpy((incopy), &(inval)[(0)], sizeof(double) * ((n) - (0) + 1));
  };
  CSPL_Sort_mergesort(incopy, n);
  for (i = 0; i < n; i++) {
    rank[i] = CSPL_Stats_percent_rank(i, n);
    if ((rank[i] > percentile) && (0 == i)) {
      answer = incopy[i];
      break;
    }
    if (rank[i] == percentile) {
      answer = incopy[i];
      break;
    } else if (rank[i] > percentile) {
      answer = inval[i - 1] + (n * ((percentile - rank[i - 1]) / 100.)) *
                                  (inval[i] - inval[i - 1]);
      break;
    }
    answer = incopy[i];
  }
  free(incopy);
  free(rank);
  return (answer);
}


x86 assembly (clang, -O0):
<CSPL_Stats_percentile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rax
sub    $0x0,%rax
add    $0x1,%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <CSPL_Sort_mergesort@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    130e <CSPL_Stats_percentile+0x1be>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <CSPL_Stats_percent_rank@plt>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    122c <CSPL_Stats_percentile+0xdc>
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    122c <CSPL_Stats_percentile+0xdc>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x38(%rbp)
jmp    130e <CSPL_Stats_percentile+0x1be>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
ucomisd -0x10(%rbp),%xmm0
jne    1261 <CSPL_Stats_percentile+0x111>
jp     1261 <CSPL_Stats_percentile+0x111>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x38(%rbp)
jmp    130e <CSPL_Stats_percentile+0x1be>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    12e6 <CSPL_Stats_percentile+0x196>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsd  (%rax,%rcx,8),%xmm1
cvtsi2sdq -0x18(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
subsd  (%rax,%rcx,8),%xmm2
movsd  0xd52(%rip),%xmm3
divsd  %xmm3,%xmm2
mulsd  %xmm2,%xmm0
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
subsd  (%rax,%rcx,8),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
jmp    130e <CSPL_Stats_percentile+0x1be>
jmp    12eb <CSPL_Stats_percentile+0x19b>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11c9 <CSPL_Stats_percentile+0x79>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movsd  -0x38(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CSPL_Stats_percentile>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r13
movsd  %xmm0,0x8(%rsp)
mov    %rdi,%r15
mov    %r13d,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,%r12
lea    0x1(%r13),%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
lea    0x8(,%r13,8),%edx
mov    %rax,%rdi
mov    %r15,%rsi
call   1070 <memcpy@plt>
mov    %r14,%rdi
mov    %r13,%rsi
call   1060 <CSPL_Sort_mergesort@plt>
test   %r13,%r13
jle    11ee <CSPL_Stats_percentile+0x9e>
xor    %ebx,%ebx
nop
mov    %rbx,%rdi
mov    %r13,%rsi
call   1040 <CSPL_Stats_percent_rank@plt>
movsd  %xmm0,(%r12,%rbx,8)
movsd  0x8(%rsp),%xmm1
ucomisd %xmm1,%xmm0
jbe    11d2 <CSPL_Stats_percentile+0x82>
test   %rbx,%rbx
je     11f0 <CSPL_Stats_percentile+0xa0>
ucomisd %xmm1,%xmm0
jne    11da <CSPL_Stats_percentile+0x8a>
jnp    11f7 <CSPL_Stats_percentile+0xa7>
ja     1205 <CSPL_Stats_percentile+0xb5>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11b0 <CSPL_Stats_percentile+0x60>
movsd  -0x8(%r14,%rbx,8),%xmm0
jmp    11fd <CSPL_Stats_percentile+0xad>
jmp    123f <CSPL_Stats_percentile+0xef>
movsd  (%r14),%xmm0
jmp    11fd <CSPL_Stats_percentile+0xad>
movsd  (%r14,%rbx,8),%xmm0
movsd  %xmm0,0x8(%rsp)
jmp    123f <CSPL_Stats_percentile+0xef>
xorps  %xmm0,%xmm0
cvtsi2sd %r13,%xmm0
subsd  -0x8(%r12,%rbx,8),%xmm1
divsd  0xde4(%rip),%xmm1
mulsd  %xmm0,%xmm1
movsd  -0x8(%r15,%rbx,8),%xmm0
movsd  (%r15,%rbx,8),%xmm2
subsd  %xmm0,%xmm2
mulsd  %xmm1,%xmm2
addsd  %xmm0,%xmm2
movsd  %xmm2,0x8(%rsp)
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
movsd  0x8(%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @CSPL_Stats_percentile(double* %0, double %1, i64 %2) {
%4 = alloca double*
%5 = alloca double
%6 = alloca i64
%7 = alloca i64
%8 = alloca double*
%9 = alloca double*
%10 = alloca double
store double* %0, double** %4
store double %1, double* %5
store i64 %2, i64* %6
%11 = load i64, i64* %6
%12 = trunc i64 %11 to i32
%13 = call i64 @calloc(i32 %12, i32 8)
%14 = inttoptr i64 %13 to double*
store double* %14, double** %9
%15 = load i64, i64* %6
%16 = sub nsw i64 %15, 0
%17 = add nsw i64 %16, 1
%18 = trunc i64 %17 to i32
%19 = call i64 @calloc(i32 %18, i32 8)
%20 = inttoptr i64 %19 to double*
store double* %20, double** %8
%21 = load double*, double** %8
%22 = load double*, double** %4
%23 = getelementptr inbounds double, double* %22, i64 0
%24 = load i64, i64* %6
%25 = sub nsw i64 %24, 0
%26 = add nsw i64 %25, 1
%27 = mul i64 8, %26
%28 = trunc i64 %27 to i32
%29 = call i32 @memcpy(double* %21, double* %23, i32 %28)
%30 = load double*, double** %8
%31 = load i64, i64* %6
%32 = call i32 @CSPL_Sort_mergesort(double* %30, i64 %31)
store i64 0, i64* %7
br label %33
33: ; preds = %111, %3
%34 = load i64, i64* %7
%35 = load i64, i64* %6
%36 = icmp slt i64 %34, %35
br i1 %36, label %37, label %114
37: ; preds = %33
%38 = load i64, i64* %7
%39 = load i64, i64* %6
%40 = call double @CSPL_Stats_percent_rank(i64 %38, i64 %39)
%41 = load double*, double** %9
%42 = load i64, i64* %7
%43 = getelementptr inbounds double, double* %41, i64 %42
store double %40, double* %43
%44 = load double*, double** %9
%45 = load i64, i64* %7
%46 = getelementptr inbounds double, double* %44, i64 %45
%47 = load double, double* %46
%48 = load double, double* %5
%49 = fcmp ogt double %47, %48
br i1 %49, label %50, label %58
50: ; preds = %37
%51 = load i64, i64* %7
%52 = icmp eq i64 0, %51
br i1 %52, label %53, label %58
53: ; preds = %50
%54 = load double*, double** %8
%55 = load i64, i64* %7
%56 = getelementptr inbounds double, double* %54, i64 %55
%57 = load double, double* %56
store double %57, double* %10
br label %114
58: ; preds = %50, %37
%59 = load double*, double** %9
%60 = load i64, i64* %7
%61 = getelementptr inbounds double, double* %59, i64 %60
%62 = load double, double* %61
%63 = load double, double* %5
%64 = fcmp oeq double %62, %63
br i1 %64, label %65, label %70
65: ; preds = %58
%66 = load double*, double** %8
%67 = load i64, i64* %7
%68 = getelementptr inbounds double, double* %66, i64 %67
%69 = load double, double* %68
store double %69, double* %10
br label %114
70: ; preds = %58
%71 = load double*, double** %9
%72 = load i64, i64* %7
%73 = getelementptr inbounds double, double* %71, i64 %72
%74 = load double, double* %73
%75 = load double, double* %5
%76 = fcmp ogt double %74, %75
br i1 %76, label %77, label %105
77: ; preds = %70
%78 = load double*, double** %4
%79 = load i64, i64* %7
%80 = sub nsw i64 %79, 1
%81 = getelementptr inbounds double, double* %78, i64 %80
%82 = load double, double* %81
%83 = load i64, i64* %6
%84 = sitofp i64 %83 to double
%85 = load double, double* %5
%86 = load double*, double** %9
%87 = load i64, i64* %7
%88 = sub nsw i64 %87, 1
%89 = getelementptr inbounds double, double* %86, i64 %88
%90 = load double, double* %89
%91 = fsub double %85, %90
%92 = fdiv double %91, 1.000000e+02
%93 = fmul double %84, %92
%94 = load double*, double** %4
%95 = load i64, i64* %7
%96 = getelementptr inbounds double, double* %94, i64 %95
%97 = load double, double* %96
%98 = load double*, double** %4
%99 = load i64, i64* %7
%100 = sub nsw i64 %99, 1
%101 = getelementptr inbounds double, double* %98, i64 %100
%102 = load double, double* %101
%103 = fsub double %97, %102
%104 = call double @llvm.fmuladd.f64(double %93, double %103, double %82)
store double %104, double* %10
br label %114
105: ; preds = %70
br label %106
106: ; preds = %105
%107 = load double*, double** %8
%108 = load i64, i64* %7
%109 = getelementptr inbounds double, double* %107, i64 %108
%110 = load double, double* %109
store double %110, double* %10
br label %111
111: ; preds = %106
%112 = load i64, i64* %7
%113 = add nsw i64 %112, 1
store i64 %113, i64* %7
br label %33
114: ; preds = %77, %65, %53, %33
%115 = load double*, double** %8
%116 = call i32 @free(double* %115)
%117 = load double*, double** %9
%118 = call i32 @free(double* %117)
%119 = load double, double* %10
ret double %119
}

LLVM IR (-O2):
define double @CSPL_Stats_percentile(double* %0, double %1, i64 %2) {
%4 = trunc i64 %2 to i32
%5 = tail call i64 @calloc(i32 %4, i32 8)
%6 = inttoptr i64 %5 to double*
%7 = add i32 %4, 1
%8 = tail call i64 @calloc(i32 %7, i32 8)
%9 = inttoptr i64 %8 to double*
%10 = shl i32 %7, 3
%11 = tail call i32 @memcpy(double* %9, double* %0, i32 %10)
%12 = tail call i32 @CSPL_Sort_mergesort(double* %9, i64 %2)
%13 = icmp sgt i64 %2, 0
br i1 %13, label %14, label %49
14: ; preds = %3, %43
%15 = phi i64 [ %44, %43 ], [ 0, %3 ]
%16 = tail call double @CSPL_Stats_percent_rank(i64 %15, i64 %2)
%17 = getelementptr inbounds double, double* %6, i64 %15
store double %16, double* %17
%18 = fcmp ogt double %16, %1
%19 = icmp eq i64 %15, 0
%20 = and i1 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = load double, double* %9
br label %49
23: ; preds = %14
%24 = fcmp oeq double %16, %1
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = getelementptr inbounds double, double* %9, i64 %15
%27 = load double, double* %26
br label %49
28: ; preds = %23
br i1 %18, label %29, label %43
29: ; preds = %28
%30 = add nsw i64 %15, -1
%31 = getelementptr inbounds double, double* %0, i64 %30
%32 = load double, double* %31
%33 = sitofp i64 %2 to double
%34 = getelementptr inbounds double, double* %6, i64 %30
%35 = load double, double* %34
%36 = fsub double %1, %35
%37 = fdiv double %36, 1.000000e+02
%38 = fmul double %37, %33
%39 = getelementptr inbounds double, double* %0, i64 %15
%40 = load double, double* %39
%41 = fsub double %40, %32
%42 = tail call double @llvm.fmuladd.f64(double %38, double %41, double %32)
br label %49
43: ; preds = %28
%44 = add nuw nsw i64 %15, 1
%45 = icmp eq i64 %44, %2
br i1 %45, label %46, label %14
46: ; preds = %43
%47 = getelementptr inbounds double, double* %9, i64 %15
%48 = load double, double* %47
br label %49
49: ; preds = %46, %3, %29, %25, %21
%50 = phi double [ %22, %21 ], [ %27, %25 ], [ %42, %29 ], [ undef, %3 ], [ %48, %46 ]
%51 = tail call i32 @free(double* %9)
%52 = tail call i32 @free(double* %6)
ret double %50
}