// File: /workspace/github_c_makefile_star2_O2/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 0x82d7
movl %eax, %ebx
movq %r14, %rdi
callq 0x82d7
shll $0x3, %eax
cmpl %eax, %ebx
jne 0x7fbb
movq %r15, %rdi
callq 0x82d7
testl %eax, %eax
je 0x7fa4
movq %r15, %rdi
callq 0x82d7
leal 0x20(,%rax,8), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
movq %r14, 0x28(%rsp)
movq %r14, %rdi
callq 0x82d7
cmpl %eax, %ebx
jge 0x7f9c
movq %r14, %rdi
movl %ebx, %esi
callq 0x831b
movzbl (%rax), %eax
movl %eax, 0x18(%rsp)
movq %rbx, 0x30(%rsp)
leal 0x8(,%rbx,8), %eax
movl %eax, 0x14(%rsp)
pushq $0x8
popq %rax
movl %eax, %ebp
movsd 0x8(%rsp), %xmm0
subl $0x1, %ebp
movsd %xmm0, 0x8(%rsp)
jb 0x7f8b
pushq $0x1
popq %rdx
movl %ebp, %ecx
shll %cl, %edx
andl 0x18(%rsp), %edx
movl %edx, 0x1c(%rsp)
movl 0x14(%rsp), %esi
subl %eax, %esi
movq 0x20(%rsp), %rdi
callq 0x831b
movq %rax, %r13
xorl %ebx, %ebx
pushq $0x2
popq %rax
movl %eax, %r14d
movl %eax, %r12d
movq %r15, %rdi
callq 0x82d7
cmpl %eax, %ebx
jge 0x7f4e
movq %r15, %rdi
movl %ebx, %esi
callq 0x831b
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, %r14d
addl %eax, %r12d
incl %ebx
jmp 0x7f18
leal (%r12,%r14), %eax
cmpl $0x0, 0x1c(%rsp)
cvtsi2sd %rax, %xmm1
cmovel %r14d, %r12d
cvtsi2sd %r12, %xmm0
divsd %xmm1, %xmm0
callq 0x20e0
mulsd 0x2f7a(%rip), %xmm0     # 0xaef0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movl %ebp, %eax
jmp 0x7edc
movq 0x30(%rsp), %rbx
incl %ebx
movq 0x28(%rsp), %r14
jmp 0x7ea0
movsd 0x8(%rsp), %xmm0
jmp 0x7fac
movsd 0x2f3c(%rip), %xmm0     # 0xaee8
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %rdi
callq 0x82d7
movl %eax, %ebx
movq %r14, %rdi
callq 0x82d7
leaq 0x2fe5(%rip), %rdi      # 0xafbb
movl %ebx, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x7514
pushq $-0x1
popq %rdi
callq 0x21f0