// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x6495
cmpq $0x0, -0x20(%rbp)
je 0x6495
jmp 0x64b4
leaq 0x29cbf(%rip), %rdi     # 0x3015b
leaq 0x29922(%rip), %rsi     # 0x2fdc5
movl $0x41b, %edx            # imm = 0x41B
leaq 0x29ccb(%rip), %rcx     # 0x3017a
callq 0x3220
movq -0x10(%rbp), %rax
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x64c7
jmp 0x64e6
leaq 0x29ce8(%rip), %rdi     # 0x301b6
leaq 0x298f0(%rip), %rsi     # 0x2fdc5
movl $0x41c, %edx            # imm = 0x41C
leaq 0x29c99(%rip), %rcx     # 0x3017a
callq 0x3220
movq -0x20(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movsd -0x18(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x650d
jp 0x650d
movq $0x0, -0x8(%rbp)
jmp 0x66f3
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x6380
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %edi
callq 0x32d0
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
callq 0x3690
movsd -0x50(%rbp), %xmm0
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
movsd 0x25547(%rip), %xmm1    # 0x2ba90
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movsd (%rax), %xmm0
movq -0x10(%rbp), %rax
mulsd 0x8(%rax), %xmm0
ucomisd -0x30(%rbp), %xmm0
jbe 0x6590
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
jmp 0x6695
movq -0x10(%rbp), %rax
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x65a3
jmp 0x65c2
leaq 0x29c0c(%rip), %rdi     # 0x301b6
leaq 0x29814(%rip), %rsi     # 0x2fdc5
movl $0x42f, %edx            # imm = 0x42F
leaq 0x29bbd(%rip), %rcx     # 0x3017a
callq 0x3220
movq -0x10(%rbp), %rax
movsd (%rax), %xmm0
movq -0x10(%rbp), %rax
movsd 0x8(%rax), %xmm2
movsd -0x38(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movq -0x48(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x6693
movq -0x48(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x6613
jmp 0x6632
leaq 0x29bb4(%rip), %rdi     # 0x301ce
leaq 0x297a4(%rip), %rsi     # 0x2fdc5
movl $0x433, %edx            # imm = 0x433
leaq 0x29b4d(%rip), %rcx     # 0x3017a
callq 0x3220
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x48(%rbp), %rax
movsd (%rax), %xmm0
movq -0x48(%rbp), %rax
movsd 0x8(%rax), %xmm2
movsd -0x38(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
ucomisd -0x30(%rbp), %xmm0
jbe 0x666b
movl -0x3c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
jmp 0x6693
movq -0x48(%rbp), %rax
movsd (%rax), %xmm0
movq -0x48(%rbp), %rax
movsd 0x8(%rax), %xmm2
movsd -0x38(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x65e5
jmp 0x6695
movq -0x48(%rbp), %rax
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x66a8
jmp 0x66c7
leaq 0x29b1f(%rip), %rdi     # 0x301ce
leaq 0x2970f(%rip), %rsi     # 0x2fdc5
movl $0x43d, %edx            # imm = 0x43D
leaq 0x29ab8(%rip), %rcx     # 0x3017a
callq 0x3220
movq -0x48(%rbp), %rax
movsd (%rax), %xmm0
movq -0x48(%rbp), %rax
mulsd 0x8(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x66eb
movq -0x48(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x66f3
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)