// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/statistics.c
// Function: file_percentile
double file_percentile(char *file, double percentile)
{
 /*Last Changed 11.07.2005*/
 FILE *myfile;
 double *observations, pos, result, weight;
 int count, i, posi;
 count = file_length(file);
 observations = safemalloc(count * sizeof(double), "file_percentile", 5);
 if ((myfile = fopen(file, "r")) == NULL)
  {
   printf(m1274, file);
   return 0;
  }
 for (i = 0; i < count; i++)
   fscanf(myfile, "%lf", &(observations[i]));
 fclose(myfile); 
 qsort(observations, count, sizeof(double), sort_function_double_ascending);
 pos = (2 * count * percentile) / 100;
 posi = (int) pos;
 if (pos <= 1)
   result = observations[0];
 else
   if (pos >= 2 * count - 1)
     result = observations[count - 1];
   else
     if (pos == posi && posi % 2 == 1)
       result = observations[(posi - 1) / 2];
     else
       if (posi % 2 == 1)
        {
         weight = (pos - posi) / 2;
         result = weight * observations[(posi - 1) / 2] + (1 - weight) * observations[(posi + 1) / 2];
        }
       else
        {
         weight = (pos - posi + 1) / 2;
         result = weight * observations[posi / 2 - 1] + (1 - weight) * observations[posi / 2];
        }
 safe_free(observations);
 return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movsd %xmm0, 0x8(%rsp)
movq %rdi, %r12
callq 0x33f62
movl %eax, %ebp
movslq %eax, %r14
leaq (,%r14,8), %rdi
leaq 0x20186(%rip), %rsi     # 0xa1703
pushq $0x5
popq %rdx
callq 0x34f59
movq %rax, %rbx
leaq 0x1a2f0(%rip), %rsi     # 0x9b87f
movq %r12, %rdi
callq 0x40f0
testq %rax, %rax
je 0x81661
movq %rax, %r15
movq %r14, 0x10(%rsp)
testl %ebp, %ebp
movl $0x0, %r13d
cmovgl %ebp, %r13d
shlq $0x3, %r13
leaq 0x185e9(%rip), %r12     # 0x99ba8
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x815de
leaq (%rbx,%r14), %rdx
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x4070
addq $0x8, %r14
jmp 0x815c2
movq %r15, %rdi
callq 0x42a0
leaq -0x4c22e(%rip), %rcx    # 0x353bf
pushq $0x8
popq %rdx
movq %rbx, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x4240
addl %ebp, %ebp
cvtsi2sd %ebp, %xmm2
mulsd 0x8(%rsp), %xmm2
divsd 0x14794(%rip), %xmm2    # 0x95da8
movsd 0x13f04(%rip), %xmm0    # 0x95520
ucomisd %xmm2, %xmm0
jae 0x81677
decl %ebp
cvtsi2sd %ebp, %xmm1
ucomisd %xmm1, %xmm2
jae 0x8167d
cvttsd2si %xmm2, %eax
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm2
setp %cl
setne %dl
orb %cl, %dl
movl %eax, %ecx
andl $0x80000001, %ecx       # imm = 0x80000001
cmpl $0x1, %ecx
setne %sil
orb %dl, %sil
jne 0x816a7
decl %eax
shrl %eax
movsd (%rbx,%rax,8), %xmm0
jmp 0x81684
leaq 0x14ba2(%rip), %rdi     # 0x9620a
movq %r12, %rsi
xorl %eax, %eax
callq 0x4030
xorps %xmm0, %xmm0
jmp 0x81698
movsd (%rbx), %xmm0
jmp 0x81684
movsd -0x8(%rbx,%r14,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %rbx, %rdi
callq 0x34ff7
movsd 0x8(%rsp), %xmm0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
subsd %xmm1, %xmm2
cmpl $0x1, %ecx
jne 0x816d1
mulsd 0x14668(%rip), %xmm2    # 0x95d20
leal -0x1(%rax), %ecx
subsd %xmm2, %xmm0
incl %eax
shrl %eax
mulsd (%rbx,%rax,8), %xmm0
shrl %ecx
mulsd (%rbx,%rcx,8), %xmm2
jmp 0x816f4
addsd %xmm0, %xmm2
mulsd 0x14643(%rip), %xmm2    # 0x95d20
pushq $0x2
popq %rcx
cltd
idivl %ecx
cltq
subsd %xmm2, %xmm0
mulsd (%rbx,%rax,8), %xmm0
mulsd -0x8(%rbx,%rax,8), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
jmp 0x8168a