// File: /workspace/github_c_makefile_star2_O3/negge[P]xlink/src/paq.c
// Function: xlink_modeler_get_entropy
double xlink_modeler_get_entropy(xlink_modeler *mod, xlink_list *models) {
  XLINK_ERROR(
   xlink_list_length(&mod->counts) != 8*xlink_list_length(&mod->bytes),
   ("Counts model does not match input bytes, counts = %i and bytes = %i",
   xlink_list_length(&mod->counts), xlink_list_length(&mod->bytes)));
  if (xlink_list_length(models) == 0) {
    return DBL_MAX;
  }
  else {
    double entropy;
    int i, j, k;
    /* Include 4 bytes for the weights + 1 byte for each model */
    entropy = 8*(4 + xlink_list_length(models));
    for (j = 0; j < xlink_list_length(&mod->bytes); j++) {
      unsigned char byte;
      byte = *xlink_list_get_byte(&mod->bytes, j);
      for (i = 8; i-- > 0; ) {
        int bit;
        xlink_counts *counts;
        unsigned int c0, c1;
        bit = !!(byte & (1 << i));
        counts = xlink_list_get(&mod->counts, j*8 + (7 - i));
        c0 = c1 = 2;
        for (k = 0; k < xlink_list_length(models); k++) {
          xlink_model *model;
          model = xlink_list_get(models, k);
          c0 += ((unsigned int)(*counts)[model->mask][0]) << model->weight;
          c1 += ((unsigned int)(*counts)[model->mask][1]) << model->weight;
        }
        if (bit) {
          entropy -= M_LOG2E*log(((double)c1)/(c0 + c1));
        }
        else {
          entropy -= M_LOG2E*log(((double)c0)/(c0 + c1));
        }
      }
    }
    return entropy;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r15
movq %rdi, %r14
addq $0x18, %rdi
movq %rdi, 0x20(%rsp)
callq 0x94be
movl %eax, %ebx
movq %r14, %rdi
callq 0x94be
shll $0x3, %eax
cmpl %eax, %ebx
jne 0x9021
movq %r15, %rdi
callq 0x94be
testl %eax, %eax
je 0x8ffe
movq %r15, %rdi
callq 0x94be
leal 0x20(,%rax,8), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0x94be
testl %eax, %eax
jle 0x900c
xorl %ebx, %ebx
movq %r14, 0x28(%rsp)
movq %r14, %rdi
movl %ebx, %esi
callq 0x9507
movzbl (%rax), %eax
movl %eax, 0x18(%rsp)
movq %rbx, 0x30(%rsp)
leal 0x8(,%rbx,8), %eax
movl %eax, 0x14(%rsp)
movl $0x8, %ecx
movl $0x7, %eax
movl 0x14(%rsp), %esi
subl %ecx, %esi
movl %eax, %ecx
movl $0x1, %r12d
movl %eax, 0x1c(%rsp)
shll %cl, %r12d
movq 0x20(%rsp), %rdi
callq 0x9507
movq %rax, %r13
movq %r15, %rdi
callq 0x94be
testl %eax, %eax
jle 0x8f85
xorl %ebx, %ebx
movl $0x2, %r14d
movl $0x2, %ebp
movq %r15, %rdi
movl %ebx, %esi
callq 0x9507
movzbl (%rax), %edx
movzbl (%r13,%rdx,2), %esi
movb 0x4(%rax), %cl
shll %cl, %esi
movzbl 0x1(%r13,%rdx,2), %eax
shll %cl, %eax
addl %esi, %ebp
addl %eax, %r14d
incl %ebx
movq %r15, %rdi
callq 0x94be
cmpl %eax, %ebx
jl 0x8f50
jmp 0x8f90
movl $0x2, %ebp
movl $0x2, %r14d
andl 0x18(%rsp), %r12d
leal (%r14,%rbp), %eax
testl %r12d, %r12d
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
cmovel %ebp, %r14d
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
divsd %xmm1, %xmm0
callq 0x20e0
mulsd 0x2f5f(%rip), %xmm0     # 0xbf20
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movl 0x1c(%rsp), %ecx
movl %ecx, %eax
subl $0x1, %eax
jae 0x8f15
movq 0x30(%rsp), %rbx
incl %ebx
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x94be
cmpl %eax, %ebx
jl 0x8eea
jmp 0x900c
movsd 0x2f12(%rip), %xmm0     # 0xbf18
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %rdi
callq 0x94be
movl %eax, %ebx
movq %r14, %rdi
callq 0x94be
leaq 0x2faf(%rip), %rdi      # 0xbfeb
movl %ebx, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x22ef
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x21f0