// File: /workspace/github_c_makefile_star2_O0/Cyan4973[P]FiniteStateEntropy/programs/../lib/fse_compress.c
// Function: FSE_normalizeM2
static size_t FSE_normalizeM2(short* norm, U32 tableLog, const unsigned* count, size_t total, U32 maxSymbolValue)
{
    short const NOT_YET_ASSIGNED = -2;
    U32 s;
    U32 distributed = 0;
    U32 ToDistribute;

    /* Init */
    U32 const lowThreshold = (U32)(total >> tableLog);
    U32 lowOne = (U32)((total * 3) >> (tableLog + 1));

    for (s=0; s<=maxSymbolValue; s++) {
        if (count[s] == 0) {
            norm[s]=0;
            continue;
        }
        if (count[s] <= lowThreshold) {
            norm[s] = -1;
            distributed++;
            total -= count[s];
            continue;
        }
        if (count[s] <= lowOne) {
            norm[s] = 1;
            distributed++;
            total -= count[s];
            continue;
        }

        norm[s]=NOT_YET_ASSIGNED;
    }
    ToDistribute = (1 << tableLog) - distributed;

    if (ToDistribute == 0)
        return 0;

    if ((total / ToDistribute) > lowOne) {
        /* risk of rounding to zero */
        lowOne = (U32)((total * 3) / (ToDistribute * 2));
        for (s=0; s<=maxSymbolValue; s++) {
            if ((norm[s] == NOT_YET_ASSIGNED) && (count[s] <= lowOne)) {
                norm[s] = 1;
                distributed++;
                total -= count[s];
                continue;
        }   }
        ToDistribute = (1 << tableLog) - distributed;
    }

    if (distributed == maxSymbolValue+1) {
        /* all values are pretty poor;
           probably incompressible data (should have already been detected);
           find max, then give all remaining points to max */
        U32 maxV = 0, maxC = 0;
        for (s=0; s<=maxSymbolValue; s++)
            if (count[s] > maxC) { maxV=s; maxC=count[s]; }
        norm[maxV] += (short)ToDistribute;
        return 0;
    }

    if (total == 0) {
        /* all of the symbols were low enough for the lowOne or lowThreshold */
        for (s=0; ToDistribute > 0; s = (s+1)%(maxSymbolValue+1))
            if (norm[s] > 0) { ToDistribute--; norm[s]++; }
        return 0;
    }

    {   U64 const vStepLog = 62 - tableLog;
        U64 const mid = (1ULL << (vStepLog-1)) - 1;
        U64 const rStep = ((((U64)1<<vStepLog) * ToDistribute) + mid) / total;   /* scale on remaining */
        U64 tmpTotal = mid;
        for (s=0; s<=maxSymbolValue; s++) {
            if (norm[s]==NOT_YET_ASSIGNED) {
                U64 const end = tmpTotal + (count[s] * rStep);
                U32 const sStart = (U32)(tmpTotal >> vStepLog);
                U32 const sEnd = (U32)(end >> vStepLog);
                U32 const weight = sEnd - sStart;
                if (weight < 1)
                    return ERROR(GENERIC);
                norm[s] = (short)weight;
                tmpTotal = end;
    }   }   }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movw $0xfffe, -0x2e(%rbp)    # imm = 0xFFFE
movl $0x0, -0x38(%rbp)
movq -0x28(%rbp), %rax
movl -0x14(%rbp), %ecx
shrq %cl, %rax
movl %eax, -0x40(%rbp)
imulq $0x3, -0x28(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
shrq %cl, %rax
movl %eax, -0x44(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x2c(%rbp), %eax
ja 0x11c8e
movq -0x20(%rbp), %rax
movl -0x34(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jne 0x11bf7
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
movw $0x0, (%rax,%rcx,2)
jmp 0x11c80
movq -0x20(%rbp), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x40(%rbp), %eax
ja 0x11c35
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
movw $0xffff, (%rax,%rcx,2)  # imm = 0xFFFF
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x28(%rbp)
jmp 0x11c80
movq -0x20(%rbp), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x44(%rbp), %eax
ja 0x11c73
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
movw $0x1, (%rax,%rcx,2)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x28(%rbp)
jmp 0x11c80
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
movw $0xfffe, (%rax,%rcx,2)  # imm = 0xFFFE
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x11bcc
movl -0x14(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
subl -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x11cb1
movq $0x0, -0x8(%rbp)
jmp 0x11f35
movq -0x28(%rbp), %rax
movl -0x3c(%rbp), %ecx
xorl %edx, %edx
divq %rcx
movl -0x44(%rbp), %ecx
cmpq %rcx, %rax
jbe 0x11d57
imulq $0x3, -0x28(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
movl %ecx, %ecx
xorl %edx, %edx
divq %rcx
movl %eax, -0x44(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x2c(%rbp), %eax
ja 0x11d47
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
movswl (%rax,%rcx,2), %eax
cmpl $-0x2, %eax
jne 0x11d3a
movq -0x20(%rbp), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x44(%rbp), %eax
ja 0x11d3a
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
movw $0x1, (%rax,%rcx,2)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x28(%rbp)
jmp 0x11d3c
jmp 0x11d3c
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x11ce4
movl -0x14(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
subl -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x11dd4
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x2c(%rbp), %eax
ja 0x11db0
movq -0x20(%rbp), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x4c(%rbp), %eax
jbe 0x11da3
movl -0x34(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
jmp 0x11da5
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x11d79
movl -0x3c(%rbp), %eax
movswl %ax, %esi
movq -0x10(%rbp), %rax
movl -0x48(%rbp), %ecx
movswl (%rax,%rcx,2), %edx
addl %esi, %edx
movw %dx, (%rax,%rcx,2)
movq $0x0, -0x8(%rbp)
jmp 0x11f35
cmpq $0x0, -0x28(%rbp)
jne 0x11e38
movl $0x0, -0x34(%rbp)
cmpl $0x0, -0x3c(%rbp)
jbe 0x11e2b
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
movswl (%rax,%rcx,2), %eax
cmpl $0x0, %eax
jle 0x11e14
movl -0x3c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
movw (%rax,%rcx,2), %dx
addw $0x1, %dx
movw %dx, (%rax,%rcx,2)
jmp 0x11e16
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
xorl %edx, %edx
divl %ecx
movl %edx, -0x34(%rbp)
jmp 0x11de2
movq $0x0, -0x8(%rbp)
jmp 0x11f35
movl $0x3e, %eax
subl -0x14(%rbp), %eax
movl %eax, %eax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rcx
subq $0x1, %rcx
movl $0x1, %eax
shlq %cl, %rax
subq $0x1, %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rcx
movl $0x1, %eax
shlq %cl, %rax
movl -0x3c(%rbp), %ecx
imulq %rcx, %rax
addq -0x60(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x28(%rbp)
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x2c(%rbp), %eax
ja 0x11f2d
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
movswl (%rax,%rcx,2), %eax
cmpl $-0x2, %eax
jne 0x11f1d
movq -0x70(%rbp), %rax
movq -0x20(%rbp), %rcx
movl -0x34(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
imulq -0x68(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rax
movq -0x58(%rbp), %rcx
shrq %cl, %rax
movl %eax, -0x7c(%rbp)
movq -0x78(%rbp), %rax
movq -0x58(%rbp), %rcx
shrq %cl, %rax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
subl -0x7c(%rbp), %eax
movl %eax, -0x84(%rbp)
cmpl $0x1, -0x84(%rbp)
jae 0x11f01
movq $-0x1, -0x8(%rbp)
jmp 0x11f35
movl -0x84(%rbp), %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
movw %dx, (%rax,%rcx,2)
movq -0x78(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x11f1f
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x11e90
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x8, %rsp
popq %rbp
retq
nop