// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/match/karlin_altschul_stat.c
// Function: gt_karlin_altschul_stat_calculate_ungapped_lambda
static double gt_karlin_altschul_stat_calculate_ungapped_lambda(
                                                     const ScoringFrequency *sf)
{
  double x0, x, lambda, tolerance, q, dq;
  GtWord low, high;
  GtUword k_max_iterations = 20, idx, jdx;

  /* solve phi(lambda) = -1 + sum_{i=l}^{u} sprob(i)*exp(i*lambda) = 0 */

  x0 = 0.5; /* x0 in (0,1) */
  tolerance = 1.e-5;

  low = sf->low_align_score;
  high = sf->high_align_score;

  /* write phi as phi(lambda) = exp(u*lambda) * q(exp(-lambda)) and solve the
    polynomial q by apply newton's method
    q(x) = -x^u + sum_{k=0}^{u-l} sprob(u-k)* x^k */
  for (idx = 0; idx < k_max_iterations; idx++)
  {
    q = -pow(x0,high);
    for (jdx = 0; jdx <= high-low; jdx++)
      q += sf->sprob[high-low-jdx] * pow(x0,jdx);

    dq = -high*pow(x0,high-1);
    for (jdx = 1; jdx <= high-low; jdx++)
      dq += sf->sprob[high-low-jdx] * jdx * pow(x0,jdx-1);

    x = x0 - (q/dq);

    if (fabs(x-x0) < tolerance)
      break;

    x0 = x;
  }

  lambda = -log(x);

  /* better solution would be to apply Horner's rule for evaluating a
     polynomial and its derivative (s. BLAST), but for the moment it works */

  return lambda;
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq $0x14, 0x28(%rsp)
movsd 0x2c8a6e(%rip), %xmm0   # 0x526f08
movsd %xmm0, 0x68(%rsp)
movsd 0x2bfdb0(%rip), %xmm0   # 0x51e258
movsd %xmm0, 0x50(%rsp)
movq 0x70(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x70(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x30(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpq 0x28(%rsp), %rax
jae 0x25e723
movsd 0x68(%rsp), %xmm0
cvtsi2sdq 0x30(%rsp), %xmm1
callq 0x19cb0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x48(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %rcx
subq 0x38(%rsp), %rcx
cmpq %rcx, %rax
ja 0x25e5bd
movq 0x70(%rsp), %rax
movq (%rax), %rcx
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rax
subq %rax, %rdx
movq 0x18(%rsp), %rax
subq %rax, %rdx
movsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x68(%rsp), %xmm0
movq %rax, %xmm1
movaps 0x2ba3c3(%rip), %xmm2   # 0x518930
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x2ba3c7(%rip), %xmm2   # 0x518940
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
callq 0x19cb0
movaps %xmm0, %xmm2
movsd 0x10(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x18(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x18(%rsp)
jmp 0x25e51b
xorl %eax, %eax
subq 0x30(%rsp), %rax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x68(%rsp), %xmm0
movq 0x30(%rsp), %rax
subq $0x1, %rax
cvtsi2sd %rax, %xmm1
callq 0x19cb0
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movq $0x1, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %rcx
subq 0x38(%rsp), %rcx
cmpq %rcx, %rax
ja 0x25e6c3
movq 0x70(%rsp), %rax
movq (%rax), %rcx
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rax
subq %rax, %rdx
movq 0x18(%rsp), %rax
subq %rax, %rdx
movsd (%rcx,%rdx,8), %xmm0
movq %rax, %xmm1
movaps 0x2ba2e6(%rip), %xmm3   # 0x518930
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movapd 0x2ba2ea(%rip), %xmm2   # 0x518940
subpd %xmm2, %xmm1
movaps %xmm1, %xmm4
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm4, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movsd 0x68(%rsp), %xmm0
decq %rax
movq %rax, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
callq 0x19cb0
movaps %xmm0, %xmm2
movsd (%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x18(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x18(%rsp)
jmp 0x25e604
movsd 0x68(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
divsd 0x40(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm1
subsd 0x68(%rsp), %xmm1
movaps 0x2ded5e(%rip), %xmm0   # 0x53d450
pand %xmm0, %xmm1
movsd 0x50(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x25e704
jmp 0x25e723
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
jmp 0x25e4d3
movsd 0x60(%rsp), %xmm0
callq 0x195c0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x58(%rsp), %xmm0
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)