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

  // create pileup for each chrom
  double fragLen = 0.0;  // weighted fragment length
  for (int i = 0; i < chromLen; i++) {
    Chrom* chr = chrom + i;
    if (chr->skip || ! chr->save)
      continue;

    // if no read coverage, save constant pileup of 0
    if (chr->diff == NULL) {
      saveConst(chr->expt, &chr->exptLen, &chr->exptMem,
        chr->len, 0.0f);
      continue;
    }

    // determine number of pileup intervals
    int bedIdx = 0;     // index into chr->bed array
    uint32_t bedPos = bedIdx < chr->bedLen ?
      chr->bed[bedIdx] : chr->len + 1;  // position of BED interval
    bool save = true;
    if (bedPos == 0) {
      // first BED interval starts at 0 (should be skipped)
      save = false;
      bedIdx++;
      bedPos = bedIdx < chr->bedLen ?
        chr->bed[bedIdx] : chr->len + 1;
    }
    Diff* d = chr->diff;
    uint32_t num = 1;   // number of intervals
    for (uint32_t j = 1; j < chr->len; j++)
      if (j == bedPos) {
        num++;
        save = ! save;
        bedIdx++;
        bedPos = bedIdx < chr->bedLen ?
          chr->bed[bedIdx] : chr->len + 1;
      } else if (save && (d->cov[j] || d->frac[j]))
        num++;

    // expand pileup arrays (if necessary)
    if (num > chr->exptMem) {
      chr->expt->end = (uint32_t*) memrealloc(chr->expt->end,
        num * sizeof(uint32_t));
      chr->expt->cov = (float*) memrealloc(chr->expt->cov,
        num * sizeof(float));
      chr->exptMem = num;
    }
    chr->exptLen = num;

    // reset BED interval values
    bedIdx = 0; // index into chr->bed array
    bedPos = bedIdx < chr->bedLen ?
      chr->bed[bedIdx] : chr->len + 1;
    save = true;
    if (bedPos == 0) {
      save = false;
      bedIdx++;
      bedPos = bedIdx < chr->bedLen ?
        chr->bed[bedIdx] : chr->len + 1;
    }

    // initialize pileup values
    int32_t cov = 0;      // current pileup value
    uint8_t frac = 0;     // current pileup value (fraction part)
    float val = updateVal(d->cov[0], d->frac[0], &cov, &frac);

    // save pileup values along the chrom
    uint32_t start = 0;   // beginning coordinate of interval
    uint32_t pos = 0;     // position in pileup arrays
    uint32_t j;
    for (j = 1; j < chr->len; j++) {

      if (j == bedPos || (save && (d->cov[j] || d->frac[j]))) {
        // save end of interval and pileup value
        chr->expt->end[pos] = j;
        if (save) {
          chr->expt->cov[pos] = val;
          fragLen += (j - start) * val; // frag. length weighted by val
        } else
          chr->expt->cov[pos] = 0.0f;
        pos++;
        start = j;
      }

      // update pileup value
      if (d->cov[j] || d->frac[j])
        val = updateVal(d->cov[j], d->frac[j], &cov, &frac);

      // update 'save' status (from BED intervals)
      if (j == bedPos) {
        save = ! save;
        bedIdx++;
        bedPos = bedIdx < chr->bedLen ?
          chr->bed[bedIdx] : chr->len + 1;
      }

    }

    // save final interval
    chr->expt->end[pos] = j;
    if (save) {
      chr->expt->cov[pos] = val;
      fragLen += (j - start) * val;
    } else
      chr->expt->cov[pos] = 0.0f;

    // verify array length
    if (pos + 1 != chr->exptLen) {
      char msg[MAX_ALNS];
      sprintf(msg, "%s (%s)", errMsg[ERRARR], chr->name);
      exit(error(msg, ERRISSUE));
    }

    // check for val error (should end at 0)
    val = updateVal(d->cov[j], d->frac[j], &cov, &frac);
    if (val) {
      char msg[MAX_ALNS];
      sprintf(msg, "Experimental pileup for ref %s finishes at %f (not 0.0)",
        chr->name, val);
      exit(error(msg, ERRISSUE));
    }
  }

  if (fragLen == 0.0)
    exit(error("", ERREXPT));
  return fragLen;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl %esi, %ebx
