// File: /workspace/github_c_makefile_star2_O2/sc932[P]ALE/src/ALElike.c
// Function: computeKmerStats
void computeKmerStats(assemblyT *theAssembly, int kmerLen){
  int i, j, k, hash;
  long totalKmers, totalKmersInit;
  totalKmers = 0;
  totalKmersInit = pow(4, kmerLen);
  // calculate total possible kmers
  long *kmerVec = malloc(totalKmersInit*sizeof(long));
  double kmerSum = 0.0;
  double kmerNorm = 0.0;
  double logkmerZnorm = 0.0;

  if (!isMetagenome()) {
	    // initialize kmerVec
	    for(j = 0; j < totalKmersInit; j++){
	      kmerVec[j] = 0;
	    }
	    totalKmers = 0;

	    // calculate the kmer spectrum
	    for(i = 0; i < theAssembly->numContigs; i++){
	    	contig_t *contig = theAssembly->contigs[i];
	    	if (contig->seqLen > kmerLen) {
	        	// add up the kmers
	        	for(j = 0; j < contig->seqLen - kmerLen; j++){
	        		hash = getKmerHash(contig->seq, j, kmerLen);
	        		////printf("Hash = %i\n", hash);
	        		if(hash > -1){
	        			kmerVec[hash]++;
	        			totalKmers++;
	        		}
	        	}
	    	}
	    }

	    // apply kmer score to total scores
	    for(i = 0; i < theAssembly->numContigs; i++){
	    	contig_t *contig = theAssembly->contigs[i];
	    	if (contig->seqLen > kmerLen) {
	    		for(j = 0; j < contig->seqLen - kmerLen; j++){
	    			hash = getKmerHash(contig->seq, j, kmerLen);
	    			if(hash > -1){
	    				double score = ((double)kmerVec[hash]) / ((double)totalKmers);
	    				double logScore = log(score);
		    			theAssembly->totalScore += logScore; // k-mer contribution to totalScore
		    			theAssembly->kmerAvgSum += logScore;
		    			theAssembly->kmerAvgNorm += 1.0;
		    			kmerSum += score;
		    			kmerNorm += 1.0;
	    			}
	    		}
	    	}
	    }
	    //printf("Kmer Normalization factor (log): %lf\n", log(kmerSum/kmerNorm));

  }

  // find all kmers present
  for(i = 0; i < theAssembly->numContigs; i++){
    contig_t *contig = theAssembly->contigs[i];
    if (contig->seqLen <= kmerLen){  // this should be exceedingly rare, the contig length <= a tetramer!!
    	// contig is too small to process
    	// there is no information to process
        for(j = 0; j < contig->seqLen; j++){
            contig->kmerLogLikelihood[j] = 0.0;
        }
        continue;
    }

    if (isMetagenome()) {
    	// initialize kmerVec
    	for(j = 0; j < totalKmersInit; j++){
    		kmerVec[j] = 0;
    	}
    	totalKmers = 0;
    	kmerSum = 0.0;
    	kmerNorm = 0.0;

    	// add up the kmer spectrum
    	for(j = 0; j < contig->seqLen - kmerLen; j++){
    		hash = getKmerHash(contig->seq, j, kmerLen);
    		////printf("Hash = %i\n", hash);
    		if(hash > -1){
    			kmerVec[hash]++;
    			totalKmers++;
    		}
    	}

    	// apply the kmer score to the total score
    	for(j = 0; j < contig->seqLen - kmerLen; j++){
    		hash = getKmerHash(contig->seq, j, kmerLen);
    		if(hash > -1){
    			double score = ((double)kmerVec[hash]) / ((double)totalKmers);
    			double logScore = log(score);

    			theAssembly->totalScore += logScore; // k-mer contribution to totalScore
    			theAssembly->kmerAvgSum += logScore;
    			theAssembly->kmerAvgNorm += 1.0;
    			kmerSum += score;
    			kmerNorm += 1.0;
    		}
    	}
    }

    logkmerZnorm = log(kmerSum/kmerNorm);

    //
    // ** Using contig->kmerLogLikelihood[] as just a likelihood.  Will convert back to log(likelihood) at the end **
    //

    ////printf("Calculated all %i kmers!\n", totalKmers);
    // calculate probability of seeing that kmer based on the rest of the contig
    // first kmer - 1 unrolled
    for(j = 0; j < kmerLen; j++){
      contig->kmerLogLikelihood[j] = 0.0;
      for(k = 0; k < j+1; k++){
        hash = getKmerHash(contig->seq, k, kmerLen);
        if(hash > -1){
          contig->kmerLogLikelihood[j] += 1.0/(double)(j+1)*(double)(kmerVec[hash])/(double)(totalKmers);
        }
      }
      ////printf("New likelihood[%i]: %f.\n", j, contig->kmerLikelihood[j]);
    }
    ////printf("First.\n");
    // middle bunch
    for(j = kmerLen; j < contig->seqLen - kmerLen; j++){
      contig->kmerLogLikelihood[j] = 0.0;
      for(k = 0; k < kmerLen; k++){
        hash = getKmerHash(contig->seq, j - k, kmerLen);
        if(hash > -1){
          contig->kmerLogLikelihood[j] += 1.0/(double)(kmerLen)*(double)(kmerVec[hash])/(double)(totalKmers);
        }
      }
      ////printf("New likelihood[%i]: %f.\n", j, contig->kmerLikelihood[j]);
    }
    ////printf("Mid.\n");
    // last bits
    for(j = contig->seqLen - kmerLen; j < contig->seqLen; j++){
      contig->kmerLogLikelihood[j] = 0.0;
      for(k = j - kmerLen; k < j - kmerLen + (contig->seqLen - j); k++){
        hash = getKmerHash(contig->seq, k, kmerLen);
        if(hash > -1){
          contig->kmerLogLikelihood[j] += 1.0/(double)(contig->seqLen - j)*(double)(kmerVec[hash])/(double)(totalKmers);
        }
      }
      ////printf("New likelihood[%i]: %f.\n", j, contig->kmerLikelihood[j]);
    }
    ////printf("Last.\n");
    //
    // ** Converting contig->kmerLogLikelihood[] back to log(likelihood) and normalizing **
    //

    // add up kmer score into total score
    long contributingKmers = 0;
    for(j = 0; j < contig->seqLen; j++){
        assert(contig->kmerLogLikelihood[j] <= 1.0 && contig->kmerLogLikelihood[j] >= 0.0);  // not log yet...
    	if (contig->kmerLogLikelihood[j] > 0.0) {
    		contig->kmerLogLikelihood[j] = validateLogLikelihood( log(contig->kmerLogLikelihood[j]) - logkmerZnorm );
    		contributingKmers++;
    	} else {
    		contig->kmerLogLikelihood[j] = 0.0; // no information, so do not floor per-base value
    	}
    }

   	// z normalize
   	if(contributingKmers - kmerLen > 0){
   		theAssembly->totalScore -= logkmerZnorm*(double)(contributingKmers - kmerLen);
   		theAssembly->kmerAvgSum -= logkmerZnorm*(double)(contributingKmers - kmerLen);
   	}

  }
  free(kmerVec);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %esi, %r14d
movq %rdi, %r15
cvtsi2sd %esi, %xmm0
movsd %xmm0, 0x18(%rsp)
addsd %xmm0, %xmm0
callq 0x3050
cvttsd2si %xmm0, %rbx
leaq (,%rbx,8), %rdi
callq 0x3420
movq %rax, 0x10(%rsp)
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x8a9d
movsd 0x18(%rsp), %xmm1
testq %rbx, %rbx
cmovleq %r12, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
movl $0x0, %r13d
testl %eax, %eax
movl %r14d, 0x4(%rsp)
movq %r15, 0x58(%rsp)
movq %rbx, 0x50(%rsp)
jne 0x4ff1
xorl %eax, %eax
movq 0x10(%rsp), %rcx
cmpq %rax, %rbx
je 0x4e80
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x4e71
movl (%r15), %eax
xorl %r13d, %r13d
testl %eax, %eax
movl $0x0, %r14d
movl %eax, 0x30(%rsp)
cmovgl %eax, %r14d
xorl %r12d, %r12d
cmpq %r14, %r13
je 0x4ef1
movq 0xa0(%r15), %rax
movq (%rax,%r13,8), %rbx
movl 0x8(%rbx), %r15d
subl 0x4(%rsp), %r15d
jle 0x4ee7
xorl %ebp, %ebp
testl %r15d, %r15d
cmovlel %ebp, %r15d
cmpl %ebp, %r15d
je 0x4ee7
movq 0x10(%rbx), %rdi
movl %ebp, %esi
movl 0x4(%rsp), %edx
callq 0x4dab
testl %eax, %eax
js 0x4ee3
movl %eax, %eax
movq 0x10(%rsp), %rcx
incq (%rcx,%rax,8)
incq %r12
incl %ebp
jmp 0x4ebd
incq %r13
movq 0x58(%rsp), %r15
jmp 0x4e99
movq %r12, %r13
cvtsi2sd %r12, %xmm0
movsd %xmm0, 0x20(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
xorl %ecx, %ecx
movl 0x30(%rsp), %edx
movl 0x4(%rsp), %r14d
movslq %edx, %rax
cmpq %rax, %rcx
jge 0x4fe1
movq 0xa0(%r15), %rax
movq (%rax,%rcx,8), %r13
movl 0x8(%r13), %ebx
cmpl 0x4(%rsp), %ebx
jle 0x4fd6
movq %rcx, 0x8(%rsp)
xorl %ebp, %ebp
movl %ebx, %eax
subl %r14d, %eax
cmpl %eax, %ebp
jge 0x4fce
movq 0x10(%r13), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x4dab
testl %eax, %eax
js 0x4fc7
movl %eax, %eax
movq 0x10(%rsp), %rcx
cvtsi2sdq (%rcx,%rax,8), %xmm0
divsd 0x20(%rsp), %xmm0
movapd %xmm0, 0x30(%rsp)
callq 0x3290
movupd 0x10(%r15), %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
addpd %xmm1, %xmm0
movupd %xmm0, 0x10(%r15)
movsd 0x20(%r15), %xmm0
addsd 0x14a91(%rip), %xmm0    # 0x19a30
movsd %xmm0, 0x20(%r15)
movapd 0x14b33(%rip), %xmm0    # 0x19ae0
unpcklpd 0x30(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
movapd 0x40(%rsp), %xmm1
addpd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
movl 0x8(%r13), %ebx
incl %ebp
jmp 0x4f40
movl (%r15), %edx
movq 0x8(%rsp), %rcx
incq %rcx
movq %r12, %r13
jmp 0x4f14
movl 0x4(%rsp), %r14d
movq 0x50(%rsp), %rbx
movsd 0x18(%rsp), %xmm1
movsd 0x14a37(%rip), %xmm0    # 0x19a30
divsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
movslq %r14d, %rax
movq %rax, 0x78(%rsp)
testl %r14d, %r14d
movl $0x0, %eax
cmovgl %r14d, %eax
movq %rax, 0x80(%rsp)
xorl %r12d, %r12d
movslq (%r15), %rax
cmpq %rax, %r12
jge 0x547e
movq 0xa0(%r15), %rax
movq (%rax,%r12,8), %rax
movq %rax, 0x8(%rsp)
movl 0x8(%rax), %eax
cmpl %r14d, %eax
jle 0x5073
xorl %eax, %eax
callq 0x8a9d
testl %eax, %eax
movq %r12, 0x68(%rsp)
je 0x5193
xorl %eax, %eax
cmpq %rax, %rbx
je 0x509a
movq 0x10(%rsp), %rcx
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x505f
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x5476
movq 0x8(%rsp), %rdx
movq 0x50(%rdx), %rdx
andl $0x0, (%rdx,%rcx,4)
incq %rcx
jmp 0x507f
movq 0x8(%rsp), %rax
movl 0x8(%rax), %r12d
movl %r12d, %ebx
subl %r14d, %ebx
xorl %r13d, %r13d
testl %ebx, %ebx
cmovlel %r13d, %ebx
xorl %ebp, %ebp
cmpl %ebp, %ebx
je 0x50e1
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x4dab
testl %eax, %eax
js 0x50dd
movl %eax, %eax
movq 0x10(%rsp), %rcx
incq (%rcx,%rax,8)
incq %r13
incl %ebp
jmp 0x50b4
cvtsi2sd %r13, %xmm0
movsd %xmm0, 0x30(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
xorl %ebp, %ebp
movq 0x8(%rsp), %rbx
movl %r12d, %eax
subl %r14d, %eax
cmpl %eax, %ebp
jge 0x518c
movq 0x10(%rbx), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x4dab
testl %eax, %eax
js 0x5185
movl %eax, %eax
movq 0x10(%rsp), %rcx
cvtsi2sdq (%rcx,%rax,8), %xmm0
divsd 0x30(%rsp), %xmm0
movapd %xmm0, 0x20(%rsp)
callq 0x3290
movupd 0x10(%r15), %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
addpd %xmm1, %xmm0
movupd %xmm0, 0x10(%r15)
movsd 0x20(%r15), %xmm0
addsd 0x148d3(%rip), %xmm0    # 0x19a30
movsd %xmm0, 0x20(%r15)
movapd 0x14975(%rip), %xmm0    # 0x19ae0
unpcklpd 0x20(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
movapd 0x40(%rsp), %xmm1
addpd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
movl 0x8(%rbx), %r12d
incl %ebp
jmp 0x50fd
movq %r13, 0x70(%rsp)
jmp 0x51a8
movq %r13, 0x70(%rsp)
cvtsi2sd %r13, %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x8(%rsp), %rbx
movapd 0x40(%rsp), %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
divsd %xmm1, %xmm0
callq 0x3290
movapd %xmm0, 0x90(%rsp)
xorl %r15d, %r15d
pushq $0x1
popq %r13
cmpq 0x80(%rsp), %r15
je 0x5277
movq %rbx, %rax
movq 0x50(%rbx), %rbx
andl $0x0, (%rbx,%r15,4)
leaq 0x1(%r15), %rcx
movq 0x10(%rax), %r12
movq %rcx, 0x60(%rsp)
cvtsi2sd %ecx, %xmm0
movsd 0x1482e(%rip), %xmm1    # 0x19a30
divsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
xorl %ebp, %ebp
xorpd %xmm0, %xmm0
movss %xmm0, 0x20(%rsp)
cmpl %ebp, %r13d
je 0x5265
movq %r12, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x4dab
testl %eax, %eax
js 0x5261
movl %eax, %eax
movq 0x10(%rsp), %rcx
cvtsi2sdq (%rcx,%rax,8), %xmm0
mulsd 0x18(%rsp), %xmm0
divsd 0x30(%rsp), %xmm0
cvtss2sd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x20(%rsp)
movss %xmm0, (%rbx,%r15,4)
incl %ebp
jmp 0x5218
incl %r13d
movq 0x60(%rsp), %r15
movq 0x8(%rsp), %rbx
jmp 0x51cf
movl 0x8(%rbx), %eax
movl %eax, 0x18(%rsp)
cltq
movq %rax, 0x60(%rsp)
movl %eax, %r13d
subl %r14d, %r13d
movslq %r13d, %r12
movq 0x78(%rsp), %r15
cmpq %r12, %r15
jge 0x53ae
movq 0x50(%rbx), %rbx
andl $0x0, (%rbx,%r15,4)
xorpd %xmm0, %xmm0
movss %xmm0, 0x20(%rsp)
movl %r15d, %ebp
movq 0x80(%rsp), %rax
movl %eax, %r14d
subl $0x1, %r14d
jb 0x5315
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movl %ebp, %esi
movl 0x4(%rsp), %edx
callq 0x4dab
testl %eax, %eax
js 0x5311
movl %eax, %eax
movq 0x10(%rsp), %rcx
cvtsi2sdq (%rcx,%rax,8), %xmm0
mulsd 0x88(%rsp), %xmm0
divsd 0x30(%rsp), %xmm0
cvtss2sd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x20(%rsp)
movss %xmm0, (%rbx,%r15,4)
decl %ebp
jmp 0x52bd
incq %r15
movl 0x4(%rsp), %r14d
movq 0x8(%rsp), %rbx
jmp 0x5293
movq 0x50(%rbx), %r15
andl $0x0, (%r15,%r12,4)
movl 0x18(%rsp), %eax
subl %r12d, %eax
cvtsi2sd %eax, %xmm0
movsd 0x146ed(%rip), %xmm1    # 0x19a30
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movl %r12d, %ebp
subl %r14d, %ebp
xorpd %xmm0, %xmm0
movss %xmm0, 0x20(%rsp)
cmpl %r13d, %ebp
jge 0x53ab
movq 0x10(%rbx), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x4dab
testl %eax, %eax
js 0x53a7
movl %eax, %eax
movq 0x10(%rsp), %rcx
cvtsi2sdq (%rcx,%rax,8), %xmm0
mulsd 0x8(%rsp), %xmm0
divsd 0x30(%rsp), %xmm0
cvtss2sd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x20(%rsp)
movss %xmm0, (%r15,%r12,4)
incl %ebp
jmp 0x535d
incq %r12
cmpq 0x60(%rsp), %r12
jl 0x5327
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x70(%rsp), %r13
movslq 0x18(%rsp), %rax
cmpq %rax, %r15
jge 0x5433
movq 0x50(%rbx), %rax
movss (%rax,%r15,4), %xmm0
movss 0x1893c(%rip), %xmm1    # 0x1dd1c
ucomiss %xmm0, %xmm1
jb 0x5499
ucomiss 0x18930(%rip), %xmm0    # 0x1dd20
jb 0x5499
jbe 0x5429
cvtss2sd %xmm0, %xmm0
callq 0x3290
subsd 0x90(%rsp), %xmm0
callq 0x8a4e
cvtsd2ss %xmm0, %xmm0
movq 0x50(%rbx), %rax
movss %xmm0, (%rax,%r15,4)
incq %r12
movl 0x8(%rbx), %eax
movl %eax, 0x18(%rsp)
jmp 0x542e
andl $0x0, (%rax,%r15,4)
incq %r15
jmp 0x53c4
subq 0x78(%rsp), %r12
jle 0x5467
cvtsi2sd %r12, %xmm0
movq 0x58(%rsp), %r15
movupd 0x10(%r15), %xmm1
movapd 0x90(%rsp), %xmm2
mulsd %xmm0, %xmm2
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
subpd %xmm2, %xmm1
movupd %xmm1, 0x10(%r15)
jmp 0x546c
movq 0x58(%rsp), %r15
movq 0x50(%rsp), %rbx
movq 0x68(%rsp), %r12
incq %r12
jmp 0x5025
movq 0x10(%rsp), %rdi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3070
leaq 0x18bb8(%rip), %rdi     # 0x1e058
leaq 0x1888e(%rip), %rsi     # 0x1dd35
leaq 0x18bf5(%rip), %rcx     # 0x1e0a3
movl $0x3ec, %edx            # imm = 0x3EC
callq 0x3230