// File: /workspace/github_cpp_makefile_star2_O3/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, 0x10(%rsp)
movq $0x0, 0x10(%rdi)
leaq 0x40(%rsp), %rdi
movq $0x0, 0x18(%rsp)
movl $0x180, %edx            # imm = 0x180
xorl %esi, %esi
callq 0x20f0
movq (%rbx), %r13
movq %rbx, 0x30(%rsp)
movq 0x8(%rbx), %rbx
movl -0x4(%rbx), %eax
movl %eax, 0x24(%rsp)
subq %r13, %rbx
leaq 0x1c0(%rsp), %rdi
movl $0x40000, %edx          # imm = 0x40000
movl $0xff, %esi
callq 0x20f0
cmpq $0x4, %rbx
je 0x3e76
movl $0x1, %edx
movb $0x64, %al
xorl %esi, %esi
movl $0x0, 0xc(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
movq %rsi, 0x38(%rsp)
movl (%r13,%rsi,4), %r15d
xorl %ebp, %ebp
movl %edx, %r13d
cmpb $0x63, %al
je 0x3d18
movzbl %al, %ecx
cmpl $0x74, %ecx
je 0x3c89
cmpl $0x64, %ecx
jne 0x3e3d
movsbl %r15b, %eax
sarl $0x7, %eax
andl %r13d, %eax
addl %eax, 0xc(%rsp)
addl %r15d, %r15d
addl %r13d, %r13d
incl %r14d
movb $0x64, %al
cmpl $0x4, %r14d
jne 0x3e3d
movb $0x63, %al
movl $0x4, %r14d
movl 0xc(%rsp), %ecx
movl %ecx, 0x2c(%rsp)
jmp 0x3e3d
movl %r13d, %edx
movq 0x30(%rsp), %rax
movq (%rax), %r13
movq 0x8(%rax), %rcx
subq %r13, %rcx
sarq $0x2, %rcx
leaq -0x2(%rcx), %rax
cmpq 0x38(%rsp), %rax
jne 0x3cbb
movl $0x8, %eax
subl %ebp, %eax
cmpl 0x24(%rsp), %eax
je 0x3e60
testb %r15b, %r15b
leal 0x2(%rbx,%rbx), %eax
leal 0x1(%rbx,%rbx), %ebx
cmovsl %eax, %ebx
movslq %ebx, %rax
movl 0x1c0(%rsp,%rax,4), %eax
testl %eax, %eax
js 0x3d68
cmpl $0x0, 0x18(%rsp)
movl %edx, %r13d
je 0x3d70
movl 0x28(%rsp), %ecx
shll $0x4, %ecx
addl %ecx, %eax
movl %eax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x3e23
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x3e2d
movslq %r12d, %rax
leaq (%rax,%rax,2), %rax
leaq (%rsp,%rax,8), %rdi
addq $0x40, %rdi
testb %r15b, %r15b
js 0x3d46
movl $0x0, 0x8(%rsp)
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x3d85
movl $0x0, (%rsi)
jmp 0x3d5e
movl $0x1, 0x8(%rsp)
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x3d91
movl $0x1, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x3d9b
movl %edx, %r13d
jmp 0x3e38
movl $0x1, %ecx
movq %rcx, 0x18(%rsp)
xorl %ebx, %ebx
movl %eax, 0x28(%rsp)
jmp 0x3e38
leaq 0x8(%rsp), %rdx
callq 0x664a
jmp 0x3d9b
leaq 0x8(%rsp), %rdx
callq 0x664a
movb $0x63, %al
decl 0x2c(%rsp)
jne 0x3db9
incl %r12d
movb $0x64, %al
movl $0x1, %r13d
xorl %r14d, %r14d
movl $0x0, 0xc(%rsp)
addl %r15d, %r15d
cmpl $0x10, %r12d
jne 0x3e3d
xorl %eax, %eax
leaq (%rax,%rax,2), %rdx
movq 0x40(%rsp,%rdx,8), %rcx
movq 0x48(%rsp,%rdx,8), %rdx
subq %rcx, %rdx
je 0x3e05
sarq $0x2, %rdx
cmpq $0x1, %rdx
adcq $0x0, %rdx
xorl %esi, %esi
xorl %edi, %edi
cmpl $0x0, (%rcx,%rsi,4)
leal 0x2(%rdi,%rdi), %r8d
leal 0x1(%rdi,%rdi), %edi
cmovnel %r8d, %edi
incq %rsi
cmpq %rsi, %rdx
jne 0x3de7
movslq %edi, %rcx
jmp 0x3e07
xorl %ecx, %ecx
movl %eax, 0x1c0(%rsp,%rcx,4)
incq %rax
cmpq $0x10, %rax
jne 0x3dc4
movb $0x74, %al
xorl %ebx, %ebx
movl $0x10, %r12d
jmp 0x3e3d
leaq 0x8(%rsp), %rdx
callq 0x664a
xorl %ebx, %ebx
movq $0x0, 0x18(%rsp)
addl %r15d, %r15d
movb $0x74, %al
incl %ebp
cmpl $0x8, %ebp
jne 0x3c38
movl %r13d, %edx
movq 0x30(%rsp), %rcx
movq (%rcx), %r13
movq 0x8(%rcx), %rcx
subq %r13, %rcx
sarq $0x2, %rcx
jmp 0x3e62
movb $0x74, %al
movq 0x38(%rsp), %rsi
incq %rsi
decq %rcx
cmpq %rsi, %rcx
ja 0x3c29
movq 0x10(%rsp), %rbx
movl $0x180, %r14d           # imm = 0x180
movq 0x28(%rsp,%r14), %rdi
testq %rdi, %rdi
je 0x3e98
movq 0x38(%rsp,%r14), %rsi
subq %rdi, %rsi
callq 0x21b0
addq $-0x18, %r14
jne 0x3e81
movq %rbx, %rax
addq $0x401c8, %rsp          # imm = 0x401C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3eb7
jmp 0x3eb7
movq %rax, %r14
movl $0x180, %ebx            # imm = 0x180
movq 0x28(%rsp,%rbx), %rdi
testq %rdi, %rdi
je 0x3ed6
movq 0x38(%rsp,%rbx), %rsi
subq %rdi, %rsi
callq 0x21b0
addq $-0x18, %rbx
jne 0x3ebf
movq 0x10(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x3efa
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
subq %rdi, %rsi
callq 0x21b0
movq %r14, %rdi
callq 0x22c0