// File: /workspace/github_c_makefile_star2_O3/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 0x821f
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 0x8132
movsd (%rax), %xmm1
mulsd 0x8(%rax), %xmm1
addsd %xmm1, %xmm0
movq 0x48(%rax), %rax
jmp 0x811a
leaq 0x14(%rsp), %rsi
movl $0xffffffff, (%rsi)     # imm = 0xFFFFFFFF
movq %rbx, %rdi
callq 0x7301
testq %rax, %rax
je 0x820c
movq %rax, %r12
movq %r14, 0x8(%rsp)
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x8174
movq 0x28(%rbx), %rdi
movq 0x30(%rax), %rsi
callq 0x3330
testl %eax, %eax
jne 0x823e
movq %rbp, %r14
movq 0x38(%r12), %rax
testq %rax, %rax
je 0x8196
movq 0x28(%rbx), %rdi
movq 0x30(%rax), %rsi
callq 0x3330
testl %eax, %eax
jne 0x825d
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x74bc
movl %eax, %ebp
cmpl $0x1, 0x3b4(%r13)
jne 0x81bb
movq %r14, %rdi
movq %r12, %rsi
callq 0x8082
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x801c
movq 0x30(%rbx), %rax
testq %rax, %rax
je 0x81e6
movq 0x30(%rax), %rdi
callq 0x3070
movq 0x30(%rbx), %rdi
callq 0x3070
movq 0x38(%rbx), %rax
testq %rax, %rax
je 0x8201
movq 0x30(%rax), %rdi
callq 0x3070
movq 0x38(%rbx), %rdi
callq 0x3070
movq 0x48(%rbx), %rbx
testq %rbx, %rbx
jne 0x81cb
jmp 0x820e
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a216(%rip), %rdi     # 0x2243c
leaq 0x19b38(%rip), %rsi     # 0x21d65
leaq 0x1a215(%rip), %rcx     # 0x22449
movl $0x561, %edx            # imm = 0x561
callq 0x3230
leaq 0x1a25d(%rip), %rdi     # 0x224a2
leaq 0x19b19(%rip), %rsi     # 0x21d65
leaq 0x1a1f6(%rip), %rcx     # 0x22449
movl $0x56f, %edx            # imm = 0x56F
callq 0x3230
leaq 0x1a27c(%rip), %rdi     # 0x224e0
leaq 0x19afa(%rip), %rsi     # 0x21d65
leaq 0x1a1d7(%rip), %rcx     # 0x22449
movl $0x571, %edx            # imm = 0x571
callq 0x3230