// File: /workspace/github_cpp_makefile_star2_O2/dgboy2000[P]compress/compress.cpp
// Function: DATCompression::huffman_compress(std::vector<int, std::allocator<int>>)
vector<int> DATCompression::huffman_compress(vector<int> data)
{
  vector<int> compressed;
  int freq[HUFFMAN_MAX], in_set[HUFFMAN_MAX], masked[HUFFMAN_MAX];
  int max_freq, second_max_freq, max_ind, second_max_ind, s, temp = 0, code_size, t = 0, q;
  vector<int> code[HUFFMAN_MAX];
  
  memset(freq, 0, sizeof(freq));
  memset(masked, 0, sizeof(masked));
  for(int i = 0; i < data.size(); i++)
  {
    if(data[i] < 0 || data[i] >= HUFFMAN_MAX * HUFFMAN_MAX)
    {
      cout << "Error: huffman_compress do not accept data out of range [0..255]" << endl;
      exit(0);
    }
    freq[data[i] / HUFFMAN_MAX]++;
    freq[data[i] % HUFFMAN_MAX]++;
  }

  for(int i = 0; i < HUFFMAN_MAX; i++)
  {
    code[i].clear();
    in_set[i] = i;
  }

  // generate code dictionary
  for(int i = 0; i < HUFFMAN_MAX - 1; i++)
  {
    // find out the minimum and second minimum freq
    max_freq = -1, second_max_freq = -1;
    for(int j = 0; j < HUFFMAN_MAX; j++)
      if(!masked[j])
      {
        if(freq[j] < max_freq || max_freq == -1)
          second_max_freq = max_freq, second_max_ind = max_ind, max_freq = freq[j], max_ind = j;
        else if(freq[j] < second_max_freq || second_max_freq == -1)
          second_max_freq = freq[j], second_max_ind = j;
      }

    // merge them
    freq[max_ind] += freq[second_max_ind];
    masked[second_max_ind] = 1;

    for(int j = 0; j < HUFFMAN_MAX; j++)
    {
      if(in_set[j] == in_set[second_max_ind])
        code[j].push_back(1);
      else if(in_set[j] == in_set[max_ind])
        code[j].push_back(0);
    }
    
    s = in_set[second_max_ind];
    for(int j = 0; j < HUFFMAN_MAX; j++)
      if(in_set[j] == s)
        in_set[j] = in_set[max_ind];
    in_set[second_max_ind] = in_set[max_ind];

  }

  for(int i = 0; i < HUFFMAN_MAX; i++)
  {
    code_size = code[i].size();
    for(int j = 0; j < 4; j++)
    {
      temp <<= 1;
      if(code_size % 2)
        temp |= 1;
      code_size /= 2, t++;
      if(t == 8)
      {
        compressed.push_back(temp);
        temp = 0, t = 0;
      }
    }

    for(int j = code[i].size() - 1; j >= 0; j--)
    {
      temp <<= 1;
      if(code[i][j])
        temp |= 1;
      t++;
      if(t == 8)
      {
        compressed.push_back(temp);
        temp = 0, t = 0;
      }
    }
  }

  /*for(int i = 0; i < HUFFMAN_MAX; i++)
  {
    for(int j = code[i].size() - 1; j >= 0; j--)
      cout << code[i][j] << " ";
    cout << endl;
  }*/

  for(int i = 0; i < data.size(); i++)
  {
    for(int k = 0; k < 2; k++)
    {
      if(k == 0)
        q = data[i] / HUFFMAN_MAX;
      else
        q = data[i] % HUFFMAN_MAX;
      for(int j = code[q].size() - 1; j >= 0; j--)
      {
        temp <<= 1;
        if(code[q][j])
          temp |= 1;
        t++;
        if(t == 8)
        {
          compressed.push_back(temp);
          temp = 0, t = 0;
        }
      }
    }
  }

  if(t)
  {
    for(int i = 0; i < 8 - t; i++)
      temp <<= 1;
    compressed.push_back(temp);
  }

  if(!t)
    compressed.push_back(0);
  else
    compressed.push_back(8 - t);

  return compressed;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
movq %rdx, %r15
movq %rdi, %rbx
xorps %xmm0, %xmm0
andq $0x0, 0x10(%rdi)
movups %xmm0, (%rdi)
andl $0x0, 0x8(%rsp)
leaq 0xf0(%rsp), %rdi
xorl %r14d, %r14d
movl $0x180, %edx            # imm = 0x180
xorl %esi, %esi
callq 0x20f0
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movq (%r15), %rax
movq %r15, 0x18(%rsp)
movq 0x8(%r15), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r14, %rcx
je 0x2dca
movl (%rax,%r14,4), %edx
cmpq $0x100, %rdx            # imm = 0x100
jae 0x314b
movl %edx, %esi
shrl $0x4, %esi
incl 0x70(%rsp,%rsi,4)
andl $0xf, %edx
incl 0x70(%rsp,%rdx,4)
incq %r14
jmp 0x2d9f
leaq 0xf8(%rsp), %rax
xorl %ecx, %ecx
cmpq $0x10, %rcx
je 0x2df3
movq -0x8(%rax), %rdx
cmpq %rdx, (%rax)
je 0x2de6
movq %rdx, (%rax)
movl %ecx, 0x30(%rsp,%rcx,4)
incq %rcx
addq $0x18, %rax
jmp 0x2dd4
xorl %eax, %eax
cmpl $0xf, %eax
je 0x2f22
movq %rax, 0x28(%rsp)
xorl %eax, %eax
pushq $-0x1
popq %rdx
movl %edx, %ecx
cmpq $0x10, %rax
je 0x2e63
cmpl $0x0, 0xb0(%rsp,%rax,4)
jne 0x2e4a
movl 0x70(%rsp,%rax,4), %esi
cmpl %ecx, %esi
setge %dil
cmpl $-0x1, %ecx
setne %r8b
testb %dil, %r8b
jne 0x2e34
movl %eax, %edi
jmp 0x2e54
cmpl %edx, %esi
setl %dil
cmpl $-0x1, %edx
sete %r8b
orb %dil, %r8b
cmovnel %esi, %edx
cmovnel %eax, %ebp
movl %ecx, %esi
movl %edx, %ecx
movl %r15d, %edi
movl %ebp, %r15d
movl %r15d, %ebp
movl %ecx, %edx
incq %rax
movl %esi, %ecx
movl %edi, %r15d
jmp 0x2e0a
movslq %ebp, %rcx
movl 0x70(%rsp,%rcx,4), %eax
movslq %r15d, %rdx
movq %rdx, 0x10(%rsp)
addl %eax, 0x70(%rsp,%rdx,4)
movl $0x1, 0xb0(%rsp,%rcx,4)
movq %rcx, 0x20(%rsp)
movl 0x30(%rsp,%rcx,4), %r14d
leaq 0xf0(%rsp), %r13
xorl %r12d, %r12d
cmpq $0x10, %r12
je 0x2ee3
movl 0x30(%rsp,%r12,4), %eax
cmpl %r14d, %eax
jne 0x2ebd
movl $0x1, 0xc(%rsp)
movq %r13, %rdi
leaq 0xc(%rsp), %rsi
callq 0x5602
jmp 0x2eda
movq 0x10(%rsp), %rcx
cmpl 0x30(%rsp,%rcx,4), %eax
jne 0x2eda
andl $0x0, 0xc(%rsp)
movq %r13, %rdi
leaq 0xc(%rsp), %rsi
callq 0x5602
incq %r12
addq $0x18, %r13
jmp 0x2e96
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2f04
cmpl %r14d, 0x30(%rsp,%rax,4)
jne 0x2eff
movq 0x10(%rsp), %rcx
movl 0x30(%rsp,%rcx,4), %ecx
movl %ecx, 0x30(%rsp,%rax,4)
incq %rax
jmp 0x2ee5
movq 0x10(%rsp), %rax
movl 0x30(%rsp,%rax,4), %eax
movq 0x20(%rsp), %rcx
movl %eax, 0x30(%rsp,%rcx,4)
movq 0x28(%rsp), %rax
incl %eax
jmp 0x2df5
xorl %eax, %eax
leaq 0x8(%rsp), %r15
pushq $0x2
popq %rbp
xorl %ecx, %ecx
cmpq $0x10, %rax
je 0x2ffa
movq %rax, 0x10(%rsp)
imulq $0x18, %rax, %rax
leaq (%rsp,%rax), %r13
addq $0xf0, %r13
movq 0x8(%r13), %r12
subq (%r13), %r12
shrq $0x2, %r12
pushq $0x4
popq %r14
subl $0x1, %r14d
jb 0x2f97
movl 0x8(%rsp), %eax
movl %r12d, %edx
andl $0x1, %edx
leal (%rdx,%rax,2), %eax
movl %eax, 0x8(%rsp)
movl %r12d, %eax
cltd
idivl %ebp
incl %ecx
movl %eax, %r12d
cmpl $0x8, %ecx
jne 0x2f5c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x51de
andl $0x0, 0x8(%rsp)
xorl %ecx, %ecx
jmp 0x2f5c
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x2, %rax
movl %eax, %r14d
decq %r14
testl %r14d, %r14d
js 0x2fed
movl 0x8(%rsp), %eax
addl %eax, %eax
movl %eax, 0x8(%rsp)
movl %r14d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq (%r13), %rsi
cmpl $0x0, (%rsi,%rdx,4)
je 0x2fd2
orl $0x1, %eax
movl %eax, 0x8(%rsp)
incl %ecx
cmpl $0x8, %ecx
jne 0x2fa6
movq %rbx, %rdi
movq %r15, %rsi
callq 0x51de
andl $0x0, 0x8(%rsp)
xorl %ecx, %ecx
jmp 0x2fa6
movq 0x10(%rsp), %rax
incq %rax
jmp 0x2f2e
xorl %r12d, %r12d
leaq 0x8(%rsp), %r15
movq 0x18(%rsp), %rdx
movq 0x8(%rdx), %rax
subq (%rdx), %rax
sarq $0x2, %rax
cmpq %r12, %rax
jbe 0x30c7
xorl %r13d, %r13d
testl %r13d, %r13d
je 0x3043
cmpl $0x2, %r13d
je 0x30bf
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl (%rax,%r12,4), %eax
cltd
pushq $0x10
popq %rsi
idivl %esi
movl %edx, %eax
jmp 0x3055
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl (%rax,%r12,4), %eax
cltd
pushq $0x10
popq %rsi
idivl %esi
cltq
imulq $0x18, %rax, %rax
leaq (%rsp,%rax), %r14
addq $0xf0, %r14
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x2, %rax
movl %eax, %ebp
decq %rbp
testl %ebp, %ebp
js 0x30b7
movl 0x8(%rsp), %eax
addl %eax, %eax
movl %eax, 0x8(%rsp)
movl %ebp, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq (%r14), %rsi
cmpl $0x0, (%rsi,%rdx,4)
je 0x309c
orl $0x1, %eax
movl %eax, 0x8(%rsp)
incl %ecx
cmpl $0x8, %ecx
jne 0x3073
movq %rbx, %rdi
movq %r15, %rsi
callq 0x51de
andl $0x0, 0x8(%rsp)
xorl %ecx, %ecx
jmp 0x3073
incl %r13d
jmp 0x301e
incq %r12
jmp 0x3002
testl %ecx, %ecx
je 0x3106
pushq $0x8
popq %rbp
subl %ecx, %ebp
movl 0x8(%rsp), %eax
xorl %ecx, %ecx
testl %ebp, %ebp
cmovgl %ebp, %ecx
subl $0x1, %ecx
jb 0x30e8
addl %eax, %eax
movl %eax, 0x8(%rsp)
jmp 0x30db
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x51de
leaq 0xc(%rsp), %rsi
movl %ebp, (%rsi)
movq %rbx, %rdi
callq 0x5602
jmp 0x3116
leaq 0xc(%rsp), %rsi
andl $0x0, (%rsi)
movq %rbx, %rdi
callq 0x5602
movl $0x168, %r14d           # imm = 0x168
leaq (%rsp,%r14), %rdi
addq $0xf0, %rdi
callq 0x5466
addq $-0x18, %r14
cmpq $-0x18, %r14
jne 0x311c
movq %rbx, %rax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7e86(%rip), %rdi      # 0xafd8
leaq 0x3ed7(%rip), %rsi      # 0x7030
callq 0x2180
movq %rax, %rdi
callq 0x20d0
xorl %edi, %edi
callq 0x21e0
jmp 0x317d
jmp 0x317d
jmp 0x317d
jmp 0x317d
jmp 0x317d
jmp 0x317d
jmp 0x317d
jmp 0x317d
movq %rax, %r14
movl $0x168, %r15d           # imm = 0x168
leaq (%rsp,%r15), %rdi
addq $0xf0, %rdi
callq 0x5466
addq $-0x18, %r15
cmpq $-0x18, %r15
jne 0x3186
movq %rbx, %rdi
callq 0x5466
movq %r14, %rdi
callq 0x22a0