// File: /workspace/github_c_makefile_star2_O2/jsh58[P]Genrich/Genrich.c
// Function: combinePval
void combinePval(Chrom* chrom, int chromLen, int n) {

  // combine p-value "pileups" for each chrom
  for (int i = 0; i < chromLen; i++) {
    Chrom* chr = chrom + i;
    if (chr->skip)
      continue;

    // make sure at least one pval array exists
    int j;
    for (j = 0; j < n; j++)
      if (chr->pval[j] != NULL)
        break;
    if (j == n) {
      // none exists: append another NULL
      chr->pval = (Pileup**) memrealloc(chr->pval,
        (n + 1) * sizeof(Pileup*));
      chr->pval[n] = NULL;
      continue;
    }

    // create additional 'pileup' array for combined p-values
    uint32_t num = countIntervals2(chr, n);
    chr->pval = (Pileup**) memrealloc(chr->pval,
      (n + 1) * sizeof(Pileup*));
    chr->pval[n] = (Pileup*) memalloc(sizeof(Pileup));
    chr->pval[n]->end = (uint32_t*) memalloc(num * sizeof(uint32_t));
    chr->pval[n]->cov = (float*) memalloc(num * sizeof(float));
    chr->pvalLen = (uint32_t*) memrealloc(chr->pvalLen,
      (n + 1) * sizeof(uint32_t));
    chr->pvalLen[n] = num;
    chr->sample++;

    // save combined p-values
    uint32_t idx[n + 1];  // indexes into each pval array
    for (int j = 0; j <= n; j++)
      idx[j] = 0;
    for (uint32_t k = 1; k <= chr->len; k++) {
      bool add = false;
      for (int j = 0; j < n; j++)
        if (chr->pval[j] != NULL
            && chr->pval[j]->end[idx[j]] == k) {
          if (! add) {
            chr->pval[n]->end[idx[n]] = k;
            chr->pval[n]->cov[idx[n]]
              = multPval(chr->pval, n, idx);
            idx[n]++;
            add = true;
          }
          idx[j]++;
        }

    }

  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %ebx
movl %esi, %r14d
movq %rdi, %r15
leal 0x1(%rbx), %eax
movslq %edx, %rcx
leaq 0x8(,%rcx,8), %rdx
movq %rdx, -0x40(%rbp)
xorl %edx, %edx
testl %ecx, %ecx
movl $0x0, %r13d
cmovgl %ebx, %r13d
movq %rcx, -0x38(%rbp)
leaq 0x4(,%rcx,4), %rcx
movq %rcx, -0x60(%rbp)
testl %esi, %esi
cmovlel %edx, %r14d
leaq 0xf(,%rax,4), %rcx
xorl %eax, %eax
andq $-0x10, %rcx
movq %rcx, -0x58(%rbp)
movq %rbx, -0x50(%rbp)
movq %r14, -0x70(%rbp)
movq %rdi, -0x68(%rbp)
movq %r13, -0x80(%rbp)
cmpq %r14, %rax
je 0x380e
imulq $0x68, %rax, %rdx
cmpb $0x0, 0xc(%r15,%rdx)
jne 0x3806
movq %rax, -0x48(%rbp)
addq %r15, %rdx
xorl %eax, %eax
cmpq %rax, %r13
je 0x367f
movq 0x48(%rdx), %rcx
cmpq $0x0, (%rcx,%rax,8)
jne 0x3682
incq %rax
jmp 0x366a
movl %r13d, %eax
cmpl %ebx, %eax
jne 0x36ae
movq 0x48(%rdx), %rdi
movq %rbx, %r12
movq %rdx, %rbx
movq -0x40(%rbp), %rsi
callq 0x2776
movq %rax, 0x48(%rbx)
movq %r12, %rbx
movq -0x38(%rbp), %rcx
andq $0x0, (%rax,%rcx,8)
jmp 0x3802
movq %rdx, %rdi
movq %rdx, -0x30(%rbp)
movl %ebx, %esi
callq 0x3549
movl %eax, %r15d
movq -0x30(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x2776
movq -0x30(%rbp), %rcx
movq %rax, 0x48(%rcx)
pushq $0x10
popq %rdi
callq 0x2752
movq -0x30(%rbp), %rcx
movq 0x48(%rcx), %rcx
movq -0x38(%rbp), %r14
movq %rax, (%rcx,%r14,8)
movl %r15d, %r12d
shlq $0x2, %r12
movq %r12, %rdi
callq 0x2752
movq -0x30(%rbp), %rcx
movq 0x48(%rcx), %rcx
movq (%rcx,%r14,8), %rcx
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x2752
movq -0x30(%rbp), %rcx
movq 0x48(%rcx), %rcx
movq (%rcx,%r14,8), %rcx
movq %rax, 0x8(%rcx)
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x60(%rbp), %rsi
callq 0x2776
movq -0x30(%rbp), %rsi
movq %rsp, -0x78(%rbp)
movq %rax, 0x50(%rsi)
movl %r15d, (%rax,%r14,4)
incb 0x58(%rsi)
movq %rsp, %r12
subq -0x58(%rbp), %r12
movq %r12, %rsp
xorl %eax, %eax
cmpq -0x38(%rbp), %rax
jg 0x3766
andl $0x0, (%r12,%rax,4)
incq %rax
jmp 0x3756
pushq $0x1
popq %rbx
cmpl 0x8(%rsi), %ebx
ja 0x37f2
xorl %r15d, %r15d
xorl %eax, %eax
cmpq %r15, %r13
je 0x37eb
movq 0x48(%rsi), %rdi
movq (%rdi,%r15,8), %rcx
testq %rcx, %rcx
je 0x37e6
movq (%rcx), %rdx
movl (%r12,%r15,4), %ecx
cmpl %ebx, (%rdx,%rcx,4)
jne 0x37e6
testb $0x1, %al
jne 0x37de
movq -0x38(%rbp), %r13
movq (%rdi,%r13,8), %rax
movq (%rax), %rax
movl (%r12,%r13,4), %r14d
movl %ebx, (%rax,%r14,4)
movq -0x50(%rbp), %rsi
movq %r12, %rdx
callq 0x34bd
movq -0x30(%rbp), %rsi
movq 0x48(%rsi), %rax
movq (%rax,%r13,8), %rax
movq 0x8(%rax), %rax
movss %xmm0, (%rax,%r14,4)
leal 0x1(%r14), %eax
movl %eax, (%r12,%r13,4)
movq -0x80(%rbp), %r13
movl (%r12,%r15,4), %ecx
incl %ecx
movl %ecx, (%r12,%r15,4)
movb $0x1, %al
incq %r15
jmp 0x3777
incl %ebx
jmp 0x3769
movq -0x78(%rbp), %rsp
movq -0x50(%rbp), %rbx
movq -0x70(%rbp), %r14
movq -0x68(%rbp), %r15
movq -0x48(%rbp), %rax
incq %rax
jmp 0x3648
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq