// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rdi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl $0x0, 0x8(%rsp)
leaq 0xf0(%rsp), %rdi
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 (%r14), %rax
movq %r14, 0x18(%rsp)
movq 0x8(%r14), %rcx
subq %rax, %rcx
je 0x329e
sarq $0x2, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %edx, %edx
movl (%rax,%rdx,4), %esi
cmpq $0x100, %rsi            # imm = 0x100
jae 0x3702
movl %esi, %edi
shrl $0x4, %edi
incl 0x70(%rsp,%rdi,4)
andl $0xf, %esi
incl 0x70(%rsp,%rsi,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x3276
leaq 0xf8(%rsp), %rax
xorl %ecx, %ecx
movq -0x8(%rax), %rdx
cmpq %rdx, (%rax)
je 0x32b4
movq %rdx, (%rax)
movl %ecx, 0x30(%rsp,%rcx,4)
incq %rcx
addq $0x18, %rax
cmpq $0x10, %rcx
jne 0x32a8
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %r12d, %edi
cmpl $0x0, 0xb0(%rsp,%rax,4)
jne 0x331a
movl 0x70(%rsp,%rax,4), %esi
cmpl %ecx, %esi
setge %r8b
cmpl $-0x1, %ecx
setne %r9b
testb %r8b, %r9b
jne 0x3303
movl %eax, %r12d
movl %edi, %r10d
jmp 0x3321
cmpl %edx, %esi
setl %r8b
cmpl $-0x1, %edx
sete %r9b
orb %r8b, %r9b
cmovnel %esi, %edx
cmovnel %eax, %r10d
movl %ecx, %esi
movl %edx, %ecx
movl %edi, %r12d
incq %rax
movl %ecx, %edx
movl %esi, %ecx
cmpq $0x10, %rax
jne 0x32d8
movl %r10d, 0x24(%rsp)
movslq %r10d, %rcx
movl 0x70(%rsp,%rcx,4), %eax
movslq %r12d, %r14
addl %eax, 0x70(%rsp,%r14,4)
movl $0x1, 0xb0(%rsp,%rcx,4)
movq %rcx, 0x28(%rsp)
movl 0x30(%rsp,%rcx,4), %ebp
leaq 0xf0(%rsp), %r13
xorl %r15d, %r15d
movl 0x30(%rsp,%r15,4), %eax
cmpl %ebp, %eax
jne 0x3384
movl $0x1, 0xc(%rsp)
movq 0x8(%r13), %rsi
cmpq 0x10(%r13), %rsi
je 0x33ad
movl $0x1, (%rsi)
jmp 0x33a3
cmpl 0x30(%rsp,%r14,4), %eax
jne 0x33c9
movl $0x0, 0xc(%rsp)
movq 0x8(%r13), %rsi
cmpq 0x10(%r13), %rsi
je 0x33bc
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%r13)
jmp 0x33c9
movq %r13, %rdi
leaq 0xc(%rsp), %rdx
callq 0x63e2
jmp 0x33c9
movq %r13, %rdi
leaq 0xc(%rsp), %rdx
callq 0x63e2
incq %r15
addq $0x18, %r13
cmpq $0x10, %r15
jne 0x3361
movq 0x28(%rsp), %rsi
movl 0x30(%rsp,%rsi,4), %eax
xorl %ecx, %ecx
movl 0x24(%rsp), %r10d
cmpl %eax, 0x30(%rsp,%rcx,4)
jne 0x33f5
movl 0x30(%rsp,%r14,4), %edx
movl %edx, 0x30(%rsp,%rcx,4)
incq %rcx
cmpq $0x10, %rcx
jne 0x33e6
movl 0x30(%rsp,%r14,4), %eax
movl %eax, 0x30(%rsp,%rsi,4)
movq 0x10(%rsp), %rax
incl %eax
cmpl $0xf, %eax
jne 0x32c7
xorl %r12d, %r12d
xorl %ebp, %ebp
leaq (%r12,%r12,2), %rax
leaq (%rsp,%rax,8), %r13
addq $0xf0, %r13
movq 0x8(%r13), %r15
subq (%r13), %r15
shrq $0x2, %r15
movl $0x4, %r14d
movl 0x8(%rsp), %eax
movl %r15d, %ecx
andl $0x1, %ecx
leal (%rcx,%rax,2), %eax
movl %eax, 0x8(%rsp)
incl %ebp
cmpl $0x8, %ebp
jne 0x3482
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x346b
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x3478
movq %rbx, %rdi
leaq 0x8(%rsp), %rdx
callq 0x62c2
movl $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movl %r15d, %eax
shrl $0x1f, %eax
addl %r15d, %eax
sarl %eax
movl %eax, %r15d
decl %r14d
jne 0x343d
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x2, %rax
decl %eax
js 0x3505
movl %eax, %r14d
incq %r14
movl 0x8(%rsp), %eax
addl %eax, %eax
movl %eax, 0x8(%rsp)
movq (%r13), %rcx
cmpl $0x0, -0x4(%rcx,%r14,4)
je 0x34c8
orl $0x1, %eax
movl %eax, 0x8(%rsp)
incl %ebp
cmpl $0x8, %ebp
jne 0x3500
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x34e9
movl 0x8(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x34f6
movq %rbx, %rdi
leaq 0x8(%rsp), %rdx
callq 0x62c2
movl $0x0, 0x8(%rsp)
xorl %ebp, %ebp
decq %r14
jg 0x34ab
incq %r12
cmpq $0x10, %r12
jne 0x341c
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rax
cmpq (%rcx), %rax
je 0x361a
movq $0x0, 0x10(%rsp)
leaq 0x8(%rsp), %r15
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rcx
movl (%rax,%rcx,4), %eax
leal 0xf(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
movl %ecx, %edx
andl $-0x10, %edx
subl %edx, %eax
sarl $0x4, %ecx
testl %r13d, %r13d
cmovnel %eax, %ecx
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
movq 0xf8(%rsp,%rax,8), %rcx
subq 0xf0(%rsp,%rax,8), %rcx
shrq $0x2, %rcx
decl %ecx
js 0x35e4
leaq (%rsp,%rax,8), %r14
addq $0xf0, %r14
movl %ecx, %r12d
incq %r12
movl 0x8(%rsp), %eax
addl %eax, %eax
movl %eax, 0x8(%rsp)
movq (%r14), %rcx
cmpl $0x0, -0x4(%rcx,%r12,4)
je 0x35a9
orl $0x1, %eax
movl %eax, 0x8(%rsp)
incl %ebp
cmpl $0x8, %ebp
jne 0x35df
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x35ca
movl 0x8(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x35d5
movq %rbx, %rdi
movq %r15, %rdx
callq 0x62c2
movl $0x0, 0x8(%rsp)
xorl %ebp, %ebp
decq %r12
jg 0x358d
leal 0x1(%r13), %eax
testl %r13d, %r13d
movl %eax, %r13d
je 0x3535
movq 0x10(%rsp), %rdx
incq %rdx
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
sarq $0x2, %rax
movq %rdx, 0x10(%rsp)
cmpq %rdx, %rax
ja 0x3532
testl %ebp, %ebp
je 0x3686
movl $0x8, %r14d
subl %ebp, %r14d
cmpl $0x7, %ebp
jg 0x3647
movl 0x8(%rsp), %eax
cmpl $0x2, %r14d
movl $0x1, %ecx
cmovgel %r14d, %ecx
addl %eax, %eax
decl %ecx
jne 0x363d
movl %eax, 0x8(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x3661
movl 0x8(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x366e
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x62c2
testl %ebp, %ebp
je 0x3686
movl %r14d, 0xc(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x36b7
movl %r14d, (%rsi)
jmp 0x369e
movl $0x0, 0xc(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x36a8
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x36c4
leaq 0xc(%rsp), %rdx
movq %rbx, %rdi
callq 0x63e2
jmp 0x36c4
leaq 0xc(%rsp), %rdx
movq %rbx, %rdi
callq 0x63e2
movl $0x180, %r14d           # imm = 0x180
movq 0xd8(%rsp,%r14), %rdi
testq %rdi, %rdi
je 0x36e7
movq 0xe8(%rsp,%r14), %rsi
subq %rdi, %rsi
callq 0x21b0
addq $-0x18, %r14
jne 0x36ca
movq %rbx, %rax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x78cf(%rip), %rdi      # 0xafd8
leaq 0x4920(%rip), %rsi      # 0x8030
movl $0x40, %edx
callq 0x21e0
movq 0x78b7(%rip), %rdi      # 0xafd8
callq 0x20d0
xorl %edi, %edi
callq 0x2200
jmp 0x373d
jmp 0x373d
jmp 0x373d
jmp 0x373d
jmp 0x373d
jmp 0x373d
jmp 0x373d
jmp 0x373d
movq %rax, %r14
movl $0x180, %r15d           # imm = 0x180
movq 0xd8(%rsp,%r15), %rdi
testq %rdi, %rdi
je 0x3763
movq 0xe8(%rsp,%r15), %rsi
subq %rdi, %rsi
callq 0x21b0
addq $-0x18, %r15
jne 0x3746
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x377d
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x21b0
movq %r14, %rdi
callq 0x22c0
nop