// File: /workspace/github_c_makefile_star2_O0/Joy-Division[P]tools-mgs/lodepng/lodepng.c
// Function: inflateHuffmanBlock
static unsigned inflateHuffmanBlock(ucvector* out, const unsigned char* in, size_t* bp,
                                    size_t* pos, size_t inlength, unsigned btype)
{
  unsigned error = 0;
  HuffmanTree tree_ll; /*the huffman tree for literal and length codes*/
  HuffmanTree tree_d; /*the huffman tree for distance codes*/
  size_t inbitlength = inlength * 8;

  HuffmanTree_init(&tree_ll);
  HuffmanTree_init(&tree_d);

  if(btype == 1) getTreeInflateFixed(&tree_ll, &tree_d);
  else if(btype == 2) error = getTreeInflateDynamic(&tree_ll, &tree_d, in, bp, inlength);

  while(!error) /*decode all symbols until end reached, breaks at end code*/
  {
    /*code_ll is literal, length or end code*/
    unsigned code_ll = huffmanDecodeSymbol(in, bp, &tree_ll, inbitlength);
    if(code_ll <= 255) /*literal symbol*/
    {
      /*ucvector_push_back would do the same, but for some reason the two lines below run 10% faster*/
      if(!ucvector_resize(out, (*pos) + 1)) ERROR_BREAK(83 /*alloc fail*/);
      out->data[*pos] = (unsigned char)code_ll;
      ++(*pos);
    }
    else if(code_ll >= FIRST_LENGTH_CODE_INDEX && code_ll <= LAST_LENGTH_CODE_INDEX) /*length code*/
    {
      unsigned code_d, distance;
      unsigned numextrabits_l, numextrabits_d; /*extra bits for length and distance*/
      size_t start, forward, backward, length;

      /*part 1: get length base*/
      length = LENGTHBASE[code_ll - FIRST_LENGTH_CODE_INDEX];

      /*part 2: get extra bits and add the value of that to length*/
      numextrabits_l = LENGTHEXTRA[code_ll - FIRST_LENGTH_CODE_INDEX];
      if((*bp + numextrabits_l) > inbitlength) ERROR_BREAK(51); /*error, bit pointer will jump past memory*/
      length += readBitsFromStream(bp, in, numextrabits_l);

      /*part 3: get distance code*/
      code_d = huffmanDecodeSymbol(in, bp, &tree_d, inbitlength);
      if(code_d > 29)
      {
        if(code_ll == (unsigned)(-1)) /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/
        {
          /*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) > inlength * 8 ? 10 : 11;
        }
        else error = 18; /*error: invalid distance code (30-31 are never used)*/
        break;
      }
      distance = DISTANCEBASE[code_d];

      /*part 4: get extra bits from distance*/
      numextrabits_d = DISTANCEEXTRA[code_d];
      if((*bp + numextrabits_d) > inbitlength) ERROR_BREAK(51); /*error, bit pointer will jump past memory*/
      distance += readBitsFromStream(bp, in, numextrabits_d);

      /*part 5: fill in all the out[n] values based on the length and dist*/
      start = (*pos);
      if(distance > start) ERROR_BREAK(52); /*too long backward distance*/
      backward = start - distance;

      if(!ucvector_resize(out, (*pos) + length)) ERROR_BREAK(83 /*alloc fail*/);
      if (distance < length) {
        for(forward = 0; forward < length; ++forward)
        {
          out->data[(*pos)++] = out->data[backward++];
        }
      } else {
        memcpy(out->data + *pos, out->data + backward, length);
        *pos += length;
      }
    }
    else if(code_ll == 256)
    {
      break; /*end code, break the loop*/
    }
    else /*if(code == (unsigned)(-1))*/ /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/
    {
      /*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) > inlength * 8) ? 10 : 11;
      break;
    }
  }

  HuffmanTree_cleanup(&tree_ll);
  HuffmanTree_cleanup(&tree_d);

  return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x28(%rbp), %rax
shlq $0x3, %rax
movq %rax, -0x78(%rbp)
leaq -0x50(%rbp), %rdi
callq 0xab40
leaq -0x70(%rbp), %rdi
callq 0xab40
cmpl $0x1, -0x2c(%rbp)
jne 0xa6dd
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0xab70
jmp 0xa701
cmpl $0x2, -0x2c(%rbp)
jne 0xa6ff
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %r8
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0xaba0
movl %eax, -0x30(%rbp)
jmp 0xa701
jmp 0xa703
cmpl $0x0, -0x30(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xa715
jmp 0xaa32
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x78(%rbp), %rcx
leaq -0x50(%rbp), %rdx
callq 0xb1b0
movl %eax, -0x7c(%rbp)
cmpl $0xff, -0x7c(%rbp)
ja 0xa784
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
addq $0x1, %rsi
callq 0xaa50
cmpl $0x0, %eax
jne 0xa75b
movl $0x53, -0x30(%rbp)
jmp 0xaa32
movl -0x7c(%rbp), %eax
movb %al, %dl
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movb %dl, (%rax,%rcx)
movq -0x20(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
jmp 0xaa2d
cmpl $0x101, -0x7c(%rbp)     # imm = 0x101
jb 0xa9fc
cmpl $0x11d, -0x7c(%rbp)     # imm = 0x11D
ja 0xa9fc
movl -0x7c(%rbp), %eax
subl $0x101, %eax            # imm = 0x101
movl %eax, %eax
movl %eax, %ecx
leaq 0x9a8f(%rip), %rax      # 0x14240
movl (%rax,%rcx,4), %eax
movq %rax, -0xb0(%rbp)
movl -0x7c(%rbp), %eax
subl $0x101, %eax            # imm = 0x101
movl %eax, %eax
movl %eax, %ecx
leaq 0x9af2(%rip), %rax      # 0x142c0
movl (%rax,%rcx,4), %eax
movl %eax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x88(%rbp), %ecx
addq %rcx, %rax
cmpq -0x78(%rbp), %rax
jbe 0xa7f9
movl $0x33, -0x30(%rbp)
jmp 0xaa32
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x88(%rbp), %eax
movl %eax, %edx
callq 0xb280
movl %eax, %eax
addq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x78(%rbp), %rcx
leaq -0x70(%rbp), %rdx
callq 0xb1b0
movl %eax, -0x80(%rbp)
cmpl $0x1d, -0x80(%rbp)
jbe 0xa872
cmpl $-0x1, -0x7c(%rbp)
jne 0xa866
movq -0x18(%rbp), %rax
movq (%rax), %rdx
movq -0x28(%rbp), %rsi
shlq $0x3, %rsi
movl $0xb, %eax
movl $0xa, %ecx
cmpq %rsi, %rdx
cmoval %ecx, %eax
movl %eax, -0x30(%rbp)
jmp 0xa86d
movl $0x12, -0x30(%rbp)
jmp 0xaa32
movl -0x80(%rbp), %eax
movl %eax, %ecx
leaq 0x9ac2(%rip), %rax      # 0x14340
movl (%rax,%rcx,4), %eax
movl %eax, -0x84(%rbp)
movl -0x80(%rbp), %eax
movl %eax, %ecx
leaq 0x9b2d(%rip), %rax      # 0x143c0
movl (%rax,%rcx,4), %eax
movl %eax, -0x8c(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x8c(%rbp), %ecx
addq %rcx, %rax
cmpq -0x78(%rbp), %rax
jbe 0xa8be
movl $0x33, -0x30(%rbp)
jmp 0xaa32
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x8c(%rbp), %eax
movl %eax, %edx
callq 0xb280
addl -0x84(%rbp), %eax
movl %eax, -0x84(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x98(%rbp)
movl -0x84(%rbp), %eax
cmpq -0x98(%rbp), %rax
jbe 0xa908
movl $0x34, -0x30(%rbp)
jmp 0xaa32
movq -0x98(%rbp), %rax
movl -0x84(%rbp), %ecx
subq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
addq -0xb0(%rbp), %rsi
callq 0xaa50
cmpl $0x0, %eax
jne 0xa947
movl $0x53, -0x30(%rbp)
jmp 0xaa32
movl -0x84(%rbp), %eax
cmpq -0xb0(%rbp), %rax
jae 0xa9c1
movq $0x0, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0xb0(%rbp), %rax
jae 0xa9bf
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0xa8(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0xa8(%rbp)
movb (%rax,%rcx), %dl
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movb %dl, (%rax,%rcx)
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0xa961
jmp 0xa9fa
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rax
addq (%rax), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rsi
addq -0xa8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
callq 0x10c0
movq -0xb0(%rbp), %rcx
movq -0x20(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
jmp 0xaa2b
cmpl $0x100, -0x7c(%rbp)     # imm = 0x100
jne 0xaa07
jmp 0xaa32
movq -0x18(%rbp), %rax
movq (%rax), %rdx
movq -0x28(%rbp), %rsi
shlq $0x3, %rsi
movl $0xb, %eax
movl $0xa, %ecx
cmpq %rsi, %rdx
cmoval %ecx, %eax
movl %eax, -0x30(%rbp)
jmp 0xaa32
jmp 0xaa2d
jmp 0xa703
leaq -0x50(%rbp), %rdi
callq 0xb300
leaq -0x70(%rbp), %rdi
callq 0xb300
movl -0x30(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq