// File: /workspace/github_cpp_makefile_star2_O2/dgboy2000[P]compress/compress.cpp
// Function: DATCompression::huffman_decompress(std::vector<int, std::allocator<int>>)
vector<int> DATCompression::huffman_decompress(vector<int> compressed)
{
  vector<int> decompressed;
  vector<int> code[HUFFMAN_MAX];
  int code_space[65536], position;
  int number, d = 0, power = 1, code_ind = 0, c, temp = 0, q, r, g = 0;
  int useless_bits = compressed[compressed.size() - 1];
  char status = 'd';

  memset(code_space, -1, sizeof(code_space));
  for(int i = 0; i < compressed.size() - 1; i++)
  {
    number = compressed[i];
    for(int j = 0; j < 8; j++)
    {
      if(status == 'd')
      {
        if((number & 0x80) == 0x80)
          temp += power;
        number <<= 1;
        power *= 2, d++;
        if(d == 4)
          status = 'c', c = temp;
      }
      else if(status == 'c')
      {
        if((number & 0x80) == 0x80)
          code[code_ind].push_back(1);
        else
          code[code_ind].push_back(0);
        number <<= 1;
        c--;
        if(!c)
          status = 'd', code_ind++, d = 0, power = 1, temp = 0;
        if(code_ind == HUFFMAN_MAX)
        {
          status = 't';
          // generate code space
          for(int k = 0; k < HUFFMAN_MAX; k++)
          {
            position = 0;
            for(int l = 0; l < code[k].size(); l++)
            {
              if(!code[k][l])
                position = position * 2 + 1;
              else
                position = position * 2 + 2;
            }
            code_space[position] = k;
          }
          position = 0;
        }
      }
      else if(status == 't')
      {
        if(i == compressed.size() - 2 && 8 - j == useless_bits)
          break;

        if((number & 0x80) == 0x80)
          position = position * 2 + 2;
        else
          position = position * 2 + 1;
        if(code_space[position] >= 0)
        {
          if(!g)
            q = code_space[position], g = 1;
          else
          {
            r = code_space[position], g = 0;
            decompressed.push_back(q * HUFFMAN_MAX + r);
          }
          position = 0;
        }
        number <<= 1;
      }
    }
  }

  return decompressed;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x401c8, %rsp          # imm = 0x401C8
movq %rdx, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq %rdi, 0x20(%rsp)
andq $0x0, 0x10(%rdi)
leaq 0x40(%rsp), %rdi
xorl %r15d, %r15d
movl $0x180, %edx            # imm = 0x180
xorl %esi, %esi
callq 0x20f0
movq (%rbx), %r13
movq %rbx, 0x30(%rsp)
movq 0x8(%rbx), %r14
movl -0x4(%r14), %eax
movl %eax, 0x2c(%rsp)
leaq 0x1c0(%rsp), %rdi
movl $0x40000, %edx          # imm = 0x40000
movl $0xff, %esi
callq 0x20f0
movb $0x64, %al
pushq $0x1
popq %rbp
xorl %ebx, %ebx
movl $0x0, 0xc(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
subq %r13, %r14
sarq $0x2, %r14
decq %r14
cmpq %r15, %r14
jbe 0x3687
movq %r15, 0x38(%rsp)
movl (%r13,%r15,4), %r11d
xorl %r15d, %r15d
movl %esi, %r9d
movl 0xc(%rsp), %edx
cmpl $0x8, %r15d
je 0x3663
cmpb $0x63, %al
je 0x3569
movzbl %al, %ecx
cmpl $0x74, %ecx
je 0x34c2
cmpl $0x64, %ecx
jne 0x365b
movsbl %r11b, %eax
sarl $0x7, %eax
andl %ebp, %eax
addl %eax, 0x18(%rsp)
addl %r11d, %r11d
addl %ebp, %ebp
incl %ebx
movb $0x64, %al
cmpl $0x4, %ebx
jne 0x365b
movb $0x63, %al
pushq $0x4
popq %rbx
movl 0x18(%rsp), %r9d
jmp 0x365b
movl %edx, 0xc(%rsp)
movl %r9d, %esi
movq 0x30(%rsp), %rax
movq (%rax), %r13
movq 0x8(%rax), %r14
movq %r14, %rax
subq %r13, %rax
sarq $0x2, %rax
addq $-0x2, %rax
cmpq 0x38(%rsp), %rax
jne 0x34fa
pushq $0x8
popq %rax
subl %r15d, %eax
cmpl 0x2c(%rsp), %eax
je 0x3683
testb %r11b, %r11b
leal 0x2(%rdi,%rdi), %eax
leal 0x1(%rdi,%rdi), %edi
cmovsl %eax, %edi
movslq %edi, %rax
movl 0x1c0(%rsp,%rax,4), %ecx
testl %ecx, %ecx
movl %esi, %r9d
js 0x363c
movl %ebx, %r13d
cmpl $0x0, 0x14(%rsp)
movl 0x1c(%rsp), %ebx
je 0x3642
movl %r11d, %r12d
movl %r9d, %r14d
movl %ebx, %eax
shll $0x4, %eax
addl %eax, %ecx
movl %ecx, 0x10(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x5602
xorl %edi, %edi
movl %ebx, %ecx
movl $0x0, 0x14(%rsp)
movl %r13d, %ebx
movl %r14d, %r9d
movl %r12d, %r11d
jmp 0x364e
movl %r15d, 0xc(%rsp)
movq %rdi, %r12
movl %ebx, %r14d
movl %r9d, %ebx
movl %edx, %r15d
movslq %edx, %rax
imulq $0x18, %rax, %rax
leaq (%rsp,%rax), %rdi
addq $0x40, %rdi
movl %r11d, %r13d
testb %r11b, %r11b
js 0x35a2
andl $0x0, 0x10(%rsp)
leaq 0x10(%rsp), %rsi
callq 0x5602
jmp 0x35b4
movl $0x1, 0x10(%rsp)
leaq 0x10(%rsp), %rsi
callq 0x5602
movl %ebx, %r9d
movl %r14d, %ebx
movq %r12, %rdi
movl %r13d, %r11d
movl %r15d, %edx
movb $0x63, %al
decl %r9d
movl 0xc(%rsp), %r15d
jne 0x35e0
incl %edx
movb $0x64, %al
xorl %ebx, %ebx
pushq $0x1
popq %rbp
movl $0x0, 0x18(%rsp)
addl %r11d, %r11d
cmpl $0x10, %edx
jne 0x365b
xorl %eax, %eax
cmpq $0x10, %rax
je 0x3633
imulq $0x18, %rax, %rdx
movq 0x40(%rsp,%rdx), %rcx
movq 0x48(%rsp,%rdx), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rdx
je 0x3624
cmpl $0x0, (%rcx,%rsi,4)
leal 0x2(%rdi,%rdi), %r8d
leal 0x1(%rdi,%rdi), %edi
cmovnel %r8d, %edi
incq %rsi
jmp 0x3609
movslq %edi, %rcx
movl %eax, 0x1c0(%rsp,%rcx,4)
incq %rax
jmp 0x35ea
movb $0x74, %al
xorl %edi, %edi
pushq $0x10
popq %rdx
jmp 0x365b
movl 0x1c(%rsp), %ecx
jmp 0x364e
xorl %edi, %edi
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
movl %r13d, %ebx
addl %r11d, %r11d
movb $0x74, %al
movl %ecx, 0x1c(%rsp)
movl 0xc(%rsp), %edx
incl %r15d
jmp 0x3471
movl %edx, 0xc(%rsp)
movl %r9d, %esi
movq 0x30(%rsp), %rcx
movq (%rcx), %r13
movq 0x8(%rcx), %r14
movq 0x38(%rsp), %r15
incq %r15
jmp 0x344a
movb $0x74, %al
jmp 0x3676
movl $0x168, %ebx            # imm = 0x168
leaq (%rsp,%rbx), %rdi
addq $0x40, %rdi
callq 0x5466
addq $-0x18, %rbx
cmpq $-0x18, %rbx
jne 0x368c
movq 0x20(%rsp), %rax
addq $0x401c8, %rsp          # imm = 0x401C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x36be
jmp 0x36be
movq %rax, %r14
movl $0x168, %ebx            # imm = 0x168
leaq (%rsp,%rbx), %rdi
addq $0x40, %rdi
callq 0x5466
addq $-0x18, %rbx
cmpq $-0x18, %rbx
jne 0x36c6
movq 0x20(%rsp), %rdi
callq 0x5466
movq %r14, %rdi
callq 0x22a0
nop