// File: /workspace/github_c_makefile_star2_O1/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 0x1625d(%rip), %xmm0    # 0x1da80
mulsd %xmm1, %xmm0
callq 0x5851
divsd 0x10(%r14,%r12), %xmm0
callq 0x3290
callq 0xa88a
movsd 0x10(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rbx)
addsd 0x38(%rbx), %xmm0
movsd %xmm0, 0x38(%rbx)
movsd 0x40(%rbx), %xmm0
addsd 0x161d6(%rip), %xmm0    # 0x1da38
movsd %xmm0, 0x40(%rbx)
movq 0x8(%r15), %rax
movq %rax, %rdx
shrq $0x30, %rdx
shlq $0x2, %rdx
addq 0x30(%r15), %rdx
movl 0x3b0(%r14), %esi
shrl $0x18, %eax
movl 0x10(%r15), %ecx
leal 0x1(%rcx), %edi
sarl %edi
movslq %edi, %rdi
addq %rax, %rdi
addq %rdx, %rdi
xorl %edx, %edx
callq 0x9888
movsd 0x161e3(%rip), %xmm1    # 0x1da88
maxsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
xorl %eax, %eax
callq 0xa8f8
xorps %xmm1, %xmm1
cvtsi2sdl 0x10(%r15), %xmm1
mulsd %xmm1, %xmm0
addsd 0x161c5(%rip), %xmm0    # 0x1da90
movsd %xmm0, 0x20(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x3290
movsd %xmm0, 0x18(%rsp)
xorl %eax, %eax
callq 0xa8f8
movsd 0x16147(%rip), %xmm2    # 0x1da38
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsi2sdl 0x10(%r15), %xmm0
mulsd %xmm1, %xmm0
addsd %xmm2, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
subsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
movapd %xmm2, %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 0x76de
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0xa88a
addsd 0x10(%rbx), %xmm0
movsd %xmm0, 0x10(%rbx)
movsd 0x3a8(%r14), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jbe 0x7992
movapd %xmm1, %xmm0
callq 0x3290
addsd 0x10(%rbx), %xmm0
jmp 0x799a
addsd 0x160fe(%rip), %xmm0    # 0x1da98
movsd %xmm0, 0x10(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq