// File: /workspace/github_c_makefile_star2_O3/snej[P]zdelta/inftrees.c
// Function: huft_build
local int huft_build(b, n, s, d, e, t, m, hp, hn, v)
uIntf *b;               /* code lengths in bits (all assumed <= BMAX) */
uInt n;                 /* number of codes (assumed <= 288) */
uInt s;                 /* number of simple-valued codes (0..s-1) */
const uIntf *d;         /* list of base values for non-simple codes */
const uIntf *e;         /* list of extra bits for non-simple codes */
inflate_huft * FAR *t;  /* result: starting table */
uIntf *m;               /* maximum lookup bits, returns actual */
inflate_huft *hp;       /* space for trees */
uInt *hn;               /* hufts used in space */
uIntf *v;               /* working area: values in order of bit length */
/* Given a list of code lengths and a maximum table size, make a set of
   tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
   if the given code set is incomplete (the tables are still built in this
   case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
   lengths), or Z_MEM_ERROR if not enough memory. */
{

  uInt a;                       /* counter for codes of length k */
  uInt c[BMAX+1];               /* bit length count table */
  uInt f;                       /* i repeats in table every f entries */
  int g;                        /* maximum code length */
  int h;                        /* table level */
  register uInt i;              /* counter, current code */
  register uInt j;              /* counter */
  register int k;               /* number of bits in current code */
  int l;                        /* bits per table (returned in m) */
  uInt mask;                    /* (1 << w) - 1, to avoid cc -O bug on HP */
  register uIntf *p;            /* pointer into c[], b[], or v[] */
  inflate_huft *q;              /* points to current table */
  struct inflate_huft_s r;      /* table entry for structure assignment */
   inflate_huft *u[BMAX];        /* table stack */
  register int w;               /* bits before this table == (l * h) */
  uInt x[BMAX+1];               /* bit offsets, then code stack */
  uIntf *xp;                    /* pointer into x */
  int y;                        /* number of dummy codes added */
  uInt z;                       /* number of entries in current table */


  /* Generate counts for each bit length */
  p = c;
#define C0 *p++ = 0;
#define C2 C0 C0 C0 C0
#define C4 C2 C2 C2 C2
  C4                            /* clear c[]--assume BMAX+1 is 16 */
  p = b;  i = n;
  do {
    c[*p++]++;                  /* assume all entries <= BMAX */
  } while (--i);
  if (c[0] == n)                /* null input--all zero length codes */
  {
    *t = (inflate_huft *)ZD_NULL;
    *m = 0;
    return ZD_OK;
  }


  /* Find minimum and maximum length, bound *m by those */
  l = *m;
  for (j = 1; j <= BMAX; j++)
    if (c[j])
      break;
  k = j;                        /* minimum code length */
  if ((uInt)l < j)
    l = j;
  for (i = BMAX; i; i--) 
    if (c[i])
      break;
  g = i;                        /* maximum code length */
  if ((uInt)l > i)
    l = i;
  *m = l;


  /* Adjust last length count to fill out codes, if needed */
  for (y = 1 << j; j < i; j++, y <<= 1)
    if ((y -= c[j]) < 0)
      return ZD_DATA_ERROR;
  if ((y -= c[i]) < 0)
    return ZD_DATA_ERROR;
  c[i] += y;


  /* Generate starting offsets into the value table for each length */
  x[1] = j = 0;
  p = c + 1;  xp = x + 2;
  while (--i) {                 /* note that i == g from above */
    *xp++ = (j += *p++);
  }


  /* Make a table of values in order of bit lengths */
  p = b;  i = 0;
  do {
    if ((j = *p++) != 0)
      v[x[j]++] = i;
  } while (++i < n);
  n = x[g];                     /* set n to length of v */


  /* Generate the Huffman codes and for each, make the table entries */
  x[0] = i = 0;                 /* first Huffman code is zero */
  p = v;                        /* grab values in bit order */
  h = -1;                       /* no tables yet--level -1 */
  w = -l;                       /* bits decoded == (l * h) */
  u[0] = (inflate_huft *)ZD_NULL;        /* just to keep compilers happy */
  q = (inflate_huft *)ZD_NULL;   /* ditto */
  z = 0;                        /* ditto */

  /* go through the bit lengths (k already is bits in shortest code) */
  for (; k <= g; k++)
  {
    a = c[k];
    while (a--)
    {
      /* here i is the Huffman code of length k bits for value *p */
      /* make tables up to required level */
      while (k > w + l)
      {
        h++;
        w += l;                 /* previous table always l bits */

        /* compute minimum size table less than or equal to l bits */
        z = g - w;
        z = z > (uInt)l ? (uInt) l : z;         /* table size upper limit */
        if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
        {                       /* too few codes for k-w bit table */
          f -= a + 1;           /* deduct codes from patterns left */
          xp = c + k;
          if (j < z)
            while (++j < z)     /* try smaller tables up to z bits */
            {
              if ((f <<= 1) <= *++xp)
                break;          /* enough codes to use up j bits */
              f -= *xp;         /* else deduct codes from patterns */
            }
        }
        z = 1 << j;             /* table entries for j-bit table */

        /* allocate new table */
        if (*hn + z > MANY)     /* (note: doesn't matter for fixed) */
          return ZD_MEM_ERROR;   /* not enough memory */
        u[h] = q = hp + *hn;
        *hn += z;

        /* connect to last table, if there is one */
        if (h)
        {
          x[h] = i;             /* save pattern for backing up */
          r.bits = (Byte)l;     /* bits to dump before this table */
          r.exop = (Byte)j;     /* bits in this table */
          j = i >> (w - l);
          r.base = (uInt)(q - u[h-1] - j);   /* offset to this table */
          u[h-1][j] = r;        /* connect to last table */
        }
        else
          *t = q;               /* first table is returned result */
      }

      /* set up table entry in r */
      r.bits = (Byte)(k - w);
      if (p >= v + n)
        r.exop = 128 + 64;      /* out of values--invalid code */
      else if (*p < s)
      {
        r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
        r.base = *p++;          /* simple code is just the value */
      }
      else
      {
        r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
        r.base = d[*p++ - s];
      }

      /* fill code-like entries with r */
      f = 1 << (k - w);
      for (j = i >> w; j < z; j += f)
        q[j] = r;

      /* backwards increment the k-bit code i */
      for (j = 1 << (k - 1); i & j; j >>= 1)
        i ^= j;
      i ^= j;

      /* backup over finished tables */
      mask = (1 << w) - 1;      /* needed on HP, cc -O bug */
      while ((i & mask) != x[h])
      {
        h--;                    /* don't need to update q */
        w -= l;
        mask = (1 << w) - 1;
      }
    }
  }


  /* Return Z_BUF_ERROR if we were given an incomplete table */
  return y != 0 && g != 1 ? ZD_BUF_ERROR : ZD_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r9, -0x48(%rsp)
movq %rcx, %r15
movq 0x138(%rsp), %r14
movq 0x120(%rsp), %r9
xorps %xmm0, %xmm0
movaps %xmm0, -0x10(%rsp)
movaps %xmm0, (%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x20(%rsp)
movl %esi, %eax
xorl %ecx, %ecx
movl (%rdi,%rcx,4), %esi
incl -0x10(%rsp,%rsi,4)
incq %rcx
cmpl %ecx, %eax
jne 0x8672
cmpl %eax, -0x10(%rsp)
jne 0x86a0
movq -0x48(%rsp), %rax
movq $0x0, (%rax)
movl $0x0, (%r9)
xorl %eax, %eax
jmp 0x8aa0
movl (%r9), %r13d
movl $0x1, %ecx
cmpl $0x0, -0x10(%rsp,%rcx,4)
jne 0x86bd
incq %rcx
cmpq $0x10, %rcx
jne 0x86a8
movl $0x10, %ecx
cmpl %ecx, %r13d
cmovbel %ecx, %r13d
movl $0xf, %r12d
movl %r12d, %esi
cmpl $0x0, -0x10(%rsp,%rsi,4)
jne 0x86dc
decl %r12d
jne 0x86ca
xorl %r12d, %r12d
cmpl %r12d, %r13d
cmovael %r12d, %r13d
movl $0x1, %ebp
shll %cl, %ebp
movl %r13d, (%r9)
movl %r12d, %r10d
cmpl %r12d, %ecx
jae 0x870b
movl %ecx, %esi
subl -0x10(%rsp,%rsi,4), %ebp
js 0x87b4
incq %rsi
addl %ebp, %ebp
cmpq %rsi, %r10
jne 0x86f7
movl -0x10(%rsp,%r10,4), %esi
cmpl %esi, %ebp
js 0x87b4
movq %r8, -0x28(%rsp)
movl %esi, -0x60(%rsp)
movl %ebp, -0x10(%rsp,%r10,4)
movl $0x0, 0x34(%rsp)
cmpl $0x1, %r12d
je 0x874e
decq %r10
xorl %esi, %esi
xorl %r9d, %r9d
addl -0xc(%rsp,%rsi,4), %r9d
movl %r9d, 0x38(%rsp,%rsi,4)
incq %rsi
cmpl %esi, %r10d
jne 0x873c
cmpq $0x1, %rax
adcq $0x0, %rax
xorl %r8d, %r8d
movl (%rdi,%r8,4), %esi
testq %rsi, %rsi
je 0x8774
movl 0x30(%rsp,%rsi,4), %r10d
leal 0x1(%r10), %r9d
movl %r9d, 0x30(%rsp,%rsi,4)
movl %r8d, (%r14,%r10,4)
incq %r8
cmpl %r8d, %eax
jne 0x8759
movslq %r12d, %rax
movl 0x30(%rsp,%rax,4), %eax
movl $0x0, 0x30(%rsp)
movq $0x0, 0x70(%rsp)
cmpl %r12d, %ecx
jle 0x87be
xorl %eax, %eax
cmpl -0x60(%rsp), %ebp
sete %al
xorl %ecx, %ecx
cmpl $0x1, %r12d
leal -0x5(%rax,%rax,4), %eax
cmovel %ecx, %eax
jmp 0x8aa0
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jmp 0x8aa0
movl %r13d, %esi
negl %esi
movq %rsi, -0x58(%rsp)
leaq (%r14,%rax,4), %rax
movq %rax, -0x18(%rsp)
movl %ecx, %r10d
leal 0x1(%r12), %eax
movq %rax, -0x38(%rsp)
leaq (%rsp,%r10,4), %rax
addq $-0xc, %rax
movq %rax, -0x50(%rsp)
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movq %rax, -0x78(%rsp)
xorl %r8d, %r8d
xorl %r11d, %r11d
movq %r13, -0x70(%rsp)
movq %r15, -0x30(%rsp)
movl %edx, -0x80(%rsp)
movl %ebp, -0x64(%rsp)
movl -0x10(%rsp,%r10,4), %eax
movl %eax, -0x7c(%rsp)
testl %eax, %eax
je 0x8a82
leal -0x1(%r10), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x5c(%rsp)
movq %r10, -0x40(%rsp)
leal (%rsi,%r13), %ecx
movslq %ecx, %rax
cmpq %rax, %r10
jle 0x896c
movq %r14, -0x20(%rsp)
movl %r12d, %ebp
subl %ecx, %ebp
movslq %r9d, %rax
movl %ecx, %r14d
cmpl %ebp, %r13d
movl %ebp, %edx
cmovbl %r13d, %edx
movl %r12d, %r9d
subl %ecx, %r9d
cmpl %r13d, %r9d
cmovael %r13d, %r9d
movl %r10d, %edi
subl %ecx, %edi
movl $0x1, %ebx
movl %edi, %ecx
shll %cl, %ebx
movl %edi, %r10d
subl -0x7c(%rsp), %ebx
jbe 0x88ad
movl %edi, %r10d
cmpl %r9d, %edi
jae 0x88ad
leal 0x1(%rdi), %r10d
cmpl %r9d, %r10d
jae 0x88ad
movq -0x50(%rsp), %rcx
addl %ebx, %ebx
subl (%rcx), %ebx
jbe 0x88ad
incl %r10d
addq $0x4, %rcx
cmpl %r9d, %r10d
jb 0x8898
movl %edx, %r10d
movl $0x1, %ebx
movl %r10d, %ecx
shll %cl, %ebx
movq 0x130(%rsp), %rdx
movl (%rdx), %ecx
leal (%rcx,%rbx), %r15d
cmpl $0x5a0, %r15d           # imm = 0x5A0
ja 0x8a9b
movq %rax, %r9
movq 0x128(%rsp), %r8
leaq (%r8,%rcx,8), %r8
incq %r9
movq %r8, 0x70(%rsp,%r9,8)
movl %r15d, (%rdx)
je 0x8927
movl %r11d, 0x30(%rsp,%r9,4)
movl %r11d, %r15d
movl %esi, %ecx
shrl %cl, %r15d
movq 0x70(%rsp,%rax,8), %rax
movq %r8, %rcx
subq %rax, %rcx
shrq $0x3, %rcx
subl %r15d, %ecx
movb %r10b, (%rax,%r15,8)
movq -0x70(%rsp), %r13
movb %r13b, 0x1(%rax,%r15,8)
movq %rcx, -0x78(%rsp)
movl %ecx, 0x4(%rax,%r15,8)
jmp 0x8934
movq -0x48(%rsp), %rax
movq %r8, (%rax)
movq -0x70(%rsp), %r13
leal (%r14,%r13), %ecx
movslq %ecx, %r10
subl %r13d, %ebp
movq %r9, %rax
movl %r14d, %esi
movq -0x40(%rsp), %rdx
cmpq %r10, %rdx
movq %rdx, %r10
jg 0x8852
movl %r14d, %esi
movq -0x30(%rsp), %r15
movl -0x80(%rsp), %edx
movq -0x20(%rsp), %r14
movl -0x64(%rsp), %ebp
jmp 0x8971
movl %r10d, %edi
subl %esi, %edi
movb $-0x40, %al
cmpq -0x18(%rsp), %r14
jae 0x89a3
movl (%r14), %eax
movl %eax, %ecx
subl %edx, %ecx
jae 0x89ab
movq %r15, %rdx
movq %rax, -0x78(%rsp)
cmpl $0x100, %eax            # imm = 0x100
movl $0x60, %eax
movl $0x0, %ecx
cmovbl %ecx, %eax
addq $0x4, %r14
jmp 0x89a6
movq %r15, %rdx
movq %r14, %r13
jmp 0x89c8
movq -0x28(%rsp), %rax
movb (%rax,%rcx,4), %al
addb $0x50, %al
addq $0x4, %r14
movq %r14, %r13
movq %r15, %rdx
movl (%r15,%rcx,4), %ecx
movq %rcx, -0x78(%rsp)
movl $0x1, %r10d
movl %edi, %ecx
shll %cl, %r10d
movl %r11d, %r14d
movl %esi, %ecx
shrl %cl, %r14d
cmpl %ebx, %r14d
movq -0x78(%rsp), %r15
jae 0x89fe
movl %r14d, %ecx
movb %al, (%r8,%rcx,8)
movb %dil, 0x1(%r8,%rcx,8)
movl %r15d, 0x4(%r8,%rcx,8)
addl %r10d, %r14d
cmpl %ebx, %r14d
jb 0x89e5
movl -0x5c(%rsp), %eax
movq %r13, %r14
testl %r11d, %eax
je 0x8a1e
movq -0x58(%rsp), %r10
movq %rdx, %r15
xorl %eax, %r11d
shrl %eax
testl %r11d, %eax
jne 0x8a12
jmp 0x8a26
movq -0x58(%rsp), %r10
movq %rdx, %r15
xorl %eax, %r11d
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %esi, %ecx
shll %cl, %edi
notl %edi
andl %r11d, %edi
movslq %r9d, %rax
cmpl 0x30(%rsp,%rax,4), %edi
movq -0x70(%rsp), %r13
movl -0x80(%rsp), %edx
je 0x8a73
leaq 0x2c(%rsp), %rcx
leaq (%rcx,%rax,4), %rax
leal (%r10,%rsi), %ecx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
shll %cl, %edi
subl %r13d, %esi
notl %edi
andl %r11d, %edi
decl %r9d
leaq -0x4(%rax), %rcx
cmpl (%rax), %edi
movq %rcx, %rax
jne 0x8a52
decl -0x7c(%rsp)
movq -0x40(%rsp), %r10
jne 0x8835
incq %r10
addq $0x4, -0x50(%rsp)
cmpq -0x38(%rsp), %r10
jne 0x8810
jmp 0x8799
movl $0xfffffffc, %eax       # imm = 0xFFFFFFFC
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq