// File: 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
movq %rsp, %rbp
subq $0x360, %rsp            # imm = 0x360
movq %rdx, -0x310(%rbp)
movq %rdi, -0x308(%rbp)
movq %rdi, %rax
movq %rax, -0x300(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
callq 0x8d20
movl $0x0, -0xf8(%rbp)
movl $0x0, -0x100(%rbp)
leaq -0x290(%rbp), %rax
movq %rax, %rcx
addq $0x180, %rcx            # imm = 0x180
movq %rcx, -0x2f8(%rbp)
movq %rax, -0x2f0(%rbp)
movq -0x2f0(%rbp), %rdi
movq %rdi, -0x318(%rbp)
callq 0x8d20
movq -0x318(%rbp), %rax
movq -0x2f8(%rbp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, -0x2f0(%rbp)
jne 0x35cb
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x2120
leaq -0xe0(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x2120
movl $0x0, -0x294(%rbp)
movq -0x310(%rbp), %rdi
movslq -0x294(%rbp), %rax
movq %rax, -0x320(%rbp)
callq 0x90a0
movq %rax, %rcx
movq -0x320(%rbp), %rax
cmpq %rcx, %rax
jae 0x375c
movq -0x310(%rbp), %rdi
movslq -0x294(%rbp), %rsi
callq 0x8db0
cmpl $0x0, (%rax)
jl 0x3689
movq -0x310(%rbp), %rdi
movslq -0x294(%rbp), %rsi
callq 0x8db0
cmpl $0x100, (%rax)          # imm = 0x100
jl 0x36f3
movq 0x12948(%rip), %rdi     # 0x15fd8
leaq 0xc999(%rip), %rsi      # 0x10030
callq 0x2200
movq %rax, -0x328(%rbp)
jmp 0x36a5
movq -0x328(%rbp), %rdi
movq 0x12915(%rip), %rsi     # 0x15fc8
callq 0x2240
jmp 0x36ba
xorl %edi, %edi
callq 0x22b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2a0(%rbp)
movl %eax, -0x2a4(%rbp)
leaq -0x290(%rbp), %rax
movq %rax, -0x338(%rbp)
addq $0x180, %rax            # imm = 0x180
movq %rax, -0x330(%rbp)
jmp 0x3ea2
movq -0x310(%rbp), %rdi
movslq -0x294(%rbp), %rsi
callq 0x8db0
movq -0x310(%rbp), %rdi
movl (%rax), %eax
movl $0x10, %ecx
cltd
idivl %ecx
cltq
movl -0x60(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x60(%rbp,%rax,4)
movslq -0x294(%rbp), %rsi
callq 0x8db0
movl (%rax), %eax
movl $0x10, %ecx
cltd
idivl %ecx
movslq %edx, %rax
movl -0x60(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x60(%rbp,%rax,4)
movl -0x294(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x294(%rbp)
jmp 0x3629
movl $0x0, -0x2a8(%rbp)
cmpl $0x10, -0x2a8(%rbp)
jge 0x37ae
movslq -0x2a8(%rbp), %rax
leaq -0x290(%rbp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x90c0
movl -0x2a8(%rbp), %ecx
movslq -0x2a8(%rbp), %rax
movl %ecx, -0xa0(%rbp,%rax,4)
movl -0x2a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2a8(%rbp)
jmp 0x3766
movl $0x0, -0x2ac(%rbp)
cmpl $0xf, -0x2ac(%rbp)
jge 0x3a38
movl $0xffffffff, -0xe4(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0xe8(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x2b0(%rbp)
cmpl $0x10, -0x2b0(%rbp)
jge 0x38ab
movslq -0x2b0(%rbp), %rax
cmpl $0x0, -0xe0(%rbp,%rax,4)
jne 0x3895
movslq -0x2b0(%rbp), %rax
movl -0x60(%rbp,%rax,4), %eax
cmpl -0xe4(%rbp), %eax
jl 0x3821
cmpl $-0x1, -0xe4(%rbp)
jne 0x3858
movl -0xe4(%rbp), %eax
movl %eax, -0xe8(%rbp)
movl -0xec(%rbp), %eax
movl %eax, -0xf0(%rbp)
movslq -0x2b0(%rbp), %rax
movl -0x60(%rbp,%rax,4), %eax
movl %eax, -0xe4(%rbp)
movl -0x2b0(%rbp), %eax
movl %eax, -0xec(%rbp)
jmp 0x3893
movslq -0x2b0(%rbp), %rax
movl -0x60(%rbp,%rax,4), %eax
cmpl -0xe8(%rbp), %eax
jl 0x3874
cmpl $-0x1, -0xe8(%rbp)
jne 0x3891
movslq -0x2b0(%rbp), %rax
movl -0x60(%rbp,%rax,4), %eax
movl %eax, -0xe8(%rbp)
movl -0x2b0(%rbp), %eax
movl %eax, -0xf0(%rbp)
jmp 0x3893
jmp 0x3895
jmp 0x3897
movl -0x2b0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2b0(%rbp)
jmp 0x37e3
movslq -0xf0(%rbp), %rax
movl -0x60(%rbp,%rax,4), %ecx
movslq -0xec(%rbp), %rax
addl -0x60(%rbp,%rax,4), %ecx
movl %ecx, -0x60(%rbp,%rax,4)
movslq -0xf0(%rbp), %rax
movl $0x1, -0xe0(%rbp,%rax,4)
movl $0x0, -0x2b4(%rbp)
cmpl $0x10, -0x2b4(%rbp)
jge 0x399c
movslq -0x2b4(%rbp), %rax
movl -0xa0(%rbp,%rax,4), %eax
movslq -0xf0(%rbp), %rcx
cmpl -0xa0(%rbp,%rcx,4), %eax
jne 0x3939
movslq -0x2b4(%rbp), %rax
leaq (%rax,%rax,2), %rax
leaq -0x290(%rbp,%rax,8), %rdi
movl $0x1, -0x2b8(%rbp)
leaq -0x2b8(%rbp), %rsi
callq 0x8ed0
jmp 0x3937
jmp 0x3986
movslq -0x2b4(%rbp), %rax
movl -0xa0(%rbp,%rax,4), %eax
movslq -0xec(%rbp), %rcx
cmpl -0xa0(%rbp,%rcx,4), %eax
jne 0x3984
movslq -0x2b4(%rbp), %rax
leaq (%rax,%rax,2), %rax
leaq -0x290(%rbp,%rax,8), %rdi
movl $0x0, -0x2bc(%rbp)
leaq -0x2bc(%rbp), %rsi
callq 0x8ed0
jmp 0x3982
jmp 0x3984
jmp 0x3986
jmp 0x3988
movl -0x2b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2b4(%rbp)
jmp 0x38e1
movslq -0xf0(%rbp), %rax
movl -0xa0(%rbp,%rax,4), %eax
movl %eax, -0xf4(%rbp)
movl $0x0, -0x2c0(%rbp)
cmpl $0x10, -0x2c0(%rbp)
jge 0x3a08
movslq -0x2c0(%rbp), %rax
movl -0xa0(%rbp,%rax,4), %eax
cmpl -0xf4(%rbp), %eax
jne 0x39f5
movslq -0xec(%rbp), %rax
movl -0xa0(%rbp,%rax,4), %ecx
movslq -0x2c0(%rbp), %rax
movl %ecx, -0xa0(%rbp,%rax,4)
jmp 0x39f7
movl -0x2c0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c0(%rbp)
jmp 0x39ba
movslq -0xec(%rbp), %rax
movl -0xa0(%rbp,%rax,4), %ecx
movslq -0xf0(%rbp), %rax
movl %ecx, -0xa0(%rbp,%rax,4)
movl -0x2ac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2ac(%rbp)
jmp 0x37b8
movl $0x0, -0x2c4(%rbp)
cmpl $0x10, -0x2c4(%rbp)
jge 0x3c02
movslq -0x2c4(%rbp), %rax
leaq -0x290(%rbp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x90a0
movl %eax, -0xfc(%rbp)
movl $0x0, -0x2c8(%rbp)
cmpl $0x4, -0x2c8(%rbp)
jge 0x3b21
movl -0xf8(%rbp), %eax
shll %eax
movl %eax, -0xf8(%rbp)
movl -0xfc(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x3ab6
movl -0xf8(%rbp), %eax
orl $0x1, %eax
movl %eax, -0xf8(%rbp)
movl -0xfc(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0xfc(%rbp)
movl -0x100(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x100(%rbp)
cmpl $0x8, -0x100(%rbp)
jne 0x3b0b
movq -0x308(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x8d40
jmp 0x3af7
movl $0x0, -0xf8(%rbp)
movl $0x0, -0x100(%rbp)
jmp 0x3b0d
movl -0x2c8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c8(%rbp)
jmp 0x3a79
movslq -0x2c4(%rbp), %rax
leaq -0x290(%rbp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x90a0
subq $0x1, %rax
movl %eax, -0x2cc(%rbp)
cmpl $0x0, -0x2cc(%rbp)
jl 0x3bec
movl -0xf8(%rbp), %eax
shll %eax
movl %eax, -0xf8(%rbp)
movslq -0x2c4(%rbp), %rax
leaq -0x290(%rbp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
movslq -0x2cc(%rbp), %rsi
callq 0x8db0
cmpl $0x0, (%rax)
je 0x3b95
movl -0xf8(%rbp), %eax
orl $0x1, %eax
movl %eax, -0xf8(%rbp)
movl -0x100(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x100(%rbp)
cmpl $0x8, -0x100(%rbp)
jne 0x3bd6
movq -0x308(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x8d40
jmp 0x3bc2
movl $0x0, -0xf8(%rbp)
movl $0x0, -0x100(%rbp)
jmp 0x3bd8
movl -0x2cc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x2cc(%rbp)
jmp 0x3b45
jmp 0x3bee
movl -0x2c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c4(%rbp)
jmp 0x3a42
movl $0x0, -0x2d0(%rbp)
movq -0x310(%rbp), %rdi
movslq -0x2d0(%rbp), %rax
movq %rax, -0x340(%rbp)
callq 0x90a0
movq %rax, %rcx
movq -0x340(%rbp), %rax
cmpq %rcx, %rax
jae 0x3d98
movl $0x0, -0x2d4(%rbp)
cmpl $0x2, -0x2d4(%rbp)
jge 0x3d82
cmpl $0x0, -0x2d4(%rbp)
jne 0x3c7e
movq -0x310(%rbp), %rdi
movslq -0x2d0(%rbp), %rsi
callq 0x8db0
movl (%rax), %eax
movl $0x10, %ecx
cltd
idivl %ecx
movl %eax, -0x104(%rbp)
jmp 0x3ca1
movq -0x310(%rbp), %rdi
movslq -0x2d0(%rbp), %rsi
callq 0x8db0
movl (%rax), %eax
movl $0x10, %ecx
cltd
idivl %ecx
movl %edx, -0x104(%rbp)
movslq -0x104(%rbp), %rax
leaq -0x290(%rbp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x90a0
subq $0x1, %rax
movl %eax, -0x2d8(%rbp)
cmpl $0x0, -0x2d8(%rbp)
jl 0x3d6c
movl -0xf8(%rbp), %eax
shll %eax
movl %eax, -0xf8(%rbp)
movslq -0x104(%rbp), %rax
leaq -0x290(%rbp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
movslq -0x2d8(%rbp), %rsi
callq 0x8db0
cmpl $0x0, (%rax)
je 0x3d15
movl -0xf8(%rbp), %eax
orl $0x1, %eax
movl %eax, -0xf8(%rbp)
movl -0x100(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x100(%rbp)
cmpl $0x8, -0x100(%rbp)
jne 0x3d56
movq -0x308(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x8d40
jmp 0x3d42
movl $0x0, -0xf8(%rbp)
movl $0x0, -0x100(%rbp)
jmp 0x3d58
movl -0x2d8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x2d8(%rbp)
jmp 0x3cc5
jmp 0x3d6e
movl -0x2d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2d4(%rbp)
jmp 0x3c43
jmp 0x3d84
movl -0x2d0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2d0(%rbp)
jmp 0x3c0c
cmpl $0x0, -0x100(%rbp)
je 0x3df6
movl $0x0, -0x2dc(%rbp)
movl -0x2dc(%rbp), %eax
movl $0x8, %ecx
subl -0x100(%rbp), %ecx
cmpl %ecx, %eax
jge 0x3ddf
movl -0xf8(%rbp), %eax
shll %eax
movl %eax, -0xf8(%rbp)
movl -0x2dc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2dc(%rbp)
jmp 0x3dab
movq -0x308(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x8d40
jmp 0x3df4
jmp 0x3df6
cmpl $0x0, -0x100(%rbp)
jne 0x3e20
movq -0x308(%rbp), %rdi
movl $0x0, -0x2e0(%rbp)
leaq -0x2e0(%rbp), %rsi
callq 0x8ed0
jmp 0x3e1e
jmp 0x3e4a
movq -0x308(%rbp), %rdi
movl -0x100(%rbp), %ecx
movl $0x8, %eax
subl %ecx, %eax
movl %eax, -0x2e4(%rbp)
leaq -0x2e4(%rbp), %rsi
callq 0x8ed0
jmp 0x3e48
jmp 0x3e4a
movb $0x1, -0x19(%rbp)
leaq -0x290(%rbp), %rax
movq %rax, -0x350(%rbp)
addq $0x180, %rax            # imm = 0x180
movq %rax, -0x348(%rbp)
movq -0x348(%rbp), %rdi
addq $-0x18, %rdi
movq %rdi, -0x358(%rbp)
callq 0x8f20
movq -0x350(%rbp), %rcx
movq -0x358(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x348(%rbp)
jne 0x3e69
testb $0x1, -0x19(%rbp)
jne 0x3eed
jmp 0x3ee1
movq -0x330(%rbp), %rdi
addq $-0x18, %rdi
movq %rdi, -0x360(%rbp)
callq 0x8f20
movq -0x338(%rbp), %rcx
movq -0x360(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x330(%rbp)
jne 0x3ea2
movq -0x308(%rbp), %rdi
callq 0x8f20
jmp 0x3efd
movq -0x308(%rbp), %rdi
callq 0x8f20
movq -0x300(%rbp), %rax
addq $0x360, %rsp            # imm = 0x360
popq %rbp
retq
movq -0x2a0(%rbp), %rdi
callq 0x2380
nopl (%rax)