// File: /workspace/github_c_makefile_star2_O2/Cyan4973[P]FiniteStateEntropy/programs/../lib/entropy_common.c
// Function: FSE_readNCount
size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
                 const void* headerBuffer, size_t hbSize)
{
    const BYTE* const istart = (const BYTE*) headerBuffer;
    const BYTE* const iend = istart + hbSize;
    const BYTE* ip = istart;
    int nbBits;
    int remaining;
    int threshold;
    U32 bitStream;
    int bitCount;
    unsigned charnum = 0;
    int previous0 = 0;

    if (hbSize < 4) {
        /* This function only works when hbSize >= 4 */
        char buffer[4] = {0};
        memcpy(buffer, headerBuffer, hbSize);
        {   size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr,
                                                    buffer, sizeof(buffer));
            if (FSE_isError(countSize)) return countSize;
            if (countSize > hbSize) return ERROR(corruption_detected);
            return countSize;
    }   }
    assert(hbSize >= 4);

    /* init */
    memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0]));   /* all symbols not present in NCount have a frequency of 0 */
    bitStream = MEM_readLE32(ip);
    nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG;   /* extract tableLog */
    if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
    bitStream >>= 4;
    bitCount = 4;
    *tableLogPtr = nbBits;
    remaining = (1<<nbBits)+1;
    threshold = 1<<nbBits;
    nbBits++;

    while ((remaining>1) & (charnum<=*maxSVPtr)) {
        if (previous0) {
            unsigned n0 = charnum;
            while ((bitStream & 0xFFFF) == 0xFFFF) {
                n0 += 24;
                if (ip < iend-5) {
                    ip += 2;
                    bitStream = MEM_readLE32(ip) >> bitCount;
                } else {
                    bitStream >>= 16;
                    bitCount   += 16;
            }   }
            while ((bitStream & 3) == 3) {
                n0 += 3;
                bitStream >>= 2;
                bitCount += 2;
            }
            n0 += bitStream & 3;
            bitCount += 2;
            if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
            while (charnum < n0) normalizedCounter[charnum++] = 0;
            if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
                assert((bitCount >> 3) <= 3); /* For first condition to work */
                ip += bitCount>>3;
                bitCount &= 7;
                bitStream = MEM_readLE32(ip) >> bitCount;
            } else {
                bitStream >>= 2;
        }   }
        {   int const max = (2*threshold-1) - remaining;
            int count;

            if ((bitStream & (threshold-1)) < (U32)max) {
                count = bitStream & (threshold-1);
                bitCount += nbBits-1;
            } else {
                count = bitStream & (2*threshold-1);
                if (count >= threshold) count -= max;
                bitCount += nbBits;
            }

            count--;   /* extra accuracy */
            remaining -= count < 0 ? -count : count;   /* -1 means +1 */
            normalizedCounter[charnum++] = (short)count;
            previous0 = !count;
            while (remaining < threshold) {
                nbBits--;
                threshold >>= 1;
            }

            if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
                ip += bitCount>>3;
                bitCount &= 7;
            } else {
                bitCount -= (int)(8 * (iend - 4 - ip));
                ip = iend - 4;
            }
            bitStream = MEM_readLE32(ip) >> (bitCount & 31);
    }   }   /* while ((remaining>1) & (charnum<=*maxSVPtr)) */
    if (remaining != 1) return ERROR(corruption_detected);
    if (bitCount > 32) return ERROR(corruption_detected);
    *maxSVPtr = charnum-1;

    ip += (bitCount+7)>>3;
    return ip-istart;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %r15
