// File: phillip-h[P]fractal-gen/lodepng.cpp
// Function: getTreeInflateDynamic(HuffmanTree*, HuffmanTree*, unsigned char const*, unsigned long*, unsigned long)
static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d,
                                      const unsigned char* in, size_t* bp, size_t inlength)
{
  /*make sure that length values that aren't filled in will be 0, or a wrong tree will be generated*/
  unsigned error = 0;
  unsigned n, HLIT, HDIST, HCLEN, i;
  size_t inbitlength = inlength * 8;

  /*see comments in deflateDynamic for explanation of the context and these variables, it is analogous*/
  unsigned* bitlen_ll = 0; /*lit,len code lengths*/
  unsigned* bitlen_d = 0; /*dist code lengths*/
  /*code length code lengths ("clcl"), the bit lengths of the huffman tree used to compress bitlen_ll and bitlen_d*/
  unsigned* bitlen_cl = 0;
  HuffmanTree tree_cl; /*the code tree for code length codes (the huffman tree for compressed huffman trees)*/

  if((*bp) + 14 > (inlength << 3)) return 49; /*error: the bit pointer is or will go past the memory*/

  /*number of literal/length codes + 257. Unlike the spec, the value 257 is added to it here already*/
  HLIT =  readBitsFromStream(bp, in, 5) + 257;
  /*number of distance codes. Unlike the spec, the value 1 is added to it here already*/
  HDIST = readBitsFromStream(bp, in, 5) + 1;
  /*number of code length codes. Unlike the spec, the value 4 is added to it here already*/
  HCLEN = readBitsFromStream(bp, in, 4) + 4;

  if((*bp) + HCLEN * 3 > (inlength << 3)) return 50; /*error: the bit pointer is or will go past the memory*/

  HuffmanTree_init(&tree_cl);

  while(!error)
  {
    /*read the code length codes out of 3 * (amount of code length codes) bits*/

    bitlen_cl = (unsigned*)lodepng_malloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned));
    if(!bitlen_cl) ERROR_BREAK(83 /*alloc fail*/);

    for(i = 0; i != NUM_CODE_LENGTH_CODES; ++i)
    {
      if(i < HCLEN) bitlen_cl[CLCL_ORDER[i]] = readBitsFromStream(bp, in, 3);
      else bitlen_cl[CLCL_ORDER[i]] = 0; /*if not, it must stay 0*/
    }

    error = HuffmanTree_makeFromLengths(&tree_cl, bitlen_cl, NUM_CODE_LENGTH_CODES, 7);
    if(error) break;

    /*now we can use this tree to read the lengths for the tree that this function will return*/
    bitlen_ll = (unsigned*)lodepng_malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned));
    bitlen_d = (unsigned*)lodepng_malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned));
    if(!bitlen_ll || !bitlen_d) ERROR_BREAK(83 /*alloc fail*/);
    for(i = 0; i != NUM_DEFLATE_CODE_SYMBOLS; ++i) bitlen_ll[i] = 0;
    for(i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen_d[i] = 0;

    /*i is the current symbol we're reading in the part that contains the code lengths of lit/len and dist codes*/
    i = 0;
    while(i < HLIT + HDIST)
    {
      unsigned code = huffmanDecodeSymbol(in, bp, &tree_cl, inbitlength);
      if(code <= 15) /*a length code*/
      {
        if(i < HLIT) bitlen_ll[i] = code;
        else bitlen_d[i - HLIT] = code;
        ++i;
      }
      else if(code == 16) /*repeat previous*/
      {
        unsigned replength = 3; /*read in the 2 bits that indicate repeat length (3-6)*/
        unsigned value; /*set value to the previous code*/

        if(i == 0) ERROR_BREAK(54); /*can't repeat previous if i is 0*/

        if((*bp + 2) > inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/
        replength += readBitsFromStream(bp, in, 2);

        if(i < HLIT + 1) value = bitlen_ll[i - 1];
        else value = bitlen_d[i - HLIT - 1];
        /*repeat this value in the next lengths*/
        for(n = 0; n < replength; ++n)
        {
          if(i >= HLIT + HDIST) ERROR_BREAK(13); /*error: i is larger than the amount of codes*/
          if(i < HLIT) bitlen_ll[i] = value;
          else bitlen_d[i - HLIT] = value;
          ++i;
        }
      }
      else if(code == 17) /*repeat "0" 3-10 times*/
      {
        unsigned replength = 3; /*read in the bits that indicate repeat length*/
        if((*bp + 3) > inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/
        replength += readBitsFromStream(bp, in, 3);

        /*repeat this value in the next lengths*/
        for(n = 0; n < replength; ++n)
        {
          if(i >= HLIT + HDIST) ERROR_BREAK(14); /*error: i is larger than the amount of codes*/

          if(i < HLIT) bitlen_ll[i] = 0;
          else bitlen_d[i - HLIT] = 0;
          ++i;
        }
      }
      else if(code == 18) /*repeat "0" 11-138 times*/
      {
        unsigned replength = 11; /*read in the bits that indicate repeat length*/
        if((*bp + 7) > inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/
        replength += readBitsFromStream(bp, in, 7);

        /*repeat this value in the next lengths*/
        for(n = 0; n < replength; ++n)
        {
          if(i >= HLIT + HDIST) ERROR_BREAK(15); /*error: i is larger than the amount of codes*/

          if(i < HLIT) bitlen_ll[i] = 0;
          else bitlen_d[i - HLIT] = 0;
          ++i;
        }
      }
      else /*if(code == (unsigned)(-1))*/ /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/
      {
        if(code == (unsigned)(-1))
        {
          /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol
          (10=no endcode, 11=wrong jump outside of tree)*/
          error = (*bp) > inbitlength ? 10 : 11;
        }
        else error = 16; /*unexisting code, this can never happen*/
        break;
      }
    }
    if(error) break;

    if(bitlen_ll[256] == 0) ERROR_BREAK(64); /*the length of the end code 256 must be larger than 0*/

    /*now we've finally got HLIT and HDIST, so generate the code trees, and the function is done*/
    error = HuffmanTree_makeFromLengths(tree_ll, bitlen_ll, NUM_DEFLATE_CODE_SYMBOLS, 15);
    if(error) break;
    error = HuffmanTree_makeFromLengths(tree_d, bitlen_d, NUM_DISTANCE_SYMBOLS, 15);

    break; /*end of error-while*/
  }

  lodepng_free(bitlen_cl);
  lodepng_free(bitlen_ll);
  lodepng_free(bitlen_d);
  HuffmanTree_cleanup(&tree_cl);

  return error;
}

// Assembly:
subq $0xa8, %rsp
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movq %rdx, 0x88(%rsp)
movq %rcx, 0x80(%rsp)
movq %r8, 0x78(%rsp)
movl $0x0, 0x74(%rsp)
movq 0x78(%rsp), %rax
shlq $0x3, %rax
movq %rax, 0x58(%rsp)
movq $0x0, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x80(%rsp), %rax
movq (%rax), %rax
addq $0xe, %rax
movq 0x78(%rsp), %rcx
shlq $0x3, %rcx
cmpq %rcx, %rax
jbe 0x1250a
movl $0x31, 0xa4(%rsp)
jmp 0x12b4c
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rsi
movl $0x5, %edx
callq 0x12c40
addl $0x101, %eax            # imm = 0x101
movl %eax, 0x6c(%rsp)
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rsi
movl $0x5, %edx
callq 0x12c40
addl $0x1, %eax
movl %eax, 0x68(%rsp)
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rsi
movl $0x4, %edx
callq 0x12c40
addl $0x4, %eax
movl %eax, 0x64(%rsp)
movq 0x80(%rsp), %rax
movq (%rax), %rax
imull $0x3, 0x64(%rsp), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq 0x78(%rsp), %rcx
shlq $0x3, %rcx
cmpq %rcx, %rax
jbe 0x125a2
movl $0x32, 0xa4(%rsp)
jmp 0x12b4c
leaq 0x20(%rsp), %rdi
callq 0x12420
cmpl $0x0, 0x74(%rsp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x125bf
jmp 0x12b19
movl $0x4c, %edi
callq 0x6cd0
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x40(%rsp)
jne 0x125e3
movl $0x53, 0x74(%rsp)
jmp 0x12b19
movl $0x0, 0x60(%rsp)
cmpl $0x13, 0x60(%rsp)
je 0x1265d
movl 0x60(%rsp), %eax
cmpl 0x64(%rsp), %eax
jae 0x12632
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rsi
movl $0x3, %edx
callq 0x12c40
movl %eax, %edx
movq 0x40(%rsp), %rax
movl 0x60(%rsp), %ecx
movl %ecx, %esi
leaq 0xa156(%rip), %rcx      # 0x1c780
movl (%rcx,%rsi,4), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x1264e
movq 0x40(%rsp), %rax
movl 0x60(%rsp), %ecx
movl %ecx, %edx
leaq 0xa13c(%rip), %rcx      # 0x1c780
movl (%rcx,%rdx,4), %ecx
movl $0x0, (%rax,%rcx,4)
jmp 0x12650
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x125eb
movq 0x40(%rsp), %rsi
leaq 0x20(%rsp), %rdi
movl $0x13, %edx
movl $0x7, %ecx
callq 0x12ec0
movl %eax, 0x74(%rsp)
cmpl $0x0, 0x74(%rsp)
je 0x12686
jmp 0x12b19
movl $0x480, %edi            # imm = 0x480
callq 0x6cd0
movq %rax, 0x50(%rsp)
movl $0x80, %edi
callq 0x6cd0
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x50(%rsp)
je 0x126b4
cmpq $0x0, 0x48(%rsp)
jne 0x126c1
movl $0x53, 0x74(%rsp)
jmp 0x12b19
movl $0x0, 0x60(%rsp)
cmpl $0x120, 0x60(%rsp)      # imm = 0x120
je 0x126f0
movq 0x50(%rsp), %rax
movl 0x60(%rsp), %ecx
movl $0x0, (%rax,%rcx,4)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x126c9
movl $0x0, 0x60(%rsp)
cmpl $0x20, 0x60(%rsp)
je 0x1271c
movq 0x48(%rsp), %rax
movl 0x60(%rsp), %ecx
movl $0x0, (%rax,%rcx,4)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x126f8
movl $0x0, 0x60(%rsp)
movl 0x60(%rsp), %eax
movl 0x6c(%rsp), %ecx
addl 0x68(%rsp), %ecx
cmpl %ecx, %eax
jae 0x12aaf
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq 0x58(%rsp), %rcx
leaq 0x20(%rsp), %rdx
callq 0x12b60
movl %eax, 0x1c(%rsp)
cmpl $0xf, 0x1c(%rsp)
ja 0x127a4
movl 0x60(%rsp), %eax
cmpl 0x6c(%rsp), %eax
jae 0x1277e
movl 0x1c(%rsp), %edx
movq 0x50(%rsp), %rax
movl 0x60(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x12794
movl 0x1c(%rsp), %edx
movq 0x48(%rsp), %rax
movl 0x60(%rsp), %ecx
subl 0x6c(%rsp), %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x12aaa
cmpl $0x10, 0x1c(%rsp)
jne 0x128ca
movl $0x3, 0x18(%rsp)
cmpl $0x0, 0x60(%rsp)
jne 0x127cb
movl $0x36, 0x74(%rsp)
jmp 0x12aaf
movq 0x80(%rsp), %rax
movq (%rax), %rax
addq $0x2, %rax
cmpq 0x58(%rsp), %rax
jbe 0x127ee
movl $0x32, 0x74(%rsp)
jmp 0x12aaf
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rsi
movl $0x2, %edx
callq 0x12c40
addl 0x18(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0x60(%rsp), %eax
movl 0x6c(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jae 0x12836
movq 0x50(%rsp), %rax
movl 0x60(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, 0x14(%rsp)
jmp 0x1284f
movq 0x48(%rsp), %rax
movl 0x60(%rsp), %ecx
subl 0x6c(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, 0x14(%rsp)
movl $0x0, 0x70(%rsp)
movl 0x70(%rsp), %eax
cmpl 0x18(%rsp), %eax
jae 0x128c5
movl 0x60(%rsp), %eax
movl 0x6c(%rsp), %ecx
addl 0x68(%rsp), %ecx
cmpl %ecx, %eax
jb 0x1287b
movl $0xd, 0x74(%rsp)
jmp 0x128c5
movl 0x60(%rsp), %eax
cmpl 0x6c(%rsp), %eax
jae 0x12897
movl 0x14(%rsp), %edx
movq 0x50(%rsp), %rax
movl 0x60(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x128ad
movl 0x14(%rsp), %edx
movq 0x48(%rsp), %rax
movl 0x60(%rsp), %ecx
subl 0x6c(%rsp), %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0x12857
jmp 0x12aa8
cmpl $0x11, 0x1c(%rsp)
jne 0x1299d
movl $0x3, 0x10(%rsp)
movq 0x80(%rsp), %rax
movq (%rax), %rax
addq $0x3, %rax
cmpq 0x58(%rsp), %rax
jbe 0x12900
movl $0x32, 0x74(%rsp)
jmp 0x12aaf
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rsi
movl $0x3, %edx
callq 0x12c40
addl 0x10(%rsp), %eax
movl %eax, 0x10(%rsp)
movl $0x0, 0x70(%rsp)
movl 0x70(%rsp), %eax
cmpl 0x10(%rsp), %eax
jae 0x12998
movl 0x60(%rsp), %eax
movl 0x6c(%rsp), %ecx
addl 0x68(%rsp), %ecx
cmpl %ecx, %eax
jb 0x1294e
movl $0xe, 0x74(%rsp)
jmp 0x12998
movl 0x60(%rsp), %eax
cmpl 0x6c(%rsp), %eax
jae 0x1296a
movq 0x50(%rsp), %rax
movl 0x60(%rsp), %ecx
movl $0x0, (%rax,%rcx,4)
jmp 0x12980
movq 0x48(%rsp), %rax
movl 0x60(%rsp), %ecx
subl 0x6c(%rsp), %ecx
movl %ecx, %ecx
movl $0x0, (%rax,%rcx,4)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0x1292a
jmp 0x12aa6
cmpl $0x12, 0x1c(%rsp)
jne 0x12a6d
movl $0xb, 0xc(%rsp)
movq 0x80(%rsp), %rax
movq (%rax), %rax
addq $0x7, %rax
cmpq 0x58(%rsp), %rax
jbe 0x129d3
movl $0x32, 0x74(%rsp)
jmp 0x12aaf
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rsi
movl $0x7, %edx
callq 0x12c40
addl 0xc(%rsp), %eax
movl %eax, 0xc(%rsp)
movl $0x0, 0x70(%rsp)
movl 0x70(%rsp), %eax
cmpl 0xc(%rsp), %eax
jae 0x12a6b
movl 0x60(%rsp), %eax
movl 0x6c(%rsp), %ecx
addl 0x68(%rsp), %ecx
cmpl %ecx, %eax
jb 0x12a21
movl $0xf, 0x74(%rsp)
jmp 0x12a6b
movl 0x60(%rsp), %eax
cmpl 0x6c(%rsp), %eax
jae 0x12a3d
movq 0x50(%rsp), %rax
movl 0x60(%rsp), %ecx
movl $0x0, (%rax,%rcx,4)
jmp 0x12a53
movq 0x48(%rsp), %rax
movl 0x60(%rsp), %ecx
subl 0x6c(%rsp), %ecx
movl %ecx, %ecx
movl $0x0, (%rax,%rcx,4)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0x129fd
jmp 0x12aa4
cmpl $-0x1, 0x1c(%rsp)
jne 0x12a9a
movq 0x80(%rsp), %rax
movq (%rax), %rdx
movq 0x58(%rsp), %rsi
movl $0xb, %eax
movl $0xa, %ecx
cmpq %rsi, %rdx
cmoval %ecx, %eax
movl %eax, 0x74(%rsp)
jmp 0x12aa2
movl $0x10, 0x74(%rsp)
jmp 0x12aaf
jmp 0x12aa6
jmp 0x12aa8
jmp 0x12aaa
jmp 0x12724
cmpl $0x0, 0x74(%rsp)
je 0x12ab8
jmp 0x12b19
movq 0x50(%rsp), %rax
cmpl $0x0, 0x400(%rax)
jne 0x12ad0
movl $0x40, 0x74(%rsp)
jmp 0x12b19
movq 0x98(%rsp), %rdi
movq 0x50(%rsp), %rsi
movl $0x120, %edx            # imm = 0x120
movl $0xf, %ecx
callq 0x12ec0
movl %eax, 0x74(%rsp)
cmpl $0x0, 0x74(%rsp)
je 0x12af9
jmp 0x12b19
movq 0x90(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl $0x20, %edx
movl $0xf, %ecx
callq 0x12ec0
movl %eax, 0x74(%rsp)
movq 0x40(%rsp), %rdi
callq 0x78b0
movq 0x50(%rsp), %rdi
callq 0x78b0
movq 0x48(%rsp), %rdi
callq 0x78b0
leaq 0x20(%rsp), %rdi
callq 0x12cc0
movl 0x74(%rsp), %eax
movl %eax, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
addq $0xa8, %rsp
retq
nopl (%rax,%rax)