// File: /workspace/github_c_makefile_star2_O1/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 0x8da6
movq $-0x5, %rax
cmpl $0xc, %ebx
ja 0x8da6
movq %rcx, %r11
bsrl %r11d, %eax
xorl $-0x20, %eax
addl $0x21, %eax
bsrl %r8d, %ecx
xorl $-0x20, %ecx
addl $0x22, %ecx
cmpl %ecx, %eax
cmovbl %eax, %ecx
movq $-0x1, %rax
cmpl %ecx, %ebx
jb 0x8da6
movq %rdx, %r9
movl $0x3e, %esi
subl %ebx, %esi
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
xorl %edx, %edx
divq %r11
movl $0x1, %r15d
movl %ebx, %ecx
shll %cl, %r15d
movq %r11, %r14
shrq %cl, %r14
movq %rsi, -0x20(%rsp)
leaq -0x14(%rsi), %rcx
movq %rcx, -0x8(%rsp)
movl %ebx, %ecx
movq %rcx, -0x10(%rsp)
movl %r15d, -0x14(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %rdi, -0x28(%rsp)
movl %ebp, %ebp
movl (%r9,%rbp,4), %edx
cmpq %r11, %rdx
je 0x8be3
testq %rdx, %rdx
je 0x8ba2
cmpl %r14d, %edx
jbe 0x8baa
imulq %rax, %rdx
movq %rdx, %r10
movq -0x20(%rsp), %rcx
shrq %cl, %r10
testl $0xfff8, %r10d          # imm = 0xFFF8
jne 0x8b89
movzwl %r10w, %esi
movl %esi, %ecx
movq %r14, %rdi
movq %r9, %r14
leaq 0xb3d0(%rip), %r9       # 0x13f30
movl (%r9,%rcx,4), %r9d
movq -0x8(%rsp), %rcx
shlq %cl, %r9
movq -0x20(%rsp), %rcx
shlq %cl, %rsi
subq %rsi, %rdx
cmpq %rdx, %r9
movq %r14, %r9
movq %rdi, %r14
movq -0x28(%rsp), %rdi
adcl $0x0, %r10d
movzwl %r10w, %ecx
cmpw %r13w, %cx
cmoval %ebp, %r12d
cmoval %ecx, %r13d
movw %cx, (%rdi,%rbp,2)
subl %ecx, %r15d
jmp 0x8bb3
movw $0x0, (%rdi,%rbp,2)
jmp 0x8bb3
movw $0xffff, (%rdi,%rbp,2)  # imm = 0xFFFF
decl %r15d
incl %ebp
cmpl %r8d, %ebp
jbe 0x8b1c
movl %r15d, %edx
negl %edx
movl %r12d, %eax
movswl (%rdi,%rax,2), %ecx
movl %ecx, %esi
sarl %esi
cmpl %edx, %esi
jle 0x8bea
addl %r15d, %ecx
movw %cx, (%rdi,%rax,2)
movq -0x10(%rsp), %rax
jmp 0x8da6
xorl %eax, %eax
jmp 0x8da6
leaq (%r11,%r11,2), %r12
incb %bl
movl %ebx, %ecx
shrq %cl, %r12
leal 0x1(%r8), %ecx
cmpl $0x1, %ecx
movl %r8d, %ebx
adcl $0x1, %ebx
xorl %eax, %eax
xorl %r15d, %r15d
movl (%r9,%rax,4), %edx
testq %rdx, %rdx
je 0x8c22
cmpl %r14d, %edx
jbe 0x8c2a
cmpl %r12d, %edx
jbe 0x8c32
movw $0xfffe, (%rdi,%rax,2)  # imm = 0xFFFE
jmp 0x8c3e
movw $0x0, (%rdi,%rax,2)
jmp 0x8c3e
movw $0xffff, (%rdi,%rax,2)  # imm = 0xFFFF
jmp 0x8c38
movw $0x1, (%rdi,%rax,2)
incl %r15d
subq %rdx, %r11
incq %rax
cmpq %rax, %rbx
jne 0x8c07
movl -0x14(%rsp), %edi
movl %edi, %r14d
subl %r15d, %r14d
je 0x8ccf
movl %r14d, %esi
movq %r11, %rax
xorl %edx, %edx
divq %rsi
movl %r12d, %edx
cmpq %rdx, %rax
jbe 0x8ca4
leaq (%r11,%r11,2), %rax
addl %r14d, %r14d
xorl %r12d, %r12d
xorl %edx, %edx
divq %r14
movq -0x28(%rsp), %rsi
cmpw $-0x2, (%rsi,%r12,2)
jne 0x8c96
movl (%r9,%r12,4), %edx
cmpl %eax, %edx
ja 0x8c96
movw $0x1, (%rsi,%r12,2)
incl %r15d
subq %rdx, %r11
incq %r12
cmpq %r12, %rbx
jne 0x8c79
subl %r15d, %edi
movl %edi, %r14d
cmpl %ecx, %r15d
jne 0x8cd7
xorl %eax, %eax
xorl %edx, %edx
xorl %ecx, %ecx
movl (%r9,%rax,4), %esi
cmpl %edx, %esi
cmoval %eax, %ecx
cmoval %esi, %edx
incq %rax
cmpq %rax, %rbx
jne 0x8caf
movl %ecx, %eax
movq -0x28(%rsp), %rcx
addw %r14w, (%rcx,%rax,2)
xorl %r8d, %r8d
jmp 0x8d99
testq %r11, %r11
je 0x8d6b
movq -0x20(%rsp), %rdx
leal -0x1(%rdx), %ecx
movq $-0x1, %rbx
shlq %cl, %rbx
movl %r14d, %eax
movl %edx, %ecx
shlq %cl, %rax
notq %rbx
addq %rbx, %rax
xorl %r14d, %r14d
xorl %edx, %edx
divq %r11
movb $0x1, %dl
movq -0x28(%rsp), %rsi
movl %r14d, %r14d
movq -0x28(%rsp), %rcx
cmpw $-0x2, (%rcx,%r14,2)
jne 0x8d53
movl (%r9,%r14,4), %r10d
movq %rbx, %r11
movq -0x20(%rsp), %rcx
shrq %cl, %r11
imulq %rax, %r10
addq %rbx, %r10
movq %r10, %r15
shrq %cl, %r15
cmpq %r11, %r15
je 0x8d4e
movl %r15d, %ecx
subl %r11d, %ecx
movw %cx, (%rsi,%r14,2)
movq %r10, %rbx
cmpq %r11, %r15
je 0x8d5e
incl %r14d
cmpl %r8d, %r14d
setbe %dl
jbe 0x8d0f
movzbl %dl, %r8d
andl $0x1, %r8d
negq %r8
jmp 0x8d99
xorl %r8d, %r8d
testl %r14d, %r14d
je 0x8d99
xorl %edx, %edx
movq -0x28(%rsp), %rsi
movl %edx, %eax
movzwl (%rsi,%rax,2), %edx
testw %dx, %dx
jle 0x8d8e
decl %r14d
incl %edx
movw %dx, (%rsi,%rax,2)
incl %eax
xorl %edx, %edx
divl %ecx
testl %r14d, %r14d
jne 0x8d7a
cmpq $-0x8, %r8
movq -0x10(%rsp), %rax
cmovaeq %r8, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq