// File: /workspace/github_c_makefile_star2_O2/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
movq %r8, -0x38(%rsp)
testl %esi, %esi
movl $0xb, %ebx
cmovnel %esi, %ebx
movabsq $-0x1, %rax
cmpl $0x5, %ebx
jb 0x7e3c
cmpl $0xc, %ebx
jbe 0x7b8f
movabsq $-0x5, %rax
jmp 0x7e3c
movq %rcx, %r11
movq %rdx, %r9
bsrl %r11d, %edx
xorl $-0x20, %edx
addl $0x21, %edx
bsrl -0x38(%rsp), %ecx
xorl $-0x20, %ecx
addl $0x22, %ecx
cmpl %ecx, %edx
cmovbl %edx, %ecx
cmpl %ecx, %ebx
jb 0x7e3c
movq %rdi, %r12
movl $0x3e, %r8d
subl %ebx, %r8d
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
xorl %edx, %edx
divq %r11
movl $0x1, %edx
movl %ebx, %ecx
shll %cl, %edx
movq %r11, %r15
shrq %cl, %r15
leaq -0x14(%r8), %rcx
movq %rcx, -0x18(%rsp)
movl %ebx, %ecx
movq %rcx, -0x30(%rsp)
xorl %r13d, %r13d
xorl %edi, %edi
xorl %ebp, %ebp
movl %edx, -0x24(%rsp)
movl %edx, %r14d
movq %r12, -0x8(%rsp)
movq %r9, -0x10(%rsp)
movq %rax, -0x20(%rsp)
cmpl -0x38(%rsp), %r13d
ja 0x7cb8
movl %r13d, %edx
movl (%r9,%rdx,4), %r10d
cmpq %r11, %r10
je 0x7cd5
testq %r10, %r10
je 0x7c9f
cmpl %r15d, %r10d
jbe 0x7ca7
imulq %rax, %r10
movq %r10, %rsi
movl %r8d, %ecx
shrq %cl, %rsi
testw $0xfff8, %si            # imm = 0xFFF8
jne 0x7c88
movq %r8, %r12
movzwl %si, %r9d
movl %r9d, %ecx
leaq 0x9348(%rip), %r8       # 0x10fa0
movl (%r8,%rcx,4), %r8d
movq -0x18(%rsp), %rcx
shlq %cl, %r8
movl %r12d, %ecx
shlq %cl, %r9
subq %r9, %r10
movq -0x10(%rsp), %r9
movq %r12, %rcx
movq -0x8(%rsp), %r12
cmpq %r10, %r8
movq -0x20(%rsp), %rax
movq %rcx, %r8
adcl $0x0, %esi
movzwl %si, %ecx
cmpw %bp, %cx
cmoval %r13d, %edi
cmoval %ecx, %ebp
movw %cx, (%r12,%rdx,2)
subl %ecx, %r14d
jmp 0x7cb0
andw $0x0, (%r12,%rdx,2)
jmp 0x7cb0
orw $-0x1, (%r12,%rdx,2)
decl %r14d
incl %r13d
jmp 0x7c0e
movl %r14d, %edx
negl %edx
movl %edi, %ecx
movswl (%r12,%rcx,2), %eax
movl %eax, %esi
sarl %esi
cmpl %edx, %esi
jle 0x7cdc
leaq (%r12,%rcx,2), %rcx
jmp 0x7e31
xorl %eax, %eax
jmp 0x7e3c
leaq (%r11,%r11,2), %rdi
incb %bl
movl %ebx, %ecx
shrq %cl, %rdi
movq -0x38(%rsp), %rax
leal 0x1(%rax), %ecx
xorl %eax, %eax
xorl %ebx, %ebx
movl -0x24(%rsp), %ebp
cmpq %rax, %rcx
je 0x7d38
movl (%r9,%rax,4), %edx
testq %rdx, %rdx
je 0x7d17
cmpl %r15d, %edx
jbe 0x7d1f
cmpl %edi, %edx
jbe 0x7d27
movw $0xfffe, (%r12,%rax,2)  # imm = 0xFFFE
jmp 0x7d33
andw $0x0, (%r12,%rax,2)
jmp 0x7d33
orw $-0x1, (%r12,%rax,2)
jmp 0x7d2e
movw $0x1, (%r12,%rax,2)
incl %ebx
subq %rdx, %r11
incq %rax
jmp 0x7cf7
movl %ebp, %r14d
subl %ebx, %r14d
je 0x7e37
movl %r14d, %esi
movq %r11, %rax
xorl %edx, %edx
divq %rsi
movl %edi, %edx
cmpq %rdx, %rax
jbe 0x7d90
leaq (%r11,%r11,2), %rax
addl %r14d, %r14d
xorl %r15d, %r15d
xorl %edx, %edx
divq %r14
cmpq %r15, %rcx
je 0x7d8b
cmpw $-0x2, (%r12,%r15,2)
jne 0x7d86
movl (%r9,%r15,4), %edx
cmpl %eax, %edx
ja 0x7d86
movw $0x1, (%r12,%r15,2)
incl %ebx
subq %rdx, %r11
incq %r15
jmp 0x7d65
subl %ebx, %ebp
movl %ebp, %r14d
cmpl %ecx, %ebx
jne 0x7db6
xorl %eax, %eax
xorl %edx, %edx
xorl %esi, %esi
cmpq %rax, %rcx
je 0x7e28
movl (%r9,%rax,4), %r8d
cmpl %esi, %r8d
cmoval %eax, %edx
cmoval %r8d, %esi
incq %rax
jmp 0x7d9a
testq %r11, %r11
je 0x7e47
leal -0x1(%r8), %ecx
movabsq $-0x1, %rbx
movq %rbx, %r15
shlq %cl, %r15
movl %r14d, %eax
movl %r8d, %ecx
shlq %cl, %rax
notq %r15
addq %r15, %rax
xorl %r14d, %r14d
xorl %edx, %edx
divq %r11
cmpl -0x38(%rsp), %r14d
ja 0x7e37
movl %r14d, %edx
cmpw $-0x2, (%r12,%rdx,2)
jne 0x7e23
movl (%r9,%rdx,4), %esi
imulq %rax, %rsi
addq %r15, %rsi
movl %r8d, %ecx
shrq %cl, %r15
movq %rsi, %r10
shrq %cl, %r10
cmpq %r15, %r10
je 0x7e72
subl %r15d, %r10d
movw %r10w, (%r12,%rdx,2)
movq %rsi, %r15
incl %r14d
jmp 0x7dea
movl %edx, %eax
leaq (%r12,%rax,2), %rcx
movzwl (%rcx), %eax
addl %r14d, %eax
movw %ax, (%rcx)
movq -0x30(%rsp), %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
xorl %ecx, %ecx
testl %r14d, %r14d
je 0x7e37
movl %ecx, %edx
movzwl (%r12,%rdx,2), %esi
testw %si, %si
jle 0x7e66
decl %r14d
incl %esi
movw %si, (%r12,%rdx,2)
cmpl -0x38(%rsp), %ecx
leal 0x1(%rcx), %ecx
cmovel %eax, %ecx
jmp 0x7e4b
movq %rbx, %rax
jmp 0x7e3c