// File: /workspace/github_c_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x18, %rsp
andl $0x0, 0x14(%rsp)
andl $0x0, 0x10(%rsp)
pushq $-0x5
popq %r15
cmpq $0x140, %r8             # imm = 0x140
jb 0xa425
movq %rcx, %rbx
movq %rsi, %r9
movq %rdi, %r14
leaq 0x40(%rcx), %rdi
movq %rdx, (%rsp)
leaq 0x10(%rsp), %rcx
leaq 0x14(%rsp), %r8
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdx
callq 0x6aad
cmpq $-0x9, %rax
ja 0xa422
movl (%r14), %ecx
movl 0x14(%rsp), %esi
movzbl %cl, %edx
incl %edx
cmpl %edx, %esi
ja 0xa425
movl %ecx, %edx
shrl $0x18, %edx
movb %cl, (%r14)
movb $0x0, 0x1(%r14)
movb %sil, 0x2(%r14)
movb %dl, 0x3(%r14)
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rcx, %rsi
je 0xa38d
movl 0x4(%rbx,%rcx,4), %edi
shll %cl, %edi
addl %edx, %edi
movl %edx, 0x4(%rbx,%rcx,4)
incq %rcx
movl %edi, %edx
jmp 0xa375
movl 0x10(%rsp), %edx
incl %esi
leaq 0x5(%r14), %rdi
xorl %r8d, %r8d
pushq $0x1
popq %r9
cmpq %rdx, %r8
je 0xa422
movzbl 0x40(%rbx,%r8), %ecx
movl %r9d, %r10d
shll %cl, %r10d
sarl %r10d
movslq %r10d, %r12
movl (%rbx,%rcx,4), %r11d
leaq (%r12,%r11), %r15
movl %esi, %r10d
subb %cl, %r10b
movl %r15d, (%rbx,%rcx,4)
cmpl $0x4, %r12d
jae 0xa3e7
leaq (%rdi,%r11,2), %rcx
xorl %r11d, %r11d
cmpq %r11, %r12
je 0xa41a
movb %r8b, -0x1(%rcx,%r11,2)
movb %r10b, (%rcx,%r11,2)
incq %r11
jmp 0xa3d4
cmpq %r15, %r11
jae 0xa41a
movb %r8b, 0x4(%r14,%r11,2)
movb %r10b, 0x5(%r14,%r11,2)
movb %r8b, 0x6(%r14,%r11,2)
movb %r10b, 0x7(%r14,%r11,2)
movb %r8b, 0x8(%r14,%r11,2)
movb %r10b, 0x9(%r14,%r11,2)
movb %r8b, 0xa(%r14,%r11,2)
movb %r10b, 0xb(%r14,%r11,2)
addq $0x4, %r11
jmp 0xa3e7
incq %r8
jmp 0xa39e
movq %rax, %r15
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq