// File: /workspace/github_c_makefile_star2_O1/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
xorps %xmm0, %xmm0
testl %esi, %esi
jle 0x859c
movq %rdi, %rbx
movl %esi, %r14d
xorl %r15d, %r15d
movq %rdi, 0x30(%rsp)
movq %r14, 0x28(%rsp)
imulq $0x68, %r15, %r12
cmpb $0x0, 0xc(%rbx,%r12)
jne 0x8590
addq %rbx, %r12
cmpb $0x1, 0xd(%r12)
jne 0x8590
movq 0x20(%r12), %r11
testq %r11, %r11
movsd %xmm0, 0x18(%rsp)
je 0x824c
movl 0x18(%r12), %eax
testl %eax, %eax
jle 0x8275
movq 0x10(%r12), %rcx
movl (%rcx), %ecx
jmp 0x827c
movq 0x28(%r12), %rdi
leaq 0x30(%r12), %rsi
movl 0x8(%r12), %ecx
addq $0x34, %r12
xorps %xmm0, %xmm0
movq %r12, %rdx
callq 0x77e7
movsd 0x18(%rsp), %xmm0
jmp 0x8590
movl 0x8(%r12), %ecx
incl %ecx
testl %ecx, %ecx
je 0x8287
movb $0x1, %sil
xorl %edx, %edx
jmp 0x82a4
cmpl $0x2, %eax
jl 0x8296
movq 0x10(%r12), %rcx
movl 0x4(%rcx), %ecx
jmp 0x829d
movl 0x8(%r12), %ecx
incl %ecx
movl $0x1, %edx
xorl %esi, %esi
movl 0x8(%r12), %edi
movl $0x1, %ebx
cmpq $0x2, %rdi
jb 0x830b
leal 0x1(%rdi), %r8d
movl $0x1, %r9d
movl %ecx, %r10d
cmpq %r10, %r9
jne 0x82e0
incl %ebx
xorb $0x1, %sil
incl %edx
cmpl %eax, %edx
jge 0x8300
movq 0x10(%r12), %rcx
movslq %edx, %r10
movl (%rcx,%r10,4), %ecx
jmp 0x8303
testb $0x1, %sil
je 0x8303
movq 0x8(%r11), %r10
cmpw $0x0, (%r10,%r9,2)
jne 0x82fc
movq (%r11), %r10
cmpb $0x0, (%r10,%r9)
je 0x8303
incl %ebx
jmp 0x8303
movl %r8d, %ecx
incq %r9
cmpq %r9, %rdi
jne 0x82be
cmpl 0x34(%r12), %ebx
movq %r11, 0x20(%rsp)
jbe 0x8355
movq 0x28(%r12), %rax
movq (%rax), %rdi
movl %ebx, %r13d
shlq $0x2, %r13
movq %r13, %rsi
callq 0x3915
movq 0x28(%r12), %rcx
movq %rax, (%rcx)
movq 0x8(%rcx), %rdi
movq %r13, %rsi
callq 0x3915
movq 0x20(%rsp), %r11
movq 0x28(%r12), %rcx
movq %rax, 0x8(%rcx)
movl %ebx, 0x34(%r12)
movq %r15, 0x38(%rsp)
movl %ebx, 0x30(%r12)
movl 0x18(%r12), %eax
testl %eax, %eax
jle 0x8372
movq 0x10(%r12), %rcx
movl (%rcx), %r15d
jmp 0x837a
movl 0x8(%r12), %r15d
incl %r15d
movb $0x1, %r13b
movl $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
testl %r15d, %r15d
jne 0x83b5
cmpl $0x2, %eax
jl 0x83a2
movq 0x10(%r12), %rax
movl 0x4(%rax), %r15d
jmp 0x83aa
movl 0x8(%r12), %r15d
incl %r15d
movl $0x1, 0xc(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x14(%rsp)
movb $0x0, 0xb(%rsp)
movq (%r11), %rax
movq 0x8(%r11), %rcx
movswl (%rcx), %edi
movzbl (%rax), %esi
leaq 0x14(%rsp), %rdx
leaq 0xb(%rsp), %rcx
callq 0x7aca
movl $0x1, %ebx
cmpl $0x2, 0x8(%r12)
jb 0x84f0
xorl %r14d, %r14d
movl $0x1, %ebx
movl $0x0, 0x10(%rsp)
movq 0x20(%rsp), %rsi
movl %r15d, %ebp
cmpq %rbp, %rbx
je 0x8426
testb $0x1, %r13b
je 0x8479
movq 0x8(%rsi), %rax
cmpw $0x0, (%rax,%rbx,2)
jne 0x8426
movq (%rsi), %rax
cmpb $0x0, (%rax,%rbx)
je 0x8479
movq 0x28(%r12), %rcx
movq (%rcx), %rdx
movl %r14d, %eax
movl %ebx, (%rdx,%rax,4)
movq 0x8(%rcx), %rcx
testb $0x1, %r13b
je 0x846b
movl %ebx, %edx
subl 0x10(%rsp), %edx
xorps %xmm1, %xmm1
cvtsi2ss %rdx, %xmm1
movss %xmm0, (%rcx,%rax,4)
mulss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x18(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x18(%rsp)
jmp 0x8472
movl $0x0, (%rcx,%rax,4)
incl %r14d
movl %ebx, 0x10(%rsp)
movq 0x8(%rsi), %rax
movzwl (%rax,%rbx,2), %eax
testw %ax, %ax
jne 0x848f
movq (%rsi), %rcx
cmpb $0x0, (%rcx,%rbx)
je 0x84ad
movq (%rsi), %rcx
movzbl (%rcx,%rbx), %esi
movswl %ax, %edi
leaq 0x14(%rsp), %rdx
leaq 0xb(%rsp), %rcx
callq 0x7aca
movq 0x20(%rsp), %rsi
cmpq %rbp, %rbx
jne 0x84dd
xorb $0x1, %r13b
movl 0xc(%rsp), %ecx
incl %ecx
cmpl 0x18(%r12), %ecx
movl %ecx, 0xc(%rsp)
jge 0x84d5
movq 0x10(%r12), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %r15d
jmp 0x84dd
movl 0x8(%r12), %r15d
incl %r15d
incq %rbx
movl 0x8(%r12), %eax
cmpq %rax, %rbx
jb 0x8404
jmp 0x84f8
xorl %r14d, %r14d
movq 0x20(%rsp), %rsi
movq 0x28(%r12), %rcx
movq (%rcx), %rdx
movl %r14d, %eax
movl %ebx, (%rdx,%rax,4)
movq 0x8(%rcx), %rcx
testb $0x1, %r13b
movq 0x38(%rsp), %r15
je 0x8545
movl %ebx, %edx
subl 0x10(%rsp), %edx
xorps %xmm1, %xmm1
cvtsi2ss %rdx, %xmm1
movss %xmm0, (%rcx,%rax,4)
mulss %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
jmp 0x854c
movl $0x0, (%rcx,%rax,4)
movq 0x28(%rsp), %r14
incl %eax
cmpl 0x30(%r12), %eax
jne 0x85ba
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
movl %ebx, %edx
movswl (%rcx,%rdx,2), %edi
movzbl (%rax,%rdx), %esi
leaq 0x14(%rsp), %rdx
leaq 0xb(%rsp), %rcx
callq 0x7aca
ucomiss 0x9bf3(%rip), %xmm0     # 0x12174
jne 0x85c7
jp 0x85c7
movsd 0x18(%rsp), %xmm0
movq 0x30(%rsp), %rbx
incq %r15
cmpq %r14, %r15
jne 0x820b
ucomisd 0x9bac(%rip), %xmm0     # 0x12150
jne 0x85a8
jnp 0x85d4
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x2d63
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x2d92
callq 0x2dbe