// File: /workspace/github_c_makefile_star2_O3/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 0x38a4a
movl %eax, %ebp
movslq %eax, %rbx
leaq (,%rbx,8), %rdi
leaq 0x226e7(%rip), %rsi     # 0xb17a3
movl $0x5, %edx
callq 0x3a0e1
movq %rax, %r14
leaq 0x1c88b(%rip), %rsi     # 0xab95b
movq %r12, %rdi
callq 0x40f0
testq %rax, %rax
je 0x8f19e
movq %rax, %r15
movq %r14, 0x10(%rsp)
testl %ebp, %ebp
jle 0x8f115
leaq 0x1ab8c(%rip), %r12     # 0xa9c80
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 0x8f0fc
movq %r15, %rdi
callq 0x42a0
leaq -0x54b31(%rip), %rcx    # 0x3a5f3
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 0x16c53(%rip), %xmm2    # 0xa5da0
movsd 0x163cb(%rip), %xmm0    # 0xa5520
ucomisd %xmm2, %xmm0
jae 0x8f1b4
decl %ebp
cvtsi2sd %ebp, %xmm1
ucomisd %xmm1, %xmm2
jae 0x8f1bb
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 0x8f1e5
decl %eax
shrl %eax
movsd (%r14,%rax,8), %xmm0
jmp 0x8f1c2
leaq 0x17065(%rip), %rdi     # 0xa620a
movq %r12, %rsi
xorl %eax, %eax
callq 0x4030
xorps %xmm0, %xmm0
jmp 0x8f1d6
movsd (%r14), %xmm0
jmp 0x8f1c2
movsd -0x8(%r14,%rbx,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0x3a180
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 0x8f211
mulsd 0x16b22(%rip), %xmm2    # 0xa5d18
leal -0x1(%rax), %ecx
subsd %xmm2, %xmm0
incl %eax
shrl %eax
mulsd (%r14,%rax,8), %xmm0
shrl %ecx
mulsd (%r14,%rcx,8), %xmm2
jmp 0x8f23a
addsd %xmm0, %xmm2
mulsd 0x16afb(%rip), %xmm2    # 0xa5d18
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 0x8f1c8