// File: /workspace/github_c_makefile_star2_O1/Cyan4973[P]FiniteStateEntropy/programs/../lib/huf_compress.c
// Function: HUF_buildCTable_wksp
size_t HUF_buildCTable_wksp (HUF_CElt* tree, const unsigned* count, U32 maxSymbolValue, U32 maxNbBits, void* workSpace, size_t wkspSize)
{
    HUF_buildCTable_wksp_tables* const wksp_tables = (HUF_buildCTable_wksp_tables*)workSpace;
    nodeElt* const huffNode0 = wksp_tables->huffNodeTbl;
    nodeElt* const huffNode = huffNode0+1;
    int nonNullRank;
    int lowS, lowN;
    int nodeNb = STARTNODE;
    int n, nodeRoot;

    /* safety checks */
    if (((size_t)workSpace & 3) != 0) return ERROR(GENERIC);  /* must be aligned on 4-bytes boundaries */
    if (wkspSize < sizeof(HUF_buildCTable_wksp_tables))
      return ERROR(workSpace_tooSmall);
    if (maxNbBits == 0) maxNbBits = HUF_TABLELOG_DEFAULT;
    if (maxSymbolValue > HUF_SYMBOLVALUE_MAX)
      return ERROR(maxSymbolValue_tooLarge);
    memset(huffNode0, 0, sizeof(huffNodeTable));

    /* sort, decreasing order */
    HUF_sort(huffNode, count, maxSymbolValue, wksp_tables->rankPosition);

    /* init for parents */
    nonNullRank = (int)maxSymbolValue;
    while(huffNode[nonNullRank].count == 0) nonNullRank--;
    lowS = nonNullRank; nodeRoot = nodeNb + lowS - 1; lowN = nodeNb;
    huffNode[nodeNb].count = huffNode[lowS].count + huffNode[lowS-1].count;
    huffNode[lowS].parent = huffNode[lowS-1].parent = (U16)nodeNb;
    nodeNb++; lowS-=2;
    for (n=nodeNb; n<=nodeRoot; n++) huffNode[n].count = (U32)(1U<<30);
    huffNode0[0].count = (U32)(1U<<31);  /* fake entry, strong barrier */

    /* create parents */
    while (nodeNb <= nodeRoot) {
        int const n1 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++;
        int const n2 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++;
        huffNode[nodeNb].count = huffNode[n1].count + huffNode[n2].count;
        huffNode[n1].parent = huffNode[n2].parent = (U16)nodeNb;
        nodeNb++;
    }

    /* distribute weights (unlimited tree height) */
    huffNode[nodeRoot].nbBits = 0;
    for (n=nodeRoot-1; n>=STARTNODE; n--)
        huffNode[n].nbBits = huffNode[ huffNode[n].parent ].nbBits + 1;
    for (n=0; n<=nonNullRank; n++)
        huffNode[n].nbBits = huffNode[ huffNode[n].parent ].nbBits + 1;

    /* enforce maxTableLog */
    maxNbBits = HUF_setMaxHeight(huffNode, (U32)nonNullRank, maxNbBits);

    /* fill result into tree (val, nbBits) */
    {   U16 nbPerRank[HUF_TABLELOG_MAX+1] = {0};
        U16 valPerRank[HUF_TABLELOG_MAX+1] = {0};
        int const alphabetSize = (int)(maxSymbolValue + 1);
        if (maxNbBits > HUF_TABLELOG_MAX) return ERROR(GENERIC);   /* check fit into table */
        for (n=0; n<=nonNullRank; n++)
            nbPerRank[huffNode[n].nbBits]++;
        /* determine stating value per rank */
        {   U16 min = 0;
            for (n=(int)maxNbBits; n>0; n--) {
                valPerRank[n] = min;      /* get starting value within each rank */
                min += nbPerRank[n];
                min >>= 1;
        }   }
        for (n=0; n<alphabetSize; n++)
            tree[huffNode[n].byte].nbBits = huffNode[n].nbBits;   /* push nbBits per symbol, symbol order */
        for (n=0; n<alphabetSize; n++)
            tree[n].val = valPerRank[tree[n].nbBits]++;   /* assign value within rank, symbol order */
    }

    return maxNbBits;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r8, %r14
movq $-0x1, %rax
testb $0x3, %r14b
jne 0xab72
movq $-0x8, %rax
cmpq $0x1100, %r9            # imm = 0x1100
jb 0xab72
movl %edx, %r15d
testl %ecx, %ecx
movl $0xb, %ebp
cmovnel %ecx, %ebp
movq $-0x6, %rax
cmpl $0xff, %edx
ja 0xab72
movq %rsi, %r12
movq %rdi, 0x58(%rsp)
leal 0x1(%r15), %r13d
xorl %ebx, %ebx
movl $0x1100, %edx           # imm = 0x1100
movq %r14, %rdi
xorl %esi, %esi
callq 0x10c0
movl (%r12,%rbx,4), %eax
incl %eax
bsrl %eax, %eax
incl 0x1000(%r14,%rax,8)
incq %rbx
cmpq %rbx, %r13
jne 0xa623
movl $0x21d, %eax            # imm = 0x21D
movl 0x8(%r14,%rax,8), %ecx
addl %ecx, (%r14,%rax,8)
decq %rax
cmpq $0x1ff, %rax            # imm = 0x1FF
jne 0xa641
movl $0x200, %eax            # imm = 0x200
movl (%r14,%rax,8), %ecx
movl %ecx, 0x4(%r14,%rax,8)
incq %rax
cmpq $0x220, %rax            # imm = 0x220
jne 0xa65a
xorl %eax, %eax
movl (%r12,%rax,4), %ecx
leal 0x1(%rcx), %edx
bsrl %edx, %edx
movl 0x100c(%r14,%rdx,8), %esi
leal 0x1(%rsi), %edi
movl %edi, 0x100c(%r14,%rdx,8)
cmpl 0x1008(%r14,%rdx,8), %esi
jbe 0xa6bb
leal -0x1(%rsi), %edi
cmpl 0x8(%r14,%rdi,8), %ecx
jbe 0xa6bb
movl %esi, %esi
movq 0x8(%r14,%rdi,8), %r8
movq %r8, 0x8(%r14,%rsi,8)
movl %edi, %esi
cmpl 0x1008(%r14,%rdx,8), %edi
ja 0xa697
jmp 0xa6bd
movl %esi, %edi
movl %edi, %edx
movl %ecx, 0x8(%r14,%rdx,8)
movb %al, 0xe(%r14,%rdx,8)
incq %rax
cmpq %r13, %rax
jne 0xa670
movl %r15d, %esi
leaq (%r14,%rsi,8), %rcx
addq $0x8, %rcx
xorl %r8d, %r8d
movl (%rcx,%r8,8), %edx
decq %r8
testl %edx, %edx
je 0xa6df
leaq (%rsi,%r8), %rcx
addq $0x100, %rcx            # imm = 0x100
leaq (%r14,%rsi,8), %rax
addl 0x8(%rax,%r8,8), %edx
movl %edx, 0x808(%r14)
movw $0x100, %dx             # imm = 0x100
movw %dx, 0xc(%rax,%r8,8)
movq %rax, 0x48(%rsp)
movw %dx, 0x14(%rax,%r8,8)
movq %rsi, 0x50(%rsp)
leaq (%rsi,%r8), %rax
incq %rax
cmpq $0x2, %rax
jl 0xa745
movslq %ecx, %rdx
movl $0x100, %esi            # imm = 0x100
movl $0x40000000, 0x10(%r14,%rsi,8) # imm = 0x40000000
incq %rsi
cmpq %rdx, %rsi
jl 0xa734
movl $0x80000000, (%r14)     # imm = 0x80000000
movq %rax, 0x8(%rsp)
cmpq $0x2, %rax
jl 0xa7ed
decl %r15d
addq %r8, %r15
movslq %ecx, %rsi
movl $0x100, %r10d           # imm = 0x100
movl $0x100, %edi            # imm = 0x100
movslq %r15d, %rdx
movl 0x8(%r14,%rdx,8), %ebx
movslq %r10d, %r11
movl 0x8(%r14,%r11,8), %r9d
cmpl %r9d, %ebx
sbbl $-0x1, %r11d
xorl %edx, %edx
movl %r10d, %r12d
cmpl %r9d, %ebx
setb %r10b
jae 0xa797
movl %r15d, %r12d
movb %r10b, %dl
movl %r15d, %ebx
subl %edx, %ebx
movslq %ebx, %r9
movl 0x8(%r14,%r9,8), %r9d
movslq %r11d, %r10
movl 0x8(%r14,%r10,8), %eax
cmpl %eax, %r9d
sbbl $-0x1, %r10d
cmpl %eax, %r9d
sbbl %edx, %r15d
cmpl %eax, %r9d
jae 0xa7c4
movl %ebx, %r11d
movslq %r12d, %rax
movslq %r11d, %rdx
movl 0x8(%r14,%rdx,8), %r9d
addl 0x8(%r14,%rax,8), %r9d
movl %r9d, 0x10(%r14,%rdi,8)
incq %rdi
movw %di, 0xc(%r14,%rdx,8)
movw %di, 0xc(%r14,%rax,8)
cmpq %rsi, %rdi
jl 0xa76f
movslq %ecx, %rax
movb $0x0, 0xf(%r14,%rax,8)
movq 0x8(%rsp), %r9
cmpq $0x2, %r9
movq 0x50(%rsp), %rsi
jl 0xa835
leaq (%rsi,%r8), %rax
addq $0xff, %rax
movl %eax, %eax
leaq (%r14,%rax,8), %rdx
addq $0xf, %rdx
movzwl -0x3(%rdx), %eax
movb 0xf(%r14,%rax,8), %al
incb %al
movb %al, (%rdx)
decl %ecx
addq $-0x8, %rdx
cmpl $0x100, %ecx            # imm = 0x100
jg 0xa81a
addq %r8, %rsi
addq $0x2, %rsi
testq %r9, %r9
js 0xa85f
movl %esi, %eax
xorl %ecx, %ecx
movzwl 0xc(%r14,%rcx,8), %edx
movb 0xf(%r14,%rdx,8), %dl
incb %dl
movb %dl, 0xf(%r14,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0xa845
movl %r9d, %eax
movzbl 0xf(%r14,%rax,8), %edi
movl %edi, %eax
movl %edi, %ecx
subl %ebp, %eax
jbe 0xaab8
movl $0x1, %r10d
movl %eax, %ecx
shll %cl, %r10d
movq 0x48(%rsp), %rcx
movzbl 0x17(%rcx,%r8,8), %r11d
movq %r9, %r8
xorl %r9d, %r9d
cmpl %r11d, %ebp
jae 0xa8c6
xorl %edx, %edx
movq 0x8(%rsp), %r8
movl %edi, %ecx
subl %r11d, %ecx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
shll %cl, %r9d
addl %r10d, %edx
addl %edx, %r9d
movb %bpl, 0xf(%r14,%r8,8)
movzbl 0x7(%r14,%r8,8), %r11d
decq %r8
movl %r9d, %edx
cmpl %r11d, %ebp
jb 0xa89c
movslq %r8d, %r8
incq %r8
movzbl 0x7(%r14,%r8,8), %ecx
decq %r8
cmpl %ecx, %ebp
je 0xa8cc
movl %eax, %ecx
sarl %cl, %r9d
movabsq $-0xf0f0f0f0f0f0f10, %rax # imm = 0xF0F0F0F0F0F0F0F0
movq %rax, 0x40(%rsp)
movaps 0x977c(%rip), %xmm0     # 0x14070
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
testq %r8, %r8
js 0xa93c
movl %r8d, %eax
leaq (%r14,%rax,8), %rax
addq $0xf, %rax
movl %r8d, %ecx
movl %ebp, %edx
movzbl (%rax), %r10d
cmpl %r10d, %edx
jbe 0xa92d
movl %ebp, %edx
subl %r10d, %edx
movl %ecx, 0x10(%rsp,%rdx,4)
movl %r10d, %edx
leal -0x1(%rcx), %r10d
addq $-0x8, %rax
testl %ecx, %ecx
movl %r10d, %ecx
jg 0xa918
testl %r9d, %r9d
jle 0xaa37
movl $0xf0f0f0f0, %eax       # imm = 0xF0F0F0F0
movl $0xf0f0f0f0, %r10d      # imm = 0xF0F0F0F0
bsrl %r9d, %ecx
xorl $0x1f, %ecx
movl $0x20, %r15d
subl %ecx, %r15d
cmpl $0x1f, %ecx
je 0xa9ae
movl %r15d, %ecx
decq %rcx
movl 0x14(%rsp,%rcx,4), %edx
cmpq %r10, %rdx
je 0xa994
movl %ecx, %r11d
movl 0x10(%rsp,%r11,4), %r11d
cmpl $0xf0f0f0f0, %r11d      # imm = 0xF0F0F0F0
je 0xa9ae
movl 0x8(%r14,%r11,8), %r11d
addl %r11d, %r11d
cmpl %r11d, 0x8(%r14,%rdx,8)
jbe 0xa9ae
decl %r15d
movq %rcx, %rdx
decq %rdx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
movq %rdx, %rcx
jne 0xa96b
movl $0x1, %r15d
cmpl $0xc, %r15d
ja 0xa9d1
movl %r15d, %r15d
cmpl $0xf0f0f0f0, 0x10(%rsp,%r15,4) # imm = 0xF0F0F0F0
jne 0xa9d1
incq %r15
cmpq $0xd, %r15
jne 0xa9b7
movl $0xd, %r15d
leal -0x1(%r15), %ecx
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
shll %cl, %r11d
cmpl $0xf0f0f0f0, 0x10(%rsp,%rcx,4) # imm = 0xF0F0F0F0
movl %r15d, %r12d
jne 0xa9f4
movl 0x10(%rsp,%r12,4), %edx
movl %edx, 0x10(%rsp,%rcx,4)
movl 0x10(%rsp,%r12,4), %ecx
incb 0xf(%r14,%rcx,8)
testq %rcx, %rcx
je 0xaa1c
decl %ecx
movl %ecx, 0x10(%rsp,%r12,4)
movzbl 0xf(%r14,%rcx,8), %edx
movl %ebp, %ebx
subl %r15d, %ebx
cmpl %edx, %ebx
cmovnel %eax, %ecx
jmp 0xaa21
movl $0xf0f0f0f0, %ecx       # imm = 0xF0F0F0F0
addl %r9d, %r11d
movl %ecx, 0x10(%rsp,%r12,4)
movl %r11d, %r9d
testl %r11d, %r11d
jg 0xa950
jmp 0xaa3a
movl %r9d, %r11d
testl %r11d, %r11d
jns 0xaab1
movl 0x14(%rsp), %eax
testl %r11d, %r11d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
cmovnsl %r11d, %r9d
notl %r9d
addl %r11d, %r9d
xorl %ecx, %ecx
leal (%rax,%rcx), %edx
cmpl $0xf0f0f0f0, %edx       # imm = 0xF0F0F0F0
je 0xaa78
leal (%rcx,%rax), %edx
incl %edx
decb 0xf(%r14,%rdx,8)
incl %ecx
movl %r9d, %edx
addl %ecx, %edx
jne 0xaa58
jmp 0xaaab
movslq %r8d, %rax
leal (%r11,%rcx), %r9d
leaq 0x1(%rax), %r8
addl $0x2, %eax
movzbl 0x7(%r14,%r8,8), %edx
decq %r8
decl %eax
cmpl %edx, %ebp
je 0xaa86
movslq %eax, %rdx
decb 0xf(%r14,%rdx,8)
addl %ecx, %r11d
incl %r11d
cmpl $-0x2, %r9d
jle 0xaa43
jmp 0xaaad
addl %ecx, %eax
movl %eax, 0x14(%rsp)
movl %ebp, %ecx
movq 0x8(%rsp), %r9
xorps %xmm0, %xmm0
movups %xmm0, 0x1a(%rsp)
movaps %xmm0, 0x10(%rsp)
movups %xmm0, 0x6a(%rsp)
movaps %xmm0, 0x60(%rsp)
cmpl $0xc, %ecx
movq 0x58(%rsp), %r8
ja 0xab62
testq %r9, %r9
js 0xaaf9
movl %esi, %eax
xorl %edx, %edx
movzbl 0xf(%r14,%rdx,8), %esi
incw 0x10(%rsp,%rsi,2)
incq %rdx
cmpq %rdx, %rax
jne 0xaae6
testl %ecx, %ecx
je 0xab25
movl %ebp, %edx
movl %edi, %eax
cmpq %rax, %rdx
cmovbq %rdx, %rax
incq %rax
xorl %edx, %edx
movw %dx, 0x5e(%rsp,%rax,2)
addw 0xe(%rsp,%rax,2), %dx
movzwl %dx, %edx
shrl %edx
decq %rax
cmpq $0x1, %rax
jg 0xab0d
xorl %eax, %eax
movb 0xf(%r14,%rax,8), %dl
movzbl 0xe(%r14,%rax,8), %esi
movb %dl, 0x2(%r8,%rsi,4)
incq %rax
cmpq %rax, %r13
jne 0xab27
xorl %eax, %eax
movzbl 0x2(%r8,%rax,4), %edx
movzwl 0x60(%rsp,%rdx,2), %esi
movl %esi, %edi
incl %edi
movw %di, 0x60(%rsp,%rdx,2)
movw %si, (%r8,%rax,4)
incq %rax
cmpq %rax, %r13
jne 0xab41
cmpl $0xd, %ecx
movl %ecx, %ecx
movq $-0x1, %rax
cmovbq %rcx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq