// File: /workspace/github_c_makefile_star2_O2/Cyan4973[P]FiniteStateEntropy/programs/../lib/huf_decompress.c
// Function: HUF_readDTableX2_wksp
size_t HUF_readDTableX2_wksp(HUF_DTable* DTable,
                       const void* src, size_t srcSize,
                             void* workSpace, size_t wkspSize)
{
    U32 tableLog, maxW, sizeOfSort, nbSymbols;
    DTableDesc dtd = HUF_getDTableDesc(DTable);
    U32 const maxTableLog = dtd.maxTableLog;
    size_t iSize;
    void* dtPtr = DTable+1;   /* force compiler to avoid strict-aliasing */
    HUF_DEltX2* const dt = (HUF_DEltX2*)dtPtr;
    U32 *rankStart;

    rankValCol_t* rankVal;
    U32* rankStats;
    U32* rankStart0;
    sortedSymbol_t* sortedSymbol;
    BYTE* weightList;
    size_t spaceUsed32 = 0;

    rankVal = (rankValCol_t *)((U32 *)workSpace + spaceUsed32);
    spaceUsed32 += (sizeof(rankValCol_t) * HUF_TABLELOG_MAX) >> 2;
    rankStats = (U32 *)workSpace + spaceUsed32;
    spaceUsed32 += HUF_TABLELOG_MAX + 1;
    rankStart0 = (U32 *)workSpace + spaceUsed32;
    spaceUsed32 += HUF_TABLELOG_MAX + 2;
    sortedSymbol = (sortedSymbol_t *)workSpace + (spaceUsed32 * sizeof(U32)) / sizeof(sortedSymbol_t);
    spaceUsed32 += HUF_ALIGN(sizeof(sortedSymbol_t) * (HUF_SYMBOLVALUE_MAX + 1), sizeof(U32)) >> 2;
    weightList = (BYTE *)((U32 *)workSpace + spaceUsed32);
    spaceUsed32 += HUF_ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;

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

    rankStart = rankStart0 + 1;
    memset(rankStats, 0, sizeof(U32) * (2 * HUF_TABLELOG_MAX + 2 + 1));

    DEBUG_STATIC_ASSERT(sizeof(HUF_DEltX2) == sizeof(HUF_DTable));   /* if compiler fails here, assertion is wrong */
    if (maxTableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);
    /* memset(weightList, 0, sizeof(weightList)); */  /* is not necessary, even though some analyzer complain ... */

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

    /* check result */
    if (tableLog > maxTableLog) return ERROR(tableLog_tooLarge);   /* DTable can't fit code depth */

    /* find maxWeight */
    for (maxW = tableLog; rankStats[maxW]==0; maxW--) {}  /* necessarily finds a solution before 0 */

    /* Get start index of each weight */
    {   U32 w, nextRankStart = 0;
        for (w=1; w<maxW+1; w++) {
            U32 current = nextRankStart;
            nextRankStart += rankStats[w];
            rankStart[w] = current;
        }
        rankStart[0] = nextRankStart;   /* put all 0w symbols at the end of sorted list*/
        sizeOfSort = nextRankStart;
    }

    /* sort symbols by weight */
    {   U32 s;
        for (s=0; s<nbSymbols; s++) {
            U32 const w = weightList[s];
            U32 const r = rankStart[w]++;
            sortedSymbol[r].symbol = (BYTE)s;
            sortedSymbol[r].weight = (BYTE)w;
        }
        rankStart[0] = 0;   /* forget 0w symbols; this is beginning of weight(1) */
    }

    /* Build rankVal */
    {   U32* const rankVal0 = rankVal[0];
        {   int const rescale = (maxTableLog-tableLog) - 1;   /* tableLog <= maxTableLog */
            U32 nextRankVal = 0;
            U32 w;
            for (w=1; w<maxW+1; w++) {
                U32 current = nextRankVal;
                nextRankVal += rankStats[w] << (w+rescale);
                rankVal0[w] = current;
        }   }
        {   U32 const minBits = tableLog+1 - maxW;
            U32 consumed;
            for (consumed = minBits; consumed < maxTableLog - minBits + 1; consumed++) {
                U32* const rankValPtr = rankVal[consumed];
                U32 w;
                for (w = 1; w < maxW+1; w++) {
                    rankValPtr[w] = rankVal0[w] >> consumed;
    }   }   }   }

    HUF_fillDTableX2(dt, maxTableLog,
                   sortedSymbol, sizeOfSort,
                   rankStart0, rankVal, maxW,
                   tableLog+1);

    dtd.tableLog = (BYTE)maxTableLog;
    dtd.tableType = 1;
    memcpy(DTable, &dtd, sizeof(dtd));
    return iSize;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
pushq $-0x5
popq %r12
cmpq $0x5dc, %r8             # imm = 0x5DC
jb 0xb63e
movq %rcx, %r14
movq %rdi, %rbx
movl (%rdi), %eax
movl %eax, 0x14(%rsp)
movzbl %al, %eax
leaq 0x270(%rcx), %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x5c(%r15)
movups %xmm0, 0x50(%r15)
movups %xmm0, 0x40(%r15)
movups %xmm0, 0x30(%r15)
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x10(%r15)
movups %xmm0, (%r15)
cmpl $0xc, %eax
ja 0xb63e
movq %rsi, %r9
movl %eax, 0x10(%rsp)
leaq 0x4dc(%r14), %rdi
movq %rdx, (%rsp)
leaq 0x20(%rsp), %rcx
leaq 0x24(%rsp), %r8
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdx
callq 0x6aad
cmpq $-0x9, %rax
ja 0xb36a
movl 0x24(%rsp), %ebp
movl 0x10(%rsp), %ecx
subl %ebp, %ecx
jb 0xb63e
movq %rax, 0x38(%rsp)
movl 0x14(%rsp), %eax
shrl $0x18, %eax
movl %eax, 0x18(%rsp)
leaq 0x4(%rbx), %rax
movq %rax, 0x28(%rsp)
leaq 0x2dc(%r14), %rax
movq %rax, 0x30(%rsp)
xorl %esi, %esi
movl %ebp, %eax
movl %eax, %edx
decl %eax
incl %esi
cmpl $0x0, (%r15,%rdx,4)
je 0xb323
movl %ebp, %eax
subl %esi, %eax
addl $0x2, %eax
movl $0x0, 0xc(%rsp)
movl $0x9d, %edx
leaq -0x9c(%rdx), %rdi
cmpq %rax, %rdi
jae 0xb372
movl (%r14,%rdx,4), %edi
movl 0xc(%rsp), %r8d
addl %r8d, %edi
movl %r8d, 0x38(%r14,%rdx,4)
incq %rdx
movl %edi, 0xc(%rsp)
jmp 0xb344
movq %rax, %r12
jmp 0xb63e
movl 0xc(%rsp), %edx
movl %edx, 0x2a8(%r14)
movl 0x20(%rsp), %edx
xorl %edi, %edi
cmpq %rdi, %rdx
je 0xb3ba
movzbl 0x4dc(%r14,%rdi), %r8d
movl 0x2a8(%r14,%r8,4), %r9d
leal 0x1(%r9), %r10d
movl %r10d, 0x2a8(%r14,%r8,4)
movb %dil, 0x2dc(%r14,%r9,2)
movb %r8b, 0x2dd(%r14,%r9,2)
incq %rdi
jmp 0xb383
andl $0x0, 0x2a8(%r14)
xorl %edi, %edi
pushq $0x1
popq %rdx
cmpq %rax, %rdx
jae 0xb3e8
movl 0x270(%r14,%rdx,4), %r8d
shll %cl, %r8d
addl %edi, %r8d
movl %edi, (%r14,%rdx,4)
incq %rdx
incl %ecx
movl %r8d, %edi
jmp 0xb3c7
incl %ebp
movl 0x10(%rsp), %edx
subl %esi, %edx
incl %edx
movl %esi, %ecx
imulq $0x34, %rcx, %rdi
pushq $0x1
popq %r9
movl %esi, %ecx
cmpl %edx, %ecx
jae 0xb426
leaq (%r14,%rdi), %r10
movq %r9, %r11
cmpq %rax, %r11
jae 0xb41e
movl (%r14,%r11,4), %r8d
shrl %cl, %r8d
movl %r8d, (%r10,%r11,4)
incq %r11
jmp 0xb409
incl %ecx
addq $0x34, %rdi
jmp 0xb3fe
movl %esi, 0x1c(%rsp)
movl %ebp, %eax
movl 0x10(%rsp), %esi
subl %esi, %eax
movq %rax, 0x50(%rsp)
movl 0x30(%r14), %eax
movl %eax, 0xe0(%rsp)
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movaps %xmm2, 0xd0(%rsp)
movaps %xmm1, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movl 0xc(%rsp), %r8d
xorl %r9d, %r9d
pushq $0x1
popq %r10
movq 0x28(%rsp), %rdi
movq %rbx, 0x58(%rsp)
movq %r8, 0x48(%rsp)
movq %r14, 0x40(%rsp)
cmpq %r8, %r9
je 0xb625
movq 0x30(%rsp), %rax
movzbl (%rax,%r9,2), %r13d
movzbl 0x1(%rax,%r9,2), %r11d
movl %ebp, %r12d
subl %r11d, %r12d
movl 0xb0(%rsp,%r11,4), %eax
movl %esi, %edx
subl %r12d, %edx
movl %r10d, %r15d
movl %edx, %ecx
shll %cl, %r15d
cmpl 0x1c(%rsp), %edx
jae 0xb4e6
addl %eax, %r15d
movzbl %r12b, %ecx
shll $0x10, %ecx
addl %r13d, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpq %r15, %rax
jae 0xb615
movl %ecx, (%rdi,%rax,4)
incq %rax
jmp 0xb4d5
movq %r11, 0x60(%rsp)
movq %r9, 0x68(%rsp)
movq 0x50(%rsp), %rcx
leal (%r12,%rcx), %r8d
cmpl $0x2, %r8d
movq %rdi, %rsi
movl %r10d, %edi
cmovgel %r8d, %edi
movl 0x2a4(%r14,%rdi,4), %ecx
movl %r12d, %r9d
imulq $0x34, %r9, %r10
movl 0x30(%r14,%r10), %r9d
movl %r9d, 0xa0(%rsp)
leaq (%rsi,%rax,4), %r9
movups (%r14,%r10), %xmm0
movups 0x10(%r14,%r10), %xmm1
movups 0x20(%r14,%r10), %xmm2
movaps %xmm2, 0x90(%rsp)
movaps %xmm1, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
cmpl $0x2, %r8d
movq %r13, %r14
jl 0xb57e
movl 0x70(%rsp,%rdi,4), %edi
movzbl %r12b, %r8d
shll $0x10, %r8d
leal (%r8,%r14), %r10d
addl $0x1000000, %r10d       # imm = 0x1000000
xorl %r8d, %r8d
cmpq %r8, %rdi
je 0xb57e
movl %r10d, (%r9,%r8,4)
incq %r8
jmp 0xb570
movl 0xc(%rsp), %edi
subl %ecx, %edi
movq 0x30(%rsp), %rsi
leaq (%rsi,%rcx,2), %r11
movl %edi, %r10d
xorl %ebx, %ebx
cmpq %r10, %rbx
je 0xb5ec
movzbl (%r11,%rbx,2), %r8d
movzbl 0x1(%r11,%rbx,2), %edi
movl %ebp, %r13d
movl %ebp, %esi
subl %edi, %esi
movl %edx, %ecx
subl %esi, %ecx
pushq $0x1
popq %rbp
shll %cl, %ebp
movl 0x70(%rsp,%rdi,4), %ecx
addl %ecx, %ebp
shll $0x8, %r8d
addl %r12d, %esi
movzbl %sil, %esi
shll $0x10, %esi
orl %r8d, %esi
leal (%r14,%rsi), %r8d
addl $0x2000000, %r8d        # imm = 0x2000000
movl %ecx, %esi
incl %ecx
movl %r8d, (%r9,%rsi,4)
cmpl %ebp, %ecx
jb 0xb5d4
movl %ebp, 0x70(%rsp,%rdi,4)
incq %rbx
movl %r13d, %ebp
jmp 0xb592
addl %eax, %r15d
movq 0x58(%rsp), %rbx
movl 0x10(%rsp), %esi
movq 0x28(%rsp), %rdi
movq 0x48(%rsp), %r8
movq 0x68(%rsp), %r9
pushq $0x1
popq %r10
movq 0x40(%rsp), %r14
movq 0x60(%rsp), %r11
movl %r15d, 0xb0(%rsp,%r11,4)
incq %r9
jmp 0xb488
movl 0x14(%rsp), %eax
movb %al, (%rbx)
movb $0x1, 0x1(%rbx)
movb %al, 0x2(%rbx)
movl 0x18(%rsp), %eax
movb %al, 0x3(%rbx)
movq 0x38(%rsp), %r12
movq %r12, %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq