// File: /workspace/github_c_makefile_star2_O2/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, 0x18(%rsp)
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
cmpq %rsi, %rax
je 0x6966
imulq $0x68, %rax, %rbp
cmpb $0x0, 0xc(%rdi,%rbp)
jne 0x695e
addq %rdi, %rbp
cmpb $0x1, 0xd(%rbp)
jne 0x695e
movq 0x20(%rbp), %rbx
testq %rbx, %rbx
je 0x695e
movq %rax, 0x30(%rsp)
movl 0x18(%rbp), %eax
testl %eax, %eax
jle 0x67cb
movq 0x10(%rbp), %rcx
movl (%rcx), %r14d
jmp 0x67d2
movl 0x8(%rbp), %r14d
incl %r14d
movb $0x1, %r12b
movl $0x0, %r13d
testl %r14d, %r14d
jne 0x67fd
cmpl $0x2, %eax
jl 0x67ef
movq 0x10(%rbp), %rax
movl 0x4(%rax), %r14d
jmp 0x67f6
movl 0x8(%rbp), %r14d
incl %r14d
xorl %r12d, %r12d
pushq $0x1
popq %r13
andl $0x0, 0x10(%rsp)
movb $0x0, 0x7(%rsp)
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movswl (%rcx), %edi
movzbl (%rax), %esi
leaq 0x10(%rsp), %rdx
leaq 0x7(%rsp), %rcx
callq 0x6647
xorl %edx, %edx
pushq $0x1
popq %r15
pushq $0x1
popq %r8
movq %rbp, 0x38(%rsp)
movl 0x8(%rbp), %eax
cmpq %rax, %r15
jae 0x6925
movl %r14d, %esi
cmpq %rsi, %r15
jne 0x684e
testb $0x1, %r12b
jne 0x686a
jmp 0x688c
movq 0x8(%rbx), %rax
testb $0x1, %r12b
je 0x6896
cmpw $0x0, (%rax,%r15,2)
jne 0x686a
movq (%rbx), %rcx
cmpb $0x0, (%rcx,%r15)
je 0x6896
movl %r15d, %eax
subl %edx, %eax
cvtsi2ss %rax, %xmm1
mulss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x8(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
movl %r15d, %edx
incl %r8d
movq 0x8(%rbx), %rax
movzwl (%rax,%r15,2), %eax
movq (%rbx), %rcx
movb (%rcx,%r15), %cl
testw %ax, %ax
jne 0x68ab
testb %cl, %cl
je 0x68f7
movswl %ax, %edi
movq %rsi, 0x40(%rsp)
movzbl %cl, %esi
movl %r13d, 0x14(%rsp)
movq %rbx, %rbp
movl %r14d, %ebx
movl %r12d, %r14d
movl %edx, %r12d
leaq 0x10(%rsp), %rdx
leaq 0x7(%rsp), %rcx
movl %r8d, %r13d
callq 0x6647
movq 0x40(%rsp), %rsi
movl %r13d, %r8d
movl %r12d, %edx
movl %r14d, %r12d
movl %ebx, %r14d
movq %rbp, %rbx
movq 0x38(%rsp), %rbp
movl 0x14(%rsp), %r13d
cmpq %rsi, %r15
jne 0x691d
xorb $0x1, %r12b
incl %r13d
cmpl 0x18(%rbp), %r13d
jge 0x6916
movq 0x10(%rbp), %rax
movslq %r13d, %rcx
movl (%rax,%rcx,4), %r14d
jmp 0x691d
movl 0x8(%rbp), %r14d
incl %r14d
incq %r15
jmp 0x6832
testb $0x1, %r12b
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rax
je 0x695a
subl %edx, %r15d
cvtsi2ss %r15, %xmm1
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movl %r8d, 0x40(%rbp)
incq %rax
jmp 0x6784
xorps %xmm0, %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
jne 0x698d
jp 0x698d
movss 0x97f3(%rip), %xmm0     # 0x10180
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq