// File: /workspace/github_c_makefile_star2_O3/snej[P]zdelta/trees.c
// Function: build_tree
local void build_tree(s, desc)
     deflate_state *s;
     tree_desc *desc; /* the tree descriptor */
{
  ct_data *tree         = desc->dyn_tree;
  const ct_data *stree  = desc->stat_desc->static_tree;
  int elems             = desc->stat_desc->elems;
  int n, m;          /* iterate over heap elements */
  int max_code = -1; /* largest code with non zero frequency */
  int node;          /* new node being created */

  /* Construct the initial heap, with least frequent element in
   * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
   * heap[0] is not used.
   */
  s->heap_len = 0, s->heap_max = HEAP_SIZE;

  for (n = 0; n < elems; n++) {
    if (tree[n].Freq != 0) {
      s->heap[++(s->heap_len)] = max_code = n;
      s->depth[n] = 0;
    } else {
      tree[n].Len = 0;
    }
  }

  /* The pkzip format requires that at least one distance code exists,
   * and that at least one bit should be sent even if there is only one
   * possible code. So to avoid special checks later on we force at least
   * two codes of non zero frequency.
   */
  while (s->heap_len < 2) {
    node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
    tree[node].Freq = 1;
    s->depth[node] = 0;
    s->opt_len--; if (stree) s->static_len -= stree[node].Len;
    /* node is 0 or 1 so it does not have extra bits */
  }
  desc->max_code = max_code;
  
  /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
   * establish sub-heaps of increasing lengths:
   */
  for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);

  /* Construct the Huffman tree by repeatedly combining the least two
   * frequent nodes.
   */
  node = elems;              /* next internal node of the tree */
  do {
    pqremove(s, tree, n);  /* n = node of least frequency */
    m = s->heap[SMALLEST]; /* m = node of next least frequency */
    
    s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
    s->heap[--(s->heap_max)] = m;
    
    /* Create a new node father of n and m */
    tree[node].Freq = tree[n].Freq + tree[m].Freq;
    s->depth[node] = (uch) (MAX(s->depth[n], s->depth[m]) + 1);
    tree[n].Dad = tree[m].Dad = (ush)node;
#ifdef DUMP_BL_TREE 
    if (tree == s->bl_tree) {
      fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
	      node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
    }
#endif
    /* and insert the new node in the heap */
    s->heap[SMALLEST] = node++;
    pqdownheap(s, tree, SMALLEST);
    
  } while (s->heap_len >= 2);
  
  s->heap[--(s->heap_max)] = s->heap[SMALLEST];
  
  /* At this point, the fields freq and dad are set. We can now
   * generate the bit lengths.
   */
  gen_bitlen(s, (tree_desc *)desc);
  
  /* The field len is now set, we can generate the bit codes */
  gen_codes ((ct_data *)tree, max_code, s->bl_count);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r14
movq (%rsi), %rbx
movq 0x10(%rsi), %rcx
movq (%rcx), %rax
movslq 0x14(%rcx), %rbp
xorl %ecx, %ecx
movabsq $0x23f00000000, %rdx    # imm = 0x23F00000000
movq %rdx, 0x17ec(%rdi)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testq %rbp, %rbp
jle 0x55ad
xorl %edx, %edx
cmpw $0x0, (%rbx,%rdx,4)
je 0x558f
movslq 0x17ec(%r14), %r8
leal 0x1(%r8), %edi
movl %edi, 0x17ec(%r14)
movl %edx, 0xef4(%r14,%r8,4)
movb $0x0, 0x17f4(%r14,%rdx)
movl %edx, %r13d
jmp 0x5596
movw $0x0, 0x2(%rbx,%rdx,4)
incq %rdx
cmpq %rdx, %rbp
jne 0x5560
movl 0x17ec(%r14), %r12d
cmpl $0x1, %r12d
jle 0x55b0
jmp 0x560d
xorl %r12d, %r12d
leal 0x1(%r13), %edx
cmpl $0x2, %r13d
cmovll %edx, %r13d
cmovgel %ecx, %edx
leal 0x1(%r12), %edi
movl %edi, 0x17ec(%r14)
movslq %r12d, %rdi
movl %edx, 0xef4(%r14,%rdi,4)
movslq %edx, %rdx
movw $0x1, (%rbx,%rdx,4)
movb $0x0, 0x17f4(%r14,%rdx)
decq 0x1a58(%r14)
testq %rax, %rax
je 0x5600
movzwl 0x2(%rax,%rdx,4), %edx
subq %rdx, 0x1a60(%r14)
movl 0x17ec(%r14), %r12d
cmpl $0x2, %r12d
jl 0x55b0
movq %rsi, 0x10(%rsp)
movl %r13d, 0x8(%rsi)
shrl %r12d
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %edx
callq 0x5fc7
leal -0x1(%r12), %eax
cmpl $0x1, %r12d
movl %eax, %r12d
ja 0x5619
movl 0x17ec(%r14), %eax
movq %rbp, %r12
movslq 0xef4(%r14), %r15
leal -0x1(%rax), %ecx
movl %ecx, 0x17ec(%r14)
cltq
movl 0xef0(%r14,%rax,4), %eax
movl %eax, 0xef4(%r14)
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x5fc7
movslq 0xef4(%r14), %rax
movslq 0x17f0(%r14), %rcx
leal -0x1(%rcx), %edx
movl %edx, 0x17f0(%r14)
movl %r15d, 0xeec(%r14,%rcx,4)
movslq 0x17f0(%r14), %rcx
leal -0x1(%rcx), %edx
movl %edx, 0x17f0(%r14)
movl %eax, 0xeec(%r14,%rcx,4)
movzwl (%rbx,%rax,4), %ecx
addw (%rbx,%r15,4), %cx
movw %cx, (%rbx,%rbp,4)
movzbl 0x17f4(%r14,%r15), %ecx
movzbl 0x17f4(%r14,%rax), %edx
cmpb %dl, %cl
cmoval %ecx, %edx
incb %dl
movb %dl, 0x17f4(%r14,%rbp)
movw %bp, 0x2(%rbx,%rax,4)
movw %bp, 0x2(%rbx,%r15,4)
incq %r12
movl %ebp, 0xef4(%r14)
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x5fc7
movl 0x17ec(%r14), %eax
movq %r12, %rbp
cmpl $0x1, %eax
jg 0x563f
movl 0xef4(%r14), %eax
movslq 0x17f0(%r14), %rcx
leal -0x1(%rcx), %edx
movl %edx, 0x17f0(%r14)
movl %eax, 0xeec(%r14,%rcx,4)
movq 0x10(%rsp), %rdx
movq (%rdx), %rax
movl 0x8(%rdx), %ecx
movq 0x10(%rdx), %rdx
movq (%rdx), %rsi
movq %rsi, 0x10(%rsp)
movq 0x8(%rdx), %rsi
movq %rsi, 0x18(%rsp)
movl 0x10(%rdx), %esi
movl %esi, 0xc(%rsp)
movslq 0x18(%rdx), %rdx
xorps %xmm0, %xmm0
movups %xmm0, 0xee0(%r14)
movups %xmm0, 0xed0(%r14)
movslq 0x17f0(%r14), %rsi
movslq 0xef0(%r14,%rsi,4), %rsi
movw $0x0, 0x2(%rax,%rsi,4)
movslq 0x17f0(%r14), %r11
cmpq $0x23d, %r11            # imm = 0x23D
jg 0x5916
leaq 0xed0(%r14), %rsi
xorl %edi, %edi
movslq 0xef4(%r14,%r11,4), %r12
movzwl 0x2(%rax,%r12,4), %r8d
movzwl 0x2(%rax,%r8,4), %r8d
xorl %ebp, %ebp
movl %edx, %r9d
cmpl %r8d, %edx
setle %r15b
jle 0x57c9
incl %r8d
movl %r8d, %r9d
movw %r9w, 0x2(%rax,%r12,4)
cmpl %ecx, %r12d
jg 0x5834
movslq %r9d, %r8
incw (%rsi,%r8,2)
xorl %r10d, %r10d
cmpl 0xc(%rsp), %r12d
jl 0x57fa
movl %r12d, %r8d
subl 0xc(%rsp), %r8d
movslq %r8d, %r8
movq 0x18(%rsp), %r10
movl (%r10,%r8,4), %r10d
movzwl (%rax,%r12,4), %r8d
addl %r10d, %r9d
movslq %r9d, %r9
imulq %r8, %r9
addq %r9, 0x1a58(%r14)
cmpq $0x0, 0x10(%rsp)
je 0x5834
movq 0x10(%rsp), %r9
movzwl 0x2(%r9,%r12,4), %r9d
movslq %r10d, %r10
addq %r9, %r10
imulq %r8, %r10
addq %r10, 0x1a60(%r14)
movb %r15b, %bpl
addl %ebp, %edi
movq %r11, %r8
incq %r8
cmpl $0x23d, %r11d           # imm = 0x23D
movq %r8, %r11
jne 0x57a1
testl %edi, %edi
je 0x5916
movq %rdx, %r9
shlq $0x20, %r9
movabsq $0x100000000, %r8       # imm = 0x100000000
addq %r9, %r8
leaq (%r14,%rdx,2), %r9
addq $0xed0, %r9             # imm = 0xED0
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
movq %r9, %r15
movq %r8, %r11
addq %r10, %r11
movzwl -0x2(%r15), %ebp
addq $-0x2, %r15
testw %bp, %bp
je 0x5886
decl %ebp
movw %bp, (%r15)
sarq $0x1f, %r11
addw $0x2, (%rsi,%r11)
decw (%rsi,%rdx,2)
leal -0x2(%rdi), %r11d
cmpl $0x2, %edi
movl %r11d, %edi
jg 0x5880
testl %edx, %edx
je 0x5916
leaq 0xeec(%r14), %rdi
movl $0x23f, %r8d            # imm = 0x23F
movzwl (%rsi,%rdx,2), %r9d
testl %r9d, %r9d
je 0x5911
movslq %r8d, %r10
leaq (%rdi,%r10,4), %r11
movslq (%r11), %r10
decl %r8d
addq $-0x4, %r11
cmpl %ecx, %r10d
jg 0x58d9
movzwl 0x2(%rax,%r10,4), %r15d
movq %rdx, %r11
subq %r15, %r11
je 0x590c
movzwl (%rax,%r10,4), %r15d
imulq %r15, %r11
addq %r11, 0x1a58(%r14)
movw %dx, 0x2(%rax,%r10,4)
decl %r9d
jne 0x58d2
decq %rdx
jne 0x58c8
xorl %eax, %eax
xorl %ecx, %ecx
addw 0xed0(%r14,%rax,2), %cx
addl %ecx, %ecx
movw %cx, 0x22(%rsp,%rax,2)
incq %rax
cmpq $0xf, %rax
jne 0x591a
testl %r13d, %r13d
js 0x5981
incl %r13d
xorl %eax, %eax
movzwl 0x2(%rbx,%rax,4), %ecx
testl %ecx, %ecx
je 0x5979
movl %ecx, %esi
movzwl 0x20(%rsp,%rsi,2), %edx
movl %edx, %edi
incl %edi
movw %di, 0x20(%rsp,%rsi,2)
incl %ecx
xorl %esi, %esi
movl %edx, %r8d
andl $0x1, %r8d
movzwl %dx, %edx
movl %esi, %edi
orl %r8d, %edi
shrl %edx
leal (%rdi,%rdi), %esi
decl %ecx
cmpl $0x1, %ecx
ja 0x595a
movw %di, (%rbx,%rax,4)
incq %rax
cmpq %r13, %rax
jne 0x593d
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq