// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x48(%rbp)
movl $0x100, -0x58(%rbp)     # imm = 0x100
movq -0x28(%rbp), %rax
andq $0x3, %rax
cmpq $0x0, %rax
je 0x155ef
movq $-0x1, -0x8(%rbp)
jmp 0x15a14
cmpq $0x1100, -0x30(%rbp)    # imm = 0x1100
jae 0x15606
movq $-0x8, -0x8(%rbp)
jmp 0x15a14
cmpl $0x0, -0x20(%rbp)
jne 0x15613
movl $0xb, -0x20(%rbp)
cmpl $0xff, -0x1c(%rbp)
jbe 0x15629
movq $-0x6, -0x8(%rbp)
jmp 0x15a14
movq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x10c0
movq -0x48(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x38(%rbp), %rcx
addq $0x1000, %rcx           # imm = 0x1000
callq 0x15a30
movl -0x1c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movslq -0x4c(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,8)
jne 0x15673
movl -0x4c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1565a
movl -0x4c(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x58(%rbp), %eax
addl -0x50(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x60(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x48(%rbp), %rax
movslq -0x50(%rbp), %rcx
movl (%rax,%rcx,8), %edx
movq -0x48(%rbp), %rax
movl -0x50(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
addl (%rax,%rcx,8), %edx
movq -0x48(%rbp), %rax
movslq -0x58(%rbp), %rcx
movl %edx, (%rax,%rcx,8)
movl -0x58(%rbp), %eax
movw %ax, %dx
movq -0x48(%rbp), %rax
movl -0x50(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movw %dx, 0x4(%rax,%rcx,8)
movq -0x48(%rbp), %rax
movslq -0x50(%rbp), %rcx
movw %dx, 0x4(%rax,%rcx,8)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movl -0x50(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x50(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x60(%rbp), %eax
jg 0x15710
movq -0x48(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movl $0x40000000, (%rax,%rcx,8) # imm = 0x40000000
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x156ee
movq -0x40(%rbp), %rax
movl $0x80000000, (%rax)     # imm = 0x80000000
movl -0x58(%rbp), %eax
cmpl -0x60(%rbp), %eax
jg 0x157ff
movq -0x48(%rbp), %rax
movslq -0x50(%rbp), %rcx
movl (%rax,%rcx,8), %eax
movq -0x48(%rbp), %rcx
movslq -0x54(%rbp), %rdx
cmpl (%rcx,%rdx,8), %eax
jae 0x15751
movl -0x50(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x50(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x15762
movl -0x54(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x54(%rbp)
movl %eax, -0xbc(%rbp)
movl -0xbc(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x48(%rbp), %rax
movslq -0x50(%rbp), %rcx
movl (%rax,%rcx,8), %eax
movq -0x48(%rbp), %rcx
movslq -0x54(%rbp), %rdx
cmpl (%rcx,%rdx,8), %eax
jae 0x15796
movl -0x50(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x50(%rbp)
movl %eax, -0xc0(%rbp)
jmp 0x157a7
movl -0x54(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x54(%rbp)
movl %eax, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x48(%rbp), %rax
movslq -0x64(%rbp), %rcx
movl (%rax,%rcx,8), %edx
movq -0x48(%rbp), %rax
movslq -0x68(%rbp), %rcx
addl (%rax,%rcx,8), %edx
movq -0x48(%rbp), %rax
movslq -0x58(%rbp), %rcx
movl %edx, (%rax,%rcx,8)
movl -0x58(%rbp), %eax
movw %ax, %dx
movq -0x48(%rbp), %rax
movslq -0x68(%rbp), %rcx
movw %dx, 0x4(%rax,%rcx,8)
movq -0x48(%rbp), %rax
movslq -0x64(%rbp), %rcx
movw %dx, 0x4(%rax,%rcx,8)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x1571a
movq -0x48(%rbp), %rax
movslq -0x60(%rbp), %rcx
movb $0x0, 0x7(%rax,%rcx,8)
movl -0x60(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x5c(%rbp)
cmpl $0x100, -0x5c(%rbp)     # imm = 0x100
jl 0x15850
movq -0x48(%rbp), %rax
movq -0x48(%rbp), %rcx
movslq -0x5c(%rbp), %rdx
movzwl 0x4(%rcx,%rdx,8), %ecx
movzbl 0x7(%rax,%rcx,8), %eax
addl $0x1, %eax
movb %al, %dl
movq -0x48(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movb %dl, 0x7(%rax,%rcx,8)
movl -0x5c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x15815
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jg 0x15891
movq -0x48(%rbp), %rax
movq -0x48(%rbp), %rcx
movslq -0x5c(%rbp), %rdx
movzwl 0x4(%rcx,%rdx,8), %ecx
movzbl 0x7(%rax,%rcx,8), %eax
addl $0x1, %eax
movb %al, %dl
movq -0x48(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movb %dl, 0x7(%rax,%rcx,8)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x15857
movq -0x48(%rbp), %rdi
movl -0x4c(%rbp), %esi
movl -0x20(%rbp), %edx
callq 0x15bd0
movl %eax, -0x20(%rbp)
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x1a, %edx
callq 0x10c0
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
movl $0x1a, %edx
callq 0x10c0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
cmpl $0xc, -0x20(%rbp)
jbe 0x158e8
movq $-0x1, -0x8(%rbp)
jmp 0x15a14
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jg 0x15923
movq -0x48(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movzbl 0x7(%rax,%rcx,8), %eax
movw -0x90(%rbp,%rax,2), %cx
addw $0x1, %cx
movw %cx, -0x90(%rbp,%rax,2)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x158ef
movw $0x0, -0xb6(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jle 0x15982
movw -0xb6(%rbp), %cx
movslq -0x5c(%rbp), %rax
movw %cx, -0xb0(%rbp,%rax,2)
movslq -0x5c(%rbp), %rax
movzwl -0x90(%rbp,%rax,2), %ecx
movzwl -0xb6(%rbp), %eax
addl %ecx, %eax
movw %ax, -0xb6(%rbp)
movzwl -0xb6(%rbp), %eax
sarl %eax
movw %ax, -0xb6(%rbp)
movl -0x5c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x15932
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0xb4(%rbp), %eax
jge 0x159c0
movq -0x48(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movb 0x7(%rax,%rcx,8), %dl
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movslq -0x5c(%rbp), %rsi
movzbl 0x6(%rcx,%rsi,8), %ecx
movb %dl, 0x2(%rax,%rcx,4)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x15989
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0xb4(%rbp), %eax
jge 0x15a0d
movq -0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movzbl 0x2(%rax,%rcx,4), %eax
movw -0xb0(%rbp,%rax,2), %dx
movw %dx, %cx
addw $0x1, %cx
movw %cx, -0xb0(%rbp,%rax,2)
movq -0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movw %dx, (%rax,%rcx,4)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x159c7
movl -0x20(%rbp), %eax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)