// File: /workspace/github_c_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movsd %xmm0, 0x28(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
testl %esi, %esi
jle 0x7e99
movl %esi, %eax
movsd %xmm0, 0x8(%rsp)
xorl %edx, %edx
movq %rdi, 0x38(%rsp)
movq %rax, 0x30(%rsp)
imulq $0x68, %rdx, %rbp
cmpb $0x0, 0xc(%rdi,%rbp)
jne 0x7e8d
addq %rdi, %rbp
cmpb $0x1, 0xd(%rbp)
jne 0x7e8d
movq 0x20(%rbp), %r14
testq %r14, %r14
je 0x7e8d
movl 0x18(%rbp), %eax
testl %eax, %eax
jle 0x7ce6
movq 0x10(%rbp), %rcx
movl (%rcx), %ebx
jmp 0x7ceb
movl 0x8(%rbp), %ebx
incl %ebx
movb $0x1, %r13b
movl $0x0, %r12d
testl %ebx, %ebx
jne 0x7d14
cmpl $0x2, %eax
jl 0x7d06
movq 0x10(%rbp), %rax
movl 0x4(%rax), %ebx
jmp 0x7d0b
movl 0x8(%rbp), %ebx
incl %ebx
movl $0x1, %r12d
xorl %r13d, %r13d
movq %rdx, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
movb $0x0, 0x7(%rsp)
movq (%r14), %rax
movq 0x8(%r14), %rcx
movswl (%rcx), %edi
movzbl (%rax), %esi
leaq 0x14(%rsp), %rdx
leaq 0x7(%rsp), %rcx
callq 0x7adf
movl $0x1, %esi
cmpl $0x2, 0x8(%rbp)
jb 0x7e4a
xorl %edx, %edx
movl $0x1, %r15d
movl %ebx, %edi
cmpq %rdi, %r15
jne 0x7d68
testb $0x1, %r13b
jne 0x7d84
jmp 0x7da9
movq 0x8(%r14), %rax
testb $0x1, %r13b
je 0x7db2
cmpw $0x0, (%rax,%r15,2)
jne 0x7d84
movq (%r14), %rcx
cmpb $0x0, (%rcx,%r15)
je 0x7db2
movl %r15d, %eax
subl %edx, %eax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
mulss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x8(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
movl %r15d, %edx
incl %esi
movq 0x8(%r14), %rax
movzwl (%rax,%r15,2), %eax
movq (%r14), %rcx
movb (%rcx,%r15), %cl
testw %ax, %ax
jne 0x7dc7
testb %cl, %cl
je 0x7e16
movq %rdi, 0x40(%rsp)
movswl %ax, %edi
movl %esi, 0x1c(%rsp)
movzbl %cl, %esi
movl %r12d, 0x18(%rsp)
movq %rbp, %r12
movq %r14, %rbp
movl %ebx, %r14d
movl %r13d, %ebx
movl %edx, %r13d
leaq 0x14(%rsp), %rdx
leaq 0x7(%rsp), %rcx
callq 0x7adf
movq 0x40(%rsp), %rdi
movl 0x1c(%rsp), %esi
movl %r13d, %edx
movl %ebx, %r13d
movl %r14d, %ebx
movq %rbp, %r14
movq %r12, %rbp
movl 0x18(%rsp), %r12d
cmpq %rdi, %r15
jne 0x7e39
xorb $0x1, %r13b
incl %r12d
cmpl 0x18(%rbp), %r12d
jge 0x7e34
movq 0x10(%rbp), %rax
movslq %r12d, %rcx
movl (%rax,%rcx,4), %ebx
jmp 0x7e39
movl 0x8(%rbp), %ebx
incl %ebx
incq %r15
movl 0x8(%rbp), %eax
cmpq %rax, %r15
jb 0x7d59
jmp 0x7e52
movl $0x1, %r15d
xorl %edx, %edx
testb $0x1, %r13b
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rax
je 0x7e85
subl %edx, %r15d
xorps %xmm1, %xmm1
cvtsi2ss %r15, %xmm1
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movq 0x20(%rsp), %rdx
movl %esi, 0x40(%rbp)
incq %rdx
cmpq %rax, %rdx
jne 0x7cae
movsd 0x8(%rsp), %xmm1
ucomisd 0xa2a1(%rip), %xmm1     # 0x12148
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
jne 0x7ec1
jp 0x7ec1
movss 0xa2a7(%rip), %xmm0     # 0x12168
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq