// File: /workspace/github_c_makefile_star2_O3/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 0x15ede(%rip), %xmm0    # 0x1da58
mulsd %xmm1, %xmm0
callq 0x5a30
divsd 0x10(%r14,%r12), %xmm0
callq 0x3290
callq 0xab52
movsd 0x10(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rbx)
movupd 0x38(%rbx), %xmm1
shufpd $0x2, 0x15f70(%rip), %xmm0 # xmm0 = xmm0[0],mem[1]
addpd %xmm1, %xmm0
movupd %xmm0, 0x38(%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 0x9b40
movsd 0x15e69(%rip), %xmm1    # 0x1da60
maxsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
xorl %eax, %eax
callq 0xabc0
xorps %xmm1, %xmm1
cvtsi2sdl 0x10(%r15), %xmm1
mulsd %xmm1, %xmm0
addsd 0x15e4b(%rip), %xmm0    # 0x1da68
movsd %xmm0, 0x20(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x3290
movsd %xmm0, 0x18(%rsp)
xorl %eax, %eax
callq 0xabc0
movsd 0x15ded(%rip), %xmm1    # 0x1da30
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
xorps %xmm0, %xmm0
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 0x7a35
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0xab52
addsd 0x10(%rbx), %xmm0
movsd %xmm0, 0x10(%rbx)
movsd 0x3a8(%r14), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jbe 0x7ce4
movapd %xmm1, %xmm0
callq 0x3290
addsd 0x10(%rbx), %xmm0
jmp 0x7cec
addsd 0x15d84(%rip), %xmm0    # 0x1da70
movsd %xmm0, 0x10(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq