// File: /workspace/github_c_makefile_star2_O2/sc932[P]ALE/src/ALElike.c
// Function: getPlacementWinner
alignSet_t *getPlacementWinner(alignSet_t *head, double likeNormalizer, int *winner) {
  assert(head != NULL && winner != NULL);
  assert(head->likelihood >= 0.0);

  *winner = -1;
  if(likeNormalizer == 0.0){ // no real placement
    return NULL;
  }

  // instead of purely random choice, choose a consistent, but still random choice
  unsigned int iseed = JSHash(head->name);
  srand(iseed); // instead of: srand ((unsigned int) time(NULL));

  double tRand = ( likeNormalizer * rand() / ( RAND_MAX + 1.0 ) );
  double soFar = 0.0;

  int i = 0;
  alignSet_t *current = head;
  if(head->likelihood*head->likelihoodInsert > tRand){
    *winner = 0;
  }else{
    assert(head->likelihood >= 0.0);
    soFar += head->likelihood*head->likelihoodInsert;
    while(current->nextAlignment != NULL){
      current = current->nextAlignment;
      assert(current->likelihood >= 0.0);
      i++;
      if(current->likelihood*current->likelihoodInsert + soFar > tRand){
        *winner = i;
        break;
      }else{
        soFar += current->likelihood*current->likelihoodInsert;
      }
    }
  }
  assert(current->likelihood >= 0.0);
  if(current->likelihood*current->likelihoodInsert > 0.0){
    return current;
  }else{ // not a real placement
    return NULL;
  }
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x10, %rsp
testq %rdi, %rdi
je 0x5610
movq %rsi, %rbx
testq %rsi, %rsi
je 0x5610
movq %rdi, %r14
movsd (%rdi), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jb 0x562f
orl $-0x1, (%rbx)
xorl %r15d, %r15d
ucomisd %xmm2, %xmm0
jne 0x553f
jnp 0x55e4
movq 0x28(%r14), %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x54b8
movl %eax, %edi
callq 0x32e0
callq 0x3690
cvtsi2sd %eax, %xmm1
mulsd 0x8(%rsp), %xmm1
mulsd 0x144e4(%rip), %xmm1    # 0x19a50
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
ja 0x55c4
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
jb 0x566d
addsd %xmm3, %xmm2
xorl %r15d, %r15d
movq %r14, %rax
movq 0x48(%r14), %r14
testq %r14, %r14
je 0x55ca
movsd (%r14), %xmm0
ucomisd %xmm3, %xmm0
jb 0x55f1
incl %r15d
movsd 0x8(%r14), %xmm4
mulsd %xmm0, %xmm4
addsd %xmm4, %xmm2
ucomisd %xmm1, %xmm2
jbe 0x5596
movl %r15d, (%rbx)
movq %r14, %rax
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x564e
mulsd 0x8(%rax), %xmm0
xorl %r15d, %r15d
ucomisd %xmm1, %xmm0
cmovaq %rax, %r15
movq %r15, %rax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq
leaq 0x18b46(%rip), %rdi     # 0x1e13e
leaq 0x18736(%rip), %rsi     # 0x1dd35
leaq 0x18ae4(%rip), %rcx     # 0x1e0ea
movl $0x433, %edx            # imm = 0x433
callq 0x3230
leaq 0x18ab4(%rip), %rdi     # 0x1e0cb
leaq 0x18717(%rip), %rsi     # 0x1dd35
leaq 0x18ac5(%rip), %rcx     # 0x1e0ea
movl $0x41b, %edx            # imm = 0x41B
callq 0x3230
leaq 0x18af0(%rip), %rdi     # 0x1e126
leaq 0x186f8(%rip), %rsi     # 0x1dd35
leaq 0x18aa6(%rip), %rcx     # 0x1e0ea
movl $0x41c, %edx            # imm = 0x41C
callq 0x3230
leaq 0x18ae9(%rip), %rdi     # 0x1e13e
leaq 0x186d9(%rip), %rsi     # 0x1dd35
leaq 0x18a87(%rip), %rcx     # 0x1e0ea
movl $0x43d, %edx            # imm = 0x43D
callq 0x3230
leaq 0x18ab2(%rip), %rdi     # 0x1e126
leaq 0x186ba(%rip), %rsi     # 0x1dd35
leaq 0x18a68(%rip), %rcx     # 0x1e0ea
movl $0x42f, %edx            # imm = 0x42F
callq 0x3230