// File: /workspace/github_c_makefile_star2_O2/zpole[P]IICV/vl/aib.c
// Function: vl_aib_update_beta
void
vl_aib_update_beta (VlAIB * aib)
{

#define PLOGP(x) ((x)*log((x)))

  vl_uint i;
  double * Px  = aib->Px;
  double * Pcx = aib->Pcx;
  double * tmp = vl_malloc(sizeof(double)*aib->nentries);
  vl_uint a, b, c ;

  /*
   * T1 = I(x,c) - I([x]_ij) = A + B - C
   *
   * A  = \sum_c p(xa,c)           \log ( p(xa,c)          /  p(xa)       )
   * B  = \sum_c p(xb,c)           \log ( p(xb,c)          /  p(xb)       )
   * C  = \sum_c (p(xa,c)+p(xb,c)) \log ((p(xa,c)+p(xb,c)) / (p(xa)+p(xb)))
   *
   * C  = C1 + C2
   * C1 = \sum_c (p(xa,c)+p(xb,c)) \log (p(xa,c)+p(xb,c))
   * C2 = - (p(xa)+p(xb) \log (p(xa)+p(xb))
   */

  /* precalculate A and B */
  for (a = 0; a < aib->nentries; a++) {
    tmp[a] = 0;
    for (c = 0; c < aib->nlabels; c++) {
        double Pac = Pcx [a*aib->nlabels + c] ;
        if(Pac != 0) tmp[a] += Pac * log (Pac / Px[a]) ;
    }
  }

  /* for each entry listed in which */
  for (i = 0 ; i < aib->nwhich; i++) {
    a = aib->which[i];

    /* for each other entry */
    for(b = 0 ; b < aib->nentries ; b++) {
      double T1 = 0 ;

      if (a == b || Px [a] == 0 || Px [b] == 0) continue ;


      T1 = PLOGP ((Px[a] + Px[b])) ;                  /* - C2 */
      T1 += tmp[a] + tmp[b] ;                         /* + A + B */

      for (c = 0 ; c < aib->nlabels; ++ c) {
        double Pac = Pcx [a*aib->nlabels + c] ;
        double Pbc = Pcx [b*aib->nlabels + c] ;
        if (Pac == 0 && Pbc == 0) continue;
        T1 += - PLOGP ((Pac + Pbc)) ;                 /* - C1 */
      }

      /*
       * Now we have beta(a,b). We check wether this is the best beta
       * for entries a and b.
       */
      {
        double beta = T1 ;

        if (beta < aib->beta[a])
          {
            aib->beta[a] = beta;
            aib->bidx[a] = b;
          }
        if (beta < aib->beta[b])
          {
            aib->beta[b] = beta;
            aib->bidx[b] = a;
          }
      }
    }
  }
  vl_free(tmp);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movq 0x38(%rdi), %r15
movq 0x30(%rdi), %r12
movl 0x8(%rdi), %edi
shlq $0x3, %rdi
callq 0x95e0
movq %rax, %r14
xorl %r13d, %r13d
movl 0x8(%rbx), %eax
cmpq %rax, %r13
jae 0x9f48
andq $0x0, (%r14,%r13,8)
xorl %ebp, %ebp
movl 0x4c(%rbx), %eax
cmpl %eax, %ebp
jae 0x9f43
imull %r13d, %eax
addl %ebp, %eax
movsd (%r12,%rax,8), %xmm0
ucomisd 0x230e8(%rip), %xmm0    # 0x2d000
jne 0x9f1c
jnp 0x9f3f
movsd %xmm0, 0x8(%rsp)
divsd (%r15,%r13,8), %xmm0
callq 0x9450
mulsd 0x8(%rsp), %xmm0
addsd (%r14,%r13,8), %xmm0
movsd %xmm0, (%r14,%r13,8)
incl %ebp
jmp 0x9efd
incq %r13
jmp 0x9eee
xorl %edx, %edx
xorpd %xmm1, %xmm1
movq %r15, 0x18(%rsp)
movl 0x28(%rbx), %ecx
cmpq %rcx, %rdx
jae 0xa086
movq 0x20(%rbx), %rcx
movq %rdx, 0x10(%rsp)
movl (%rcx,%rdx,4), %ebp
xorl %r13d, %r13d
movl %eax, %ecx
cmpq %rcx, %r13
jae 0xa079
cmpq %rbp, %r13
je 0xa06e
movsd (%r15,%rbp,8), %xmm0
ucomisd %xmm1, %xmm0
jne 0x9f94
jnp 0xa06e
movsd (%r15,%r13,8), %xmm2
ucomisd %xmm1, %xmm2
jne 0x9fa6
jnp 0xa06e
addsd %xmm2, %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x9450
xorpd %xmm1, %xmm1
mulsd 0x8(%rsp), %xmm0
movsd (%r14,%rbp,8), %xmm2
addsd (%r14,%r13,8), %xmm2
addsd %xmm0, %xmm2
xorl %r15d, %r15d
movl 0x4c(%rbx), %eax
cmpl %eax, %r15d
jae 0xa033
movl %r13d, %ecx
imull %eax, %ecx
imull %ebp, %eax
addl %r15d, %eax
movsd (%r12,%rax,8), %xmm0
addl %r15d, %ecx
movsd (%r12,%rcx,8), %xmm3
ucomisd %xmm1, %xmm0
jne 0xa005
jp 0xa005
ucomisd %xmm1, %xmm3
jne 0xa005
jnp 0xa02e
addsd %xmm3, %xmm0
movsd %xmm0, 0x20(%rsp)
movsd %xmm2, 0x8(%rsp)
callq 0x9450
movsd 0x8(%rsp), %xmm2
xorpd %xmm1, %xmm1
mulsd 0x20(%rsp), %xmm0
subsd %xmm0, %xmm2
incl %r15d
jmp 0x9fd2
movq 0x10(%rbx), %rax
movsd (%rax,%rbp,8), %xmm0
ucomisd %xmm2, %xmm0
jbe 0xa04f
movsd %xmm2, (%rax,%rbp,8)
movq 0x18(%rbx), %rcx
movl %r13d, (%rcx,%rbp,4)
movsd (%rax,%r13,8), %xmm0
ucomisd %xmm2, %xmm0
movq 0x18(%rsp), %r15
jbe 0xa06e
movsd %xmm2, (%rax,%r13,8)
movq 0x18(%rbx), %rax
movl %ebp, (%rax,%r13,4)
incq %r13
movl 0x8(%rbx), %eax
jmp 0x9f6e
movq 0x10(%rsp), %rdx
incq %rdx
jmp 0x9f53
movq %r14, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x95b0