// File: /workspace/github_c_makefile_star2_O3/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 $0xa8, %rsp
movq %r8, %r14
movq $-0x1, %rax
testb $0x3, %r14b
jne 0xba49
movq $-0x8, %rax
cmpq $0x1100, %r9            # imm = 0x1100
jb 0xba49
movl %edx, %r15d
testl %ecx, %ecx
movl $0xb, %eax
cmovnel %ecx, %eax
movl %eax, 0xc(%rsp)
movq $-0x6, %rax
cmpl $0xff, %edx
ja 0xba49
movq %rsi, %r12
movq %rdi, %r13
leal 0x1(%r15), %ebp
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, %rbp
jne 0xb3bc
movl 0x10f0(%r14), %eax
movl $0x21d, %ecx            # imm = 0x21D
addl (%r14,%rcx,8), %eax
movl %eax, (%r14,%rcx,8)
decq %rcx
cmpq $0x1ff, %rcx            # imm = 0x1FF
jne 0xb3e1
movl $0x200, %eax            # imm = 0x200
movl (%r14,%rax,8), %ecx
movl %ecx, 0x4(%r14,%rax,8)
incq %rax
cmpq $0x220, %rax            # imm = 0x220
jne 0xb3fa
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 0xb45b
leal -0x1(%rsi), %edi
cmpl 0x8(%r14,%rdi,8), %ecx
jbe 0xb45b
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 0xb437
jmp 0xb45d
movl %esi, %edi
movl %edi, %edx
movl %ecx, 0x8(%r14,%rdx,8)
movb %al, 0xe(%r14,%rdx,8)
incq %rax
cmpq %rbp, %rax
jne 0xb410
movabsq $-0x100000000, %r11     # imm = 0xFFFFFFFF00000000
movl %r15d, %ebx
movq %rbx, %rax
shlq $0x20, %rax
leaq (%r14,%rbx,8), %rdi
addq $0x17, %rdi
movabsq $0x100000000, %rdx      # imm = 0x100000000
addq %rax, %rdx
xorl %r12d, %r12d
movq %rdi, %r10
movl -0xf(%rdi,%r12,8), %ecx
decq %r12
addq $-0x8, %r10
addq %r11, %rdx
testl %ecx, %ecx
je 0xb4a0
leaq (%rbx,%r12), %r9
incq %r9
leaq (%rbx,%r12), %rax
addq $0x100, %rax            # imm = 0x100
addl -0xf(%rdi,%r12,8), %ecx
movl %ecx, 0x808(%r14)
movw $0x100, %cx             # imm = 0x100
movw %cx, -0xb(%rdi,%r12,8)
movw %cx, -0x3(%rdi,%r12,8)
cmpq $0x1, %r9
jg 0xb4f2
movl $0x80000000, (%r14)     # imm = 0x80000000
jmp 0xb63d
decl %r15d
addq %r12, %r15
leaq (%rbx,%r12), %rcx
addq $0xff, %rcx
movq %rcx, 0x78(%rsp)
movl $0x102, %ecx            # imm = 0x102
cmpl %ecx, %eax
movl $0x101, %r8d            # imm = 0x101
movl $0x101, %esi            # imm = 0x101
cmovgel %eax, %esi
addq $-0x101, %rsi           # imm = 0xFEFF
movq %rsi, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
cmpl %ecx, %eax
cmovgel %eax, %r8d
addq $-0xfd, %r8
andq $-0x4, %r8
xorl %ecx, %ecx
movdqa 0x7b87(%rip), %xmm1     # 0x130d0
movdqa 0x7b8f(%rip), %xmm2     # 0x130e0
movdqa 0x7b97(%rip), %xmm3     # 0x130f0
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movq %rcx, %xmm5
pshufd $0x44, %xmm5, %xmm5     # xmm5 = xmm5[0,1,0,1]
movdqa %xmm5, %xmm6
por %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %esi
notl %esi
testb $0x1, %sil
je 0xb5b0
movl $0x40000000, 0x810(%r14,%rcx,8) # imm = 0x40000000
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %esi
testb $0x1, %sil
je 0xb5cb
movl $0x40000000, 0x818(%r14,%rcx,8) # imm = 0x40000000
por %xmm1, %xmm5
pxor %xmm3, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
pxor %xmm4, %xmm5
pextrw $0x0, %xmm5, %esi
testb $0x1, %sil
je 0xb60c
movl $0x40000000, 0x820(%r14,%rcx,8) # imm = 0x40000000
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0xb623
movl $0x40000000, 0x828(%r14,%rcx,8) # imm = 0x40000000
addq $0x4, %rcx
cmpq %rcx, %r8
jne 0xb561
movl $0x80000000, (%r14)     # imm = 0x80000000
cmpq $0x1, %r9
jg 0xb64a
cltq
movb $0x0, 0xf(%r14,%rax,8)
jmp 0xb73f
movq %rbx, 0x60(%rsp)
movq %r9, 0x18(%rsp)
movq %rbp, 0x68(%rsp)
movq %r13, 0x70(%rsp)
movslq %eax, %rcx
movq %rcx, 0x10(%rsp)
movl $0x100, %esi            # imm = 0x100
movl $0x100, %r8d            # imm = 0x100
movslq %r15d, %r9
movl 0x8(%r14,%r9,8), %ebp
movslq %esi, %r11
movl 0x8(%r14,%r11,8), %ecx
cmpl %ecx, %ebp
sbbl $-0x1, %r11d
xorl %ebx, %ebx
movl %esi, %r9d
cmpl %ecx, %ebp
setb %sil
jae 0xb697
movl %r15d, %r9d
movb %sil, %bl
movl %r15d, %ebp
subl %ebx, %ebp
movslq %ebp, %rcx
movl 0x8(%r14,%rcx,8), %ecx
movslq %r11d, %rsi
movl 0x8(%r14,%rsi,8), %r13d
cmpl %r13d, %ecx
sbbl $-0x1, %esi
cmpl %r13d, %ecx
sbbl %ebx, %r15d
cmpl %r13d, %ecx
jae 0xb6c3
movl %ebp, %r11d
movslq %r9d, %rcx
movslq %r11d, %r9
movl 0x8(%r14,%r9,8), %r11d
addl 0x8(%r14,%rcx,8), %r11d
movl %r11d, 0x10(%r14,%r8,8)
incq %r8
movw %r8w, 0xc(%r14,%r9,8)
movw %r8w, 0xc(%r14,%rcx,8)
cmpq 0x10(%rsp), %r8
jl 0xb671
movq 0x10(%rsp), %rcx
movb $0x0, 0xf(%r14,%rcx,8)
movl 0x78(%rsp), %ecx
leaq (%r14,%rcx,8), %rcx
addq $0xf, %rcx
movq 0x70(%rsp), %r13
movq 0x68(%rsp), %rbp
movq 0x18(%rsp), %r9
movabsq $-0x100000000, %r11     # imm = 0xFFFFFFFF00000000
movq 0x60(%rsp), %rbx
movzwl -0x3(%rcx), %esi
movb 0xf(%r14,%rsi,8), %sil
incb %sil
movb %sil, (%rcx)
decl %eax
addq $-0x8, %rcx
cmpl $0x100, %eax            # imm = 0x100
jg 0xb723
leaq (%rbx,%r12), %r15
addq $0x2, %r15
testq %r9, %r9
js 0xb76c
movl %r15d, %eax
xorl %ecx, %ecx
movzwl 0xc(%r14,%rcx,8), %esi
movb 0xf(%r14,%rsi,8), %sil
incb %sil
movb %sil, 0xf(%r14,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0xb751
movl %r9d, %eax
movzbl 0xf(%r14,%rax,8), %r8d
movl %r8d, %eax
movl %r8d, %ecx
subl 0xc(%rsp), %eax
jbe 0xb97c
movq %r15, 0x10(%rsp)
movl $0x1, %r15d
movl %eax, %ecx
shll %cl, %r15d
movzbl (%rdi,%r12,8), %esi
cmpl %esi, 0xc(%rsp)
movq %r9, 0x18(%rsp)
jae 0xb7eb
movq %rbx, %rcx
movq %r11, %rbx
leal (%rcx,%r12), %r11d
incl %r11d
xorl %edi, %edi
movl 0xc(%rsp), %r12d
movl %edi, %r9d
movl %r8d, %ecx
subl %esi, %ecx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
shll %cl, %edi
addl %r15d, %r9d
addl %r9d, %edi
movb %r12b, (%r10)
movzbl -0x8(%r10), %esi
addq $-0x8, %r10
addq %rbx, %rdx
decl %r11d
cmpl %esi, %r12d
jb 0xb7b9
sarq $0x20, %rdx
jmp 0xb7f8
xorl %edi, %edi
movq %r9, %rdx
movl %r9d, %r11d
movl 0xc(%rsp), %r12d
incl %r11d
incq %rdx
movq 0x10(%rsp), %r15
movzbl 0x7(%r14,%rdx,8), %ecx
decl %r11d
decq %rdx
cmpl %ecx, %r12d
je 0xb803
movl %eax, %ecx
sarl %cl, %edi
movabsq $-0xf0f0f0f0f0f0f10, %rax # imm = 0xF0F0F0F0F0F0F0F0
movq %rax, 0x50(%rsp)
movdqa 0x9961(%rip), %xmm0     # 0x15190
movdqa %xmm0, 0x40(%rsp)
movdqa %xmm0, 0x30(%rsp)
movdqa %xmm0, 0x20(%rsp)
testq %rdx, %rdx
js 0xb87e
movl %r11d, %eax
leaq (%r14,%rax,8), %rcx
addq $0xf, %rcx
movl %r12d, %esi
movzbl (%rcx), %r10d
cmpl %r10d, %esi
jbe 0xb86f
movl %r12d, %esi
subl %r10d, %esi
movl %eax, 0x20(%rsp,%rsi,4)
movl %eax, %r9d
movl %r10d, %esi
jmp 0xb872
movl %eax, %r9d
decq %rax
addq $-0x8, %rcx
testl %r9d, %r9d
jg 0xb854
testl %edi, %edi
jle 0xb968
movl $0xf0f0f0f0, %eax       # imm = 0xF0F0F0F0
movl $0xf0f0f0f0, %r9d       # imm = 0xF0F0F0F0
bsrl %edi, %ecx
xorl $0x1f, %ecx
cmpl $0x1f, %ecx
je 0xb8de
movl $0x20, %r11d
subl %ecx, %r11d
movl 0x20(%rsp,%r11,4), %esi
leaq -0x1(%r11), %rcx
cmpq %r9, %rsi
je 0xb8d3
movl %ecx, %r10d
movl 0x20(%rsp,%r10,4), %r10d
cmpl $0xf0f0f0f0, %r10d      # imm = 0xF0F0F0F0
je 0xb900
movl 0x8(%r14,%r10,8), %r10d
addl %r10d, %r10d
cmpl %r10d, 0x8(%r14,%rsi,8)
jbe 0xb900
movq %rcx, %r11
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
jne 0xb8a5
movl $0x1, %r11d
cmpl $0xf0f0f0f0, 0x20(%rsp,%r11,4) # imm = 0xF0F0F0F0
jne 0xb906
incq %r11
cmpq $0xd, %r11
jne 0xb8e4
movl $0xd, %r11d
jmp 0xb906
cmpq $0xc, %r11
jbe 0xb8e4
leal -0x1(%r11), %ecx
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
shll %cl, %r10d
movl 0x20(%rsp,%rcx,4), %ebx
cmpl $0xf0f0f0f0, %ebx       # imm = 0xF0F0F0F0
movl %r11d, %esi
movl 0x20(%rsp,%rsi,4), %r11d
cmovel %r11d, %ebx
movl %ebx, 0x20(%rsp,%rcx,4)
incb 0xf(%r14,%r11,8)
testq %r11, %r11
je 0xb94e
leal -0x1(%r11), %ecx
movzbl 0xf(%r14,%rcx,8), %r11d
movl %r12d, %ebx
subl %esi, %ebx
cmpl %r11d, %ebx
cmovnel %eax, %ecx
jmp 0xb953
movl $0xf0f0f0f0, %ecx       # imm = 0xF0F0F0F0
addl %edi, %r10d
movl %ecx, 0x20(%rsp,%rsi,4)
movl %r10d, %edi
testl %r10d, %r10d
jg 0xb891
jmp 0xb96b
movl %edi, %r10d
testl %r10d, %r10d
movq 0x18(%rsp), %r9
js 0xba5b
movl %r12d, %ecx
pxor %xmm0, %xmm0
movdqu %xmm0, 0x2a(%rsp)
movdqa %xmm0, 0x20(%rsp)
movdqu %xmm0, 0x8a(%rsp)
movdqa %xmm0, 0x80(%rsp)
cmpl $0xc, %ecx
ja 0xba39
testq %r9, %r9
js 0xb9c4
movl %r15d, %eax
xorl %edx, %edx
movzbl 0xf(%r14,%rdx,8), %esi
incw 0x20(%rsp,%rsi,2)
incq %rdx
cmpq %rdx, %rax
jne 0xb9b1
testl %ecx, %ecx
movl 0xc(%rsp), %eax
je 0xb9f5
movl %eax, %edx
movl %r8d, %eax
cmpq %rax, %rdx
cmovbq %rdx, %rax
incq %rax
xorl %edx, %edx
movw %dx, 0x7e(%rsp,%rax,2)
addw 0x1e(%rsp,%rax,2), %dx
movzwl %dx, %edx
shrl %edx
decq %rax
cmpq $0x1, %rax
ja 0xb9dd
xorl %eax, %eax
movb 0xf(%r14,%rax,8), %dl
movzbl 0xe(%r14,%rax,8), %esi
movb %dl, 0x2(%r13,%rsi,4)
incq %rax
cmpq %rax, %rbp
jne 0xb9f7
xorl %eax, %eax
movzbl 0x2(%r13,%rax,4), %edx
movzwl 0x80(%rsp,%rdx,2), %esi
movl %esi, %edi
incl %edi
movw %di, 0x80(%rsp,%rdx,2)
movw %si, (%r13,%rax,4)
incq %rax
cmpq %rax, %rbp
jne 0xba11
cmpl $0xd, %ecx
movl %ecx, %ecx
movq $-0x1, %rax
cmovbq %rcx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x24(%rsp), %eax
leal 0x1(%rax), %esi
addl $0xf0f0f10, %eax        # imm = 0xF0F0F10
xorl %ecx, %ecx
cmpl %ecx, %eax
je 0xba82
movl %esi, %edi
decb 0xf(%r14,%rdi,8)
incl %esi
decl %ecx
cmpl %ecx, %r10d
jne 0xba69
jmp 0xb979
movslq %edx, %rax
subl %ecx, %r10d
leaq 0x1(%rax), %rdx
addl $0x2, %eax
movzbl 0x7(%r14,%rdx,8), %ecx
decq %rdx
decl %eax
cmpl %ecx, %r12d
je 0xba8f
movslq %eax, %rcx
decb 0xf(%r14,%rcx,8)
leal 0x1(%r10), %ecx
cmpl $-0x2, %r10d
movl %ecx, %r10d
jle 0xba5f
jmp 0xb979