// File: /workspace/github_c_makefile_star2_O1/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, -0x40(%rsp)
movq %rcx, %r9
movq 0x138(%rsp), %rbx
movq 0x120(%rsp), %r10
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 0x842a
cmpl %eax, -0x10(%rsp)
jne 0x8458
movq -0x40(%rsp), %rax
movq $0x0, (%rax)
movl $0x0, (%r10)
xorl %eax, %eax
jmp 0x8845
movl (%r10), %esi
movl $0x1, %ecx
cmpl $0x0, -0x10(%rsp,%rcx,4)
jne 0x8475
incq %rcx
cmpq $0x10, %rcx
jne 0x8460
movl $0x10, %ecx
cmpl %ecx, %esi
cmovbel %ecx, %esi
movl %esi, %ebp
movl $0xf, %r15d
movl %r15d, %esi
cmpl $0x0, -0x10(%rsp,%rsi,4)
jne 0x8494
decl %r15d
jne 0x8482
xorl %r15d, %r15d
cmpl %r15d, %ebp
movl %r15d, %r12d
cmovbl %ebp, %r12d
movl $0x1, %r13d
shll %cl, %r13d
movl %r12d, (%r10)
movl %r15d, %r10d
cmpl %r15d, %ecx
jae 0x84ca
movl %ecx, %esi
subl -0x10(%rsp,%rsi,4), %r13d
js 0x857e
addl %r13d, %r13d
incq %rsi
cmpl %esi, %r10d
jne 0x84b4
movl -0x10(%rsp,%r10,4), %esi
cmpl %esi, %r13d
js 0x857e
movl %esi, -0x60(%rsp)
movq %r9, -0x30(%rsp)
movl %r13d, -0x10(%rsp,%r10,4)
movl $0x0, 0x34(%rsp)
cmpl $0x1, %r15d
je 0x850e
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 0x84fc
cmpq $0x1, %rax
adcq $0x0, %rax
xorl %r11d, %r11d
movl (%rdi,%r11,4), %esi
testq %rsi, %rsi
je 0x8534
movl 0x30(%rsp,%rsi,4), %r10d
leal 0x1(%r10), %r9d
movl %r9d, 0x30(%rsp,%rsi,4)
movl %r11d, (%rbx,%r10,4)
incq %r11
cmpl %r11d, %eax
jne 0x8519
movl %edx, -0x5c(%rsp)
movq %r8, -0x28(%rsp)
movslq %r15d, %rax
movl 0x30(%rsp,%rax,4), %eax
movl $0x0, 0x30(%rsp)
movq $0x0, 0x70(%rsp)
cmpl %r15d, %ecx
jle 0x8588
xorl %eax, %eax
cmpl -0x60(%rsp), %r13d
sete %al
xorl %ecx, %ecx
cmpl $0x1, %r15d
leal -0x5(%rax,%rax,4), %eax
cmovel %ecx, %eax
jmp 0x8845
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jmp 0x8845
movl %r12d, %edx
negl %edx
leaq (%rbx,%rax,4), %rax
movq %rax, -0x18(%rsp)
movl %ecx, %eax
leal 0x1(%r15), %ecx
movq %rcx, -0x38(%rsp)
cmpl %ebp, %r15d
cmovbl %r15d, %ebp
movl %ebp, -0x54(%rsp)
movq %rdx, %rdi
movq %rdx, -0x50(%rsp)
leaq (%rsp,%rax,4), %rcx
addq $-0xc, %rcx
movq %rcx, -0x48(%rsp)
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
xorl %r9d, %r9d
movq %rcx, -0x70(%rsp)
xorl %r14d, %r14d
xorl %edx, %edx
movl -0x10(%rsp,%rax,4), %ecx
movl %ecx, -0x74(%rsp)
testl %ecx, %ecx
je 0x8823
leal -0x1(%rax), %ecx
movl $0x1, %esi
shll %cl, %esi
movl %esi, -0x58(%rsp)
movq %rax, -0x68(%rsp)
leal (%rdi,%r12), %ecx
movslq %ecx, %rsi
cmpq %rsi, %rax
jle 0x8719
movq %rbx, -0x20(%rsp)
movl %r15d, %ebx
subl %ecx, %ebx
movslq %r11d, %rbp
movl %ecx, %esi
movl -0x54(%rsp), %ecx
cmpl %ebx, %ecx
movl %ebx, %r9d
cmovbl %ecx, %r9d
movl %r15d, %r11d
subl %esi, %r11d
cmpl %r12d, %r11d
cmovael %r12d, %r11d
movl %eax, %r10d
subl %esi, %r10d
movl $0x1, %r14d
movl %r10d, %ecx
shll %cl, %r14d
subl -0x74(%rsp), %r14d
jbe 0x8672
cmpl %r11d, %r10d
jae 0x8672
incl %r10d
cmpl %r11d, %r10d
jae 0x8672
movq -0x48(%rsp), %rcx
addl %r14d, %r14d
subl (%rcx), %r14d
jbe 0x8672
incl %r10d
addq $0x4, %rcx
cmpl %r11d, %r10d
jb 0x865b
movl %r9d, %r10d
movl $0x1, %r9d
movl %r10d, %ecx
shll %cl, %r9d
movq 0x130(%rsp), %r11
movl (%r11), %ecx
leal (%rcx,%r9), %r8d
cmpl $0x5a0, %r8d            # imm = 0x5A0
ja 0x8840
movq %r11, %rax
movq %rbp, %r11
movq 0x128(%rsp), %r14
leaq (%r14,%rcx,8), %r14
incq %r11
movq %r14, 0x70(%rsp,%r11,8)
movl %r8d, (%rax)
je 0x86ed
movl %edx, 0x30(%rsp,%r11,4)
movl %edx, %r8d
movl %edi, %ecx
shrl %cl, %r8d
movq 0x70(%rsp,%rbp,8), %rcx
movq %r14, %rax
subq %rcx, %rax
shrq $0x3, %rax
subl %r8d, %eax
movb %r10b, (%rcx,%r8,8)
movb %r12b, 0x1(%rcx,%r8,8)
movq %rax, -0x70(%rsp)
movl %eax, 0x4(%rcx,%r8,8)
jmp 0x86f5
movq -0x40(%rsp), %rcx
movq %r14, (%rcx)
movq -0x68(%rsp), %rax
leal (%rsi,%r12), %ecx
movslq %ecx, %r8
subl %r12d, %ebx
movq %r11, %rbp
movl %esi, %edi
cmpq %r8, %rax
jg 0x8614
movq -0x20(%rsp), %rbx
jmp 0x871b
movl %edi, %esi
movb $-0x40, %r10b
cmpq -0x18(%rsp), %rbx
jae 0x874d
movl (%rbx), %edi
movl %edi, %ecx
subl -0x5c(%rsp), %ecx
jae 0x8752
movq %rdi, -0x70(%rsp)
cmpl $0x100, %edi            # imm = 0x100
movl $0x60, %r10d
movl $0x0, %ecx
cmovbl %ecx, %r10d
addq $0x4, %rbx
movq %rbx, %r8
jmp 0x8778
movq -0x28(%rsp), %rax
movb (%rax,%rcx,4), %r10b
addb $0x50, %r10b
addq $0x4, %rbx
movq %rbx, %r8
movq -0x30(%rsp), %rax
movl (%rax,%rcx,4), %eax
movq %rax, -0x70(%rsp)
movq -0x68(%rsp), %rax
movl %eax, %edi
subl %esi, %edi
movl $0x1, %ebx
movl %edi, %ecx
shll %cl, %ebx
movl %edx, %ebp
movl %esi, %ecx
shrl %cl, %ebp
cmpl %r9d, %ebp
movq -0x70(%rsp), %rax
jae 0x87ac
movl %ebp, %ecx
movb %r10b, (%r14,%rcx,8)
movb %dil, 0x1(%r14,%rcx,8)
movl %eax, 0x4(%r14,%rcx,8)
addl %ebx, %ebp
cmpl %r9d, %ebp
jb 0x8795
movl -0x58(%rsp), %ecx
movq %r8, %rbx
testl %edx, %ecx
je 0x87c6
movq -0x50(%rsp), %rax
xorl %ecx, %edx
shrl %ecx
testl %edx, %ecx
jne 0x87bc
jmp 0x87cb
movq -0x50(%rsp), %rax
xorl %ecx, %edx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %esi, %ecx
shll %cl, %edi
notl %edi
andl %edx, %edi
movslq %r11d, %rcx
cmpl 0x30(%rsp,%rcx,4), %edi
je 0x8810
leaq 0x2c(%rsp), %rdi
leaq (%rdi,%rcx,4), %rdi
leal (%rax,%rsi), %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
shll %cl, %r8d
subl %r12d, %esi
notl %r8d
andl %edx, %r8d
decl %r11d
leaq -0x4(%rdi), %rcx
cmpl (%rdi), %r8d
movq %rcx, %rdi
jne 0x87ec
movl %esi, %edi
decl -0x74(%rsp)
movq -0x68(%rsp), %rax
jne 0x85f7
jmp 0x8825
movl %edi, %esi
incq %rax
addq $0x4, -0x48(%rsp)
movl %esi, %edi
cmpq -0x38(%rsp), %rax
jne 0x85d4
jmp 0x8562
movl $0xfffffffc, %eax       # imm = 0xFFFFFFFC
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq