// File: /workspace/github_c_makefile_star2_O1/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 %r14
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x70d5
movq %rsi, %rbx
testq %rsi, %rsi
je 0x70d5
movq %rdi, %r14
movsd (%rdi), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jb 0x70f4
movl $0xffffffff, (%rbx)     # imm = 0xFFFFFFFF
ucomisd %xmm2, %xmm0
jne 0x6fe2
jp 0x6fe2
xorl %eax, %eax
jmp 0x70ae
movq 0x28(%r14), %rax
movl $0x4e67c6a7, %edi       # imm = 0x4E67C6A7
testq %rax, %rax
movsd %xmm0, (%rsp)
je 0x701a
movb (%rax), %cl
testb %cl, %cl
je 0x701a
incq %rax
movsbl %cl, %ecx
movl %edi, %edx
shll $0x5, %edx
movl %edi, %esi
shrl $0x2, %esi
addl %edx, %esi
addl %ecx, %esi
xorl %esi, %edi
movb (%rax), %cl
incq %rax
testb %cl, %cl
jne 0x6ffe
callq 0x32e0
callq 0x3690
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd (%rsp), %xmm0
mulsd 0x16a40(%rip), %xmm0    # 0x1da78
movsd (%r14), %xmm3
movsd 0x8(%r14), %xmm1
mulsd %xmm3, %xmm1
xorl %eax, %eax
ucomisd %xmm0, %xmm1
ja 0x708c
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm3
jb 0x7132
addsd %xmm2, %xmm1
xorl %eax, %eax
movq %r14, %rcx
movq 0x48(%r14), %r14
testq %r14, %r14
je 0x7091
movsd (%r14), %xmm3
ucomisd %xmm2, %xmm3
jb 0x70b6
mulsd 0x8(%r14), %xmm3
incl %eax
addsd %xmm3, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x7063
movl %eax, (%rbx)
movq %r14, %rcx
movsd (%rcx), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x7113
mulsd 0x8(%rcx), %xmm0
xorl %eax, %eax
ucomisd %xmm1, %xmm0
cmovaq %rcx, %rax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
leaq 0x1b051(%rip), %rdi     # 0x2210e
leaq 0x1ac41(%rip), %rsi     # 0x21d05
leaq 0x1afef(%rip), %rcx     # 0x220ba
movl $0x433, %edx            # imm = 0x433
callq 0x3230
leaq 0x1afbf(%rip), %rdi     # 0x2209b
leaq 0x1ac22(%rip), %rsi     # 0x21d05
leaq 0x1afd0(%rip), %rcx     # 0x220ba
movl $0x41b, %edx            # imm = 0x41B
callq 0x3230
leaq 0x1affb(%rip), %rdi     # 0x220f6
leaq 0x1ac03(%rip), %rsi     # 0x21d05
leaq 0x1afb1(%rip), %rcx     # 0x220ba
movl $0x41c, %edx            # imm = 0x41C
callq 0x3230
leaq 0x1aff4(%rip), %rdi     # 0x2210e
leaq 0x1abe4(%rip), %rsi     # 0x21d05
leaq 0x1af92(%rip), %rcx     # 0x220ba
movl $0x43d, %edx            # imm = 0x43D
callq 0x3230
leaq 0x1afbd(%rip), %rdi     # 0x220f6
leaq 0x1abc5(%rip), %rsi     # 0x21d05
leaq 0x1af73(%rip), %rcx     # 0x220ba
movl $0x42f, %edx            # imm = 0x42F
callq 0x3230