// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movq %rdx, -0x38(%rbp)
testl %esi, %esi
jle 0x4a24
movq %rdi, %rbx
movq -0x38(%rbp), %rax
leal 0x1(%rax), %ecx
movslq %ecx, %rdx
leaq (,%rdx,8), %rdi
movq %rdi, -0x48(%rbp)
movslq %eax, %rdi
shlq $0x2, %rdx
movq %rdx, -0x68(%rbp)
movl %esi, %r15d
movq %rdi, -0x40(%rbp)
movl %edi, %r13d
xorl %r12d, %r12d
leaq 0xf(,%rcx,4), %rax
shlq $0x2, %rcx
movq %rcx, -0x50(%rbp)
andq $-0x10, %rax
movq %rax, -0x58(%rbp)
movq %rbx, -0x70(%rbp)
movq %r15, -0x60(%rbp)
movq %r13, -0x88(%rbp)
imulq $0x68, %r12, %r14
cmpb $0x0, 0xc(%rbx,%r14)
jne 0x4a18
addq %rbx, %r14
cmpl $0x0, -0x38(%rbp)
jle 0x487c
movq 0x48(%r14), %rcx
xorl %eax, %eax
cmpq $0x0, (%rcx,%rax,8)
jne 0x487e
incq %rax
cmpq %rax, %r13
jne 0x486b
jmp 0x4883
xorl %eax, %eax
cmpl -0x38(%rbp), %eax
jne 0x48a5
movq 0x48(%r14), %rdi
movq -0x48(%rbp), %rsi
callq 0x3925
movq %rax, 0x48(%r14)
movq -0x40(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
jmp 0x4a18
movq %r12, -0x80(%rbp)
movq %r14, %rdi
movq -0x38(%rbp), %rbx
movq %r14, -0x30(%rbp)
movl %ebx, %esi
callq 0x4730
movl %eax, %r15d
movq -0x30(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x48(%rbp), %rsi
callq 0x3925
movq -0x30(%rbp), %rcx
movq %rax, 0x48(%rcx)
movl $0x10, %edi
callq 0x3913
movq -0x30(%rbp), %rcx
movq 0x48(%rcx), %rcx
movq -0x40(%rbp), %r14
movq %rax, (%rcx,%r14,8)
movl %r15d, %r12d
shlq $0x2, %r12
movq %r12, %rdi
callq 0x3913
movq -0x30(%rbp), %rcx
movq 0x48(%rcx), %rcx
movq (%rcx,%r14,8), %rcx
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x3913
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 -0x68(%rbp), %rsi
callq 0x3925
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
testl %ebx, %ebx
js 0x496b
movq %r12, %rdi
xorl %esi, %esi
movq -0x50(%rbp), %rdx
callq 0x2140
movq -0x30(%rbp), %rsi
movl 0x8(%rsi), %eax
testl %eax, %eax
je 0x4a08
movl $0x1, %ebx
cmpl $0x0, -0x38(%rbp)
jle 0x49fe
movq 0x48(%rsi), %rdi
xorl %r15d, %r15d
xorl %eax, %eax
movq (%rdi,%r15,8), %rcx
testq %rcx, %rcx
je 0x49f3
movq (%rcx), %rdx
movl (%r12,%r15,4), %ecx
cmpl %ebx, (%rdx,%rcx,4)
jne 0x49f3
testb $0x1, %al
jne 0x49eb
movq -0x40(%rbp), %r13
movq (%rdi,%r13,8), %rax
movq (%rax), %rax
movl (%r12,%r13,4), %r14d
movl %ebx, (%rax,%r14,4)
movq -0x38(%rbp), %rsi
movq %r12, %rdx
callq 0x4698
movq -0x30(%rbp), %rsi
movq 0x48(%rsi), %rdi
movq (%rdi,%r13,8), %rax
movq 0x8(%rax), %rax
movss %xmm0, (%rax,%r14,4)
leal 0x1(%r14), %eax
movl %eax, (%r12,%r13,4)
movq -0x88(%rbp), %r13
movl (%r12,%r15,4), %ecx
incl %ecx
movl %ecx, (%r12,%r15,4)
movb $0x1, %al
incq %r15
cmpq %r15, %r13
jne 0x498a
movl 0x8(%rsi), %eax
incl %ebx
cmpl %eax, %ebx
jbe 0x497b
movq -0x78(%rbp), %rsp
movq -0x70(%rbp), %rbx
movq -0x60(%rbp), %r15
movq -0x80(%rbp), %r12
incq %r12
cmpq %r15, %r12
jne 0x484c
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq