// File: /workspace/github_c_makefile_star2_O3/Cyan4973[P]FiniteStateEntropy/programs/../lib/entropy_common.c
// Function: HUF_readStats
size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
                     U32* nbSymbolsPtr, U32* tableLogPtr,
                     const void* src, size_t srcSize)
{
    U32 weightTotal;
    const BYTE* ip = (const BYTE*) src;
    size_t iSize;
    size_t oSize;

    if (!srcSize) return ERROR(srcSize_wrong);
    iSize = ip[0];
    /* memset(huffWeight, 0, hwSize);   *//* is not necessary, even though some analyzer complain ... */

    if (iSize >= 128) {  /* special header */
        oSize = iSize - 127;
        iSize = ((oSize+1)/2);
        if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
        if (oSize >= hwSize) return ERROR(corruption_detected);
        ip += 1;
        {   U32 n;
            for (n=0; n<oSize; n+=2) {
                huffWeight[n]   = ip[n/2] >> 4;
                huffWeight[n+1] = ip[n/2] & 15;
    }   }   }
    else  {   /* header compressed with FSE (normal case) */
        FSE_DTable fseWorkspace[FSE_DTABLE_SIZE_U32(6)];  /* 6 is max possible tableLog for HUF header (maybe even 5, to be tested) */
        if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
        oSize = FSE_decompress_wksp(huffWeight, hwSize-1, ip+1, iSize, fseWorkspace, 6);   /* max (hwSize-1) values decoded, as last one is implied */
        if (FSE_isError(oSize)) return oSize;
    }

    /* collect weight stats */
    memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
    weightTotal = 0;
    {   U32 n; for (n=0; n<oSize; n++) {
            if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected);
            rankStats[huffWeight[n]]++;
            weightTotal += (1 << huffWeight[n]) >> 1;
    }   }
    if (weightTotal == 0) return ERROR(corruption_detected);

    /* get last non-null symbol weight (implied, total must be 2^n) */
    {   U32 const tableLog = BIT_highbit32(weightTotal) + 1;
        if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
        *tableLogPtr = tableLog;
        /* determine last weight */
        {   U32 const total = 1 << tableLog;
            U32 const rest = total - weightTotal;
            U32 const verif = 1 << BIT_highbit32(rest);
            U32 const lastWeight = BIT_highbit32(rest) + 1;
            if (verif != rest) return ERROR(corruption_detected);    /* last value must be a clean power of 2 */
            huffWeight[oSize] = (BYTE)lastWeight;
            rankStats[lastWeight]++;
    }   }

    /* check tree construction validity */
    if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected);   /* by construction : at least 2 elts of rank 1, must be even */

    /* results */
    *nbSymbolsPtr = (U32)(oSize+1);
    return iSize+1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x110, %rsp            # imm = 0x110
movq %rdx, %r15
movq 0x140(%rsp), %rdx
movq $-0x3, %rax
testq %rdx, %rdx
je 0x7bd6
movq %r8, %r13
movq %rcx, %rbx
movq %rdi, %r12
movzbl (%r9), %r14d
testb %r14b, %r14b
js 0x7a92
cmpq %rdx, %r14
jae 0x7bd6
decq %rsi
incq %r9
movq %rsp, %r8
movq %r12, %rdi
movq %r9, %rdx
movq %r14, %rcx
movl $0x6, %r9d
callq 0x8a33
movq %rax, %rdx
cmpq $-0x9, %rax
jbe 0x7aeb
jmp 0x7bd6
leaq -0x7e(%r14), %rcx
shrq %rcx
cmpq %rdx, %rcx
jae 0x7bd6
addq $-0x7f, %r14
movq $-0x4, %rax
cmpq %rsi, %r14
jae 0x7bd6
testq %r14, %r14
je 0x7bbd
incq %r9
xorl %eax, %eax
movb (%r9), %dl
shrb $0x4, %dl
movb %dl, (%r12,%rax)
movb (%r9), %dl
andb $0xf, %dl
movb %dl, 0x1(%r12,%rax)
addq $0x2, %rax
incq %r9
cmpl %eax, %r14d
ja 0x7ac4
movq %r14, %rdx
movq %rcx, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x10(%r15)
movups %xmm0, (%r15)
movl $0x0, 0x30(%r15)
movq $-0x4, %rax
testq %rdx, %rdx
je 0x7bd6
xorl %ecx, %ecx
movl $0x1, %edi
xorl %esi, %esi
movzbl (%r12,%rcx), %r8d
cmpq $0xb, %r8
ja 0x7bd6
incl (%r15,%r8,4)
movb (%r12,%rcx), %cl
movl $0x1, %r8d
shll %cl, %r8d
sarl %r8d
addl %r8d, %esi
movl %edi, %ecx
incl %edi
cmpq %rcx, %rdx
ja 0x7b1d
testl %esi, %esi
je 0x7bd6
bsrl %esi, %ecx
cmpl $0xb, %ecx
ja 0x7bd6
movl %ecx, %r8d
xorl $0x1f, %r8d
movl $0x20, %edi
movl $0x20, %r9d
subl %r8d, %r9d
movl %r9d, (%r13)
movl $0x2, %r8d
shll %cl, %r8d
subl %esi, %r8d
bsrl %r8d, %ecx
movl $0x1, %esi
shll %cl, %esi
cmpl %r8d, %esi
jne 0x7bd6
xorl $0x1f, %ecx
subl %ecx, %edi
movb %dil, (%r12,%rdx)
incl (%r15,%rdi,4)
movl 0x4(%r15), %ecx
cmpl $0x2, %ecx
setb %sil
orb %sil, %cl
testb $0x1, %cl
jne 0x7bd6
incl %edx
movl %edx, (%rbx)
incq %r14
movq %r14, %rax
jmp 0x7bd6
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x10(%r15)
movups %xmm0, (%r15)
movl $0x0, 0x30(%r15)
addq $0x110, %rsp            # imm = 0x110
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop