// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rcx, %rbx
movq 0x140(%rsp), %rax
pushq $-0x3
popq %rcx
testq %rax, %rax
je 0x6c16
movq %r8, %r13
movq %rdx, %r15
movq %rdi, %r12
movzbl (%r9), %r14d
pushq $-0x4
popq %rbp
testb %r14b, %r14b
js 0x6b1e
cmpq %rax, %r14
jae 0x6c16
decq %rsi
incq %r9
movq %rsp, %r8
pushq $0x6
popq %rax
movq %r12, %rdi
movq %r9, %rdx
movq %r14, %rcx
movl %eax, %r9d
callq 0x73ca
movq %rax, %rcx
cmpq $-0x9, %rax
jbe 0x6b69
jmp 0x6c16
leaq -0x7e(%r14), %rdx
shrq %rdx
cmpq %rax, %rdx
jae 0x6c16
addq $-0x7f, %r14
cmpq %rsi, %r14
jae 0x6c13
incq %r9
xorl %eax, %eax
cmpl %r14d, %eax
jae 0x6b63
movb (%r9), %cl
shrb $0x4, %cl
movb %cl, (%r12,%rax)
movb (%r9), %cl
andb $0xf, %cl
movb %cl, 0x1(%r12,%rax)
addq $0x2, %rax
incq %r9
jmp 0x6b40
movq %r14, %rax
movq %rdx, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x10(%r15)
andl $0x0, 0x30(%r15)
movups %xmm0, (%r15)
xorl %ecx, %ecx
pushq $0x1
popq %rdx
xorl %esi, %esi
movl %ecx, %edi
cmpq %rdi, %rax
jbe 0x6bb1
movzbl (%r12,%rdi), %ecx
cmpq $0xb, %rcx
ja 0x6c13
incl (%r15,%rcx,4)
movb (%r12,%rdi), %cl
movl %edx, %r8d
shll %cl, %r8d
sarl %r8d
addl %r8d, %esi
leal 0x1(%rdi), %ecx
jmp 0x6b86
testl %esi, %esi
je 0x6c13
bsrl %esi, %ecx
cmpl $0xb, %ecx
ja 0x6c13
movl %ecx, %r8d
xorl $0x1f, %r8d
pushq $0x20
popq %rdi
movl %edi, %r9d
subl %r8d, %r9d
pushq $0x2
popq %r8
shll %cl, %r8d
movl %r9d, (%r13)
subl %esi, %r8d
bsrl %r8d, %ecx
shll %cl, %edx
cmpl %r8d, %edx
jne 0x6c13
xorl $0x1f, %ecx
subl %ecx, %edi
movb %dil, (%r12,%rax)
incl (%r15,%rdi,4)
movl 0x4(%r15), %edx
cmpl $0x2, %edx
setb %cl
orb %cl, %dl
movq %rbp, %rcx
testb $0x1, %dl
jne 0x6c16
incl %eax
movl %eax, (%rbx)
incq %r14
movq %r14, %rcx
jmp 0x6c16
movq %rbp, %rcx
movq %rcx, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop