// File: /workspace/github_c_makefile_star2_O1/sc932[P]ALE/src/ALElike.c
// Function: applyPlacement
int applyPlacement(alignSet_t *head, assemblyT *theAssembly, libraryParametersT *libParams, samfile_t *placementBam, FILE *snpPhaseFile){

  assert(head != NULL);
  // normalize the probs
  double likeNormalizer = getTotalLikelihood(head); // sum of all likelihoods
  
  // finds where to place the read (if multiple mappings) if anywhere
  int winner = -1;
  alignSet_t *current = getPlacementWinner(head, likeNormalizer, &winner);

  if(current == NULL){ // no winner
	//fprintf(stderr, "applyPlacement(%s): unmapped\n", head->name);
    return 0; // 0 mapped
  }

  if (current->bamOfAlignment1 != NULL)
	  assert(strcmp(head->name, bam1_qname(current->bamOfAlignment1)) == 0);
  if (current->bamOfAlignment2 != NULL)
	  assert(strcmp(head->name, bam1_qname(current->bamOfAlignment2)) == 0);

  // apply the placement
  int numberMapped = applyDepthAndMatchToContig(current, theAssembly, likeNormalizer, libParams);

  if (libParams->isSortedByName == 1) {
	  // ordering is preserved because this alignment is ready
	  // only after all possible placements have been found for this read
	  savePlacement(placementBam, current);
  }
  recordSNPPhases(snpPhaseFile, current, theAssembly);

  // destroy all stored bams
  current=head;
  while(current != NULL) {
    if (current->bamOfAlignment1 != 0)
        bam_destroy1(current->bamOfAlignment1);
    if (current->bamOfAlignment2 != 0)
        bam_destroy1(current->bamOfAlignment2);
    current = current->nextAlignment;
  }
  //fprintf(stderr, "applyPlacement(%s): %d\n", head->name, numberMapped);

  return numberMapped;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdi, %rdi
je 0x7ee0
movq %r8, %r14
movq %rcx, %rbp
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %rbx
movsd (%rdi), %xmm1
mulsd 0x8(%rdi), %xmm1
xorpd %xmm0, %xmm0
addsd %xmm1, %xmm0
movq 0x48(%rdi), %rax
testq %rax, %rax
je 0x7df3
movsd (%rax), %xmm1
mulsd 0x8(%rax), %xmm1
addsd %xmm1, %xmm0
movq 0x48(%rax), %rax
jmp 0x7ddb
leaq 0x14(%rsp), %rsi
movl $0xffffffff, (%rsi)     # imm = 0xFFFFFFFF
movq %rbx, %rdi
callq 0x6f9f
testq %rax, %rax
je 0x7ecd
movq %rax, %r12
movq %r14, 0x8(%rsp)
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x7e35
movq 0x28(%rbx), %rdi
movq 0x30(%rax), %rsi
callq 0x3330
testl %eax, %eax
jne 0x7eff
movq %rbp, %r14
movq 0x38(%r12), %rax
testq %rax, %rax
je 0x7e57
movq 0x28(%rbx), %rdi
movq 0x30(%rax), %rsi
callq 0x3330
testl %eax, %eax
jne 0x7f1e
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x7151
movl %eax, %ebp
cmpl $0x1, 0x3b4(%r13)
jne 0x7e7c
movq %r14, %rdi
movq %r12, %rsi
callq 0x7d43
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x7cdd
movq 0x30(%rbx), %rax
testq %rax, %rax
je 0x7ea7
movq 0x30(%rax), %rdi
callq 0x3070
movq 0x30(%rbx), %rdi
callq 0x3070
movq 0x38(%rbx), %rax
testq %rax, %rax
je 0x7ec2
movq 0x30(%rax), %rdi
callq 0x3070
movq 0x38(%rbx), %rdi
callq 0x3070
movq 0x48(%rbx), %rbx
testq %rbx, %rbx
jne 0x7e8c
jmp 0x7ecf
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a4f5(%rip), %rdi     # 0x223dc
leaq 0x19e17(%rip), %rsi     # 0x21d05
leaq 0x1a4f4(%rip), %rcx     # 0x223e9
movl $0x561, %edx            # imm = 0x561
callq 0x3230
leaq 0x1a53c(%rip), %rdi     # 0x22442
leaq 0x19df8(%rip), %rsi     # 0x21d05
leaq 0x1a4d5(%rip), %rcx     # 0x223e9
movl $0x56f, %edx            # imm = 0x56F
callq 0x3230
leaq 0x1a55b(%rip), %rdi     # 0x22480
leaq 0x19dd9(%rip), %rsi     # 0x21d05
leaq 0x1a4b6(%rip), %rcx     # 0x223e9
movl $0x571, %edx            # imm = 0x571
callq 0x3230