// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %rcx, %r12
movl %edx, %r13d
movl %esi, %r15d
movq %rdi, 0x30(%rsp)
movb 0x78(%rsp), %bl
movq 0x70(%rsp), %rcx
movq %rcx, 0x28(%rsp)
decq %rcx
movq %r8, 0x20(%rsp)
movq %r8, %rdi
movq %r9, %r14
movq %r9, %rsi
xorl %edx, %edx
callq 0x3a5e
testq %r12, %r12
movb %bl, 0xf(%rsp)
js 0x3b38
cvtsi2ss %r12, %xmm0
jmp 0x3b4e
movq %r12, %rax
shrq %rax
andl $0x1, %r12d
orq %rax, %r12
cvtsi2ss %r12, %xmm0
addss %xmm0, %xmm0
callq 0x21e0
movss %xmm0, 0x18(%rsp)
movq 0x28(%rsp), %rbx
leaq 0x4(,%rbx,4), %rdi
callq 0x3903
movq %rax, %r12
movl $0x7f7fffff, (%rax,%rbx,4) # imm = 0x7F7FFFFF
testq %rbx, %rbx
jle 0x3c91
movq %r14, %rbx
movl $0x1, %ebp
movq 0x28(%rsp), %r14
testq %rbp, %rbp
js 0x3b9a
xorps %xmm0, %xmm0
cvtsi2ss %rbp, %xmm0
jmp 0x3bb4
movq %rbp, %rax
shrq %rax
movl %ebp, %ecx
andl $0x1, %ecx
orq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
movq 0x20(%rsp), %rax
movss -0x4(%rax,%r14,4), %xmm1
subss 0x18(%rsp), %xmm1
movss %xmm1, 0x8(%rsp)
movss %xmm0, 0x10(%rsp)
callq 0x21e0
addss 0x8(%rsp), %xmm0
movss (%r12,%r14,4), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x3c14
movq 0x20(%rsp), %rax
movss -0x4(%rax,%r14,4), %xmm0
subss 0x18(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
movss 0x10(%rsp), %xmm0
callq 0x21e0
movaps %xmm0, %xmm1
addss 0x8(%rsp), %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jbe 0x3c7c
movq 0x20(%rsp), %rax
movss -0x4(%rax,%r14,4), %xmm0
subss 0x18(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
movss 0x10(%rsp), %xmm0
callq 0x21e0
movaps %xmm0, %xmm1
addss 0x8(%rsp), %xmm1
movss (%r12,%r14,4), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x3c7c
movq 0x20(%rsp), %rax
movss -0x4(%rax,%r14,4), %xmm0
subss 0x18(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
movss 0x10(%rsp), %xmm0
callq 0x21e0
addss 0x8(%rsp), %xmm0
movss %xmm0, -0x4(%r12,%r14,4)
addq -0x8(%rbx,%r14,8), %rbp
decq %r14
jg 0x3b8b
testl %r15d, %r15d
jle 0x3d47
movslq %r13d, %rax
movq %rax, 0x10(%rsp)
movl %r15d, %eax
movq %rax, 0x18(%rsp)
xorl %r15d, %r15d
movss 0xe4a3(%rip), %xmm2     # 0x12158
imulq $0x68, %r15, %r14
movq 0x30(%rsp), %rax
cmpb $0x0, 0xc(%rax,%r14)
jne 0x3d39
addq 0x30(%rsp), %r14
movq 0x48(%r14), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rax
testq %rax, %rax
je 0x3d39
movq 0x50(%r14), %rcx
movq 0x10(%rsp), %rdx
movl (%rcx,%rdx,4), %r13d
testq %r13, %r13
je 0x3d39
movq 0x8(%rax), %rbp
xorl %ebx, %ebx
movss (%rbp,%rbx,4), %xmm0
ucomiss %xmm2, %xmm0
movaps %xmm2, %xmm1
jne 0x3d05
jnp 0x3d24
movq 0x20(%rsp), %rdi
xorl %esi, %esi
movq 0x28(%rsp), %rdx
movq %r12, %rcx
callq 0x3ab0
movss 0xe437(%rip), %xmm2     # 0x12158
movaps %xmm0, %xmm1
movq 0x60(%r14), %rax
movq 0x8(%rax), %rax
movss %xmm1, (%rax,%rbx,4)
incq %rbx
cmpq %rbx, %r13
jne 0x3cf5
incq %r15
cmpq 0x18(%rsp), %r15
jne 0x3cb5
cmpb $0x0, 0xf(%rsp)
je 0x3d64
movq 0x28(%rsp), %rax
movss -0x4(%r12,%rax,4), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x3d64
jnp 0x3d7a
movq %r12, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2040
callq 0x2a4c
jmp 0x3d64