// File: /workspace/github_c_makefile_star2_O0/snej[P]zdelta/trees.c
// Function: gen_bitlen
local void gen_bitlen(s, desc)
    deflate_state *s;
    tree_desc *desc;    /* the tree descriptor */
{
  ct_data *tree        = desc->dyn_tree;
  int max_code         = desc->max_code;
  const ct_data *stree = desc->stat_desc->static_tree;
  const intf *extra    = desc->stat_desc->extra_bits;
  int base             = desc->stat_desc->extra_base;
  int max_length       = desc->stat_desc->max_length;
  int h;              /* heap index */
  int n, m;           /* iterate over the tree elements */
  int bits;           /* bit length */
  int xbits;          /* extra bits */
  ush f;              /* frequency  */
  int overflow = 0;   /* number of elements with bit length too large */

  for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;

  /* In a first pass, compute the optimal bit lengths (which may
   * overflow in the case of the bit length tree).
   */
  tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
  
  for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
    n = s->heap[h];
    bits = tree[tree[n].Dad].Len + 1;
 
    if (bits > max_length) bits = max_length, overflow++;

    tree[n].Len = (ush)bits;
    /* We overwrite tree[n].Dad which is no longer needed */

    if (n > max_code) continue; /* not a leaf node */

    s->bl_count[bits]++;
    xbits = 0;
    if (n >= base) xbits = extra[n-base];
    f = tree[n].Freq;
    s->opt_len += (ulg)f * (bits + xbits);
    if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
  }
  if (overflow == 0) return;

  Trace((stderr,"\nbit length overflow\n"));
  /* This happens for example on obj2 and pic of the Calgary corpus */

  /* Find the first bit length which could increase: */
  do {
    bits = max_length-1;
    while (s->bl_count[bits] == 0) bits--;
    s->bl_count[bits]--;      /* move one leaf down the tree */
    s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
    s->bl_count[max_length]--;
    /* The brother of the overflow item also moves one step up,
     * but this does not affect bl_count[max_length]
     */
    overflow -= 2;
  } while (overflow > 0);

  /* Now recompute all bit lengths, scanning in increasing frequency.
   * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
   * lengths instead of fixing only the wrong ones. This idea is taken
   * from 'ar' written by Haruhiko Okumura.)
   */
  for (bits = max_length; bits != 0; bits--) {
    n = s->bl_count[bits];
    while (n != 0) {
      m = s->heap[--h];
      if (m > max_code) continue;
      if (tree[m].Len != (unsigned) bits) {
	Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
	s->opt_len += ((long)bits - (long)tree[m].Len)
	  *(long)tree[m].Freq;
	tree[m].Len = (ush)bits;
      }
      n--;
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x10(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x18(%rax), %eax
movl %eax, -0x38(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x48(%rbp)
cmpl $0xf, -0x48(%rbp)
jg 0x9c8d
movq -0x8(%rbp), %rax
movslq -0x48(%rbp), %rcx
movw $0x0, 0xed0(%rax,%rcx,2)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x9c6a
movq -0x18(%rbp), %rax
movq -0x8(%rbp), %rcx
movq -0x8(%rbp), %rdx
movslq 0x17f0(%rdx), %rdx
movslq 0xef0(%rcx,%rdx,4), %rcx
movw $0x0, 0x2(%rax,%rcx,4)
movq -0x8(%rbp), %rax
movl 0x17f0(%rax), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
cmpl $0x23f, -0x3c(%rbp)     # imm = 0x23F
jge 0x9de8
movq -0x8(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movl 0xef0(%rax,%rcx,4), %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq -0x40(%rbp), %rdx
movzwl 0x2(%rcx,%rdx,4), %ecx
movzwl 0x2(%rax,%rcx,4), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x38(%rbp), %eax
jle 0x9d11
movl -0x38(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movl -0x48(%rbp), %eax
movw %ax, %dx
movq -0x18(%rbp), %rax
movslq -0x40(%rbp), %rcx
movw %dx, 0x2(%rax,%rcx,4)
movl -0x40(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jle 0x9d31
jmp 0x9dda
movq -0x8(%rbp), %rax
movslq -0x48(%rbp), %rcx
movw 0xed0(%rax,%rcx,2), %dx
addw $0x1, %dx
movw %dx, 0xed0(%rax,%rcx,2)
movl $0x0, -0x4c(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x34(%rbp), %eax
jl 0x9d6f
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
subl -0x34(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movslq -0x40(%rbp), %rcx
movw (%rax,%rcx,4), %ax
movw %ax, -0x4e(%rbp)
movzwl -0x4e(%rbp), %eax
movl %eax, %ecx
movl -0x48(%rbp), %eax
addl -0x4c(%rbp), %eax
cltq
imulq %rax, %rcx
movq -0x8(%rbp), %rax
addq 0x1a58(%rax), %rcx
movq %rcx, 0x1a58(%rax)
cmpq $0x0, -0x28(%rbp)
je 0x9dd8
movzwl -0x4e(%rbp), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movslq -0x40(%rbp), %rdx
movzwl 0x2(%rax,%rdx,4), %eax
addl -0x4c(%rbp), %eax
cltq
imulq %rax, %rcx
movq -0x8(%rbp), %rax
addq 0x1a60(%rax), %rcx
movq %rcx, 0x1a60(%rax)
jmp 0x9dda
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x9cbf
cmpl $0x0, -0x54(%rbp)
jne 0x9df3
jmp 0x9f53
jmp 0x9df5
movl -0x38(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movslq -0x48(%rbp), %rcx
movzwl 0xed0(%rax,%rcx,2), %eax
cmpl $0x0, %eax
jne 0x9e1e
movl -0x48(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x9dfe
movq -0x8(%rbp), %rax
movslq -0x48(%rbp), %rcx
movw 0xed0(%rax,%rcx,2), %dx
addw $-0x1, %dx
movw %dx, 0xed0(%rax,%rcx,2)
movq -0x8(%rbp), %rax
movl -0x48(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzwl 0xed0(%rax,%rcx,2), %edx
addl $0x2, %edx
movw %dx, 0xed0(%rax,%rcx,2)
movq -0x8(%rbp), %rax
movslq -0x38(%rbp), %rcx
movw 0xed0(%rax,%rcx,2), %dx
addw $-0x1, %dx
movw %dx, 0xed0(%rax,%rcx,2)
movl -0x54(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jg 0x9df5
movl -0x38(%rbp), %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x9f53
movq -0x8(%rbp), %rax
movslq -0x48(%rbp), %rcx
movzwl 0xed0(%rax,%rcx,2), %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x9f43
movq -0x8(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x3c(%rbp)
movslq %ecx, %rcx
movl 0xef0(%rax,%rcx,4), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jle 0x9eda
jmp 0x9eac
movq -0x18(%rbp), %rax
movslq -0x44(%rbp), %rcx
movzwl 0x2(%rax,%rcx,4), %eax
cmpl -0x48(%rbp), %eax
je 0x9f35
movslq -0x48(%rbp), %rcx
movq -0x18(%rbp), %rax
movslq -0x44(%rbp), %rdx
movzwl 0x2(%rax,%rdx,4), %eax
subq %rax, %rcx
movq -0x18(%rbp), %rax
movslq -0x44(%rbp), %rdx
movzwl (%rax,%rdx,4), %eax
imulq %rax, %rcx
movq -0x8(%rbp), %rax
addq 0x1a58(%rax), %rcx
movq %rcx, 0x1a58(%rax)
movl -0x48(%rbp), %eax
movw %ax, %dx
movq -0x18(%rbp), %rax
movslq -0x44(%rbp), %rcx
movw %dx, 0x2(%rax,%rcx,4)
movl -0x40(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x9eac
jmp 0x9f45
movl -0x48(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x9e8f
popq %rbp
retq
nopw %cs:(%rax,%rax)