// File: /workspace/github_c_makefile_star2_O2/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
testb $0x3, %r14b
je 0x93b6
pushq $-0x1
jmp 0x93d7
cmpq $0x1100, %r9            # imm = 0x1100
jae 0x93c3
pushq $-0x8
jmp 0x93d7
movl %edx, %ebx
testl %ecx, %ecx
pushq $0xb
popq %rbp
cmovnel %ecx, %ebp
cmpl $0xff, %edx
jbe 0x93ea
pushq $-0x6
popq %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %r12
movq %rdi, 0x58(%rsp)
leal 0x1(%rbx), %r13d
xorl %r15d, %r15d
movl $0x1100, %edx           # imm = 0x1100
movq %r14, %rdi
xorl %esi, %esi
callq 0x10c0
cmpq %r15, %r13
je 0x9423
movl (%r12,%r15,4), %eax
incl %eax
bsrl %eax, %eax
incl 0x1000(%r14,%rax,8)
incq %r15
jmp 0x9408
movl $0x21e, %eax            # imm = 0x21E
cmpq $0x200, %rax            # imm = 0x200
je 0x9447
movl (%r14,%rax,8), %ecx
leal -0x201(%rax), %edx
addl %ecx, 0x1000(%r14,%rdx,8)
decq %rax
jmp 0x9428
movl $0x200, %eax            # imm = 0x200
cmpq $0x220, %rax            # imm = 0x220
je 0x9462
movl (%r14,%rax,8), %ecx
movl %ecx, 0x4(%r14,%rax,8)
incq %rax
jmp 0x944c
xorl %eax, %eax
cmpq %r13, %rax
je 0x94b6
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)
movl %esi, %edi
cmpl 0x1008(%r14,%rdx,8), %esi
jbe 0x94a7
decl %esi
cmpl 0x8(%r14,%rsi,8), %ecx
jbe 0x94a7
movq 0x8(%r14,%rsi,8), %r8
movq %r8, 0x8(%r14,%rdi,8)
jmp 0x9486
movl %ecx, 0x8(%r14,%rdi,8)
movb %al, 0xe(%r14,%rdi,8)
incq %rax
jmp 0x9464
movabsq $-0x100000000, %rdi     # imm = 0xFFFFFFFF00000000
movl %ebx, %r9d
leaq (,%r9,8), %rax
leaq (%r14,%r9,8), %r8
addq $0x17, %r8
movq %r9, 0x8(%rsp)
shlq $0x20, %r9
movabsq $0x10000000000, %rcx    # imm = 0x10000000000
addq %r9, %rcx
movabsq $0x100000000, %rsi      # imm = 0x100000000
addq %r9, %rsi
xorl %r9d, %r9d
movq %r8, %r10
movl -0xf(%r8,%r9,8), %r11d
addq $-0x8, %r10
decq %r9
addq %rdi, %rcx
addq %rdi, %rsi
testl %r11d, %r11d
je 0x94fc
addq %r14, %rax
addl 0x8(%rax,%r9,8), %r11d
movl %r11d, 0x808(%r14)
movw $0x100, %di             # imm = 0x100
movw %di, 0xc(%rax,%r9,8)
movw %di, 0x14(%rax,%r9,8)
sarq $0x20, %rcx
movq 0x8(%rsp), %rax
leaq (%rax,%r9), %rdi
addq $0x2, %rdi
leaq 0xff(%rax,%r9), %rdx
movq %rdx, 0x48(%rsp)
addq %r9, %rax
incq %rax
movq %rax, 0x50(%rsp)
movl $0x101, %eax            # imm = 0x101
cmpq %rcx, %rax
jg 0x9573
movl $0x40000000, 0x8(%r14,%rax,8) # imm = 0x40000000
incq %rax
jmp 0x9560
leal (%rbx,%r9), %r15d
decl %r15d
movl $0x80000000, (%r14)     # imm = 0x80000000
movl $0x100, %r8d            # imm = 0x100
movl $0x101, %r11d           # imm = 0x101
cmpq %rcx, %r11
jg 0x960b
movslq %r15d, %rax
movl 0x8(%r14,%rax,8), %r9d
movslq %r8d, %r12
movl 0x8(%r14,%r12,8), %r13d
cmpl %r13d, %r9d
sbbl $-0x1, %r12d
xorl %eax, %eax
cmpl %r13d, %r9d
setb %r9b
jae 0x95b7
movl %r15d, %r8d
movb %r9b, %al
movl %r15d, %r9d
subl %eax, %r9d
movslq %r9d, %r13
movl 0x8(%r14,%r13,8), %edx
movslq %r12d, %r13
movl 0x8(%r14,%r13,8), %ebx
cmpl %ebx, %edx
sbbl $-0x1, %r13d
cmpl %ebx, %edx
sbbl %eax, %r15d
cmpl %ebx, %edx
jae 0x95e2
movl %r9d, %r12d
movslq %r8d, %rax
movslq %r12d, %rdx
movl 0x8(%r14,%rdx,8), %r8d
addl 0x8(%r14,%rax,8), %r8d
movl %r8d, 0x8(%r14,%r11,8)
movw %r11w, 0xc(%r14,%rdx,8)
movw %r11w, 0xc(%r14,%rax,8)
incq %r11
movl %r13d, %r8d
jmp 0x958d
movb $0x0, 0xf(%r14,%rcx,8)
movl 0x48(%rsp), %eax
cmpl $0xff, %eax
jle 0x9633
movzwl 0xc(%r14,%rax,8), %ecx
movb 0xf(%r14,%rcx,8), %cl
incb %cl
movb %cl, 0xf(%r14,%rax,8)
decq %rax
jmp 0x9615
sarq $0x20, %rsi
xorl %eax, %eax
cmpq %rsi, %rax
jg 0x9655
movzwl 0xc(%r14,%rax,8), %ecx
movb 0xf(%r14,%rcx,8), %cl
incb %cl
movb %cl, 0xf(%r14,%rax,8)
incq %rax
jmp 0x9639
movl 0x50(%rsp), %eax
movzbl 0xf(%r14,%rax,8), %r8d
movl %r8d, %eax
movl %r8d, %ecx
subl %ebp, %eax
jbe 0x9832
pushq $0x1
popq %r12
movl %eax, %ecx
shll %cl, %r12d
xorl %r9d, %r9d
pushq $-0x1
popq %r11
movzbl (%r10), %r15d
cmpl %r15d, %ebp
jae 0x96a3
movl %r8d, %ecx
subl %r15d, %ecx
movl %r11d, %edx
shll %cl, %edx
addl %r12d, %r9d
addl %edx, %r9d
movb %bpl, (%r10)
decq %rdi
addq $-0x8, %r10
jmp 0x967d
movzbl (%r10), %ecx
decq %rdi
addq $-0x8, %r10
cmpl %ecx, %ebp
je 0x96a3
movabsq $-0xf0f0f0f0f0f0f10, %rcx # imm = 0xF0F0F0F0F0F0F0F0
movq %rcx, 0x40(%rsp)
movaps 0x7a18(%rip), %xmm0     # 0x110e0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movl %ebp, %r10d
movl %edi, %ecx
testl %ecx, %ecx
js 0x96fd
movl %ecx, %edx
movzbl 0xf(%r14,%rdx,8), %r15d
cmpl %r15d, %r10d
jbe 0x96f9
movl %ebp, %edx
subl %r15d, %edx
movl %ecx, 0x10(%rsp,%rdx,4)
movl %r15d, %r10d
decl %ecx
jmp 0x96dc
movl %eax, %ecx
sarl %cl, %r9d
movl $0xf0f0f0f0, %eax       # imm = 0xF0F0F0F0
pushq $0xd
popq %r10
testl %r9d, %r9d
jle 0x97da
bsrl %r9d, %r13d
xorl $-0x20, %r13d
addl $0x21, %r13d
cmpq $0x2, %r13
jb 0x9755
movl 0x10(%rsp,%r13,4), %ecx
cmpq %rax, %rcx
je 0x9750
leal -0x1(%r13), %edx
movl 0x10(%rsp,%rdx,4), %r15d
cmpl $0xf0f0f0f0, %r15d      # imm = 0xF0F0F0F0
je 0x975b
movl 0x8(%r14,%r15,8), %edx
addl %edx, %edx
cmpl %edx, 0x8(%r14,%rcx,8)
jbe 0x975b
decq %r13
jmp 0x9720
pushq $0x1
popq %rcx
movl %ecx, %r13d
movl %r13d, %ecx
cmpl $0xe, %r13d
cmovbl %r10d, %r13d
cmpq $0xc, %rcx
ja 0x977e
cmpl $0xf0f0f0f0, 0x10(%rsp,%rcx,4) # imm = 0xF0F0F0F0
jne 0x977b
incq %rcx
jmp 0x9766
movl %ecx, %r13d
leal -0x1(%r13), %ecx
movl %r11d, %r15d
shll %cl, %r15d
movl 0x10(%rsp,%rcx,4), %edx
cmpl $0xf0f0f0f0, %edx       # imm = 0xF0F0F0F0
movl %r13d, %r13d
movl 0x10(%rsp,%r13,4), %r12d
cmovel %r12d, %edx
movl %edx, 0x10(%rsp,%rcx,4)
incb 0xf(%r14,%r12,8)
testq %r12, %r12
je 0x97c8
leal -0x1(%r12), %ecx
movzbl 0xf(%r14,%rcx,8), %edx
movl %ebp, %ebx
subl %r13d, %ebx
cmpl %edx, %ebx
movl $0xf0f0f0f0, %edx       # imm = 0xF0F0F0F0
cmovnel %edx, %ecx
jmp 0x97cd
movl $0xf0f0f0f0, %ecx       # imm = 0xF0F0F0F0
addl %r15d, %r9d
movl %ecx, 0x10(%rsp,%r13,4)
jmp 0x970b
movl 0x14(%rsp), %eax
leal 0x1(%rax), %r10d
addl $0xf0f0f10, %eax        # imm = 0xF0F0F10
xorl %ecx, %ecx
movl %r9d, %r11d
testl %r11d, %r11d
jns 0x9830
cmpl %ecx, %eax
je 0x9807
movl %r10d, %edx
decb 0xf(%r14,%rdx,8)
incl %r11d
incl %r10d
decl %ecx
jmp 0x97ec
movslq %edi, %rax
leaq 0x1(%rax), %rdi
addl $0x2, %eax
movzbl 0x7(%r14,%rdi,8), %edx
decq %rdi
decl %eax
cmpl %edx, %ebp
je 0x9811
movslq %eax, %rdx
decb 0xf(%r14,%rdx,8)
subl %ecx, %r9d
incl %r9d
jmp 0x97de
movl %ebp, %ecx
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), %r9
movq 0x8(%rsp), %r10
ja 0x98d7
xorl %eax, %eax
cmpq %rsi, %rax
jg 0x986f
movzbl 0xf(%r14,%rax,8), %edx
incw 0x10(%rsp,%rdx,2)
incq %rax
jmp 0x985a
movl %ebp, %edx
movl %r8d, %eax
cmpq %rax, %rdx
cmovbq %rdx, %rax
xorl %esi, %esi
testq %rax, %rax
jle 0x9896
movw %si, 0x60(%rsp,%rax,2)
addw 0x10(%rsp,%rax,2), %si
movzwl %si, %esi
shrl %esi
decq %rax
jmp 0x987d
xorl %eax, %eax
cmpq %r10, %rax
ja 0x98b2
movb 0xf(%r14,%rax,8), %dl
movzbl 0xe(%r14,%rax,8), %esi
movb %dl, 0x2(%r9,%rsi,4)
incq %rax
jmp 0x9898
xorl %eax, %eax
cmpq %r10, %rax
ja 0x98d7
movzbl 0x2(%r9,%rax,4), %edx
movzwl 0x60(%rsp,%rdx,2), %esi
movl %esi, %edi
incl %edi
movw %di, 0x60(%rsp,%rdx,2)
movw %si, (%r9,%rax,4)
incq %rax
jmp 0x98b4
cmpl $0xd, %ecx
movl %ecx, %ecx
pushq $-0x1
popq %rax
cmovbq %rcx, %rax
jmp 0x93d8