// File: /workspace/github_cpp_makefile_star2_O3/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, 0x10(%rsp)
movq 0x8(%r14), %rcx
subq %rax, %rcx
je 0x3314
sarq $0x2, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %edx, %edx
movl (%rax,%rdx,4), %esi
cmpq $0x100, %rsi            # imm = 0x100
jae 0x3747
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 0x32ec
leaq 0xf8(%rsp), %rax
xorl %ecx, %ecx
movq -0x8(%rax), %rdx
cmpq %rdx, (%rax)
je 0x332a
movq %rdx, (%rax)
movl %ecx, 0x30(%rsp,%rcx,4)
incq %rcx
addq $0x18, %rax
cmpq $0x10, %rcx
jne 0x331e
xorl %eax, %eax
movq %rax, 0x28(%rsp)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %r12d, %edi
cmpl $0x0, 0xb0(%rsp,%rax,4)
jne 0x3390
movl 0x70(%rsp,%rax,4), %esi
cmpl %ecx, %esi
setge %r8b
cmpl $-0x1, %ecx
setne %r9b
testb %r8b, %r9b
jne 0x3379
movl %eax, %r12d
movl %edi, %r10d
jmp 0x3397
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 0x334e
movl %r10d, 0x1c(%rsp)
movslq %r10d, %rcx
movl 0x70(%rsp,%rcx,4), %eax
movslq %r12d, %rbp
addl %eax, 0x70(%rsp,%rbp,4)
movl $0x1, 0xb0(%rsp,%rcx,4)
movq %rcx, 0x20(%rsp)
movl 0x30(%rsp,%rcx,4), %r14d
leaq 0xf0(%rsp), %r13
xorl %r15d, %r15d
movl 0x30(%rsp,%r15,4), %eax
cmpl %r14d, %eax
jne 0x33fb
movl $0x1, 0xc(%rsp)
movq 0x8(%r13), %rsi
cmpq 0x10(%r13), %rsi
je 0x3423
movl $0x1, (%rsi)
jmp 0x3419
cmpl 0x30(%rsp,%rbp,4), %eax
jne 0x343f
movl $0x0, 0xc(%rsp)
movq 0x8(%r13), %rsi
cmpq 0x10(%r13), %rsi
je 0x3432
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%r13)
jmp 0x343f
movq %r13, %rdi
leaq 0xc(%rsp), %rdx
callq 0x664a
jmp 0x343f
movq %r13, %rdi
leaq 0xc(%rsp), %rdx
callq 0x664a
incq %r15
addq $0x18, %r13
cmpq $0x10, %r15
jne 0x33d7
xorl %eax, %eax
cmpl %r14d, 0x30(%rsp,%rax,4)
jne 0x345d
movl 0x30(%rsp,%rbp,4), %ecx
movl %ecx, 0x30(%rsp,%rax,4)
incq %rax
cmpq $0x10, %rax
jne 0x344e
movl 0x30(%rsp,%rbp,4), %eax
movq 0x20(%rsp), %rcx
movl %eax, 0x30(%rsp,%rcx,4)
movq 0x28(%rsp), %rax
incl %eax
cmpl $0xf, %eax
movl 0x1c(%rsp), %r10d
jne 0x333d
xorl %r12d, %r12d
leaq 0x8(%rsp), %r15
movl (%r15), %eax
xorl %ecx, %ecx
leaq (%r12,%r12,2), %rdx
leaq (%rsp,%rdx,8), %r13
addq $0xf0, %r13
movq 0x8(%r13), %r14
subq (%r13), %r14
shrq $0x2, %r14
movl $0x4, %ebp
movl %r14d, %edx
andl $0x1, %edx
leal (%rdx,%rax,2), %eax
movl %eax, 0x8(%rsp)
incl %ecx
cmpl $0x8, %ecx
jne 0x34f6
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x34df
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x34ea
movq %rbx, %rdi
movq %r15, %rdx
callq 0x652a
movl $0x0, 0x8(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
movl %r14d, %edx
shrl $0x1f, %edx
addl %r14d, %edx
sarl %edx
movl %edx, %r14d
decl %ebp
jne 0x34b5
movq 0x8(%r13), %rdx
subq (%r13), %rdx
shrq $0x2, %rdx
decl %edx
js 0x3570
movl %edx, %r14d
incq %r14
addl %eax, %eax
movl %eax, 0x8(%rsp)
movq (%r13), %rdx
cmpl $0x0, -0x4(%rdx,%r14,4)
je 0x3537
orl $0x1, %eax
movl %eax, 0x8(%rsp)
incl %ecx
cmpl $0x8, %ecx
jne 0x356b
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x3554
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x355f
movq %rbx, %rdi
movq %r15, %rdx
callq 0x652a
movl $0x0, 0x8(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
decq %r14
jg 0x351e
incq %r12
cmpq $0x10, %r12
jne 0x3495
movq 0x10(%rsp), %rsi
movq 0x8(%rsi), %rdx
cmpq (%rsi), %rdx
je 0x366b
xorl %r12d, %r12d
leaq 0x8(%rsp), %r15
movb $0x1, %bpl
movq 0x10(%rsp), %rdx
movq (%rdx), %rdx
movl (%rdx,%r12,4), %edx
leal 0xf(%rdx), %esi
testl %edx, %edx
cmovnsl %edx, %esi
movl %esi, %edi
andl $-0x10, %edi
subl %edi, %edx
sarl $0x4, %esi
testb $0x1, %bpl
cmovel %edx, %esi
movslq %esi, %rdx
leaq (%rdx,%rdx,2), %rdx
movq 0xf8(%rsp,%rdx,8), %rsi
subq 0xf0(%rsp,%rdx,8), %rsi
shrq $0x2, %rsi
decl %esi
js 0x3640
leaq (%rsp,%rdx,8), %r14
addq $0xf0, %r14
movl %esi, %r13d
incq %r13
addl %eax, %eax
movl %eax, 0x8(%rsp)
movq (%r14), %rdx
cmpl $0x0, -0x4(%rdx,%r13,4)
je 0x3607
orl $0x1, %eax
movl %eax, 0x8(%rsp)
incl %ecx
cmpl $0x8, %ecx
jne 0x363b
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x3624
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x362f
movq %rbx, %rdi
movq %r15, %rdx
callq 0x652a
movl $0x0, 0x8(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
decq %r13
jg 0x35ef
testb $0x1, %bpl
movl $0x0, %ebp
jne 0x359a
incq %r12
movq 0x10(%rsp), %rsi
movq 0x8(%rsi), %rdx
subq (%rsi), %rdx
sarq $0x2, %rdx
cmpq %r12, %rdx
ja 0x3597
testl %ecx, %ecx
je 0x36a9
movl $0x8, %ebp
subl %ecx, %ebp
cmpl $0x7, %ecx
jg 0x3690
cmpl $0x2, %ebp
movl $0x1, %ecx
cmovgel %ebp, %ecx
addl %eax, %eax
decl %ecx
jne 0x3686
movl %eax, 0x8(%rsp)
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rcx
cmpq %rcx, %rsi
je 0x36c3
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x36d8
movl $0x0, 0xc(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x36fc
movl $0x0, (%rsi)
jmp 0x36e3
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x652a
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rcx
movl %ebp, 0xc(%rsp)
cmpq %rcx, %rsi
je 0x36ed
movl %ebp, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x3709
leaq 0xc(%rsp), %rdx
movq %rbx, %rdi
callq 0x664a
jmp 0x3709
leaq 0xc(%rsp), %rdx
movq %rbx, %rdi
callq 0x664a
movl $0x180, %r14d           # imm = 0x180
movq 0xd8(%rsp,%r14), %rdi
testq %rdi, %rdi
je 0x372c
movq 0xe8(%rsp,%r14), %rsi
subq %rdi, %rsi
callq 0x21b0
addq $-0x18, %r14
jne 0x370f
movq %rbx, %rax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x788a(%rip), %rdi      # 0xafd8
leaq 0x48fb(%rip), %rsi      # 0x8050
movl $0x40, %edx
callq 0x21e0
movq 0x7872(%rip), %rdi      # 0xafd8
callq 0x20d0
xorl %edi, %edi
callq 0x2200
jmp 0x3782
jmp 0x3782
jmp 0x3782
jmp 0x3782
jmp 0x3782
jmp 0x3782
jmp 0x3782
jmp 0x3782
movq %rax, %r14
movl $0x180, %r15d           # imm = 0x180
movq 0xd8(%rsp,%r15), %rdi
testq %rdi, %rdi
je 0x37a8
movq 0xe8(%rsp,%r15), %rsi
subq %rdi, %rsi
callq 0x21b0
addq $-0x18, %r15
jne 0x378b
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x37c2
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x21b0
movq %r14, %rdi
callq 0x22c0