// File: /workspace/github_c_makefile_star2_O3/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, %r15d
movq $-0x1, %rax
incl %r15d
je 0x8f5d
addq %rdi, %rsi
leal -0x5(%r8), %r11d
movl $0x1, %r14d
movl %r8d, %ecx
shll %cl, %r14d
leal 0x1(%r14), %ebp
incl %r8d
addq $-0x2, %rsi
movl %r15d, %eax
movq %rax, -0x8(%rsp)
movl $0x4, %ecx
xorl %r12d, %r12d
movq %rdi, -0x10(%rsp)
movq %rdi, %rbx
xorl %r13d, %r13d
movq %rsi, -0x18(%rsp)
movl %r9d, -0x1c(%rsp)
movl %r15d, -0x20(%rsp)
testl %r12d, %r12d
je 0x8db9
movl %r13d, %r10d
cmpl %r15d, %r13d
movq -0x8(%rsp), %rax
jae 0x8da2
movl %r13d, %r10d
cmpw $0x0, (%rdx,%r10,2)
jne 0x8da2
incq %r10
cmpq %r10, %rax
jne 0x8d8d
jmp 0x8f1a
cmpl %r15d, %r10d
je 0x8f1a
leal 0x18(%r13), %eax
cmpl %eax, %r10d
jae 0x8dc1
movl %r13d, %edi
jmp 0x8df5
movl %r13d, %r10d
jmp 0x8e4d
movl $0xffff, %eax           # imm = 0xFFFF
shll %cl, %eax
testl %r9d, %r9d
jne 0x8dd6
cmpq %rsi, %rbx
ja 0x8f56
addl %eax, %r11d
movw %r11w, (%rbx)
addq $0x2, %rbx
shrl $0x10, %r11d
leal 0x18(%r13), %edi
addl $0x30, %r13d
cmpl %r13d, %r10d
movl %edi, %r13d
jae 0x8dc8
leal 0x3(%rdi), %eax
cmpl %eax, %r10d
jb 0x8e17
movl $0x3, %eax
shll %cl, %eax
addl %eax, %r11d
addl $0x2, %ecx
leal 0x3(%rdi), %eax
addl $0x6, %edi
cmpl %edi, %r10d
movl %eax, %edi
jae 0x8dfd
movl %r11d, %eax
movl %r10d, %r11d
subl %edi, %r11d
shll %cl, %r11d
addl %eax, %r11d
cmpl $0xf, %ecx
jl 0x8e4a
testl %r9d, %r9d
jne 0x8e39
cmpq %rsi, %rbx
ja 0x8f56
movw %r11w, (%rbx)
addq $0x2, %rbx
shrl $0x10, %r11d
addl $-0xe, %ecx
jmp 0x8e4d
addl $0x2, %ecx
movl %r10d, %eax
movswl (%rdx,%rax,2), %eax
movl %ebp, %esi
notl %esi
movl %eax, %edi
negl %edi
cmovsl %eax, %edi
leal (%rsi,%r14,2), %esi
leal 0x1(%rax), %r15d
cmpl %r14d, %r15d
movl %esi, %r15d
movl $0x0, %r9d
cmovll %r9d, %r15d
leal (%r15,%rax), %r13d
incl %r13d
movl %r13d, %r15d
shll %cl, %r15d
xorl %eax, %eax
cmpl %esi, %r13d
setl %sil
xorl %r12d, %r12d
cmpl $0x1, %r13d
sete %r13b
subl %edi, %ebp
jle 0x8f68
movl %r8d, %edi
cmpl %r14d, %ebp
jge 0x8eaf
decl %edi
sarl %r14d
jmp 0x8ea3
addl %r15d, %r11d
addl %r8d, %ecx
movb %sil, %al
subl %eax, %ecx
cmpl $0x11, %ecx
jl 0x8ef1
movl -0x1c(%rsp), %r9d
testl %r9d, %r9d
sete %al
movq -0x18(%rsp), %rsi
cmpq %rsi, %rbx
seta %r8b
testb %r8b, %al
movl -0x20(%rsp), %r15d
jne 0x8f56
movw %r11w, (%rbx)
addq $0x2, %rbx
shrl $0x10, %r11d
addl $-0x10, %ecx
jmp 0x8f00
movq -0x18(%rsp), %rsi
movl -0x1c(%rsp), %r9d
movl -0x20(%rsp), %r15d
incl %r10d
cmpl %r15d, %r10d
jae 0x8f1a
movb %r13b, %r12b
movl %r10d, %r13d
movl %edi, %r8d
cmpl $0x1, %ebp
jne 0x8d78
leal 0x7(%rcx), %eax
addl $0xe, %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
cmpl $0x1, %ebp
movq $-0x1, %rax
jne 0x8f5d
testl %r9d, %r9d
jne 0x8f42
movq $-0x2, %rax
cmpq %rsi, %rbx
ja 0x8f5d
sarl $0x3, %ecx
movslq %ecx, %rax
movw %r11w, (%rbx)
addq %rbx, %rax
subq -0x10(%rsp), %rax
jmp 0x8f5d
movq $-0x2, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $-0x1, %rax
jmp 0x8f5d