// File: /workspace/github_c_makefile_star2_O2/Cyan4973[P]FiniteStateEntropy/programs/../lib/fse_compress.c
// Function: FSE_writeNCount_generic
static size_t
FSE_writeNCount_generic (void* header, size_t headerBufferSize,
                   const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog,
                         unsigned writeIsSafe)
{
    BYTE* const ostart = (BYTE*) header;
    BYTE* out = ostart;
    BYTE* const oend = ostart + headerBufferSize;
    int nbBits;
    const int tableSize = 1 << tableLog;
    int remaining;
    int threshold;
    U32 bitStream = 0;
    int bitCount = 0;
    unsigned symbol = 0;
    unsigned const alphabetSize = maxSymbolValue + 1;
    int previousIs0 = 0;

    /* Table Size */
    bitStream += (tableLog-FSE_MIN_TABLELOG) << bitCount;
    bitCount  += 4;

    /* Init */
    remaining = tableSize+1;   /* +1 for extra accuracy */
    threshold = tableSize;
    nbBits = tableLog+1;

    while ((symbol < alphabetSize) && (remaining>1)) {  /* stops at 1 */
        if (previousIs0) {
            unsigned start = symbol;
            while ((symbol < alphabetSize) && !normalizedCounter[symbol]) symbol++;
            if (symbol == alphabetSize) break;   /* incorrect distribution */
            while (symbol >= start+24) {
                start+=24;
                bitStream += 0xFFFFU << bitCount;
                if ((!writeIsSafe) && (out > oend-2))
                    return ERROR(dstSize_tooSmall);   /* Buffer overflow */
                out[0] = (BYTE) bitStream;
                out[1] = (BYTE)(bitStream>>8);
                out+=2;
                bitStream>>=16;
            }
            while (symbol >= start+3) {
                start+=3;
                bitStream += 3 << bitCount;
                bitCount += 2;
            }
            bitStream += (symbol-start) << bitCount;
            bitCount += 2;
            if (bitCount>16) {
                if ((!writeIsSafe) && (out > oend - 2))
                    return ERROR(dstSize_tooSmall);   /* Buffer overflow */
                out[0] = (BYTE)bitStream;
                out[1] = (BYTE)(bitStream>>8);
                out += 2;
                bitStream >>= 16;
                bitCount -= 16;
        }   }
        {   int count = normalizedCounter[symbol++];
            int const max = (2*threshold-1) - remaining;
            remaining -= count < 0 ? -count : count;
            count++;   /* +1 for extra accuracy */
            if (count>=threshold)
                count += max;   /* [0..max[ [max..threshold[ (...) [threshold+max 2*threshold[ */
            bitStream += count << bitCount;
            bitCount  += nbBits;
            bitCount  -= (count<max);
            previousIs0  = (count==1);
            if (remaining<1) return ERROR(GENERIC);
            while (remaining<threshold) { nbBits--; threshold>>=1; }
        }
        if (bitCount>16) {
            if ((!writeIsSafe) && (out > oend - 2))
                return ERROR(dstSize_tooSmall);   /* Buffer overflow */
            out[0] = (BYTE)bitStream;
            out[1] = (BYTE)(bitStream>>8);
            out += 2;
            bitStream >>= 16;
            bitCount -= 16;
    }   }

    if (remaining != 1)
        return ERROR(GENERIC);  /* incorrect normalized distribution */
    assert(symbol <= alphabetSize);

    /* flush remaining bitStream */
    if ((!writeIsSafe) && (out > oend - 2))
        return ERROR(dstSize_tooSmall);   /* Buffer overflow */
    out[0] = (BYTE)bitStream;
    out[1] = (BYTE)(bitStream>>8);
    out+= (bitCount+7) /8;

    return (out-ostart);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %ecx, %r10d
movl $0x1, %ebx
movl %r8d, %ecx
shll %cl, %ebx
incl %r10d
leal -0x5(%r8), %r14d
leal 0x1(%rbx), %ebp
incl %r8d
addq %rdi, %rsi
addq $-0x2, %rsi
movl $0x4, %ecx
xorl %r12d, %r12d
movq %rdi, -0x8(%rsp)
movq %rdi, %r11
xorl %edi, %edi
movq %rsi, -0x10(%rsp)
cmpl %r10d, %r12d
jae 0x7a7e
cmpl $0x2, %ebp
jl 0x7a7e
testb $0x1, %dil
je 0x7940
movl %r12d, %r15d
xorl %r13d, %r13d
cmpq %r10, %r15
je 0x7a7e
cmpw $0x0, (%rdx,%r15,2)
jne 0x79ea
incq %r15
incl %r13d
jmp 0x7923
movl %r14d, %r13d
movl %r12d, %r15d
movl %r15d, %eax
movswl (%rdx,%rax,2), %eax
movl %ebp, %esi
notl %esi
movl %eax, %r12d
negl %r12d
cmovsl %eax, %r12d
leal (%rsi,%rbx,2), %esi
leal 0x1(%rax), %edi
cmpl %ebx, %edi
movl %esi, %edi
movl $0x0, %r14d
cmovll %r14d, %edi
addl %eax, %edi
incl %edi
movl %edi, %r14d
shll %cl, %r14d
xorl %eax, %eax
cmpl %esi, %edi
setl %sil
cmpl $0x1, %edi
sete %dil
subl %r12d, %ebp
jle 0x7ad3
movl %r15d, %r12d
incl %r12d
addl %r13d, %r14d
addl %r8d, %ecx
movb %sil, %al
subl %eax, %ecx
movq -0x10(%rsp), %rsi
cmpl %ebx, %ebp
jge 0x79b2
decl %r8d
sarl %ebx
jmp 0x79a7
cmpl $0x11, %ecx
jl 0x7905
testl %r9d, %r9d
sete %al
cmpq %rsi, %r11
seta %sil
testb %sil, %al
jne 0x7abe
movw %r14w, (%r11)
addq $0x2, %r11
shrl $0x10, %r14d
addl $-0x10, %ecx
movq -0x10(%rsp), %rsi
jmp 0x7905
cmpl %r10d, %r15d
je 0x7a7e
movl $0xffff, %eax           # imm = 0xFFFF
shll %cl, %eax
leal 0x3(%r12), %edi
addl $0x18, %r12d
cmpl %r15d, %r12d
ja 0x7a2e
testl %r9d, %r9d
jne 0x7a16
cmpq %rsi, %r11
ja 0x7abe
addl %eax, %r14d
movw %r14w, (%r11)
addq $0x2, %r11
shrl $0x10, %r14d
addl $-0x18, %r13d
addl $0x18, %edi
jmp 0x79ff
movl $0x3, %r12d
cmpl %r15d, %edi
ja 0x7a4d
movl %r12d, %eax
shll %cl, %eax
addl %eax, %r14d
addl $0x2, %ecx
addl $-0x3, %r13d
addl $0x3, %edi
jmp 0x7a34
shll %cl, %r13d
addl %r14d, %r13d
cmpl $0xf, %ecx
jl 0x7a76
testl %r9d, %r9d
jne 0x7a62
cmpq %rsi, %r11
ja 0x7abe
movw %r13w, (%r11)
addq $0x2, %r11
shrl $0x10, %r13d
addl $-0xe, %ecx
jmp 0x7946
addl $0x2, %ecx
jmp 0x7946
cmpl $0x1, %ebp
movabsq $-0x1, %rax
jne 0x7ac8
testl %r9d, %r9d
jne 0x7aa1
movabsq $-0x2, %rax
cmpq %rsi, %r11
ja 0x7ac8
movw %r14w, (%r11)
addl $0x7, %ecx
movl $0x8, %esi
movl %ecx, %eax
cltd
idivl %esi
cltq
addq %r11, %rax
subq -0x8(%rsp), %rax
jmp 0x7ac8
movabsq $-0x2, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movabsq $-0x1, %rax
jmp 0x7ac8