// File: /workspace/github_c_makefile_star2_O3/Cyan4973[P]FiniteStateEntropy/programs/../lib/fse_decompress.c
// Function: FSE_buildDTable
size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
{
    void* const tdPtr = dt+1;   /* because *dt is unsigned, 32-bits aligned on 32-bits */
    FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr);
    U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];

    U32 const maxSV1 = maxSymbolValue + 1;
    U32 const tableSize = 1 << tableLog;
    U32 highThreshold = tableSize-1;

    /* Sanity Checks */
    if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);
    if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);

    /* Init, lay down lowprob symbols */
    {   FSE_DTableHeader DTableH;
        DTableH.tableLog = (U16)tableLog;
        DTableH.fastMode = 1;
        {   S16 const largeLimit= (S16)(1 << (tableLog-1));
            U32 s;
            for (s=0; s<maxSV1; s++) {
                if (normalizedCounter[s]==-1) {
                    tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
                    symbolNext[s] = 1;
                } else {
                    if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;
                    symbolNext[s] = normalizedCounter[s];
        }   }   }
        memcpy(dt, &DTableH, sizeof(DTableH));
    }

    /* Spread symbols */
    {   U32 const tableMask = tableSize-1;
        U32 const step = FSE_TABLESTEP(tableSize);
        U32 s, position = 0;
        for (s=0; s<maxSV1; s++) {
            int i;
            for (i=0; i<normalizedCounter[s]; i++) {
                tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
                position = (position + step) & tableMask;
                while (position > highThreshold) position = (position + step) & tableMask;   /* lowprob area */
        }   }
        if (position!=0) return ERROR(GENERIC);   /* position must reach all cells once, otherwise normalizedCounter is incorrect */
    }

    /* Build Decoding table */
    {   U32 u;
        for (u=0; u<tableSize; u++) {
            FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
            U32 const nextState = symbolNext[symbol]++;
            tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32(nextState) );
            tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
    }   }

    return 0;
}

// Assembly:
movl $0x1, %r9d
shll %cl, %r9d
movq $-0x6, %rax
cmpl $0xff, %edx
ja 0x813b
movl %ecx, %r8d
movq $-0x5, %rax
cmpl $0xc, %ecx
ja 0x813b
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movl $0x8000, %r10d          # imm = 0x8000
movl %r8d, %ecx
shll %cl, %r10d
incl %edx
leal -0x1(%r9), %eax
shrl $0x10, %r10d
movl %edx, %ecx
movw $0x1, %r11w
xorl %ebx, %ebx
xorl %r14d, %r14d
movl %eax, %edx
movswl (%rsi,%r14,2), %ebp
cmpl $-0x1, %ebp
je 0x807a
cmpl %ebp, %r10d
cmovlel %ebx, %r11d
jmp 0x8088
movl %edx, %r15d
decl %edx
movb %r14b, 0x6(%rdi,%r15,4)
movw $0x1, %bp
movw %bp, -0x80(%rsp,%r14,2)
incq %r14
cmpq %r14, %rcx
jne 0x8067
movw %r8w, (%rdi)
movw %r11w, 0x2(%rdi)
movl %r9d, %r10d
shrl $0x3, %r10d
movl %r9d, %r11d
shrl %r11d
addl %r11d, %r10d
addl $0x3, %r10d
xorl %r11d, %r11d
xorl %ebx, %ebx
movswl (%rsi,%r11,2), %ebp
testl %ebp, %ebp
jle 0x80dd
xorl %r14d, %r14d
movl %ebx, %r15d
movb %r11b, 0x6(%rdi,%r15,4)
addl %r10d, %ebx
andl %eax, %ebx
cmpl %edx, %ebx
ja 0x80cc
incl %r14d
cmpl %ebp, %r14d
jne 0x80c4
incq %r11
cmpq %rcx, %r11
jne 0x80b8
movq $-0x1, %rax
testl %ebx, %ebx
jne 0x812e
movl %r9d, %edx
xorl %eax, %eax
xorl %esi, %esi
movzbl 0x6(%rdi,%rsi,4), %ecx
movzwl -0x80(%rsp,%rcx,2), %r10d
leal 0x1(%r10), %r11d
movw %r11w, -0x80(%rsp,%rcx,2)
bsrl %r10d, %r11d
movl %r8d, %ecx
subl %r11d, %ecx
movb %cl, 0x7(%rdi,%rsi,4)
shll %cl, %r10d
subl %r9d, %r10d
movw %r10w, 0x4(%rdi,%rsi,4)
incq %rsi
cmpq %rsi, %rdx
jne 0x80f7
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r14
popq %r15
popq %rbp
retq