#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 scalar_t__ uint64_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
double floor(double);
double get_percentile(uint64_t *sorted_values, size_t n_values,
                      double percentile, size_t denominator) {
  double n = (double)n_values;
  assert(1.0 / n <= percentile && percentile <= (n - 1.0) / n);
  double r_p = 0.5 + percentile * n;
  double r_floor = floor(r_p);
  double r_frac = r_p - r_floor;
  double result = 0.0;
  if (r_frac < 0.001) {
    size_t r_ind = (size_t)r_floor - 1;
    assert(r_ind < n_values);
    result = (double)sorted_values[r_ind];
  } else {
    size_t r_ind = (size_t)r_floor - 1;
    size_t r_ind2 = r_ind + 1;
    assert(r_ind2 < n_values);
    result = (1.0 - r_frac) * (double)sorted_values[r_ind];
    result += r_frac * (double)sorted_values[r_ind2];
  }
  return result / ((double)denominator);
}


x86 assembly (clang, -O0):
<get_percentile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movaps 0xee0(%rip),%xmm1
movq   -0x10(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xedf(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xe93(%rip),%xmm1
divsd  -0x28(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
xor    %eax,%eax
ucomisd %xmm1,%xmm0
mov    %al,-0x61(%rbp)
jb     11ab <get_percentile+0x8b>
movsd  -0x18(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
movsd  0xe68(%rip),%xmm2
subsd  %xmm2,%xmm0
divsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
setae  %al
mov    %al,-0x61(%rbp)
mov    -0x61(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
movsd  -0x18(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe42(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1040 <floor@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
subsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  0xe03(%rip),%xmm0
ucomisd -0x40(%rbp),%xmm0
jbe    1270 <get_percentile+0x150>
movsd  -0x38(%rbp),%xmm0
movsd  0xdfb(%rip),%xmm2
movaps %xmm0,%xmm1
subsd  %xmm2,%xmm1
cvttsd2si %xmm1,%rcx
cvttsd2si %xmm0,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
and    %rdx,%rcx
or     %rcx,%rax
sub    $0x1,%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x10(%rbp),%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x8(%rbp),%rax
mov    -0x50(%rbp),%rcx
cvtsi2sdq (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x48(%rbp)
jmp    130d <get_percentile+0x1ed>
movsd  -0x38(%rbp),%xmm0
movsd  0xd9b(%rip),%xmm2
movaps %xmm0,%xmm1
subsd  %xmm2,%xmm1
cvttsd2si %xmm1,%rcx
cvttsd2si %xmm0,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
and    %rdx,%rcx
or     %rcx,%rax
sub    $0x1,%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
cmp    -0x10(%rbp),%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
movsd  0xd34(%rip),%xmm0
subsd  -0x40(%rbp),%xmm0
mov    -0x8(%rbp),%rax
mov    -0x58(%rbp),%rcx
cvtsi2sdq (%rax,%rcx,8),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x40(%rbp),%xmm0
mov    -0x8(%rbp),%rax
mov    -0x60(%rbp),%rcx
cvtsi2sdq (%rax,%rcx,8),%xmm2
movsd  -0x48(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
movaps 0xd07(%rip),%xmm2
movq   -0x20(%rbp),%xmm1
punpckldq %xmm2,%xmm1
movapd 0xd06(%rip),%xmm2
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_percentile>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r12
movq   %rsi,%xmm1
punpckldq 0xec5(%rip),%xmm1
subpd  0xecd(%rip),%xmm1
mov    %rdx,%r14
mov    %rdi,%r15
movapd %xmm1,%xmm2
unpckhpd %xmm1,%xmm2
addsd  %xmm1,%xmm2
movsd  0xec3(%rip),%xmm1
divsd  %xmm2,%xmm1
xor    %edi,%edi
movsd  %xmm0,0x8(%rsp)
ucomisd %xmm1,%xmm0
jb     118b <get_percentile+0x6b>
movsd  0xeb1(%rip),%xmm0
addsd  %xmm2,%xmm0
divsd  %xmm2,%xmm0
xor    %edi,%edi
ucomisd 0x8(%rsp),%xmm0
setae  %dil
movapd %xmm2,0x10(%rsp)
call   1030 <assert@plt>
movsd  0x8(%rsp),%xmm0
mulsd  0x10(%rsp),%xmm0
addsd  0xe86(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
call   1040 <floor@plt>
movsd  0x8(%rsp),%xmm1
subsd  %xmm0,%xmm1
cvttsd2si %xmm0,%rax
mov    %rax,%rcx
subsd  0xe69(%rip),%xmm0
sar    $0x3f,%rcx
cvttsd2si %xmm0,%rbx
and    %rcx,%rbx
or     %rax,%rbx
movsd  0xe5a(%rip),%xmm0
movsd  %xmm1,0x8(%rsp)
ucomisd %xmm1,%xmm0
jbe    1210 <get_percentile+0xf0>
lea    -0x1(%rbx),%rax
xor    %edi,%edi
cmp    %r12,%rax
setb   %dil
call   1030 <assert@plt>
xorps  %xmm0,%xmm0
cvtsi2sdq -0x8(%r15,%rbx,8),%xmm0
jmp    124f <get_percentile+0x12f>
xor    %edi,%edi
cmp    %r12,%rbx
setb   %dil
call   1030 <assert@plt>
movsd  0x8(%rsp),%xmm2
movsd  0xdf4(%rip),%xmm0
subsd  %xmm2,%xmm0
xorps  %xmm1,%xmm1
cvtsi2sdq -0x8(%r15,%rbx,8),%xmm1
mulsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sdq (%r15,%rbx,8),%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movq   %r14,%xmm1
punpckldq 0xda4(%rip),%xmm1
subpd  0xdac(%rip),%xmm1
movapd %xmm1,%xmm2
unpckhpd %xmm1,%xmm2
addsd  %xmm1,%xmm2
divsd  %xmm2,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @get_percentile(i64* %0, i64 %1, double %2, i64 %3) {
%5 = alloca i64*
%6 = alloca i64
%7 = alloca double
%8 = alloca i64
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
store i64* %0, i64** %5
store i64 %1, i64* %6
store double %2, double* %7
store i64 %3, i64* %8
%17 = load i64, i64* %6
%18 = uitofp i64 %17 to double
store double %18, double* %9
%19 = load double, double* %9
%20 = fdiv double 1.000000e+00, %19
%21 = load double, double* %7
%22 = fcmp ole double %20, %21
br i1 %22, label %23, label %30
23: ; preds = %4
%24 = load double, double* %7
%25 = load double, double* %9
%26 = fsub double %25, 1.000000e+00
%27 = load double, double* %9
%28 = fdiv double %26, %27
%29 = fcmp ole double %24, %28
br label %30
30: ; preds = %23, %4
%31 = phi i1 [ false, %4 ], [ %29, %23 ]
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load double, double* %7
%35 = load double, double* %9
%36 = call double @llvm.fmuladd.f64(double %34, double %35, double 5.000000e-01)
store double %36, double* %10
%37 = load double, double* %10
%38 = call double @llvm.floor.f64(double %37)
store double %38, double* %11
%39 = load double, double* %10
%40 = load double, double* %11
%41 = fsub double %39, %40
store double %41, double* %12
store double 0.000000e+00, double* %13
%42 = load double, double* %12
%43 = fcmp olt double %42, 1.000000e-03
br i1 %43, label %44, label %58
44: ; preds = %30
%45 = load double, double* %11
%46 = fptoui double %45 to i64
%47 = sub i64 %46, 1
store i64 %47, i64* %14
%48 = load i64, i64* %14
%49 = load i64, i64* %6
%50 = icmp ult i64 %48, %49
%51 = zext i1 %50 to i32
%52 = call i32 @assert(i32 %51)
%53 = load i64*, i64** %5
%54 = load i64, i64* %14
%55 = getelementptr inbounds i64, i64* %53, i64 %54
%56 = load i64, i64* %55
%57 = sitofp i64 %56 to double
store double %57, double* %13
br label %85
58: ; preds = %30
%59 = load double, double* %11
%60 = fptoui double %59 to i64
%61 = sub i64 %60, 1
store i64 %61, i64* %15
%62 = load i64, i64* %15
%63 = add i64 %62, 1
store i64 %63, i64* %16
%64 = load i64, i64* %16
%65 = load i64, i64* %6
%66 = icmp ult i64 %64, %65
%67 = zext i1 %66 to i32
%68 = call i32 @assert(i32 %67)
%69 = load double, double* %12
%70 = fsub double 1.000000e+00, %69
%71 = load i64*, i64** %5
%72 = load i64, i64* %15
%73 = getelementptr inbounds i64, i64* %71, i64 %72
%74 = load i64, i64* %73
%75 = sitofp i64 %74 to double
%76 = fmul double %70, %75
store double %76, double* %13
%77 = load double, double* %12
%78 = load i64*, i64** %5
%79 = load i64, i64* %16
%80 = getelementptr inbounds i64, i64* %78, i64 %79
%81 = load i64, i64* %80
%82 = sitofp i64 %81 to double
%83 = load double, double* %13
%84 = call double @llvm.fmuladd.f64(double %77, double %82, double %83)
store double %84, double* %13
br label %85
85: ; preds = %58, %44
%86 = load double, double* %13
%87 = load i64, i64* %8
%88 = uitofp i64 %87 to double
%89 = fdiv double %86, %88
ret double %89
}

LLVM IR (-O2):
define double @get_percentile(i64* nocapture %0, i64 %1, double %2, i64 %3) {
%5 = uitofp i64 %1 to double
%6 = fdiv double 1.000000e+00, %5
%7 = fcmp ugt double %6, %2
br i1 %7, label %13, label %8
8: ; preds = %4
%9 = fadd double %5, -1.000000e+00
%10 = fdiv double %9, %5
%11 = fcmp oge double %10, %2
%12 = zext i1 %11 to i32
br label %13
13: ; preds = %8, %4
%14 = phi i32 [ 0, %4 ], [ %12, %8 ]
%15 = tail call i32 @assert(i32 %14)
%16 = tail call double @llvm.fmuladd.f64(double %2, double %5, double 5.000000e-01)
%17 = tail call double @llvm.floor.f64(double %16)
%18 = fsub double %16, %17
%19 = fcmp olt double %18, 1.000000e-03
%20 = fptoui double %17 to i64
%21 = add i64 %20, -1
br i1 %19, label %22, label %29
22: ; preds = %13
%23 = icmp ult i64 %21, %1
%24 = zext i1 %23 to i32
%25 = tail call i32 @assert(i32 %24)
%26 = getelementptr inbounds i64, i64* %0, i64 %21
%27 = load i64, i64* %26
%28 = sitofp i64 %27 to double
br label %42
29: ; preds = %13
%30 = icmp ult i64 %20, %1
%31 = zext i1 %30 to i32
%32 = tail call i32 @assert(i32 %31)
%33 = fsub double 1.000000e+00, %18
%34 = getelementptr inbounds i64, i64* %0, i64 %21
%35 = load i64, i64* %34
%36 = sitofp i64 %35 to double
%37 = fmul double %33, %36
%38 = getelementptr inbounds i64, i64* %0, i64 %20
%39 = load i64, i64* %38
%40 = sitofp i64 %39 to double
%41 = tail call double @llvm.fmuladd.f64(double %18, double %40, double %37)
br label %42
42: ; preds = %29, %22
%43 = phi double [ %28, %22 ], [ %41, %29 ]
%44 = uitofp i64 %3 to double
%45 = fdiv double %43, %44
ret double %45
}