// File: /workspace/github_c_makefile_star2_O3/Cyan4973[P]FiniteStateEntropy/programs/../lib/fse_compress.c
// Function: FSE_normalizeCount
size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog,
                           const unsigned* count, size_t total,
                           unsigned maxSymbolValue)
{
    /* Sanity checks */
    if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
    if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC);   /* Unsupported size */
    if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);   /* Unsupported size */
    if (tableLog < FSE_minTableLog(total, maxSymbolValue)) return ERROR(GENERIC);   /* Too small tableLog, compression potentially impossible */

    {   static U32 const rtbTable[] = {     0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 };
        U64 const scale = 62 - tableLog;
        U64 const step = ((U64)1<<62) / total;   /* <== here, one division ! */
        U64 const vStep = 1ULL<<(scale-20);
        int stillToDistribute = 1<<tableLog;
        unsigned s;
        unsigned largest=0;
        short largestP=0;
        U32 lowThreshold = (U32)(total >> tableLog);

        for (s=0; s<=maxSymbolValue; s++) {
            if (count[s] == total) return 0;   /* rle special case */
            if (count[s] == 0) { normalizedCounter[s]=0; continue; }
            if (count[s] <= lowThreshold) {
                normalizedCounter[s] = -1;
                stillToDistribute--;
            } else {
                short proba = (short)((count[s]*step) >> scale);
                if (proba<8) {
                    U64 restToBeat = vStep * rtbTable[proba];
                    proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat;
                }
                if (proba > largestP) { largestP=proba; largest=s; }
                normalizedCounter[s] = proba;
                stillToDistribute -= proba;
        }   }
        if (-stillToDistribute >= (normalizedCounter[largest] >> 1)) {
            /* corner case, need another normalization method */
            size_t const errorCode = FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue);
            if (FSE_isError(errorCode)) return errorCode;
        }
        else normalizedCounter[largest] += (short)stillToDistribute;
    }

#if 0
    {   /* Print Table (debug) */
        U32 s;
        U32 nTotal = 0;
        for (s=0; s<=maxSymbolValue; s++)
            RAWLOG(2, "%3i: %4i \n", s, normalizedCounter[s]);
        for (s=0; s<=maxSymbolValue; s++)
            nTotal += abs(normalizedCounter[s]);
        if (nTotal != (1U<<tableLog))
            RAWLOG(2, "Warning !!! Total == %u != %u !!!", nTotal, 1U<<tableLog);
        getchar();
    }
#endif

    return tableLog;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
