// File: /workspace/github_c_makefile_star2_O1/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 0x7aeb
movl %ecx, %r8d
movq $-0x5, %rax
cmpl $0xc, %ecx
ja 0x7aeb
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x180, %rsp            # imm = 0x180
leal -0x1(%r8), %ecx
movl $0x10000, %r10d         # imm = 0x10000
shll %cl, %r10d
incl %edx
leal -0x1(%r9), %eax
sarl $0x10, %r10d
movl %edx, %ebx
movw $0x1, %r11w
xorl %r14d, %r14d
xorl %r15d, %r15d
movl %eax, %ecx
movswl (%rsi,%r15,2), %ebp
cmpl $-0x1, %ebp
je 0x7a22
cmpl %ebp, %r10d
cmovlel %r14d, %r11d
jmp 0x7a30
movl %ecx, %r12d
decl %ecx
movb %r15b, 0x6(%rdi,%r12,4)
movw $0x1, %bp
movw %bp, -0x80(%rsp,%r15,2)
incq %r15
cmpq %r15, %rbx
jne 0x7a0f
movw %r8w, (%rdi)
movw %r11w, 0x2(%rdi)
movl %r9d, %r10d
shrl $0x3, %r10d
movl %r9d, %r11d
shrl %r11d
addl %r11d, %r10d
addl $0x3, %r10d
cmpl $0x1, %edx
adcl $0x0, %edx
xorl %r11d, %r11d
xorl %ebx, %ebx
movswl (%rsi,%r11,2), %ebp
testl %ebp, %ebp
jle 0x7a8b
xorl %r14d, %r14d
movl %ebx, %r15d
movb %r11b, 0x6(%rdi,%r15,4)
addl %r10d, %ebx
andl %eax, %ebx
cmpl %ecx, %ebx
ja 0x7a7a
incl %r14d
cmpl %ebp, %r14d
jne 0x7a72
incq %r11
cmpq %rdx, %r11
jne 0x7a66
movq $-0x1, %rax
testl %ebx, %ebx
jne 0x7adc
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 0x7aa5
addq $0x180, %rsp            # imm = 0x180
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq