// File: snaury[P]t-digest-simple/./merging_tdigest.hpp
// Function: MergingTDigest::quantile(double)
double quantile(double q) {
        compress();
        if (summary_.empty())
            return NAN;
        if (summary_.size() == 1)
            return summary_[0].mean;
        double index = q * (count_ - 1);
        double a = summary_[0].mean;
        double aIndex = (summary_[0].count - 1) * 0.5;
        int64_t sum = summary_[0].count;
        double b = summary_[1].mean;
        double bIndex = sum + (summary_[1].count - 1) * 0.5;
        for (size_t i = 2; i < summary_.size(); ++i) {
            if (index <= bIndex) {
                return interpolate(index, aIndex, a, bIndex, b);
            }
            a = b;
            aIndex = bIndex;
            sum += summary_[i-1].count;
            b = summary_[i].mean;
            bIndex = sum + (summary_[i].count - 1) * 0.5;
        }
        // either there are exactly two centroids or index is past the end
        return interpolate(index, aIndex, a, bIndex, b);
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x58(%rbp)
callq 0x2c70
movq -0x58(%rbp), %rdi
callq 0x7520
testb $0x1, %al
jne 0x316d
jmp 0x317f
movsd 0x5ebb(%rip), %xmm0     # 0x9030
movsd %xmm0, -0x8(%rbp)
jmp 0x33ad
movq -0x58(%rbp), %rdi
callq 0x3ec0
cmpq $0x1, %rax
jne 0x31a9
movq -0x58(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x7560
movsd (%rax), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x33ad
movq -0x58(%rbp), %rdi
movsd -0x18(%rbp), %xmm0
movq 0x30(%rdi), %rax
decq %rax
movq %rax, %xmm1
movaps 0x5e7b(%rip), %xmm2     # 0x9040
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x5e7f(%rip), %xmm2     # 0x9050
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0x7560
movq -0x58(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x28(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0x7560
movq -0x58(%rbp), %rdi
movq 0x8(%rax), %rax
subq $0x1, %rax
cvtsi2sd %rax, %xmm0
movsd 0x5df7(%rip), %xmm1     # 0x9018
mulsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0x7560
movq -0x58(%rbp), %rdi
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movl $0x1, %esi
callq 0x7560
movq -0x58(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
cvtsi2sdq -0x38(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movl $0x1, %esi
callq 0x7560
movsd -0x60(%rbp), %xmm1
movq 0x8(%rax), %rax
subq $0x1, %rax
cvtsi2sd %rax, %xmm0
movsd 0x5d93(%rip), %xmm2     # 0x9018
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movq $0x2, -0x50(%rbp)
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rax
movq %rax, -0x68(%rbp)
callq 0x3ec0
movq %rax, %rcx
movq -0x68(%rbp), %rax
cmpq %rcx, %rax
jae 0x338a
movsd -0x20(%rbp), %xmm1
movsd -0x48(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x32f3
movsd -0x20(%rbp), %xmm0
movsd -0x30(%rbp), %xmm1
movsd -0x28(%rbp), %xmm2
movsd -0x48(%rbp), %xmm3
movsd -0x40(%rbp), %xmm4
callq 0x7580
movsd %xmm0, -0x8(%rbp)
jmp 0x33ad
movq -0x58(%rbp), %rdi
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x50(%rbp), %rsi
subq $0x1, %rsi
callq 0x7560
movq -0x58(%rbp), %rdi
movq 0x8(%rax), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x50(%rbp), %rsi
callq 0x7560
movq -0x58(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
cvtsi2sdq -0x38(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x50(%rbp), %rsi
callq 0x7560
movsd -0x70(%rbp), %xmm1
movq 0x8(%rax), %rax
subq $0x1, %rax
cvtsi2sd %rax, %xmm0
movsd 0x5cac(%rip), %xmm2     # 0x9018
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x329a
movsd -0x20(%rbp), %xmm0
movsd -0x30(%rbp), %xmm1
movsd -0x28(%rbp), %xmm2
movsd -0x48(%rbp), %xmm3
movsd -0x40(%rbp), %xmm4
callq 0x7580
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)