testl %esi, %esi
movl $0xb, %ebx
cmovnel %esi, %ebx
movq $-0x1, %rax
cmpl $0x5, %ebx
jb 0x9293
movq $-0x5, %rax
cmpl $0xc, %ebx
ja 0x9293
movl %r8d, %r11d
movq %rcx, %r8
bsrl %r8d, %eax
xorl $-0x20, %eax
addl $0x21, %eax
bsrl %r11d, %ecx
xorl $-0x20, %ecx
addl $0x22, %ecx
cmpl %ecx, %eax
cmovbl %eax, %ecx
movq $-0x1, %rax
cmpl %ecx, %ebx
jb 0x9293
movq %rdx, %r9
movl $0x3e, %r15d
subl %ebx, %r15d
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
xorl %edx, %edx
divq %r8
movl $0x1, %edx
movl %ebx, %ecx
shll %cl, %edx
movq %r8, %rsi
shrq %cl, %rsi
leaq -0x14(%r15), %rcx
movq %rcx, -0x8(%rsp)
movl %ebx, %ecx
movq %rcx, -0x28(%rsp)
movl %edx, -0x1c(%rsp)
movl %edx, %r14d
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %rsi, -0x18(%rsp)
movq %r11, -0x10(%rsp)
movl %ebp, %ebp
movl (%r9,%rbp,4), %edx
cmpq %r8, %rdx
je 0x919f
testq %rdx, %rdx
je 0x9166
cmpl %esi, %edx
jbe 0x916e
imulq %rax, %rdx
movq %rdx, %r10
movl %r15d, %ecx
shrq %cl, %r10
testl $0xfff8, %r10d          # imm = 0xFFF8
jne 0x914d
movzwl %r10w, %esi
movl %esi, %ecx
movq %r9, %r11
leaq 0xbf2c(%rip), %r9       # 0x15050
movl (%r9,%rcx,4), %r9d
movq -0x8(%rsp), %rcx
shlq %cl, %r9
movl %r15d, %ecx
shlq %cl, %rsi
subq %rsi, %rdx
movq -0x18(%rsp), %rsi
cmpq %rdx, %r9
movq %r11, %r9
movq -0x10(%rsp), %r11
adcl $0x0, %r10d
movzwl %r10w, %ecx
cmpw %r13w, %cx
cmoval %ebp, %r12d
cmoval %ecx, %r13d
movw %cx, (%rdi,%rbp,2)
subl %ecx, %r14d
jmp 0x9177
movw $0x0, (%rdi,%rbp,2)
jmp 0x9177
movw $0xffff, (%rdi,%rbp,2)  # imm = 0xFFFF
decl %r14d
incl %ebp
cmpl %r11d, %ebp
jbe 0x90e6
movl %r14d, %edx
negl %edx
movl %r12d, %ecx
movswl (%rdi,%rcx,2), %eax
movl %eax, %esi
sarl %esi
cmpl %edx, %esi
jle 0x91a6
leaq (%rdi,%rcx,2), %rcx
jmp 0x9288
xorl %eax, %eax
jmp 0x9293
leaq (%r8,%r8,2), %r12
incb %bl
movl %ebx, %ecx
shrq %cl, %r12
leal 0x1(%r11), %ecx
xorl %eax, %eax
xorl %ebx, %ebx
movl -0x1c(%rsp), %r10d
movq -0x18(%rsp), %rsi
movl (%r9,%rax,4), %edx
testq %rdx, %rdx
je 0x91dd
cmpl %esi, %edx
jbe 0x91e5
cmpl %r12d, %edx
jbe 0x91ed
movw $0xfffe, (%rdi,%rax,2)  # imm = 0xFFFE
jmp 0x91f8
movw $0x0, (%rdi,%rax,2)
jmp 0x91f8
movw $0xffff, (%rdi,%rax,2)  # imm = 0xFFFF
jmp 0x91f3
movw $0x1, (%rdi,%rax,2)
incl %ebx
subq %rdx, %r8
incq %rax
cmpq %rax, %rcx
jne 0x91c3
movl %r10d, %r14d
subl %ebx, %r14d
je 0x928e
movl %r14d, %esi
movq %r8, %rax
xorl %edx, %edx
divq %rsi
movl %r12d, %edx
cmpq %rdx, %rax
jbe 0x925e
movq %r15, %rsi
leaq (%r8,%r8,2), %rax
addl %r14d, %r14d
xorl %r15d, %r15d
xorl %edx, %edx
divq %r14
cmpw $-0x2, (%rdi,%r15,2)
jne 0x924d
movl (%r9,%r15,4), %edx
cmpl %eax, %edx
ja 0x924d
movw $0x1, (%rdi,%r15,2)
incl %ebx
subq %rdx, %r8
incq %r15
cmpq %r15, %rcx
jne 0x9231
subl %ebx, %r10d
movl %r10d, %r14d
movq %rsi, %r15
cmpl %ecx, %ebx
jne 0x929e
xorl %eax, %eax
xorl %r8d, %r8d
xorl %edx, %edx
movl (%r9,%rax,4), %esi
cmpl %r8d, %esi
cmoval %eax, %edx
cmoval %esi, %r8d
incq %rax
cmpq %rax, %rcx
jne 0x9269
movl %edx, %eax
leaq (%rdi,%rax,2), %rcx
movzwl (%rcx), %eax
addl %r14d, %eax
movw %ax, (%rcx)
movq -0x28(%rsp), %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r8, %r8
je 0x9304
leal -0x1(%r15), %ecx
movq $-0x1, %rbx
shlq %cl, %rbx
movl %r14d, %eax
movl %r15d, %ecx
shlq %cl, %rax
notq %rbx
addq %rbx, %rax
xorl %r14d, %r14d
xorl %edx, %edx
divq %r8
movl %r14d, %r14d
cmpw $-0x2, (%rdi,%r14,2)
jne 0x92fa
movl (%r9,%r14,4), %edx
imulq %rax, %rdx
addq %rbx, %rdx
movl %r15d, %ecx
shrq %cl, %rbx
movq %rdx, %r10
shrq %cl, %r10
cmpq %rbx, %r10
je 0x9334
subl %ebx, %r10d
movw %r10w, (%rdi,%r14,2)
movq %rdx, %rbx
incl %r14d
cmpl %r11d, %r14d
jbe 0x92c8
jmp 0x928e
testl %r14d, %r14d
je 0x928e
xorl %eax, %eax
xorl %edx, %edx
movl %edx, %ecx
movzwl (%rdi,%rcx,2), %edx
testw %dx, %dx
jle 0x9321
decl %r14d
incl %edx
movw %dx, (%rdi,%rcx,2)
leal 0x1(%rcx), %edx
cmpl %r11d, %ecx
cmovel %eax, %edx
testl %r14d, %r14d
jne 0x930d
jmp 0x928e
movq $-0x1, %rax
jmp 0x9293