// File: /workspace/github_c_makefile_star2_O3/Cyan4973[P]FiniteStateEntropy/programs/../lib/huf_decompress.c
// Function: HUF_readDTableX1_wksp
size_t HUF_readDTableX1_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize)
{
    U32 tableLog = 0;
    U32 nbSymbols = 0;
    size_t iSize;
    void* const dtPtr = DTable + 1;
    HUF_DEltX1* const dt = (HUF_DEltX1*)dtPtr;

    U32* rankVal;
    BYTE* huffWeight;
    size_t spaceUsed32 = 0;

    rankVal = (U32 *)workSpace + spaceUsed32;
    spaceUsed32 += HUF_TABLELOG_ABSOLUTEMAX + 1;
    huffWeight = (BYTE *)((U32 *)workSpace + spaceUsed32);
    spaceUsed32 += HUF_ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;

    if ((spaceUsed32 << 2) > wkspSize) return ERROR(tableLog_tooLarge);

    DEBUG_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUF_DTable));
    /* memset(huffWeight, 0, sizeof(huffWeight)); */   /* is not necessary, even though some analyzer complain ... */

    iSize = HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
    if (HUF_isError(iSize)) return iSize;

    /* Table header */
    {   DTableDesc dtd = HUF_getDTableDesc(DTable);
        if (tableLog > (U32)(dtd.maxTableLog+1)) return ERROR(tableLog_tooLarge);   /* DTable too small, Huffman tree cannot fit in */
        dtd.tableType = 0;
        dtd.tableLog = (BYTE)tableLog;
        memcpy(DTable, &dtd, sizeof(dtd));
    }

    /* Calculate starting value for each rank */
    {   U32 n, nextRankStart = 0;
        for (n=1; n<tableLog+1; n++) {
            U32 const current = nextRankStart;
            nextRankStart += (rankVal[n] << (n-1));
            rankVal[n] = current;
    }   }

    /* fill DTable */
    {   U32 n;
        size_t const nEnd = nbSymbols;
        for (n=0; n<nEnd; n++) {
            size_t const w = huffWeight[n];
            size_t const length = (1 << w) >> 1;
            size_t const uStart = rankVal[w];
            size_t const uEnd = uStart + length;
            size_t u;
            HUF_DEltX1 D;
            D.byte = (BYTE)n;
            D.nbBits = (BYTE)(tableLog + 1 - w);
            rankVal[w] = (U32)uEnd;
            if (length < 4) {
                /* Use length in the loop bound so the compiler knows it is short. */
                for (u = 0; u < length; ++u)
                    dt[uStart + u] = D;
            } else {
                /* Unroll the loop 4 times, we know it is a power of 2. */
                for (u = uStart; u < uEnd; u += 4) {
                    dt[u + 0] = D;
                    dt[u + 1] = D;
                    dt[u + 2] = D;
                    dt[u + 3] = D;
    }   }   }   }
    return iSize;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x10, %rsp
xorl %eax, %eax
movl %eax, 0xc(%rsp)
movl %eax, 0x8(%rsp)
movq $-0x5, %r15
cmpq $0x140, %r8             # imm = 0x140
jb 0xc6b6
movq %rcx, %rbx
movq %rsi, %r9
movq %rdi, %r14
leaq 0x40(%rcx), %rdi
movq %rdx, (%rsp)
leaq 0x8(%rsp), %rcx
leaq 0xc(%rsp), %r8
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdx
callq 0x7a21
cmpq $-0x9, %rax
ja 0xc6b3
movl (%r14), %ecx
movl 0xc(%rsp), %esi
movzbl %cl, %edx
incl %edx
cmpl %edx, %esi
ja 0xc6b6
movl %ecx, %edx
shrl $0x18, %edx
movb %cl, (%r14)
movb $0x0, 0x1(%r14)
movb %sil, 0x2(%r14)
movb %dl, 0x3(%r14)
testq %rsi, %rsi
je 0xc608
xorl %ecx, %ecx
xorl %edx, %edx
movl 0x4(%rbx,%rcx,4), %edi
shll %cl, %edi
addl %edx, %edi
movl %edx, 0x4(%rbx,%rcx,4)
incq %rcx
movl %edi, %edx
cmpl %ecx, %esi
jne 0xc5f3
movl 0x8(%rsp), %edx
testq %rdx, %rdx
je 0xc6b3
incl %esi
leaq 0x5(%r14), %rdi
xorl %r8d, %r8d
movzbl 0x40(%rbx,%r8), %ecx
movl $0x1, %r9d
shll %cl, %r9d
sarl %r9d
movslq %r9d, %r11
movl (%rbx,%rcx,4), %r10d
leaq (%r11,%r10), %r15
movl %esi, %r9d
subb %cl, %r9b
movl %r15d, (%rbx,%rcx,4)
cmpl $0x4, %r11d
jae 0xc671
testb %cl, %cl
je 0xc6a7
cmpq $0x1, %r11
adcq $0x0, %r11
leaq (%rdi,%r10,2), %rcx
xorl %r10d, %r10d
movb %r8b, -0x1(%rcx,%r10,2)
movb %r9b, (%rcx,%r10,2)
incq %r10
cmpq %r10, %r11
jne 0xc65e
jmp 0xc6a7
cmpq %r10, %r15
jbe 0xc6a7
movb %r8b, 0x4(%r14,%r10,2)
movb %r9b, 0x5(%r14,%r10,2)
movb %r8b, 0x6(%r14,%r10,2)
movb %r9b, 0x7(%r14,%r10,2)
movb %r8b, 0x8(%r14,%r10,2)
movb %r9b, 0x9(%r14,%r10,2)
movb %r8b, 0xa(%r14,%r10,2)
movb %r9b, 0xb(%r14,%r10,2)
addq $0x4, %r10
cmpq %r15, %r10
jb 0xc676
incq %r8
cmpq %rdx, %r8
jne 0xc61e
movq %rax, %r15
movq %r15, %rax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq