// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x78e5
jmp 0x7904
leaq 0x28bb0(%rip), %rdi     # 0x3049c
leaq 0x284d2(%rip), %rsi     # 0x2fdc5
movl $0x561, %edx            # imm = 0x561
leaq 0x28baa(%rip), %rcx     # 0x304a9
callq 0x3220
movq -0x10(%rbp), %rdi
callq 0x63f0
movsd %xmm0, -0x38(%rbp)
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
movsd -0x38(%rbp), %xmm0
leaq -0x3c(%rbp), %rsi
callq 0x6470
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x7942
movl $0x0, -0x4(%rbp)
jmp 0x7abc
movq -0x48(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x798e
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x30(%rax), %rsi
callq 0x3330
cmpl $0x0, %eax
jne 0x796d
jmp 0x798c
leaq 0x28b8e(%rip), %rdi     # 0x30502
leaq 0x2844a(%rip), %rsi     # 0x2fdc5
movl $0x56f, %edx            # imm = 0x56F
leaq 0x28b22(%rip), %rcx     # 0x304a9
callq 0x3220
jmp 0x798e
movq -0x48(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x79da
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x48(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x30(%rax), %rsi
callq 0x3330
cmpl $0x0, %eax
jne 0x79b9
jmp 0x79d8
leaq 0x28b80(%rip), %rdi     # 0x30540
leaq 0x283fe(%rip), %rsi     # 0x2fdc5
movl $0x571, %edx            # imm = 0x571
leaq 0x28ad6(%rip), %rcx     # 0x304a9
callq 0x3220
jmp 0x79da
movq -0x48(%rbp), %rdi
movq -0x18(%rbp), %rsi
movsd -0x38(%rbp), %xmm0
movq -0x20(%rbp), %rdx
callq 0x6700
movl %eax, -0x4c(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x1, 0x3b4(%rax)
jne 0x7a0d
movq -0x28(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x7840
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x77b0
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x7ab6
movq -0x48(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x7a6b
jmp 0x7a3e
movq -0x48(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x7a67
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x30(%rax), %rdi
callq 0x3050
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x3050
jmp 0x7a69
jmp 0x7a6b
movq -0x48(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x7aa5
jmp 0x7a78
movq -0x48(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x7aa1
movq -0x48(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x30(%rax), %rdi
callq 0x3050
movq -0x48(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x3050
jmp 0x7aa3
jmp 0x7aa5
movq -0x48(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x7a26
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)