// File: /workspace/github_c_makefile_star2_O3/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
movl 0x8(%rbx), %eax
testl %eax, %eax
je 0x9f8c
movl 0x4c(%rbx), %ecx
xorl %r13d, %r13d
movl %ecx, %edx
movq $0x0, (%r14,%r13,8)
testl %edx, %edx
je 0x9f7e
xorl %ebp, %ebp
imull %r13d, %edx
addl %ebp, %edx
movsd (%r12,%rdx,8), %xmm0
ucomisd 0x280eb(%rip), %xmm0    # 0x32030
jne 0x9f49
jnp 0x9f6f
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)
movl 0x4c(%rbx), %ecx
incl %ebp
movl %ecx, %edx
cmpl %ecx, %ebp
jb 0x9f31
movl 0x8(%rbx), %eax
movl %ecx, %edx
jmp 0x9f80
xorl %edx, %edx
incq %r13
movl %eax, %esi
cmpq %rsi, %r13
jb 0x9f23
jmp 0x9f8e
xorl %eax, %eax
movl 0x28(%rbx), %ecx
testl %ecx, %ecx
je 0xa0e7
xorl %esi, %esi
xorpd %xmm1, %xmm1
movq %r15, 0x18(%rsp)
testl %eax, %eax
movl $0x0, %eax
je 0xa0d9
movq 0x20(%rbx), %rax
movq %rsi, 0x10(%rsp)
movl (%rax,%rsi,4), %ebp
xorl %r13d, %r13d
cmpq %rbp, %r13
je 0xa0c2
movsd (%r15,%rbp,8), %xmm0
ucomisd %xmm1, %xmm0
jne 0x9fdb
jnp 0xa0c2
movsd (%r15,%r13,8), %xmm2
ucomisd %xmm1, %xmm2
jne 0x9fed
jnp 0xa0c2
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 0xa083
xorl %r15d, %r15d
xorpd %xmm1, %xmm1
movl %eax, %ecx
imull %ebp, %ecx
addl %r15d, %ecx
movsd (%r12,%rcx,8), %xmm0
movl %r13d, %ecx
imull %eax, %ecx
addl %r15d, %ecx
movsd (%r12,%rcx,8), %xmm3
ucomisd %xmm1, %xmm0
jne 0xa04d
jp 0xa04d
ucomisd %xmm1, %xmm3
jne 0xa04d
jnp 0xa079
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
movl 0x4c(%rbx), %eax
incl %r15d
cmpl %eax, %r15d
jb 0xa020
jmp 0xa087
xorpd %xmm1, %xmm1
movq 0x10(%rbx), %rax
movsd (%rax,%rbp,8), %xmm0
ucomisd %xmm2, %xmm0
movq 0x18(%rsp), %r15
jbe 0xa0a8
movsd %xmm2, (%rax,%rbp,8)
movq 0x18(%rbx), %rcx
movl %r13d, (%rcx,%rbp,4)
movsd (%rax,%r13,8), %xmm0
ucomisd %xmm2, %xmm0
jbe 0xa0c2
movsd %xmm2, (%rax,%r13,8)
movq 0x18(%rbx), %rax
movl %ebp, (%rax,%r13,4)
incq %r13
movl 0x8(%rbx), %eax
cmpq %rax, %r13
jb 0x9fc0
movl 0x28(%rbx), %ecx
movq 0x10(%rsp), %rsi
incq %rsi
movl %ecx, %edx
cmpq %rdx, %rsi
jb 0x9fa4
movq %r14, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9580