// File: /workspace/github_c_makefile_star2_O0/jsh58[P]Genrich/Genrich.c
// Function: calcFactor
float calcFactor(Chrom* chrom, int chromLen,
    double fragLen) {

  // sum weighted lengths of control fragments
  double ctrlFrag = 0.0;
  for (int i = 0; i < chromLen; i++) {
    Chrom* chr = chrom + i;
    if (chr->skip || ! chr->save || chr->diff == NULL)
      continue;

    // initialize BED interval variables
    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
      save = false;
      bedIdx++;
      bedPos = bedIdx < chr->bedLen ?
        chr->bed[bedIdx] : chr->len + 1;
    }

    // initialize variables
    uint32_t num = 1;       // number of intervals to create for pileup
    Diff* d = chr->diff;
    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);

    // calculate fragment lengths along the chrom
    uint32_t start = 0;     // beginning coordinate of interval
    uint32_t j;
    for (j = 1; j < chr->len; j++) {

      if (j == bedPos || (save && (d->cov[j] || d->frac[j]))) {
        if (save)
          // save frag. length weighted by val
          ctrlFrag += (j - start) * val;
        num++;
        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
    if (save)
      ctrlFrag += (j - start) * val;
    chr->ctrlLen = num; // save number of intervals
  }

  // return ratio of experimental frags to ctrl frags
  if (! ctrlFrag)
    return 1.0f;
  return fragLen / ctrlFrag;
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movl %esi, 0x64(%rsp)
movsd %xmm0, 0x58(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x64(%rsp), %eax
jge 0x9bc7
movq 0x68(%rsp), %rax
movslq 0x4c(%rsp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
testb $0x1, 0xc(%rax)
jne 0x991a
movq 0x40(%rsp), %rax
testb $0x1, 0xd(%rax)
je 0x991a
movq 0x40(%rsp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x991f
jmp 0x9bb7
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x994c
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x3c(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x10(%rsp)
jmp 0x995b
movq 0x40(%rsp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x38(%rsp)
movb $0x1, 0x37(%rsp)
cmpl $0x0, 0x38(%rsp)
jne 0x99bb
movb $0x0, 0x37(%rsp)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x99a4
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x3c(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0xc(%rsp)
jmp 0x99b3
movq 0x40(%rsp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x38(%rsp)
movl $0x1, 0x30(%rsp)
movq 0x40(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movb $0x0, 0x23(%rsp)
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movw (%rax), %si
movq 0x28(%rsp), %rax
movq (%rax), %rax
leaq 0x24(%rsp), %rdx
leaq 0x23(%rsp), %rcx
movswl %si, %edi
movzbl (%rax), %esi
callq 0x9650
movss %xmm0, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x1, 0x14(%rsp)
movl 0x14(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jae 0x9b7f
movl 0x14(%rsp), %eax
cmpl 0x38(%rsp), %eax
je 0x9a6b
testb $0x1, 0x37(%rsp)
je 0x9aaa
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x14(%rsp), %ecx
movswl (%rax,%rcx,2), %eax
cmpl $0x0, %eax
jne 0x9a6b
movq 0x28(%rsp), %rax
movq (%rax), %rax
movl 0x14(%rsp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x9aaa
testb $0x1, 0x37(%rsp)
je 0x9a97
movl 0x14(%rsp), %eax
movl 0x18(%rsp), %ecx
subl %ecx, %eax
cvtsi2ss %rax, %xmm0
mulss 0x1c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
addsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x14(%rsp), %ecx
movswl (%rax,%rcx,2), %eax
cmpl $0x0, %eax
jne 0x9ad5
movq 0x28(%rsp), %rax
movq (%rax), %rax
movl 0x14(%rsp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x9b10
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x14(%rsp), %ecx
movw (%rax,%rcx,2), %di
movq 0x28(%rsp), %rax
movq (%rax), %rax
movl 0x14(%rsp), %ecx
movl %ecx, %esi
leaq 0x24(%rsp), %rdx
leaq 0x23(%rsp), %rcx
movswl %di, %edi
movzbl (%rax,%rsi), %esi
callq 0x9650
movss %xmm0, 0x1c(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x38(%rsp), %eax
jne 0x9b6d
movb 0x37(%rsp), %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, 0x37(%rsp)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x9b56
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x3c(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x8(%rsp)
jmp 0x9b65
movq 0x40(%rsp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x38(%rsp)
jmp 0x9b6f
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x9a1d
testb $0x1, 0x37(%rsp)
je 0x9bab
movl 0x14(%rsp), %eax
movl 0x18(%rsp), %ecx
subl %ecx, %eax
cvtsi2ss %rax, %xmm0
mulss 0x1c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
addsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movl 0x30(%rsp), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x40(%rax)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x98d4
movsd 0x50(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x9be8
jp 0x9be8
movss 0x145cc(%rip), %xmm0    # 0x1e1ac
movss %xmm0, 0x74(%rsp)
jmp 0x9bfe
movsd 0x58(%rsp), %xmm0
divsd 0x50(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x74(%rsp)
movss 0x74(%rsp), %xmm0
addq $0x78, %rsp
retq
nopl (%rax)