// File: drummyfish[P]flowchart/src/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) >> 3 >= inlength - 2) 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;

  HuffmanTree_init(&tree_cl);

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

    bitlen_cl = (unsigned*)mymalloc(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*)mymalloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned));
    bitlen_d = (unsigned*)mymalloc(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(*bp >= inbitlength) ERROR_BREAK(50); /*error, bit pointer jumps past memory*/
        if (i == 0) ERROR_BREAK(54); /*can't repeat previous if i is 0*/

        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 >= 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 >= 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*/
  }

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

  return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x30(%rbp), %rax
shlq $0x3, %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
shrq $0x3, %rax
movq -0x30(%rbp), %rcx
subq $0x2, %rcx
cmpq %rcx, %rax
jb 0xd89e
movl $0x31, -0x4(%rbp)
jmp 0xddf9
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x5, %edx
callq 0xdee0
addl $0x101, %eax            # imm = 0x101
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x5, %edx
callq 0xdee0
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x4, %edx
callq 0xdee0
addl $0x4, %eax
movl %eax, -0x44(%rbp)
leaq -0x88(%rbp), %rdi
callq 0xd7d0
cmpl $0x0, -0x34(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xd906
jmp 0xddcc
movl $0x4c, %edi
callq 0x3840
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0xd927
movl $0x53, -0x34(%rbp)
jmp 0xddcc
movl $0x0, -0x48(%rbp)
cmpl $0x13, -0x48(%rbp)
jae 0xd98f
movl -0x48(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0xd968
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x3, %edx
callq 0xdee0
movl %eax, %edx
movq -0x68(%rbp), %rax
movl -0x48(%rbp), %ecx
movl %ecx, %esi
leaq 0x10ad0(%rip), %rcx     # 0x1e430
movl (%rcx,%rsi,4), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0xd982
movq -0x68(%rbp), %rax
movl -0x48(%rbp), %ecx
movl %ecx, %edx
leaq 0x10ab8(%rip), %rcx     # 0x1e430
movl (%rcx,%rdx,4), %ecx
movl $0x0, (%rax,%rcx,4)
jmp 0xd984
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xd92e
movq -0x68(%rbp), %rsi
leaq -0x88(%rbp), %rdi
movl $0x13, %edx
movl $0x7, %ecx
callq 0xe140
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0xd9b7
jmp 0xddcc
movl $0x480, %edi            # imm = 0x480
callq 0x3840
movq %rax, -0x58(%rbp)
movl $0x80, %edi
callq 0x3840
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0xd9e1
cmpq $0x0, -0x60(%rbp)
jne 0xd9ed
movl $0x53, -0x34(%rbp)
jmp 0xddcc
movl $0x0, -0x48(%rbp)
cmpl $0x120, -0x48(%rbp)     # imm = 0x120
jae 0xda16
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xd9f4
movl $0x0, -0x48(%rbp)
cmpl $0x20, -0x48(%rbp)
jae 0xda3c
movq -0x60(%rbp), %rax
movl -0x48(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xda1d
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movl -0x3c(%rbp), %ecx
addl -0x40(%rbp), %ecx
cmpl %ecx, %eax
jae 0xdd72
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x50(%rbp), %rcx
leaq -0x88(%rbp), %rdx
callq 0xde10
movl %eax, -0x8c(%rbp)
cmpl $0xf, -0x8c(%rbp)
ja 0xdab8
movl -0x48(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0xda95
movl -0x8c(%rbp), %edx
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0xdaaa
movl -0x8c(%rbp), %edx
movq -0x60(%rbp), %rax
movl -0x48(%rbp), %ecx
subl -0x3c(%rbp), %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xdd6d
cmpl $0x10, -0x8c(%rbp)
jne 0xdbc4
movl $0x3, -0x90(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
cmpq -0x50(%rbp), %rax
jb 0xdae8
movl $0x32, -0x34(%rbp)
jmp 0xdd72
cmpl $0x0, -0x48(%rbp)
jne 0xdafa
movl $0x36, -0x34(%rbp)
jmp 0xdd72
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x2, %edx
callq 0xdee0
addl -0x90(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x48(%rbp), %eax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jae 0xdb3c
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x94(%rbp)
jmp 0xdb54
movq -0x60(%rbp), %rax
movl -0x48(%rbp), %ecx
subl -0x3c(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x94(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x90(%rbp), %eax
jae 0xdbbf
movl -0x48(%rbp), %eax
movl -0x3c(%rbp), %ecx
addl -0x40(%rbp), %ecx
cmpl %ecx, %eax
jb 0xdb7c
movl $0xd, -0x34(%rbp)
jmp 0xdbbf
movl -0x48(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0xdb96
movl -0x94(%rbp), %edx
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0xdbab
movl -0x94(%rbp), %edx
movq -0x60(%rbp), %rax
movl -0x48(%rbp), %ecx
subl -0x3c(%rbp), %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xdb5b
jmp 0xdd6b
cmpl $0x11, -0x8c(%rbp)
jne 0xdc7e
movl $0x3, -0x98(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
cmpq -0x50(%rbp), %rax
jb 0xdbf4
movl $0x32, -0x34(%rbp)
jmp 0xdd72
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x3, %edx
callq 0xdee0
addl -0x98(%rbp), %eax
movl %eax, -0x98(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x98(%rbp), %eax
jae 0xdc79
movl -0x48(%rbp), %eax
movl -0x3c(%rbp), %ecx
addl -0x40(%rbp), %ecx
cmpl %ecx, %eax
jb 0xdc3a
movl $0xe, -0x34(%rbp)
jmp 0xdc79
movl -0x48(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0xdc52
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
jmp 0xdc65
movq -0x60(%rbp), %rax
movl -0x48(%rbp), %ecx
subl -0x3c(%rbp), %ecx
movl %ecx, %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xdc19
jmp 0xdd69
cmpl $0x12, -0x8c(%rbp)
jne 0xdd35
movl $0xb, -0x9c(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
cmpq -0x50(%rbp), %rax
jb 0xdcae
movl $0x32, -0x34(%rbp)
jmp 0xdd72
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x7, %edx
callq 0xdee0
addl -0x9c(%rbp), %eax
movl %eax, -0x9c(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x9c(%rbp), %eax
jae 0xdd33
movl -0x48(%rbp), %eax
movl -0x3c(%rbp), %ecx
addl -0x40(%rbp), %ecx
cmpl %ecx, %eax
jb 0xdcf4
movl $0xf, -0x34(%rbp)
jmp 0xdd33
movl -0x48(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0xdd0c
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
jmp 0xdd1f
movq -0x60(%rbp), %rax
movl -0x48(%rbp), %ecx
subl -0x3c(%rbp), %ecx
movl %ecx, %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xdcd3
jmp 0xdd67
cmpl $-0x1, -0x8c(%rbp)
jne 0xdd5e
movq -0x28(%rbp), %rax
movq (%rax), %rdx
movq -0x50(%rbp), %rsi
movl $0xb, %eax
movl $0xa, %ecx
cmpq %rsi, %rdx
cmoval %ecx, %eax
movl %eax, -0x34(%rbp)
jmp 0xdd65
movl $0x10, -0x34(%rbp)
jmp 0xdd72
jmp 0xdd69
jmp 0xdd6b
jmp 0xdd6d
jmp 0xda43
cmpl $0x0, -0x34(%rbp)
je 0xdd7a
jmp 0xddcc
movq -0x58(%rbp), %rax
cmpl $0x0, 0x400(%rax)
jne 0xdd90
movl $0x40, -0x34(%rbp)
jmp 0xddcc
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl $0x120, %edx            # imm = 0x120
movl $0xf, %ecx
callq 0xe140
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0xddb2
jmp 0xddcc
movq -0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl $0x20, %edx
movl $0xf, %ecx
callq 0xe140
movl %eax, -0x34(%rbp)
movq -0x68(%rbp), %rdi
callq 0x4050
movq -0x58(%rbp), %rdi
callq 0x4050
movq -0x60(%rbp), %rdi
callq 0x4050
leaq -0x88(%rbp), %rdi
callq 0xdf60
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)