// File: /workspace/github_c_makefile_star2_O0/tditlu[P]shadetabler/src/lodepng/lodepng.c
// Function: HuffmanTree_makeTable
static unsigned HuffmanTree_makeTable(HuffmanTree* tree) {
  static const unsigned headsize = 1u << FIRSTBITS; /*size of the first table*/
  static const unsigned mask = (1u << FIRSTBITS) /*headsize*/ - 1u;
  size_t i, numpresent, pointer, size; /*total table size*/
  unsigned* maxlens = (unsigned*)lodepng_malloc(headsize * sizeof(unsigned));
  if(!maxlens) return 83; /*alloc fail*/

  /* compute maxlens: max total bit length of symbols sharing prefix in the first table*/
  lodepng_memset(maxlens, 0, headsize * sizeof(*maxlens));
  for(i = 0; i < tree->numcodes; i++) {
    unsigned symbol = tree->codes[i];
    unsigned l = tree->lengths[i];
    unsigned index;
    if(l <= FIRSTBITS) continue; /*symbols that fit in first table don't increase secondary table size*/
    /*get the FIRSTBITS MSBs, the MSBs of the symbol are encoded first. See later comment about the reversing*/
    index = reverseBits(symbol >> (l - FIRSTBITS), FIRSTBITS);
    maxlens[index] = LODEPNG_MAX(maxlens[index], l);
  }
  /* compute total table size: size of first table plus all secondary tables for symbols longer than FIRSTBITS */
  size = headsize;
  for(i = 0; i < headsize; ++i) {
    unsigned l = maxlens[i];
    if(l > FIRSTBITS) size += (1u << (l - FIRSTBITS));
  }
  tree->table_len = (unsigned char*)lodepng_malloc(size * sizeof(*tree->table_len));
  tree->table_value = (unsigned short*)lodepng_malloc(size * sizeof(*tree->table_value));
  if(!tree->table_len || !tree->table_value) {
    lodepng_free(maxlens);
    /* freeing tree->table values is done at a higher scope */
    return 83; /*alloc fail*/
  }
  /*initialize with an invalid length to indicate unused entries*/
  for(i = 0; i < size; ++i) tree->table_len[i] = 16;

  /*fill in the first table for long symbols: max prefix size and pointer to secondary tables*/
  pointer = headsize;
  for(i = 0; i < headsize; ++i) {
    unsigned l = maxlens[i];
    if(l <= FIRSTBITS) continue;
    tree->table_len[i] = l;
    tree->table_value[i] = pointer;
    pointer += (1u << (l - FIRSTBITS));
  }
  lodepng_free(maxlens);

  /*fill in the first table for short symbols, or secondary table for long symbols*/
  numpresent = 0;
  for(i = 0; i < tree->numcodes; ++i) {
    unsigned l = tree->lengths[i];
    unsigned symbol = tree->codes[i]; /*the huffman bit pattern. i itself is the value.*/
    /*reverse bits, because the huffman bits are given in MSB first order but the bit reader reads LSB first*/
    unsigned reverse = reverseBits(symbol, l);
    if(l == 0) continue;
    numpresent++;

    if(l <= FIRSTBITS) {
      /*short symbol, fully in first table, replicated num times if l < FIRSTBITS*/
      unsigned num = 1u << (FIRSTBITS - l);
      unsigned j;
      for(j = 0; j < num; ++j) {
        /*bit reader will read the l bits of symbol first, the remaining FIRSTBITS - l bits go to the MSB's*/
        unsigned index = reverse | (j << l);
        if(tree->table_len[index] != 16) return 55; /*invalid tree: long symbol shares prefix with short symbol*/
        tree->table_len[index] = l;
        tree->table_value[index] = i;
      }
    } else {
      /*long symbol, shares prefix with other long symbols in first lookup table, needs second lookup*/
      /*the FIRSTBITS MSBs of the symbol are the first table index*/
      unsigned index = reverse & mask;
      unsigned maxlen = tree->table_len[index];
      /*log2 of secondary table length, should be >= l - FIRSTBITS*/
      unsigned tablelen = maxlen - FIRSTBITS;
      unsigned start = tree->table_value[index]; /*starting index in secondary table*/
      unsigned num = 1u << (tablelen - (l - FIRSTBITS)); /*amount of entries of this symbol in secondary table*/
      unsigned j;
      if(maxlen < l) return 55; /*invalid tree: long symbol shares prefix with short symbol*/
      for(j = 0; j < num; ++j) {
        unsigned reverse2 = reverse >> FIRSTBITS; /* l - FIRSTBITS bits */
        unsigned index2 = start + (reverse2 | (j << (l - FIRSTBITS)));
        tree->table_len[index2] = l;
        tree->table_value[index2] = i;
      }
    }
  }

  if(numpresent < 2) {
    /* In case of exactly 1 symbol, in theory the huffman symbol needs 0 bits,
    but deflate uses 1 bit instead. In case of 0 symbols, no symbols can
    appear at all, but such huffman tree could still exist (e.g. if distance
    codes are never used). In both cases, not all symbols of the table will be
    filled in. Fill them in with an invalid symbol value so returning them from
    huffmanDecodeSymbol will cause error. */
    for(i = 0; i < size; ++i) {
      if(tree->table_len[i] == 16) {
        /* As length, use a value smaller than FIRSTBITS for the head table,
        and a value larger than FIRSTBITS for the secondary table, to ensure
        valid behavior for advanceBits when reading this symbol. */
        tree->table_len[i] = (i < headsize) ? 1 : (FIRSTBITS + 1);
        tree->table_value[i] = INVALIDSYMBOL;
      }
    }
  } else {
    /* A good huffman tree has N * 2 - 1 nodes, of which N - 1 are internal nodes.
    If that is not the case (due to too long length codes), the table will not
    have been fully used, and this is an error (not all bit combinations can be
    decoded): an oversubscribed huffman tree, indicated by error 55. */
    for(i = 0; i < size; ++i) {
      if(tree->table_len[i] == 16) return 55;
    }
  }

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl $0x800, %edi            # imm = 0x800
callq 0x3de0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x116c0
movl $0x53, -0x4(%rbp)
jmp 0x11b97
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x4a10
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x14(%rcx), %ecx
cmpq %rcx, %rax
jae 0x1177a
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x40(%rbp)
cmpl $0x9, -0x40(%rbp)
ja 0x11717
jmp 0x11769
movl -0x3c(%rbp), %edi
movl -0x40(%rbp), %ecx
subl $0x9, %ecx
shrl %cl, %edi
movl $0x9, %esi
callq 0x11bb0
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rax
movl -0x44(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x40(%rbp), %eax
jbe 0x11750
movq -0x38(%rbp), %rax
movl -0x44(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x88(%rbp)
jmp 0x11759
movl -0x40(%rbp), %eax
movl %eax, -0x88(%rbp)
movl -0x88(%rbp), %edx
movq -0x38(%rbp), %rax
movl -0x44(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x116d8
movq $0x200, -0x30(%rbp)     # imm = 0x200
movq $0x0, -0x18(%rbp)
cmpq $0x200, -0x18(%rbp)     # imm = 0x200
jae 0x117cf
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x48(%rbp)
cmpl $0x9, -0x48(%rbp)
jbe 0x117bf
movl -0x48(%rbp), %ecx
subl $0x9, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %eax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x117c1
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x1178a
movq -0x30(%rbp), %rdi
shlq $0x0, %rdi
callq 0x3de0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x30(%rbp), %rdi
shlq %rdi
callq 0x3de0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x11814
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x11829
movq -0x38(%rbp), %rdi
callq 0x5060
movl $0x53, -0x4(%rbp)
jmp 0x11b97
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x11859
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
movb $0x10, (%rax,%rcx)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x11831
movq $0x200, -0x28(%rbp)     # imm = 0x200
movq $0x0, -0x18(%rbp)
cmpq $0x200, -0x18(%rbp)     # imm = 0x200
jae 0x118d9
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x9, -0x4c(%rbp)
ja 0x11889
jmp 0x118cb
movl -0x4c(%rbp), %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x28(%rbp), %rax
movw %ax, %dx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movl -0x4c(%rbp), %ecx
subl $0x9, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %eax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x11869
movq -0x38(%rbp), %rdi
callq 0x5060
movq $0x0, -0x20(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x14(%rcx), %ecx
cmpq %rcx, %rax
jae 0x11ad3
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %edi
movl -0x50(%rbp), %esi
callq 0x11bb0
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x50(%rbp)
jne 0x11942
jmp 0x11ac2
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
cmpl $0x9, -0x50(%rbp)
ja 0x119e4
movl $0x9, %ecx
subl -0x50(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jae 0x119df
movl -0x58(%rbp), %eax
movl -0x60(%rbp), %edx
movl -0x50(%rbp), %ecx
shll %cl, %edx
movl %edx, %ecx
orl %ecx, %eax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x64(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x10, %eax
je 0x119ab
movl $0x37, -0x4(%rbp)
jmp 0x11b97
movl -0x50(%rbp), %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x64(%rbp), %ecx
movb %dl, (%rax,%rcx)
movq -0x18(%rbp), %rax
movw %ax, %dx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x64(%rbp), %ecx
movw %dx, (%rax,%rcx,2)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x11971
jmp 0x11ac0
movl -0x58(%rbp), %eax
andl $0x1ff, %eax            # imm = 0x1FF
movl %eax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x68(%rbp), %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
subl $0x9, %eax
movl %eax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x68(%rbp), %ecx
movzwl (%rax,%rcx,2), %eax
movl %eax, -0x74(%rbp)
movl -0x70(%rbp), %ecx
movl -0x50(%rbp), %eax
subl $0x9, %eax
subl %eax, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x78(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jae 0x11a45
movl $0x37, -0x4(%rbp)
jmp 0x11b97
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x78(%rbp), %eax
jae 0x11abe
movl -0x58(%rbp), %eax
shrl $0x9, %eax
movl %eax, -0x80(%rbp)
movl -0x74(%rbp), %eax
movl -0x80(%rbp), %ecx
movl %ecx, -0x8c(%rbp)
movl -0x7c(%rbp), %edx
movl -0x50(%rbp), %ecx
subl $0x9, %ecx
shll %cl, %edx
movl -0x8c(%rbp), %ecx
orl %edx, %ecx
addl %ecx, %eax
movl %eax, -0x84(%rbp)
movl -0x50(%rbp), %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x84(%rbp), %ecx
movb %dl, (%rax,%rcx)
movq -0x18(%rbp), %rax
movw %ax, %dx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x84(%rbp), %ecx
movw %dx, (%rax,%rcx,2)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x11a4c
jmp 0x11ac0
jmp 0x11ac2
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x118f2
cmpq $0x2, -0x20(%rbp)
jae 0x11b4e
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x11b4c
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x10, %eax
jne 0x11b3c
movq -0x18(%rbp), %rdx
movl $0xa, %eax
movl $0x1, %ecx
cmpq $0x200, %rdx            # imm = 0x200
cmovbl %ecx, %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movw $0xffff, (%rax,%rcx,2)  # imm = 0xFFFF
jmp 0x11b3e
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x11ae2
jmp 0x11b90
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x11b8e
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x10, %eax
jne 0x11b7e
movl $0x37, -0x4(%rbp)
jmp 0x11b97
jmp 0x11b80
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x11b56
jmp 0x11b90
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)