movq %rdi, %r15
xorl %r14d, %r14d
testl %esi, %esi
cmovlel %r14d, %ebx
xorps %xmm0, %xmm0
movq %rbx, 0x30(%rsp)
movq %rdi, 0x38(%rsp)
cmpq %rbx, %r14
je 0x70f8
imulq $0x68, %r14, %r13
cmpb $0x0, 0xc(%r15,%r13)
jne 0x70f0
addq %r15, %r13
cmpb $0x1, 0xd(%r13)
jne 0x70f0
movq 0x20(%r13), %r11
testq %r11, %r11
movsd %xmm0, 0x10(%rsp)
je 0x6deb
movl 0x18(%r13), %eax
testl %eax, %eax
jle 0x6e11
movq 0x10(%r13), %rcx
movl (%rcx), %edx
jmp 0x6e17
movq 0x28(%r13), %rdi
leaq 0x30(%r13), %rsi
movl 0x8(%r13), %ecx
addq $0x34, %r13
xorps %xmm0, %xmm0
movq %r13, %rdx
callq 0x633b
movsd 0x10(%rsp), %xmm0
jmp 0x70f0
movl 0x8(%r13), %edx
incl %edx
xorl %ecx, %ecx
testl %edx, %edx
setne %sil
jne 0x6e38
cmpl $0x2, %eax
jl 0x6e2f
movq 0x10(%r13), %rcx
movl 0x4(%rcx), %edx
jmp 0x6e35
movl 0x8(%r13), %edx
incl %edx
pushq $0x1
popq %rcx
movl 0x8(%r13), %edi
leal 0x1(%rdi), %r8d
pushq $0x1
popq %r9
pushq $0x1
popq %rbx
cmpq %rdi, %r9
jae 0x6e9c
movl %edx, %r10d
cmpq %r10, %r9
jne 0x6e6d
incl %ebx
xorb $0x1, %sil
incl %ecx
cmpl %eax, %ecx
jge 0x6e90
movq 0x10(%r13), %rdx
movslq %ecx, %r10
movl (%rdx,%r10,4), %edx
jmp 0x6e97
testb $0x1, %sil
je 0x6e95
movq 0x8(%r11), %rsi
cmpw $0x0, (%rsi,%r9,2)
jne 0x6e89
movq (%r11), %rsi
cmpb $0x0, (%rsi,%r9)
je 0x6e8b
incl %ebx
movb $0x1, %sil
jmp 0x6e97
movl %r8d, %edx
jmp 0x6e97
xorl %esi, %esi
incq %r9
jmp 0x6e47
cmpl 0x34(%r13), %ebx
movq %r11, 0x18(%rsp)
jbe 0x6ee4
movq 0x28(%r13), %rax
movq (%rax), %rdi
movl %ebx, %ebp
shlq $0x2, %rbp
movq %rbp, %rsi
callq 0x2776
movq 0x28(%r13), %rcx
movq %rax, (%rcx)
movq 0x8(%rcx), %rdi
movq %rbp, %rsi
callq 0x2776
movq 0x18(%rsp), %r11
movq 0x28(%r13), %rcx
movq %rax, 0x8(%rcx)
movl %ebx, 0x34(%r13)
movl 0x18(%r13), %eax
movq %r14, 0x28(%rsp)
movl %ebx, 0x30(%r13)
testl %eax, %eax
jle 0x6efa
movq 0x10(%r13), %rcx
movl (%rcx), %r14d
jmp 0x6f01
movl 0x8(%r13), %r14d
incl %r14d
movb $0x1, %r12b
movl $0x0, %ecx
testl %r14d, %r14d
jne 0x6f2a
cmpl $0x2, %eax
jl 0x6f1d
movq 0x10(%r13), %rax
movl 0x4(%rax), %r14d
jmp 0x6f24
movl 0x8(%r13), %r14d
incl %r14d
xorl %r12d, %r12d
pushq $0x1
popq %rcx
movl %ecx, 0x20(%rsp)
andl $0x0, 0xc(%rsp)
movb $0x0, 0xb(%rsp)
movq (%r11), %rax
movq 0x8(%r11), %rcx
movswl (%rcx), %edi
movzbl (%rax), %esi
leaq 0xc(%rsp), %rdx
leaq 0xb(%rsp), %rcx
callq 0x6647
xorl %edi, %edi
pushq $0x1
popq %rbx
xorl %ebp, %ebp
movl 0x8(%r13), %ecx
movl %ebp, %eax
cmpq %rcx, %rbx
jae 0x7065
movl %r14d, %r15d
cmpq %r15, %rbx
jne 0x6f91
movq 0x28(%r13), %rcx
movq (%rcx), %rdx
movl %ebx, (%rdx,%rax,4)
testb $0x1, %r12b
movq 0x18(%rsp), %rsi
jne 0x6fba
movq 0x8(%rcx), %rcx
andl $0x0, (%rcx,%rax,4)
jmp 0x6fe4
movq 0x18(%rsp), %rsi
movq 0x8(%rsi), %rcx
testb $0x1, %r12b
je 0x6fec
cmpw $0x0, (%rcx,%rbx,2)
jne 0x6fb0
movq (%rsi), %rdx
cmpb $0x0, (%rdx,%rbx)
je 0x6fec
movq 0x28(%r13), %rcx
movq (%rcx), %rdx
movl %ebx, (%rdx,%rax,4)
movq 0x8(%rcx), %rcx
movss %xmm0, (%rcx,%rax,4)
movl %ebx, %eax
subl %edi, %eax
cvtsi2ss %rax, %xmm1
mulss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x10(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
movl %ebx, %edi
incl %ebp
movq 0x8(%rsi), %rcx
movzwl (%rcx,%rbx,2), %eax
movq (%rsi), %rcx
movb (%rcx,%rbx), %cl
testw %ax, %ax
jne 0x6fff
testb %cl, %cl
je 0x7030
movl %r14d, 0x24(%rsp)
movl %r12d, %r14d
movq %r13, %r12
movl %edi, %r13d
movswl %ax, %edi
movzbl %cl, %esi
leaq 0xc(%rsp), %rdx
leaq 0xb(%rsp), %rcx
callq 0x6647
movl %r13d, %edi
movq %r12, %r13
movl %r14d, %r12d
movl 0x24(%rsp), %r14d
cmpq %r15, %rbx
jne 0x705d
xorb $0x1, %r12b
movl 0x20(%rsp), %ecx
incl %ecx
cmpl 0x18(%r13), %ecx
movl %ecx, 0x20(%rsp)
jge 0x7056
movq 0x10(%r13), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %r14d
jmp 0x705d
movl 0x8(%r13), %r14d
incl %r14d
incq %rbx
jmp 0x6f5b
movq 0x28(%r13), %rcx
movq (%rcx), %rdx
movl %ebx, (%rdx,%rax,4)
movq 0x8(%rcx), %rcx
testb $0x1, %r12b
je 0x70a1
movl %ebx, %edx
subl %edi, %edx
cvtsi2ss %rdx, %xmm1
movss %xmm0, (%rcx,%rax,4)
mulss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
jmp 0x70a5
andl $0x0, (%rcx,%rax,4)
movq 0x28(%rsp), %r14
movq 0x18(%rsp), %rcx
incl %ebp
cmpl 0x30(%r13), %ebp
jne 0x711b
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movswl (%rcx,%rbx,2), %edi
movzbl (%rax,%rbx), %esi
leaq 0xc(%rsp), %rdx
leaq 0xb(%rsp), %rcx
callq 0x6647
ucomiss 0x90ac(%rip), %xmm0     # 0x10188
movq 0x38(%rsp), %r15
jne 0x713e
jp 0x713e
movsd 0x10(%rsp), %xmm0
movq 0x30(%rsp), %rbx
incq %r14
jmp 0x6da5
movaps %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x7106
jnp 0x716f
movaps %xmm1, %xmm0
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10186(%rip), %rdx     # 0x172a8
movq (%r13), %rcx
leaq 0xa71e(%rip), %rsi      # 0x1184b
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2290
jmp 0x715c
movq (%r13), %rdx
cvtss2sd %xmm0, %xmm0
leaq 0xa739(%rip), %rsi      # 0x11886
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movb $0x1, %al
callq 0x2290
pushq $0x21
popq %rsi
movq %rbx, %rdi
callq 0x2724
pushq $0x1
popq %rdi
callq 0x22a0
leaq 0xa999(%rip), %rdi      # 0x11b0f
pushq $0x15
popq %rsi
jmp 0x7162