// File: /workspace/github_c_makefile_star2_O1/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
movq %rdx, -0x18(%rsp)
addq %rdi, %rsi
leal -0x5(%r8), %ebp
movl $0x0, -0x24(%rsp)
incl %r10d
movq %rdi, -0x10(%rsp)
movq %rsi, -0x8(%rsp)
jne 0x8751
movl $0x1, %eax
movq %rdi, %rbx
jmp 0x8955
movl $0x1, %r15d
movl %r8d, %ecx
shll %cl, %r15d
leal 0x1(%r15), %r14d
incl %r8d
leaq -0x2(%rsi), %rax
movq %rax, -0x20(%rsp)
movl $0x4, %ecx
xorl %r11d, %r11d
movq %rdi, %rbx
xorl %r13d, %r13d
testl %r11d, %r11d
je 0x8877
movl %r13d, %r11d
cmpl %r10d, %r13d
jae 0x87b0
movl %r13d, %eax
movq -0x18(%rsp), %rdx
leaq (%rdx,%rax,2), %rax
movl %r13d, %r11d
cmpw $0x0, (%rax)
jne 0x87b0
incl %r11d
addq $0x2, %rax
cmpl %r11d, %r10d
jne 0x879b
movl %r10d, %r11d
movl $0x3, %esi
cmpl %r10d, %r11d
je 0x886c
leal 0x18(%r13), %eax
cmpl %eax, %r11d
jae 0x87cc
movl %r13d, %eax
jmp 0x87fb
movl $0xffff, %edx           # imm = 0xFFFF
shll %cl, %edx
addl %edx, %ebp
testl %r9d, %r9d
jne 0x87e1
cmpq -0x20(%rsp), %rbx
ja 0x884f
movw %bp, (%rbx)
addq $0x2, %rbx
shrl $0x10, %ebp
leal 0x18(%r13), %eax
addl $0x30, %r13d
cmpl %r13d, %r11d
movl %eax, %r13d
jae 0x87d3
leal 0x3(%rax), %edx
cmpl %edx, %r11d
jb 0x881c
movl $0x3, %edx
shll %cl, %edx
addl %edx, %ebp
addl $0x2, %ecx
leal 0x3(%rax), %edx
addl $0x6, %eax
cmpl %eax, %r11d
movl %edx, %eax
jae 0x8803
movl %ebp, %edx
movl %r11d, %ebp
subl %eax, %ebp
shll %cl, %ebp
addl %edx, %ebp
leal 0x2(%rcx), %eax
xorl %esi, %esi
cmpl $0xf, %ecx
jl 0x884b
testl %r9d, %r9d
jne 0x885f
cmpq -0x20(%rsp), %rbx
jbe 0x885f
movq $-0x2, -0x30(%rsp)
movl $0x1, %esi
movl %eax, %ecx
jmp 0x886c
movq $-0x2, -0x30(%rsp)
movl $0x1, %esi
jmp 0x886c
movw %bp, (%rbx)
addq $0x2, %rbx
shrl $0x10, %ebp
addl $-0xe, %ecx
testl %esi, %esi
jne 0x8934
movl %r11d, %r13d
movl %r13d, %eax
movq -0x18(%rsp), %rdx
movswl (%rdx,%rax,2), %eax
movl %r14d, %edx
notl %edx
movl %eax, %esi
negl %esi
cmovsl %eax, %esi
leal (%rdx,%r15,2), %edx
leal 0x1(%rax), %edi
cmpl %r15d, %edi
movl %edx, %edi
movl $0x0, %r11d
cmovll %r11d, %edi
addl %eax, %edi
incl %edi
movl %edi, %r12d
shll %cl, %r12d
xorl %eax, %eax
cmpl %edx, %edi
setl %dl
xorl %r11d, %r11d
cmpl $0x1, %edi
sete %dil
subl %esi, %r14d
jle 0x88d9
cmpl %r15d, %r14d
jge 0x88e2
movl %r8d, %esi
decl %esi
sarl %r15d
cmpl %r15d, %r14d
jl 0x88cd
jmp 0x88e5
movq $-0x1, -0x30(%rsp)
movl %r8d, %esi
testl %r14d, %r14d
jle 0x899c
addl %r12d, %ebp
addl %r8d, %ecx
movb %dl, %al
subl %eax, %ecx
cmpl $0x11, %ecx
jl 0x891a
testl %r9d, %r9d
jne 0x890d
cmpq -0x20(%rsp), %rbx
ja 0x8993
movw %bp, (%rbx)
addq $0x2, %rbx
shrl $0x10, %ebp
addl $-0x10, %ecx
incl %r13d
cmpl %r10d, %r13d
jae 0x8939
movb %dil, %r11b
movl %esi, %r8d
cmpl $0x1, %r14d
jne 0x877b
jmp 0x8939
cmpl $0x3, %esi
jne 0x899c
cmpl $0x1, %r14d
sete %al
movl %eax, -0x24(%rsp)
leal 0x7(%rcx), %eax
addl $0xe, %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x3, %ecx
movslq %ecx, %rax
movq $-0x1, -0x30(%rsp)
cmpb $0x0, -0x24(%rsp)
je 0x899c
testl %r9d, %r9d
jne 0x8981
movq -0x8(%rsp), %rcx
addq $-0x2, %rcx
movq $-0x2, -0x30(%rsp)
cmpq %rcx, %rbx
ja 0x899c
movw %bp, (%rbx)
addq %rbx, %rax
subq -0x10(%rsp), %rax
movq %rax, -0x30(%rsp)
jmp 0x899c
movq $-0x2, -0x30(%rsp)
movq -0x30(%rsp), %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq