// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq %r9, %r12
movq %rcx, %r13
movl %edx, 0x3c(%rsp)
movl %esi, %r14d
movq %rdi, 0x40(%rsp)
movb 0x88(%rsp), %al
movb %al, 0xf(%rsp)
movq 0x80(%rsp), %rcx
movq %rcx, 0x30(%rsp)
leaq -0x1(%rcx), %r15
movq %r8, 0x28(%rsp)
movq %r8, %rdi
movq %r9, %rsi
xorl %edx, %edx
movq %r15, %rcx
callq 0x28ee
testq %r13, %r13
js 0x29d0
cvtsi2ss %r13, %xmm0
jmp 0x29e6
movq %r13, %rax
shrq %rax
andl $0x1, %r13d
orq %rax, %r13
cvtsi2ss %r13, %xmm0
addss %xmm0, %xmm0
callq 0x21d0
movss %xmm0, 0x20(%rsp)
movq 0x30(%rsp), %rbx
leaq 0x4(,%rbx,4), %rdi
callq 0x2752
movq %rax, %r13
movl $0x7f7fffff, (%rax,%rbx,4) # imm = 0x7F7FFFFF
pushq $0x1
popq %rbp
testq %r15, %r15
js 0x2b1a
testq %rbp, %rbp
js 0x2a25
cvtsi2ss %rbp, %xmm0
jmp 0x2a3c
movq %rbp, %rax
shrq %rax
movl %ebp, %ecx
andl $0x1, %ecx
orq %rax, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
movq 0x28(%rsp), %rax
movss (%rax,%r15,4), %xmm1
subss 0x20(%rsp), %xmm1
movss %xmm1, 0x10(%rsp)
movss %xmm0, 0x18(%rsp)
callq 0x21d0
addss 0x10(%rsp), %xmm0
movss 0x4(%r13,%r15,4), %xmm1
ucomiss %xmm0, %xmm1
movss %xmm1, 0x10(%rsp)
jbe 0x2aa1
movq 0x28(%rsp), %rax
movss (%rax,%r15,4), %xmm0
subss 0x20(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
movss 0x18(%rsp), %xmm0
callq 0x21d0
movaps %xmm0, %xmm1
addss 0x14(%rsp), %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jbe 0x2b07
movq 0x28(%rsp), %rax
movss (%rax,%r15,4), %xmm0
subss 0x20(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
movss 0x18(%rsp), %xmm0
callq 0x21d0
addss 0x14(%rsp), %xmm0
movss 0x10(%rsp), %xmm1
ucomiss %xmm0, %xmm1
movaps %xmm1, %xmm0
jbe 0x2b07
movq 0x28(%rsp), %rax
movss (%rax,%r15,4), %xmm0
subss 0x20(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
movss 0x18(%rsp), %xmm0
callq 0x21d0
addss 0x10(%rsp), %xmm0
movss %xmm0, (%r13,%r15,4)
addq (%r12,%r15,8), %rbp
decq %r15
jmp 0x2a10
movslq 0x3c(%rsp), %rax
movq %rax, 0x18(%rsp)
xorl %ebp, %ebp
testl %r14d, %r14d
cmovlel %ebp, %r14d
movq %r14, 0x20(%rsp)
movss 0xd62e(%rip), %xmm2     # 0x10168
cmpq 0x20(%rsp), %rbp
je 0x2bd0
imulq $0x68, %rbp, %r12
movq 0x40(%rsp), %rax
cmpb $0x0, 0xc(%rax,%r12)
jne 0x2bc8
addq 0x40(%rsp), %r12
movq 0x48(%r12), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %r15
testq %r15, %r15
je 0x2bc8
movq 0x50(%r12), %rax
movq 0x18(%rsp), %rcx
movl (%rax,%rcx,4), %r14d
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x2bc8
movq 0x8(%r15), %rax
movss (%rax,%rbx,4), %xmm0
ucomiss %xmm2, %xmm0
movaps %xmm2, %xmm1
jne 0x2b96
jnp 0x2bb5
movq 0x28(%rsp), %rdi
xorl %esi, %esi
movq 0x30(%rsp), %rdx
movq %r13, %rcx
callq 0x2940
movss 0xd5b6(%rip), %xmm2     # 0x10168
movaps %xmm0, %xmm1
movq 0x60(%r12), %rax
movq 0x8(%rax), %rax
movss %xmm1, (%rax,%rbx,4)
incq %rbx
jmp 0x2b7e
incq %rbp
jmp 0x2b3a
cmpb $0x0, 0xf(%rsp)
je 0x2bed
movq 0x30(%rsp), %rax
movss -0x4(%r13,%rax,4), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x2bed
jnp 0x2c03
movq %r13, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2040
movq 0x143ee(%rip), %rax     # 0x16ff8
movq (%rax), %rcx
leaq 0xe72d(%rip), %rdi      # 0x11341
pushq $0x1c
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
jmp 0x2bed