// File: lvandeve[P]lodepng/lodepng.cpp
// Function: getTreeInflateDynamic(HuffmanTree*, HuffmanTree*, LodePNGBitReader*)
static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d,
                                      LodePNGBitReader* reader) {
  /*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;

  /*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(reader->bitsize - reader->bp < 14) return 49; /*error: the bit pointer is or will go past the memory*/
  ensureBits17(reader, 14);

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

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

  HuffmanTree_init(&tree_cl);

  while(!error) {
    /*read the code length codes out of 3 * (amount of code length codes) bits*/
    if(lodepng_gtofl(reader->bp, HCLEN * 3, reader->bitsize)) {
      ERROR_BREAK(50); /*error: the bit pointer is or will go past the memory*/
    }
    for(i = 0; i != HCLEN; ++i) {
      ensureBits9(reader, 3); /*out of bounds already checked above */
      bitlen_cl[CLCL_ORDER[i]] = readBits(reader, 3);
    }
    for(i = HCLEN; i != NUM_CODE_LENGTH_CODES; ++i) {
      bitlen_cl[CLCL_ORDER[i]] = 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*/);
    lodepng_memset(bitlen_ll, 0, NUM_DEFLATE_CODE_SYMBOLS * sizeof(*bitlen_ll));
    lodepng_memset(bitlen_d, 0, NUM_DISTANCE_SYMBOLS * sizeof(*bitlen_d));

    /*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;
      ensureBits25(reader, 22); /* up to 15 bits for huffman code, up to 7 extra bits below*/
      code = huffmanDecodeSymbol(reader, &tree_cl);
      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*/

        replength += readBits(reader, 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*/
        replength += readBits(reader, 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*/
        replength += readBits(reader, 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 == INVALIDSYMBOL)*/ {
        ERROR_BREAK(16); /*error: tried to read disallowed huffman symbol*/
      }
      /*check if any of the ensureBits above went out of bounds*/
      if(reader->bp > reader->bitsize) {
        /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol
        (10=no endcode, 11=wrong jump outside of tree)*/
        /* TODO: revise error codes 10,11,50: the above comment is no longer valid */
        ERROR_BREAK(50); /*error, bit pointer jumps past memory*/
      }
    }
    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:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
subq 0x18(%rcx), %rax
cmpq $0xe, %rax
jae 0x12e48
movl $0x31, -0x4(%rbp)
jmp 0x1332f
movq -0x20(%rbp), %rdi
movl $0xe, %esi
callq 0x14140
movq -0x20(%rbp), %rdi
movl $0x5, %esi
callq 0x12590
addl $0x101, %eax            # imm = 0x101
movl %eax, -0x2c(%rbp)
movq -0x20(%rbp), %rdi
movl $0x5, %esi
callq 0x12590
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
movl $0x4, %esi
callq 0x12590
addl $0x4, %eax
movl %eax, -0x34(%rbp)
movl $0x4c, %edi
callq 0x3bc0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x12eb5
movl $0x53, -0x4(%rbp)
jmp 0x1332f
leaq -0x78(%rbp), %rdi
callq 0x12d70
cmpl $0x0, -0x24(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x12ed0
jmp 0x13305
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdi
imull $0x3, -0x34(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x14250
cmpl $0x0, %eax
je 0x12efe
movl $0x32, -0x24(%rbp)
jmp 0x13305
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
je 0x12f4c
movq -0x20(%rbp), %rdi
movl $0x3, %esi
callq 0x124c0
movq -0x20(%rbp), %rdi
movl $0x3, %esi
callq 0x12590
movl %eax, %edx
movq -0x50(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %esi
leaq 0x9e545(%rip), %rcx     # 0xb1480
movl (%rcx,%rsi,4), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x12f05
movl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
cmpl $0x13, -0x38(%rbp)
je 0x12f7d
movq -0x50(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %edx
leaq 0x9e518(%rip), %rcx     # 0xb1480
movl (%rcx,%rdx,4), %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x12f52
movq -0x50(%rbp), %rsi
leaq -0x78(%rbp), %rdi
movl $0x13, %edx
movl $0x7, %ecx
callq 0x13920
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x12fa2
jmp 0x13305
movl $0x480, %edi            # imm = 0x480
callq 0x3bc0
movq %rax, -0x40(%rbp)
movl $0x80, %edi
callq 0x3bc0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x12fcc
cmpq $0x0, -0x48(%rbp)
jne 0x12fd8
movl $0x53, -0x24(%rbp)
jmp 0x13305
movq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x480, %edx            # imm = 0x480
callq 0x3be0
movq -0x48(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x3be0
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movl -0x2c(%rbp), %ecx
addl -0x30(%rbp), %ecx
cmpl %ecx, %eax
jae 0x132ab
movq -0x20(%rbp), %rdi
movl $0x16, %esi
callq 0x135e0
movq -0x20(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x13500
movl %eax, -0x7c(%rbp)
cmpl $0xf, -0x7c(%rbp)
ja 0x1306b
movl -0x38(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x1304b
movl -0x7c(%rbp), %edx
movq -0x40(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x1305d
movl -0x7c(%rbp), %edx
movq -0x48(%rbp), %rax
movl -0x38(%rbp), %ecx
subl -0x2c(%rbp), %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1328b
cmpl $0x10, -0x7c(%rbp)
jne 0x1314b
movl $0x3, -0x80(%rbp)
cmpl $0x0, -0x38(%rbp)
jne 0x1308e
movl $0x36, -0x24(%rbp)
jmp 0x132ab
movq -0x20(%rbp), %rdi
movl $0x2, %esi
callq 0x12590
addl -0x80(%rbp), %eax
movl %eax, -0x80(%rbp)
movl -0x38(%rbp), %eax
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jae 0x130c6
movq -0x40(%rbp), %rax
movl -0x38(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x84(%rbp)
jmp 0x130de
movq -0x48(%rbp), %rax
movl -0x38(%rbp), %ecx
subl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x84(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x80(%rbp), %eax
jae 0x13146
movl -0x38(%rbp), %eax
movl -0x2c(%rbp), %ecx
addl -0x30(%rbp), %ecx
cmpl %ecx, %eax
jb 0x13103
movl $0xd, -0x24(%rbp)
jmp 0x13146
movl -0x38(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x1311d
movl -0x84(%rbp), %edx
movq -0x40(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x13132
movl -0x84(%rbp), %edx
movq -0x48(%rbp), %rax
movl -0x38(%rbp), %ecx
subl -0x2c(%rbp), %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x130e5
jmp 0x13289
cmpl $0x11, -0x7c(%rbp)
jne 0x131e5
movl $0x3, -0x88(%rbp)
movq -0x20(%rbp), %rdi
movl $0x3, %esi
callq 0x12590
addl -0x88(%rbp), %eax
movl %eax, -0x88(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x88(%rbp), %eax
jae 0x131e0
movl -0x38(%rbp), %eax
movl -0x2c(%rbp), %ecx
addl -0x30(%rbp), %ecx
cmpl %ecx, %eax
jb 0x131a1
movl $0xe, -0x24(%rbp)
jmp 0x131e0
movl -0x38(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x131b9
movq -0x40(%rbp), %rax
movl -0x38(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
jmp 0x131cc
movq -0x48(%rbp), %rax
movl -0x38(%rbp), %ecx
subl -0x2c(%rbp), %ecx
movl %ecx, %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x13180
jmp 0x13287
cmpl $0x12, -0x7c(%rbp)
jne 0x1327c
movl $0xb, -0x8c(%rbp)
movq -0x20(%rbp), %rdi
movl $0x7, %esi
callq 0x12590
addl -0x8c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jae 0x1327a
movl -0x38(%rbp), %eax
movl -0x2c(%rbp), %ecx
addl -0x30(%rbp), %ecx
cmpl %ecx, %eax
jb 0x1323b
movl $0xf, -0x24(%rbp)
jmp 0x1327a
movl -0x38(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x13253
movq -0x40(%rbp), %rax
movl -0x38(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
jmp 0x13266
movq -0x48(%rbp), %rax
movl -0x38(%rbp), %ecx
subl -0x2c(%rbp), %ecx
movl %ecx, %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1321a
jmp 0x13285
movl $0x10, -0x24(%rbp)
jmp 0x132ab
jmp 0x13287
jmp 0x13289
jmp 0x1328b
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jbe 0x132a6
movl $0x32, -0x24(%rbp)
jmp 0x132ab
jmp 0x12fff
cmpl $0x0, -0x24(%rbp)
je 0x132b3
jmp 0x13305
movq -0x40(%rbp), %rax
cmpl $0x0, 0x400(%rax)
jne 0x132c9
movl $0x40, -0x24(%rbp)
jmp 0x13305
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl $0x120, %edx            # imm = 0x120
movl $0xf, %ecx
callq 0x13920
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x132eb
jmp 0x13305
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl $0x20, %edx
movl $0xf, %ecx
callq 0x13920
movl %eax, -0x24(%rbp)
movq -0x50(%rbp), %rdi
callq 0x4230
movq -0x40(%rbp), %rdi
callq 0x4230
movq -0x48(%rbp), %rdi
callq 0x4230
leaq -0x78(%rbp), %rdi
callq 0x13730
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)