// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x1191b
movl $0xb, -0x14(%rbp)
cmpl $0x5, -0x14(%rbp)
jae 0x1192e
movq $-0x1, -0x8(%rbp)
jmp 0x11b65
cmpl $0xc, -0x14(%rbp)
jbe 0x11941
movq $-0x5, -0x8(%rbp)
jmp 0x11b65
movl -0x14(%rbp), %eax
movl %eax, -0x74(%rbp)
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x11860
movl %eax, %ecx
movl -0x74(%rbp), %eax
cmpl %ecx, %eax
jae 0x11969
movq $-0x1, -0x8(%rbp)
jmp 0x11b65
movl $0x3e, %eax
subl -0x14(%rbp), %eax
movl %eax, %eax
movq %rax, -0x38(%rbp)
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x28(%rbp)
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rcx
subq $0x14, %rcx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0x48(%rbp)
movl -0x14(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x4c(%rbp)
movl $0x0, -0x54(%rbp)
movw $0x0, -0x56(%rbp)
movq -0x28(%rbp), %rax
movl -0x14(%rbp), %ecx
shrq %cl, %rax
movl %eax, -0x5c(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x2c(%rbp), %eax
ja 0x11afb
movq -0x20(%rbp), %rax
movl -0x50(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpq -0x28(%rbp), %rax
jne 0x119f8
movq $0x0, -0x8(%rbp)
jmp 0x11b65
movq -0x20(%rbp), %rax
movl -0x50(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jne 0x11a17
movq -0x10(%rbp), %rax
movl -0x50(%rbp), %ecx
movw $0x0, (%rax,%rcx,2)
jmp 0x11aed
movq -0x20(%rbp), %rax
movl -0x50(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x5c(%rbp), %eax
ja 0x11a41
movq -0x10(%rbp), %rax
movl -0x50(%rbp), %ecx
movw $0xffff, (%rax,%rcx,2)  # imm = 0xFFFF
movl -0x4c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x11aeb
movq -0x20(%rbp), %rax
movl -0x50(%rbp), %ecx
movl (%rax,%rcx,4), %eax
imulq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
shrq %cl, %rax
movw %ax, -0x5e(%rbp)
movswl -0x5e(%rbp), %eax
cmpl $0x8, %eax
jge 0x11ab6
movq -0x48(%rbp), %rax
movswq -0x5e(%rbp), %rdx
leaq 0x155bc(%rip), %rcx     # 0x27030
movl (%rcx,%rdx,4), %ecx
imulq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
movl -0x50(%rbp), %ecx
movl (%rax,%rcx,4), %eax
imulq -0x40(%rbp), %rax
movswq -0x5e(%rbp), %rdx
movq -0x38(%rbp), %rcx
shlq %cl, %rdx
movq %rdx, %rcx
subq %rcx, %rax
cmpq -0x68(%rbp), %rax
seta %al
andb $0x1, %al
movzbl %al, %ecx
movswl -0x5e(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x5e(%rbp)
movswl -0x5e(%rbp), %eax
movswl -0x56(%rbp), %ecx
cmpl %ecx, %eax
jle 0x11ad0
movw -0x5e(%rbp), %ax
movw %ax, -0x56(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0x54(%rbp)
movw -0x5e(%rbp), %dx
movq -0x10(%rbp), %rax
movl -0x50(%rbp), %ecx
movw %dx, (%rax,%rcx,2)
movswl -0x5e(%rbp), %ecx
movl -0x4c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x4c(%rbp)
jmp 0x11aed
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x119cf
xorl %eax, %eax
subl -0x4c(%rbp), %eax
movq -0x10(%rbp), %rcx
movl -0x54(%rbp), %edx
movswl (%rcx,%rdx,2), %ecx
sarl %ecx
cmpl %ecx, %eax
jl 0x11b47
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movl -0x2c(%rbp), %r8d
callq 0x11b80
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
callq 0x11f40
cmpl $0x0, %eax
je 0x11b45
movq -0x70(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x11b65
jmp 0x11b5e
movl -0x4c(%rbp), %eax
movswl %ax, %esi
movq -0x10(%rbp), %rax
movl -0x54(%rbp), %ecx
movswl (%rax,%rcx,2), %edx
addl %esi, %edx
movw %dx, (%rax,%rcx,2)
movl -0x14(%rbp), %eax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)