movq %rcx, %rbx
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r12
cmpq $0x3, %r8
ja 0x68a6
leaq 0x24(%rsp), %rbp
andl $0x0, (%rbp)
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1130
pushq $0x4
popq %r8
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x683d
movq %rax, %rcx
cmpq %r15, %rax
pushq $-0x4
popq %rax
cmovbeq %rcx, %rax
cmpq $-0x8, %rcx
cmovaeq %rcx, %rax
jmp 0x6a9e
movl (%r14), %edx
incl %edx
addq %rdx, %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x10c0
movq %r14, %rsi
movl (%rbx), %r14d
movl %r14d, %edx
andl $0xf, %edx
cmpl $0xa, %edx
jbe 0x68d0
pushq $-0x5
jmp 0x6a9d
addq %rbx, %r15
leal 0x5(%rdx), %eax
shrl $0x4, %r14d
movl %eax, (%r13)
pushq $0x20
popq %rdi
movl %edx, %ecx
shll %cl, %edi
leal 0x1(%rdi), %r8d
addl $0x6, %edx
movq %rsi, 0x10(%rsp)
movl (%rsi), %r9d
leaq -0x5(%r15), %r11
leaq -0x7(%r15), %rax
movq %rax, 0x8(%rsp)
addq $-0x4, %r15
movb $0x1, %sil
pushq $0x4
popq %rcx
xorl %r10d, %r10d
movq %rbx, 0x18(%rsp)
cmpl $0x2, %r8d
jl 0x6a6c
cmpl %r9d, %r10d
ja 0x6a6c
testb $0x1, %sil
je 0x6933
movl %ecx, %esi
jmp 0x69ba
leal 0x2(%rcx), %esi
movl %r10d, %ebp
cmpw $-0x1, %r14w
jne 0x696e
cmpq %r11, %rbx
jae 0x6955
movl 0x2(%rbx), %r14d
addq $0x2, %rbx
shrl %cl, %r14d
addl $0x18, %ebp
jmp 0x6939
shrl $0x10, %r14d
addl $0x10, %ecx
addl $0x18, %ebp
addl $0x10, %esi
jmp 0x6939
addl $0x3, %ebp
shrl $0x2, %r14d
addl $0x2, %esi
movl %r14d, %ecx
notl %ecx
testb $0x3, %cl
je 0x6964
movl %r14d, %ecx
andl $0x3, %ecx
addl %ebp, %ecx
cmpl %r9d, %ecx
ja 0x6a9b
movl %r10d, %r10d
movl %ecx, %ecx
cmpq %rcx, %r10
jae 0x699e
andw $0x0, (%r12,%r10,2)
incq %r10
jmp 0x698e
movl %esi, %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
leaq (%rbx,%rcx), %rbp
cmpq 0x8(%rsp), %rbx
jbe 0x69bf
cmpq %r15, %rbp
jbe 0x69bf
shrl $0x2, %r14d
movq %rbx, %rbp
jmp 0x69cb
andl $0x7, %esi
movl (%rbx,%rcx), %r14d
movl %esi, %ecx
shrl %cl, %r14d
leal -0x1(,%rdi,2), %ebx
movl %ebx, %ecx
subl %r8d, %ecx
leal -0x1(%rdi), %r13d
andl %r14d, %r13d
cmpl %ecx, %r13d
jae 0x69e8
leal -0x1(%rdx), %ecx
jmp 0x69fe
andl %ebx, %r14d
cmpl %edi, %r14d
movl $0x0, %eax
cmovll %eax, %ecx
subl %ecx, %r14d
movl %edx, %ecx
movl %r14d, %r13d
leal -0x1(%r13), %ebx
pushq $0x1
popq %r14
subl %r13d, %r14d
testl %r13d, %r13d
cmovgl %ebx, %r14d
subl %r14d, %r8d
movl %r10d, %r14d
movw %bx, (%r12,%r14,2)
cmpl %edi, %r8d
jge 0x6a26
decl %edx
sarl %edi
jmp 0x6a1b
addl %esi, %ecx
incl %r10d
testl %ebx, %ebx
setne %sil
cmpq 0x8(%rsp), %rbp
seta %r14b
movl %ecx, %ebx
sarl $0x3, %ebx
movslq %ebx, %rbx
addq %rbp, %rbx
cmpq %r15, %rbx
seta %r13b
subl %r15d, %ebp
movl %ecx, %eax
andl $0x7, %eax
leal (%rcx,%rbp,8), %ecx
testb %r13b, %r14b
cmovel %eax, %ecx
cmovneq %r15, %rbx
movl (%rbx), %r14d
shrl %cl, %r14d
jmp 0x6913
pushq $-0x4
popq %rax
cmpl $0x1, %r8d
jne 0x6a9e
cmpl $0x20, %ecx
jg 0x6a9e
decl %r10d
movq 0x10(%rsp), %rax
movl %r10d, (%rax)
addl $0x7, %ecx
sarl $0x3, %ecx
movslq %ecx, %rax
addq %rax, %rbx
subq 0x18(%rsp), %rbx
movq %rbx, %rax
jmp 0x6a9e
pushq $-0x7
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq