// File: /workspace/github_c_makefile_star2_O3/jsh58[P]Genrich/Genrich.c
// Function: saveQval
void saveQval(Chrom* chrom, int chromLen, int n,
    uint64_t genomeLen, float* pVal, uint64_t* pEnd,
    int64_t pLen, bool verbose) {

  // sort pileup by p-values
  quickSort(pVal, pEnd, 0, pLen - 1);

  // calculate q-values for each p-value: -log(q) = -log(p*N/k)
  uint64_t k = 1;  // 1 + number of bases with higher -log(p)
  float logN = -log10f(genomeLen);
  float* qVal = (float*) memalloc((pLen + 1) * sizeof(float));
  qVal[pLen] = FLT_MAX;
  for (int64_t i = pLen - 1; i > -1; i--) {
    // ensure monotonicity
    qVal[i] = MAX( MIN( pVal[i] + logN + log10f(k),
      qVal[i + 1]), 0.0f);
    k += pEnd[i];
  }

  // save pileups of q-values for each chrom
  for (int i = 0; i < chromLen; i++) {
    Chrom* chr = chrom + i;
    if (chr->skip || chr->pval[n] == NULL)
      continue;
    for (uint32_t j = 0; j < chr->pvalLen[n]; j++)
      if (chr->pval[n]->cov[j] == SKIP)
        chr->qval->cov[j] = SKIP; // skipped region
      else
        chr->qval->cov[j] = lookup(pVal, 0, pLen,
          qVal, chr->pval[n]->cov[j]);
  }

  // check if all q-values are 1
  if (verbose && qVal[pLen-1] == 0.0f)
    fprintf(stderr, "Warning! All q-values are 1\n");

  // free memory
  free(qVal);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r13
movq %r8, %rbx
movq %rcx, %r15
movl %edx, 0x18(%rsp)
movl %esi, %r12d
movq %rdi, 0x20(%rsp)
movb 0x68(%rsp), %bpl
movq 0x60(%rsp), %r14
leaq -0x1(%r14), %rcx
movq %r8, %rdi
movq %r9, %rsi
xorl %edx, %edx
callq 0x3a6e
testq %r15, %r15
movb %bpl, 0x7(%rsp)
movl %r12d, 0x1c(%rsp)
js 0x3b59
cvtsi2ss %r15, %xmm0
jmp 0x3b6f
movq %r15, %rax
shrq %rax
andl $0x1, %r15d
orq %rax, %r15
cvtsi2ss %r15, %xmm0
addss %xmm0, %xmm0
callq 0x21e0
movss %xmm0, 0xc(%rsp)
leaq 0x4(,%r14,4), %rdi
callq 0x3913
movq %rax, %r15
movl $0x7f7fffff, (%rax,%r14,4) # imm = 0x7F7FFFFF
testq %r14, %r14
jle 0x3c9b
movl $0x1, %ebp
movq %r14, %r12
testq %rbp, %rbp
js 0x3bb2
xorps %xmm0, %xmm0
cvtsi2ss %rbp, %xmm0
jmp 0x3bcc
movq %rbp, %rax
shrq %rax
movl %ebp, %ecx
andl $0x1, %ecx
orq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
movss -0x4(%rbx,%r12,4), %xmm1
subss 0xc(%rsp), %xmm1
movss %xmm1, 0x8(%rsp)
movss %xmm0, 0x14(%rsp)
callq 0x21e0
addss 0x8(%rsp), %xmm0
movss (%r15,%r12,4), %xmm1
ucomiss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp)
jbe 0x3c28
movss -0x4(%rbx,%r12,4), %xmm0
subss 0xc(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
movss 0x14(%rsp), %xmm0
callq 0x21e0
movaps %xmm0, %xmm1
addss 0x10(%rsp), %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jbe 0x3c86
movss -0x4(%rbx,%r12,4), %xmm0
subss 0xc(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
movss 0x14(%rsp), %xmm0
callq 0x21e0
addss 0x10(%rsp), %xmm0
movss 0x8(%rsp), %xmm1
ucomiss %xmm0, %xmm1
movaps %xmm1, %xmm0
jbe 0x3c86
movss -0x4(%rbx,%r12,4), %xmm0
subss 0xc(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
movss 0x14(%rsp), %xmm0
callq 0x21e0
addss 0x8(%rsp), %xmm0
movss %xmm0, -0x4(%r15,%r12,4)
addq -0x8(%r13,%r12,8), %rbp
decq %r12
jg 0x3ba3
movl 0x1c(%rsp), %ecx
testl %ecx, %ecx
movb 0x7(%rsp), %bpl
movq 0x20(%rsp), %r13
jle 0x3d79
movslq 0x18(%rsp), %rax
movl %ecx, %ecx
xorl %edx, %edx
movss 0xe48e(%rip), %xmm0     # 0x12150
imulq $0x68, %rdx, %r8
cmpb $0x0, 0xc(%r13,%r8)
jne 0x3d6d
addq %r13, %r8
movq 0x48(%r8), %rsi
movq (%rsi,%rax,8), %rdi
testq %rdi, %rdi
je 0x3d6d
movq 0x50(%r8), %rsi
movl (%rsi,%rax,4), %esi
testq %rsi, %rsi
je 0x3d6d
movq 0x8(%rdi), %rdi
movq 0x60(%r8), %r8
movq 0x8(%r8), %r8
xorl %r9d, %r9d
movss (%rdi,%r9,4), %xmm1
ucomiss %xmm0, %xmm1
movaps %xmm0, %xmm2
jne 0x3d11
jnp 0x3d5f
testq %r14, %r14
je 0x3d4c
movq %r14, %r10
xorl %r11d, %r11d
leaq (%r10,%r11), %r12
shrq %r12
movss (%rbx,%r12,4), %xmm2
ucomiss %xmm1, %xmm2
jne 0x3d30
jnp 0x3d51
jbe 0x3d3f
decq %r12
movq %r12, %r10
cmpq %r12, %r11
jne 0x3d1c
jmp 0x3d56
incq %r12
movq %r12, %r11
cmpq %r10, %r12
jne 0x3d1c
jmp 0x3d59
xorl %r10d, %r10d
jmp 0x3d59
movq %r12, %r10
jmp 0x3d59
movq %r11, %r10
movss (%r15,%r10,4), %xmm2
movss %xmm2, (%r8,%r9,4)
incq %r9
cmpq %rsi, %r9
jne 0x3d01
incq %rdx
cmpq %rcx, %rdx
jne 0x3cc2
testb %bpl, %bpl
je 0x3d8f
movss -0x4(%r15,%r14,4), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x3d8f
jnp 0x3da5
movq %r15, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2040
callq 0x2a75
jmp 0x3d8f