// File: /workspace/github_c_makefile_star2_O1/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 0x37e22
movl %eax, %ebp
movslq %eax, %rbx
leaq (,%rbx,8), %rdi
leaq 0x21df4(%rip), %rsi     # 0xae713
movl $0x5, %edx
callq 0x390f5
movq %rax, %r14
leaq 0x1bf5c(%rip), %rsi     # 0xa888f
movq %r12, %rdi
callq 0x40f0
testq %rax, %rax
je 0x8ca01
movq %rax, %r15
movq %r14, 0x10(%rsp)
testl %ebp, %ebp
jle 0x8c978
leaq 0x1a2b9(%rip), %r12     # 0xa6c10
movq %rbx, %r14
movq 0x10(%rsp), %r13
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x4070
addq $0x8, %r13
decq %r14
jne 0x8c95f
movq %r15, %rdi
callq 0x42a0
leaq -0x533a0(%rip), %rcx    # 0x395e7
movl $0x8, %edx
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4240
addl %ebp, %ebp
cvtsi2sd %ebp, %xmm2
mulsd 0x8(%rsp), %xmm2
divsd 0x163f0(%rip), %xmm2    # 0xa2da0
movsd 0x15b68(%rip), %xmm0    # 0xa2520
ucomisd %xmm2, %xmm0
jae 0x8ca17
decl %ebp
cvtsi2sd %ebp, %xmm1
ucomisd %xmm1, %xmm2
jae 0x8ca1e
cvttsd2si %xmm2, %eax
xorps %xmm1, %xmm1
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 0x8ca48
decl %eax
shrl %eax
movsd (%r14,%rax,8), %xmm0
jmp 0x8ca25
leaq 0x16802(%rip), %rdi     # 0xa320a
movq %r12, %rsi
xorl %eax, %eax
callq 0x4030
xorps %xmm0, %xmm0
jmp 0x8ca39
movsd (%r14), %xmm0
jmp 0x8ca25
movsd -0x8(%r14,%rbx,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0x39194
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 0x8ca74
mulsd 0x162bf(%rip), %xmm2    # 0xa2d18
leal -0x1(%rax), %ecx
subsd %xmm2, %xmm0
incl %eax
shrl %eax
mulsd (%r14,%rax,8), %xmm0
shrl %ecx
mulsd (%r14,%rcx,8), %xmm2
jmp 0x8ca9d
addsd %xmm0, %xmm2
mulsd 0x16298(%rip), %xmm2    # 0xa2d18
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movslq %ecx, %rax
subsd %xmm2, %xmm0
mulsd (%r14,%rax,8), %xmm0
mulsd -0x8(%r14,%rax,8), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
jmp 0x8ca2b