// File: /workspace/github_c_makefile_star2_O2/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 0x62d4
movq %r8, %r14
movq %rcx, %rbp
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %rbx
callq 0x54df
leaq 0x14(%rsp), %rsi
orl $-0x1, (%rsi)
movq %rbx, %rdi
callq 0x54fa
testq %rax, %rax
je 0x62c1
movq %rax, %r12
movq %r14, 0x8(%rsp)
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x6229
movq 0x28(%rbx), %rdi
movq 0x30(%rax), %rsi
callq 0x3330
testl %eax, %eax
jne 0x62f3
movq %rbp, %r14
movq 0x38(%r12), %rax
testq %rax, %rax
je 0x624b
movq 0x28(%rbx), %rdi
movq 0x30(%rax), %rsi
callq 0x3330
testl %eax, %eax
jne 0x6312
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x568c
movl %eax, %ebp
cmpl $0x1, 0x3b4(%r13)
jne 0x6270
movq %r14, %rdi
movq %r12, %rsi
callq 0x6162
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x60ff
testq %rbx, %rbx
je 0x62c3
movq 0x30(%rbx), %rax
testq %rax, %rax
je 0x62a0
movq 0x30(%rax), %rdi
callq 0x3070
movq 0x30(%rbx), %rdi
callq 0x3070
movq 0x38(%rbx), %rax
testq %rax, %rax
je 0x62bb
movq 0x30(%rax), %rdi
callq 0x3070
movq 0x38(%rbx), %rdi
callq 0x3070
movq 0x48(%rbx), %rbx
jmp 0x6280
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18131(%rip), %rdi     # 0x1e40c
leaq 0x17a53(%rip), %rsi     # 0x1dd35
leaq 0x18130(%rip), %rcx     # 0x1e419
movl $0x561, %edx            # imm = 0x561
callq 0x3230
leaq 0x18178(%rip), %rdi     # 0x1e472
leaq 0x17a34(%rip), %rsi     # 0x1dd35
leaq 0x18111(%rip), %rcx     # 0x1e419
movl $0x56f, %edx            # imm = 0x56F
callq 0x3230
leaq 0x18197(%rip), %rdi     # 0x1e4b0
leaq 0x17a15(%rip), %rsi     # 0x1dd35
leaq 0x180f2(%rip), %rcx     # 0x1e419
movl $0x571, %edx            # imm = 0x571
callq 0x3230