// File: /workspace/github_c_makefile_star2_O3/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
testl %esi, %esi
jle 0x8682
movq %rdi, %rbx
movl %esi, %r14d
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r15d, %r15d
movq %rdi, 0x30(%rsp)
movq %r14, 0x20(%rsp)
imulq $0x68, %r15, %r12
cmpb $0x0, 0xc(%rbx,%r12)
jne 0x8635
addq %rbx, %r12
cmpb $0x1, 0xd(%r12)
jne 0x8635
movq 0x20(%r12), %r11
testq %r11, %r11
je 0x82e3
movl 0x18(%r12), %eax
testl %eax, %eax
jle 0x8306
movq 0x10(%r12), %rcx
movl (%rcx), %edx
jmp 0x830d
movq 0x28(%r12), %rdi
leaq 0x30(%r12), %rsi
movl 0x8(%r12), %ecx
addq $0x34, %r12
xorps %xmm0, %xmm0
movq %r12, %rdx
callq 0x7801
jmp 0x8635
movl 0x8(%r12), %edx
incl %edx
xorl %ecx, %ecx
testl %edx, %edx
setne %sil
jne 0x8332
cmpl $0x2, %eax
jl 0x8326
movq 0x10(%r12), %rcx
movl 0x4(%rcx), %edx
jmp 0x832d
movl 0x8(%r12), %edx
incl %edx
movl $0x1, %ecx
movl 0x8(%r12), %edi
movl $0x1, %ebx
cmpq $0x2, %rdi
jb 0x83a0
leal 0x1(%rdi), %r8d
movl $0x1, %r9d
movl %edx, %r10d
cmpq %r10, %r9
jne 0x836e
incl %ebx
xorb $0x1, %sil
incl %ecx
cmpl %eax, %ecx
jge 0x8391
movq 0x10(%r12), %rdx
movslq %ecx, %r10
movl (%rdx,%r10,4), %edx
jmp 0x8398
testb $0x1, %sil
je 0x8396
movq 0x8(%r11), %rsi
cmpw $0x0, (%rsi,%r9,2)
jne 0x838a
movq (%r11), %rsi
cmpb $0x0, (%rsi,%r9)
je 0x838c
incl %ebx
movb $0x1, %sil
jmp 0x8398
movl %r8d, %edx
jmp 0x8398
xorl %esi, %esi
incq %r9
cmpq %r9, %rdi
jne 0x834c
cmpl 0x34(%r12), %ebx
movq %r11, 0x18(%rsp)
jbe 0x83ef
movq 0x28(%r12), %rax
movq (%rax), %rdi
movl %ebx, %r13d
shlq $0x2, %r13
movq %r13, %rsi
callq 0x3925
movq 0x28(%r12), %rcx
movq %rax, (%rcx)
movq 0x8(%rcx), %rdi
movq %r13, %rsi
callq 0x3925
movq 0x18(%rsp), %r11
movq 0x28(%r12), %rcx
movq %rax, 0x8(%rcx)
movl %ebx, 0x34(%r12)
movl 0x18(%r12), %eax
movl %ebx, 0x30(%r12)
testl %eax, %eax
jle 0x8402
movq 0x10(%r12), %rcx
movl (%rcx), %r13d
jmp 0x840a
movl 0x8(%r12), %r13d
incl %r13d
movb $0x1, %bpl
movl $0x0, 0x8(%rsp)
testl %r13d, %r13d
jne 0x843c
cmpl $0x2, %eax
jl 0x842a
movq 0x10(%r12), %rax
movl 0x4(%rax), %r13d
jmp 0x8432
movl 0x8(%r12), %r13d
incl %r13d
movl $0x1, 0x8(%rsp)
xorl %ebp, %ebp
movq %r15, 0x28(%rsp)
movl $0x0, 0xc(%rsp)
movb $0x0, 0x7(%rsp)
movq (%r11), %rax
movq 0x8(%r11), %rcx
movswl (%rcx), %edi
movzbl (%rax), %esi
leaq 0xc(%rsp), %rdx
leaq 0x7(%rsp), %rcx
callq 0x7adf
movl $0x1, %ebx
cmpl $0x2, 0x8(%r12)
jb 0x859b
xorl %r14d, %r14d
movl $0x1, %ebx
xorl %edi, %edi
movq 0x18(%rsp), %rsi
movq %r12, 0x38(%rsp)
movl %r13d, %r15d
cmpq %r15, %rbx
jne 0x84b8
movq 0x28(%r12), %rax
movq (%rax), %rdx
movl %r14d, %ecx
movl %ebx, (%rdx,%rcx,4)
testb $0x1, %bpl
jne 0x84e0
movq 0x8(%rax), %rax
movl $0x0, (%rax,%rcx,4)
jmp 0x850d
movq 0x8(%rsi), %rax
testb $0x1, %bpl
je 0x8516
cmpw $0x0, (%rax,%rbx,2)
jne 0x84d2
movq (%rsi), %rcx
cmpb $0x0, (%rcx,%rbx)
je 0x8516
movq 0x28(%r12), %rax
movq (%rax), %rdx
movl %r14d, %ecx
movl %ebx, (%rdx,%rcx,4)
movq 0x8(%rax), %rax
movss %xmm0, (%rax,%rcx,4)
movl %ebx, %eax
subl %edi, %eax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
mulss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x10(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
movl %ebx, %edi
incl %r14d
movq 0x8(%rsi), %rax
movzwl (%rax,%rbx,2), %eax
movq (%rsi), %rcx
movb (%rcx,%rbx), %cl
testw %ax, %ax
jne 0x8529
testb %cl, %cl
je 0x8558
movl %r13d, %r12d
movl %ebp, %r13d
movl %edi, %ebp
movswl %ax, %edi
movzbl %cl, %esi
leaq 0xc(%rsp), %rdx
leaq 0x7(%rsp), %rcx
callq 0x7adf
movl %ebp, %edi
movl %r13d, %ebp
movl %r12d, %r13d
movq 0x38(%rsp), %r12
movq 0x18(%rsp), %rsi
cmpq %r15, %rbx
jne 0x8588
xorb $0x1, %bpl
movl 0x8(%rsp), %ecx
incl %ecx
cmpl 0x18(%r12), %ecx
movl %ecx, 0x8(%rsp)
jge 0x8580
movq 0x10(%r12), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %r13d
jmp 0x8588
movl 0x8(%r12), %r13d
incl %r13d
incq %rbx
movl 0x8(%r12), %eax
cmpq %rax, %rbx
jb 0x848f
jmp 0x85a5
xorl %r14d, %r14d
movq 0x18(%rsp), %rsi
xorl %edi, %edi
movq 0x28(%r12), %rcx
movq (%rcx), %rdx
movl %r14d, %eax
movl %ebx, (%rdx,%rax,4)
movq 0x8(%rcx), %rcx
testb $0x1, %bpl
je 0x85eb
movl %ebx, %edx
subl %edi, %edx
xorps %xmm1, %xmm1
cvtsi2ss %rdx, %xmm1
movss %xmm0, (%rcx,%rax,4)
mulss %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
jmp 0x85f2
movl $0x0, (%rcx,%rax,4)
movq 0x20(%rsp), %r14
movq 0x28(%rsp), %r15
incl %eax
cmpl 0x30(%r12), %eax
jne 0x8668
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
movl %ebx, %edx
movswl (%rcx,%rdx,2), %edi
movzbl (%rax,%rdx), %esi
leaq 0xc(%rsp), %rdx
leaq 0x7(%rsp), %rcx
callq 0x7adf
ucomiss 0x9b44(%rip), %xmm0     # 0x12170
jne 0x8675
jp 0x8675
movq 0x30(%rsp), %rbx
incq %r15
cmpq %r14, %r15
jne 0x82a8
xorps %xmm0, %xmm0
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x8652
jnp 0x8682
movapd %xmm1, %xmm0
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x2d25
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x2d54
callq 0x2d80