// File: /workspace/github_c_makefile_star2_O1/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, 0x18(%rsp)
testl %esi, %esi
jle 0x7e7b
movl %esi, %eax
movsd %xmm0, 0x18(%rsp)
xorl %ecx, %ecx
movq %rdi, 0x38(%rsp)
movq %rax, 0x30(%rsp)
imulq $0x68, %rcx, %rbp
cmpb $0x0, 0xc(%rdi,%rbp)
jne 0x7e6f
addq %rdi, %rbp
cmpb $0x1, 0xd(%rbp)
jne 0x7e6f
movq 0x20(%rbp), %r12
testq %r12, %r12
je 0x7e6f
movq %rcx, 0x40(%rsp)
movl 0x18(%rbp), %eax
testl %eax, %eax
jle 0x7cd0
movq 0x10(%rbp), %rcx
movl (%rcx), %ebx
jmp 0x7cd5
movl 0x8(%rbp), %ebx
incl %ebx
movb $0x1, %r14b
movl $0x0, 0xc(%rsp)
testl %ebx, %ebx
jne 0x7d02
cmpl $0x2, %eax
jl 0x7cf2
movq 0x10(%rbp), %rax
movl 0x4(%rax), %ebx
jmp 0x7cf7
movl 0x8(%rbp), %ebx
incl %ebx
movl $0x1, 0xc(%rsp)
xorl %r14d, %r14d
movl $0x0, 0x24(%rsp)
movb $0x0, 0xb(%rsp)
movq (%r12), %rax
movq 0x8(%r12), %rcx
movswl (%rcx), %edi
movzbl (%rax), %esi
leaq 0x24(%rsp), %rdx
leaq 0xb(%rsp), %rcx
callq 0x7aca
cmpl $0x2, 0x8(%rbp)
jb 0x7e18
movl $0x1, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x1, %r15d
movl %ebx, %r13d
cmpq %r13, %r15
je 0x7d73
testb $0x1, %r14b
je 0x7da9
movq 0x8(%r12), %rax
cmpw $0x0, (%rax,%r15,2)
jne 0x7d73
movq (%r12), %rax
cmpb $0x0, (%rax,%r15)
je 0x7da9
testb $0x1, %r14b
je 0x7da0
movl %r15d, %eax
subl 0x10(%rsp), %eax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
mulss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x18(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x18(%rsp)
incl 0x14(%rsp)
movl %r15d, 0x10(%rsp)
movq 0x8(%r12), %rax
movzwl (%rax,%r15,2), %eax
testw %ax, %ax
jne 0x7dc3
movq (%r12), %rcx
cmpb $0x0, (%rcx,%r15)
je 0x7dde
movq (%r12), %rcx
movzbl (%rcx,%r15), %esi
movswl %ax, %edi
leaq 0x24(%rsp), %rdx
leaq 0xb(%rsp), %rcx
callq 0x7aca
cmpq %r13, %r15
jne 0x7e07
xorb $0x1, %r14b
movl 0xc(%rsp), %ecx
incl %ecx
cmpl 0x18(%rbp), %ecx
movl %ecx, 0xc(%rsp)
jge 0x7e02
movq 0x10(%rbp), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %ebx
jmp 0x7e07
movl 0x8(%rbp), %ebx
incl %ebx
incq %r15
movl 0x8(%rbp), %eax
cmpq %rax, %r15
jb 0x7d4d
jmp 0x7e2e
movl $0x1, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x1, %r15d
testb $0x1, %r14b
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rax
movq 0x40(%rsp), %rcx
je 0x7e68
subl 0x10(%rsp), %r15d
xorps %xmm1, %xmm1
cvtsi2ss %r15, %xmm1
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movl 0x14(%rsp), %edx
movl %edx, 0x40(%rbp)
incq %rcx
cmpq %rax, %rcx
jne 0x7c93
movsd 0x18(%rsp), %xmm1
ucomisd 0xa2c7(%rip), %xmm1     # 0x12150
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
jne 0x7ea3
jp 0x7ea3
movss 0xa2cd(%rip), %xmm0     # 0x12170
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq