// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rcx, %r14
movq %rdx, %rbp
movq %rsi, %rbx
movq %rdi, %rax
cmpq $0x3, %r8
ja 0x7183
leaq 0x44(%rsp), %r13
movl $0x0, (%r13)
movq %r13, %rdi
movq %r14, %rsi
movq %r8, %rdx
movq %r8, %r14
movq %rax, %r15
callq 0x1130
movl $0x4, %r8d
movq %r15, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x710d
movq %rax, %rcx
cmpq %r14, %rax
movq $-0x4, %rax
cmovbeq %rcx, %rax
cmpq $-0x8, %rcx
cmovaeq %rcx, %rax
jmp 0x73ff
movq %r8, %r15
movl (%rbx), %edx
incl %edx
addq %rdx, %rdx
movq %rax, 0x8(%rsp)
movq %rax, %rdi
xorl %esi, %esi
callq 0x10c0
movl (%r14), %r12d
movl %r12d, %r13d
andl $0xf, %r13d
movq $-0x5, %rax
cmpl $0xa, %r13d
ja 0x73ff
movq %r15, %r8
addq %r14, %r8
leal 0x5(%r13), %eax
movq %rbx, %rdx
movl $0x20, %ebx
movl %r13d, %ecx
shll %cl, %ebx
movl %eax, (%rbp)
movq %rdx, 0x20(%rsp)
movl (%rdx), %eax
movl %eax, (%rsp)
leal 0x1(%rbx), %r11d
addl $0x6, %r13d
shrl $0x4, %r12d
leaq -0x5(%r8), %rax
movq %rax, 0x38(%rsp)
leaq -0x7(%r8), %rax
movq %rax, 0x10(%rsp)
addq $-0x4, %r8
movl $0x4, %ebp
movb $0x1, %sil
movq %r14, 0x18(%rsp)
movq %r14, %r15
xorl %eax, %eax
movq 0x8(%rsp), %r9
xorl %r10d, %r10d
movq %r8, 0x28(%rsp)
testb $0x1, %sil
je 0x7229
movl %eax, %edx
jmp 0x731d
movl %eax, %r14d
cmpw $-0x1, %r12w
movq 0x38(%rsp), %rdx
jne 0x726b
cmpq %rdx, %r15
jae 0x724c
movl 0x2(%r15), %r12d
addq $0x2, %r15
movl %ebp, %ecx
shrl %cl, %r12d
jmp 0x7253
shrl $0x10, %r12d
addl $0x10, %ebp
addl $0x18, %r14d
cmpw $-0x1, %r12w
je 0x7238
jmp 0x726b
addl $0x3, %r14d
shrl $0x2, %r12d
addl $0x2, %ebp
movl %r12d, %ecx
andl $0x3, %ecx
cmpl $0x3, %ecx
je 0x7260
addl %ecx, %r14d
addl $0x2, %ebp
cmpl (%rsp), %r14d
ja 0x7311
cmpl %r14d, %eax
jae 0x72dd
movl %r11d, 0x4(%rsp)
movl %eax, %ecx
movq %rcx, 0x30(%rsp)
leaq (%r9,%rcx,2), %rdi
notl %eax
addl %r14d, %eax
leaq 0x2(,%rax,2), %rdx
xorl %esi, %esi
callq 0x10c0
movq 0x30(%rsp), %rdx
movl %r14d, %eax
movq %rdx, %rcx
subq %rax, %rcx
xorl %eax, %eax
decq %rax
cmpq %rax, %rcx
jne 0x72bf
subl %eax, %edx
movl %edx, %eax
movq 0x28(%rsp), %r8
movq 0x8(%rsp), %r9
xorl %r10d, %r10d
movl 0x4(%rsp), %r11d
cmpq 0x10(%rsp), %r15
jbe 0x72fa
movl %ebp, %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
addq %r15, %rcx
cmpq %r8, %rcx
jbe 0x72fa
shrl $0x2, %r12d
jmp 0x7311
movl %ebp, %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
movl (%r15,%rcx), %r12d
addq %rcx, %r15
andl $0x7, %ebp
movl %ebp, %ecx
shrl %cl, %r12d
movl %eax, %edx
cmpl (%rsp), %r14d
ja 0x73f8
leal -0x1(,%rbx,2), %esi
movl %esi, %eax
subl %r11d, %eax
leal -0x1(%rbx), %ecx
andl %r12d, %ecx
cmpl %eax, %ecx
jae 0x7339
leal -0x1(%r13), %eax
jmp 0x734c
andl %esi, %r12d
cmpl %ebx, %r12d
cmovll %r10d, %eax
subl %eax, %r12d
movl %r13d, %eax
movl %r12d, %ecx
leal -0x1(%rcx), %esi
movl $0x1, %edi
subl %ecx, %edi
testl %ecx, %ecx
cmovgl %esi, %edi
subl %edi, %r11d
movl %edx, %ecx
movw %si, (%r9,%rcx,2)
testl %esi, %esi
setne %sil
cmpl %ebx, %r11d
jge 0x7377
decl %r13d
sarl %ebx
jmp 0x736b
addl %ebp, %eax
cmpq 0x10(%rsp), %r15
jbe 0x739c
movl %eax, %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
addq %r15, %rcx
cmpq %r8, %rcx
jbe 0x739c
subl %r8d, %r15d
leal (%rax,%r15,8), %ebp
movq %r8, %r15
jmp 0x73ac
movl %eax, %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
addq %rcx, %r15
andl $0x7, %eax
movl %eax, %ebp
movl (%r15), %r12d
movl %ebp, %ecx
shrl %cl, %r12d
cmpl $0x2, %r11d
jl 0x73c6
leal 0x1(%rdx), %eax
cmpl (%rsp), %eax
jbe 0x721c
movq $-0x4, %rax
cmpl $0x1, %r11d
jne 0x73ff
cmpl $0x20, %ebp
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %rsi
jg 0x73ff
movl %edx, (%rcx)
addl $0x7, %ebp
sarl $0x3, %ebp
movslq %ebp, %rax
addq %rax, %r15
subq %rsi, %r15
movq %r15, %rax
jmp 0x73ff
movq $-0x7, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq