// File: /workspace/github_c_makefile_star2_O3/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, %rbx
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r15
cmpq $0x3, %r8
ja 0x77c8
leaq 0x44(%rsp), %r13
movl $0x0, (%r13)
movq %r13, %rdi
movq %rbx, %rsi
movq %r8, %rdx
movq %r8, %rbx
callq 0x1130
movl $0x4, %r8d
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x7755
movq %rax, %rcx
cmpq %rbx, %rax
movq $-0x4, %rax
cmovbeq %rcx, %rax
cmpq $-0x8, %rcx
cmovaeq %rcx, %rax
jmp 0x7a12
movq %r8, %r12
movl (%r14), %edx
incl %edx
addq %rdx, %rdx
movq %r15, 0x8(%rsp)
movq %r15, %rdi
xorl %esi, %esi
callq 0x10c0
movl (%rbx), %r15d
movl %r15d, %r13d
andl $0xf, %r13d
movq $-0x5, %rax
cmpl $0xa, %r13d
ja 0x7a12
movq %r12, %r9
addq %rbx, %r9
leal 0x5(%r13), %eax
movq %rbx, %r12
movl $0x20, %ebx
movl %r13d, %ecx
shll %cl, %ebx
movl %eax, (%rbp)
movq %r14, 0x20(%rsp)
movl (%r14), %r11d
leal 0x1(%rbx), %r14d
addl $0x6, %r13d
shrl $0x4, %r15d
leaq -0x5(%r9), %rsi
leaq -0x7(%r9), %rax
movq %rax, 0x10(%rsp)
addq $-0x4, %r9
movl $0x4, %ebp
movb $0x1, %dl
movq %r12, 0x18(%rsp)
xorl %eax, %eax
movq %rsi, 0x38(%rsp)
movq %r9, 0x28(%rsp)
movl %r11d, 0x4(%rsp)
testb $0x1, %dl
je 0x7866
movl %eax, %r10d
jmp 0x790f
movl %eax, %edx
cmpw $-0x1, %r15w
jne 0x789a
cmpq %rsi, %r12
jae 0x7884
movl 0x2(%r12), %r15d
addq $0x2, %r12
movl %ebp, %ecx
shrl %cl, %r15d
jmp 0x788b
shrl $0x10, %r15d
addl $0x10, %ebp
addl $0x18, %edx
jmp 0x7868
addl $0x3, %edx
shrl $0x2, %r15d
addl $0x2, %ebp
movl %r15d, %ecx
andl $0x3, %ecx
cmpl $0x3, %ecx
je 0x7890
leal (%rdx,%rcx), %r8d
cmpl %r11d, %r8d
ja 0x7a0b
cmpl %r8d, %eax
jae 0x78ed
movl %eax, %esi
movq 0x8(%rsp), %rdi
leaq (%rdi,%rsi,2), %rdi
addl %ecx, %edx
notl %eax
addl %edx, %eax
leaq 0x2(,%rax,2), %rdx
xorl %esi, %esi
movq %r8, 0x30(%rsp)
callq 0x10c0
movq 0x30(%rsp), %r10
movl 0x4(%rsp), %r11d
movq 0x28(%rsp), %r9
jmp 0x78f0
movl %eax, %r10d
addl $0x2, %ebp
movl %ebp, %eax
sarl $0x3, %eax
movslq %eax, %rcx
leaq (%r12,%rcx), %rax
cmpq 0x10(%rsp), %r12
jbe 0x7914
cmpq %r9, %rax
jbe 0x7914
shrl $0x2, %r15d
movq %r12, %rax
jmp 0x7920
andl $0x7, %ebp
movl (%r12,%rcx), %r15d
movl %ebp, %ecx
shrl %cl, %r15d
leal -0x1(,%rbx,2), %esi
movl %esi, %ecx
subl %r14d, %ecx
leal -0x1(%rbx), %edx
andl %r15d, %edx
cmpl %ecx, %edx
jae 0x793c
leal -0x1(%r13), %ecx
jmp 0x7953
andl %esi, %r15d
cmpl %ebx, %r15d
movl $0x0, %edx
cmovll %edx, %ecx
subl %ecx, %r15d
movl %r13d, %ecx
movl %r15d, %edx
leal -0x1(%rdx), %esi
movl $0x1, %edi
subl %edx, %edi
testl %edx, %edx
cmovgl %esi, %edi
subl %edi, %r14d
movl %r10d, %edx
movq 0x8(%rsp), %rdi
movw %si, (%rdi,%rdx,2)
testl %esi, %esi
setne %dl
cmpl %ebx, %r14d
jge 0x7982
decl %r13d
sarl %ebx
jmp 0x7976
addl %ebp, %ecx
cmpq 0x10(%rsp), %rax
seta %sil
movl %ecx, %edi
sarl $0x3, %edi
movslq %edi, %r12
addq %rax, %r12
cmpq %r9, %r12
seta %dil
subl %r9d, %eax
movl %ecx, %r8d
andl $0x7, %r8d
leal (%rcx,%rax,8), %ebp
testb %dil, %sil
cmovel %r8d, %ebp
cmovneq %r9, %r12
movl (%r12), %r15d
movl %ebp, %ecx
shrl %cl, %r15d
cmpl $0x2, %r14d
movq 0x38(%rsp), %rsi
jl 0x79d8
leal 0x1(%r10), %eax
cmpl %r11d, %eax
jbe 0x7859
movq $-0x4, %rax
cmpl $0x1, %r14d
jne 0x7a12
cmpl $0x20, %ebp
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %rdx
jg 0x7a12
movl %r10d, (%rcx)
addl $0x7, %ebp
sarl $0x3, %ebp
movslq %ebp, %rax
addq %rax, %r12
subq %rdx, %r12
movq %r12, %rax
jmp 0x7a12
movq $-0x7, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq