// File: /workspace/github_c_makefile_star2_O1/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 0x95b0
movq %rax, %r14
cmpl $0x0, 0x8(%rbx)
je 0x9f7b
xorl %r13d, %r13d
movq $0x0, (%r14,%r13,8)
movl 0x4c(%rbx), %eax
testl %eax, %eax
je 0x9f70
xorl %ebp, %ebp
imull %r13d, %eax
addl %ebp, %eax
movsd (%r12,%rax,8), %xmm0
ucomisd 0x290f0(%rip), %xmm0    # 0x33030
jne 0x9f44
jnp 0x9f67
movsd %xmm0, 0x8(%rsp)
divsd (%r15,%r13,8), %xmm0
callq 0x9430
mulsd 0x8(%rsp), %xmm0
addsd (%r14,%r13,8), %xmm0
movsd %xmm0, (%r14,%r13,8)
incl %ebp
movl 0x4c(%rbx), %eax
cmpl %eax, %ebp
jb 0x9f2c
incq %r13
movl 0x8(%rbx), %eax
cmpq %rax, %r13
jb 0x9f1b
cmpl $0x0, 0x28(%rbx)
je 0xa0d8
movq $0x0, 0x10(%rsp)
xorpd %xmm1, %xmm1
movq %r15, 0x18(%rsp)
cmpl $0x0, 0x8(%rbx)
je 0xa0bf
movq 0x20(%rbx), %rax
movq 0x10(%rsp), %rcx
movl (%rax,%rcx,4), %ebp
xorl %r13d, %r13d
cmpq %rbp, %r13
je 0xa0b0
movsd (%r15,%rbp,8), %xmm0
ucomisd %xmm1, %xmm0
jne 0x9fcb
jnp 0xa0b0
movsd (%r15,%r13,8), %xmm2
ucomisd %xmm1, %xmm2
jne 0x9fdd
jnp 0xa0b0
addsd %xmm2, %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x9430
movsd (%r14,%rbp,8), %xmm2
addsd (%r14,%r13,8), %xmm2
mulsd 0x8(%rsp), %xmm0
addsd %xmm0, %xmm2
movl 0x4c(%rbx), %eax
testl %eax, %eax
je 0xa071
xorl %r15d, %r15d
xorpd %xmm1, %xmm1
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 0xa03b
jp 0xa03b
ucomisd %xmm1, %xmm3
jne 0xa03b
jnp 0xa064
addsd %xmm3, %xmm0
movsd %xmm0, 0x20(%rsp)
movsd %xmm2, 0x8(%rsp)
callq 0x9430
movsd 0x8(%rsp), %xmm2
xorpd %xmm1, %xmm1
mulsd 0x20(%rsp), %xmm0
subsd %xmm0, %xmm2
incl %r15d
movl 0x4c(%rbx), %eax
cmpl %eax, %r15d
jb 0xa010
jmp 0xa075
xorpd %xmm1, %xmm1
movq 0x10(%rbx), %rax
movsd (%rax,%rbp,8), %xmm0
ucomisd %xmm2, %xmm0
movq 0x18(%rsp), %r15
jbe 0xa096
movsd %xmm2, (%rax,%rbp,8)
movq 0x18(%rbx), %rcx
movl %r13d, (%rcx,%rbp,4)
movsd (%rax,%r13,8), %xmm0
ucomisd %xmm2, %xmm0
jbe 0xa0b0
movsd %xmm2, (%rax,%r13,8)
movq 0x18(%rbx), %rax
movl %ebp, (%rax,%r13,4)
incq %r13
movl 0x8(%rbx), %eax
cmpq %rax, %r13
jb 0x9fb0
movq 0x10(%rsp), %rdx
incq %rdx
movl 0x28(%rbx), %eax
movq %rdx, 0x10(%rsp)
cmpq %rax, %rdx
jb 0x9f97
movq %r14, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9580