// File: /workspace/github_c_makefile_star2_O2/sc932[P]ALE/src/ALElike.c
// Function: applyUnmapped
void applyUnmapped(bam1_t *read, assemblyT *theAssembly, libraryParametersT *libParams) {
	libraryMateParametersT *primaryMateParameters = &libParams->mateParameters[libParams->primaryOrientation];

	// Pinsert
	double loglikelihoodInsert = log(GetCappedInsertProbNormal(SIGMA_RANGE+1.0, primaryMateParameters->insertStd) / primaryMateParameters->zNormalizationInsert);
	loglikelihoodInsert = validateLogLikelihood( loglikelihoodInsert );

	theAssembly->totalScore += loglikelihoodInsert; // match contribution to totalScore from insert
	theAssembly->insertAvgSum += loglikelihoodInsert;
	theAssembly->insertAvgNorm += 1.0;

	// add placement to total for this unmapped read
	double avgQual = getAverageQualityScoreRead(read, libParams->qOff);
	if (avgQual < 0.25)
		avgQual = 0.25;

	// unplaced likelihood is calculated as having matched bases 1 less than the worst read that mapped (by % identity)
	double logunplacedReadPlacement = ( getLeastIdentity() * read->core.l_qseq - 1.0) * log(avgQual)  // bases that would have matched
			+ ((1.0 - getLeastIdentity()) * read->core.l_qseq + 1.0 )* log((1.0-avgQual)*avgQual); // bases that would have mis-matched

	double logznorm = logzNormalizationReadQual(read, libParams->qOff);
	//printf("Pplacement and Zplacement for unmapped %lf%% identity %lf %lf %lf, %s\n", getLeastIdentity()*100.0, logunplacedReadPlacement, logznorm, logunplacedReadPlacement - logznorm, bam1_qname(read));

	theAssembly->totalScore += validateLogLikelihood( logunplacedReadPlacement - logznorm ); // Pmatch - Zmatch
	//// previously
	//   theAssembly->totalScore += getMinLogLike();

	if (libParams->totalUnmappedFraction > 0) // happens if a read is mapped but not placed...
		theAssembly->totalScore += log(libParams->totalUnmappedFraction); // Porientation
	else
		theAssembly->totalScore += log(0.001); // 0.1%
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
movl 0x3b8(%rdx), %r12d
shlq $0x6, %r12
movsd 0x8(%rdx,%r12), %xmm1
movsd 0x13d8e(%rip), %xmm0    # 0x19a58
callq 0x41dc
divsd 0x10(%r14,%r12), %xmm0
callq 0x3290
callq 0x8a4e
movsd 0x10(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rbx)
shufpd $0x2, 0x13df9(%rip), %xmm0 # xmm0 = xmm0[0],mem[1]
movupd 0x38(%rbx), %xmm1
addpd %xmm1, %xmm0
movupd %xmm0, 0x38(%rbx)
movl 0x3b0(%r14), %esi
movq %r15, %rdi
callq 0x5e1c
movsd 0x13d44(%rip), %xmm1    # 0x19a60
maxsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
xorl %eax, %eax
callq 0x8abc
cvtsi2sdl 0x10(%r15), %xmm1
mulsd %xmm1, %xmm0
addsd 0x13d29(%rip), %xmm0    # 0x19a68
movsd %xmm0, 0x20(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x3290
movsd %xmm0, 0x18(%rsp)
xorl %eax, %eax
callq 0x8abc
movsd 0x13ccb(%rip), %xmm1    # 0x19a30
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
cvtsi2sdl 0x10(%r15), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
subsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x3290
mulsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
mulsd 0x20(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x3b0(%r14), %esi
movq %r15, %rdi
callq 0x5bbf
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x8a4e
addsd 0x10(%rbx), %xmm0
movsd %xmm0, 0x10(%rbx)
movsd 0x3a8(%r14), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jbe 0x5e03
movapd %xmm1, %xmm0
callq 0x3290
addsd 0x10(%rbx), %xmm0
jmp 0x5e0b
addsd 0x13c65(%rip), %xmm0    # 0x19a70
movsd %xmm0, 0x10(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq