#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 floor(int);
double dpercentile_ordered2(const double *v, int len, double percentile) {
  if (len == 1) {
    return v[0];
  }
  int pos = percentile * (len + 1.0) / 100.0;
  double val = 0;
  if (pos < 1) {
    val = v[0];
  } else if (pos >= len) {
    val = v[len - 1];
  } else {
    double lower = v[(int)floor(pos)];
    double upper = v[(int)floor(pos) + 1];
    val = lower + (pos - floor(pos)) * (upper - lower);
  }
  return val;
}


x86 assembly (clang, -O0):
<dpercentile_ordered2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movsd  %xmm0,-0x20(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    1140 <dpercentile_ordered2+0x30>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1250 <dpercentile_ordered2+0x140>
movsd  -0x20(%rbp),%xmm0
cvtsi2sdl -0x14(%rbp),%xmm1
movsd  0xeb6(%rip),%xmm2
addsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe9e(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x24(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
cmpl   $0x1,-0x24(%rbp)
jge    1191 <dpercentile_ordered2+0x81>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1246 <dpercentile_ordered2+0x136>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     11b9 <dpercentile_ordered2+0xa9>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1241 <dpercentile_ordered2+0x131>
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <floor@plt>
mov    %eax,%ecx
mov    -0x60(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <floor@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
add    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <floor@plt>
movsd  -0x50(%rbp),%xmm1
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
sub    %ecx,%eax
cvtsi2sd %eax,%xmm0
movsd  -0x40(%rbp),%xmm2
subsd  -0x38(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1246 <dpercentile_ordered2+0x136>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dpercentile_ordered2>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
cmp    $0x1,%esi
je     1192 <dpercentile_ordered2+0x82>
cvtsi2sd %esi,%xmm1
addsd  0xed6(%rip),%xmm1
mulsd  %xmm0,%xmm1
divsd  0xed2(%rip),%xmm1
cvttsd2si %xmm1,%ebp
test   %ebp,%ebp
jle    1192 <dpercentile_ordered2+0x82>
cmp    %esi,%ebp
jge    1198 <dpercentile_ordered2+0x88>
mov    %ebp,%edi
call   1030 <floor@plt>
cltq
movsd  (%rbx,%rax,8),%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %ebp,%edi
call   1030 <floor@plt>
cltq
movsd  0x8(%rbx,%rax,8),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %ebp,%edi
call   1030 <floor@plt>
sub    %eax,%ebp
cvtsi2sd %ebp,%xmm2
movsd  0x10(%rsp),%xmm1
movsd  0x8(%rsp),%xmm0
subsd  %xmm1,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
jmp    11a1 <dpercentile_ordered2+0x91>
movsd  (%rbx),%xmm0
jmp    11a1 <dpercentile_ordered2+0x91>
movslq %esi,%rax
movsd  -0x8(%rbx,%rax,8),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define double @dpercentile_ordered2(double* %0, i32 %1, double %2) {
%4 = alloca double
%5 = alloca double*
%6 = alloca i32
%7 = alloca double
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca double
store double* %0, double** %5
store i32 %1, i32* %6
store double %2, double* %7
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load double*, double** %5
%16 = getelementptr inbounds double, double* %15, i64 0
%17 = load double, double* %16
store double %17, double* %4
br label %70
18: ; preds = %3
%19 = load double, double* %7
%20 = load i32, i32* %6
%21 = sitofp i32 %20 to double
%22 = fadd double %21, 1.000000e+00
%23 = fmul double %19, %22
%24 = fdiv double %23, 1.000000e+02
%25 = fptosi double %24 to i32
store i32 %25, i32* %8
store double 0.000000e+00, double* %9
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 1
br i1 %27, label %28, label %32
28: ; preds = %18
%29 = load double*, double** %5
%30 = getelementptr inbounds double, double* %29, i64 0
%31 = load double, double* %30
store double %31, double* %9
br label %68
32: ; preds = %18
%33 = load i32, i32* %8
%34 = load i32, i32* %6
%35 = icmp sge i32 %33, %34
br i1 %35, label %36, label %43
36: ; preds = %32
%37 = load double*, double** %5
%38 = load i32, i32* %6
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds double, double* %37, i64 %40
%42 = load double, double* %41
store double %42, double* %9
br label %67
43: ; preds = %32
%44 = load double*, double** %5
%45 = load i32, i32* %8
%46 = call i32 @floor(i32 %45)
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds double, double* %44, i64 %47
%49 = load double, double* %48
store double %49, double* %10
%50 = load double*, double** %5
%51 = load i32, i32* %8
%52 = call i32 @floor(i32 %51)
%53 = add nsw i32 %52, 1
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds double, double* %50, i64 %54
%56 = load double, double* %55
store double %56, double* %11
%57 = load double, double* %10
%58 = load i32, i32* %8
%59 = load i32, i32* %8
%60 = call i32 @floor(i32 %59)
%61 = sub nsw i32 %58, %60
%62 = sitofp i32 %61 to double
%63 = load double, double* %11
%64 = load double, double* %10
%65 = fsub double %63, %64
%66 = call double @llvm.fmuladd.f64(double %62, double %65, double %57)
store double %66, double* %9
br label %67
67: ; preds = %43, %36
br label %68
68: ; preds = %67, %28
%69 = load double, double* %9
store double %69, double* %4
br label %70
70: ; preds = %68, %14
%71 = load double, double* %4
ret double %71
}

LLVM IR (-O2):
define double @dpercentile_ordered2(double* nocapture %0, i32 %1, double %2) {
%4 = icmp eq i32 %1, 1
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = load double, double* %0
br label %38
7: ; preds = %3
%8 = sitofp i32 %1 to double
%9 = fadd double %8, 1.000000e+00
%10 = fmul double %9, %2
%11 = fdiv double %10, 1.000000e+02
%12 = fptosi double %11 to i32
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = load double, double* %0
br label %38
16: ; preds = %7
%17 = icmp slt i32 %12, %1
br i1 %17, label %23, label %18
18: ; preds = %16
%19 = add nsw i32 %1, -1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds double, double* %0, i64 %20
%22 = load double, double* %21
br label %38
23: ; preds = %16
%24 = tail call i32 @floor(i32 %12)
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds double, double* %0, i64 %25
%27 = load double, double* %26
%28 = tail call i32 @floor(i32 %12)
%29 = add nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds double, double* %0, i64 %30
%32 = load double, double* %31
%33 = tail call i32 @floor(i32 %12)
%34 = sub nsw i32 %12, %33
%35 = sitofp i32 %34 to double
%36 = fsub double %32, %27
%37 = tail call double @llvm.fmuladd.f64(double %35, double %36, double %27)
br label %38
38: ; preds = %14, %23, %18, %5
%39 = phi double [ %6, %5 ], [ %15, %14 ], [ %22, %18 ], [ %37, %23 ]
ret double %39
}