// File: dgboy2000[P]compress/compress.cpp
// Function: DATCompression::frame_diff_compress(std::vector<int, std::allocator<int>>)
vector<int> DATCompression::frame_diff_compress(vector<int> data)
{
  vector<int> compressed;
  double total;
  int x = data[0], y = data[1], l = data[2], d;

  compressed.push_back(x);
  compressed.push_back(y);
  compressed.push_back(l);

  // for the first frame, l_ind = 0
  for(int x_ind = 0; x_ind < x; x_ind++)
    for(int y_ind = 0; y_ind < y; y_ind++)
    {
      compressed.push_back(data[3 + l * (x_ind * y + y_ind)] / 256);
      compressed.push_back(data[3 + l * (x_ind * y + y_ind)] % 256);
    }

  // for the following frames
  for(int l_ind = 0; l_ind < l - 1; l_ind++)
  {
    // calculate the different between the l_ind frame and (l_ind + 1) frame
    total = 0.0;
    for(int x_ind = 0; x_ind < x; x_ind++)
      for(int y_ind = 0; y_ind < y; y_ind++)
        total += data[3 + l * (x_ind * y + y_ind) + l_ind] - data[3 + l * (x_ind * y + y_ind) + l_ind + 1];
    total /= (x * y);
    total = floor(total + 0.5 + 1e-8);

    if(total < 0)
      compressed.push_back(-2 * total);
    else if(total > 0)
      compressed.push_back(2 * total - 1);
    else if(!total)
      compressed.push_back(0);
    cout << total << " ";
    for(int x_ind = 0; x_ind < x; x_ind++)
      for(int y_ind = 0; y_ind < y; y_ind++)
      {
        d = data[3 + l * (x_ind * y + y_ind) + l_ind] - data[3 + l * (x_ind * y + y_ind) + l_ind + 1] - total;
        if(d < 0)
          d = -2 * d;
        else if(d > 0)
          d = 2 * d - 1;
        compressed.push_back(d);
        cout << d << " ";
      }
    cout << endl;
  }

  return compressed;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdx, -0x88(%rbp)
movq %rdi, -0x80(%rbp)
movq %rdi, %rax
movq %rax, -0x90(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
callq 0x8d20
movq -0x88(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x8db0
movq -0x88(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
movl $0x1, %esi
callq 0x8db0
movq -0x88(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x30(%rbp)
movl $0x2, %esi
callq 0x8db0
movq -0x80(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x34(%rbp)
leaq -0x2c(%rbp), %rsi
callq 0x8d40
jmp 0x5485
movq -0x80(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x8d40
jmp 0x5494
movq -0x80(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x8d40
jmp 0x54a3
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x559e
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x558e
movq -0x88(%rbp), %rdi
movl -0x34(%rbp), %eax
movl -0x48(%rbp), %ecx
movl -0x30(%rbp), %edx
imull %edx, %ecx
movl -0x4c(%rbp), %edx
addl %edx, %ecx
imull %ecx, %eax
addl $0x3, %eax
movslq %eax, %rsi
callq 0x8db0
movq -0x80(%rbp), %rdi
movl (%rax), %eax
movl %eax, %ecx
addl $0xff, %ecx
testl %eax, %eax
cmovsl %ecx, %eax
sarl $0x8, %eax
movl %eax, -0x50(%rbp)
leaq -0x50(%rbp), %rsi
callq 0x8ed0
jmp 0x5513
movq -0x88(%rbp), %rdi
movl -0x34(%rbp), %eax
movl -0x48(%rbp), %ecx
movl -0x30(%rbp), %edx
imull %edx, %ecx
movl -0x4c(%rbp), %edx
addl %edx, %ecx
imull %ecx, %eax
addl $0x3, %eax
movslq %eax, %rsi
callq 0x8db0
movq -0x80(%rbp), %rdi
movl (%rax), %eax
movl %eax, %edx
addl $0xff, %edx
testl %eax, %eax
movl %eax, %ecx
cmovsl %edx, %ecx
andl $0xffffff00, %ecx       # imm = 0xFFFFFF00
subl %ecx, %eax
movl %eax, -0x54(%rbp)
leaq -0x54(%rbp), %rsi
callq 0x8ed0
jmp 0x5564
jmp 0x5566
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x54bd
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x8f20
jmp 0x58d0
jmp 0x5590
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x54aa
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x58ad
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x566c
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x565c
movq -0x88(%rbp), %rdi
movl -0x34(%rbp), %eax
movl -0x5c(%rbp), %ecx
imull -0x30(%rbp), %ecx
addl -0x60(%rbp), %ecx
imull %ecx, %eax
addl $0x3, %eax
addl -0x58(%rbp), %eax
movslq %eax, %rsi
callq 0x8db0
movq -0x88(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x94(%rbp)
movl -0x34(%rbp), %eax
movl -0x5c(%rbp), %ecx
imull -0x30(%rbp), %ecx
addl -0x60(%rbp), %ecx
imull %ecx, %eax
addl $0x3, %eax
addl -0x58(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x8db0
movq %rax, %rcx
movl -0x94(%rbp), %eax
subl (%rcx), %eax
cvtsi2sd %eax, %xmm0
addsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x55d8
jmp 0x565e
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x55c5
movl -0x2c(%rbp), %eax
imull -0x30(%rbp), %eax
cvtsi2sd %eax, %xmm1
movsd -0x28(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movsd 0xa98b(%rip), %xmm0     # 0x10018
addsd -0x28(%rbp), %xmm0
movsd 0xa96e(%rip), %xmm1     # 0x10008
addsd %xmm1, %xmm0
callq 0x2360
movsd %xmm0, -0x28(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x28(%rbp), %xmm0
jbe 0x56db
movq -0x80(%rbp), %rdi
movsd -0x28(%rbp), %xmm0
movsd 0xa965(%rip), %xmm1     # 0x10028
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x64(%rbp)
leaq -0x64(%rbp), %rsi
callq 0x8ed0
jmp 0x56d9
jmp 0x5742
movsd -0x28(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5716
movq -0x80(%rbp), %rdi
movsd -0x28(%rbp), %xmm0
addsd %xmm0, %xmm0
movsd 0xa922(%rip), %xmm1     # 0x10020
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x68(%rbp)
leaq -0x68(%rbp), %rsi
callq 0x8ed0
jmp 0x5714
jmp 0x5740
movsd -0x28(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x573e
jp 0x573e
movq -0x80(%rbp), %rdi
movl $0x0, -0x6c(%rbp)
leaq -0x6c(%rbp), %rsi
callq 0x8ed0
jmp 0x573c
jmp 0x573e
jmp 0x5740
jmp 0x5742
movsd -0x28(%rbp), %xmm0
movq 0x1088a(%rip), %rdi     # 0x15fd8
callq 0x23c0
movq %rax, -0xa0(%rbp)
jmp 0x575c
movq -0xa0(%rbp), %rdi
leaq 0xa93a(%rip), %rsi      # 0x100a4
callq 0x2200
jmp 0x5771
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x5888
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x5878
movq -0x88(%rbp), %rdi
movl -0x34(%rbp), %eax
movl -0x70(%rbp), %ecx
imull -0x30(%rbp), %ecx
addl -0x74(%rbp), %ecx
imull %ecx, %eax
addl $0x3, %eax
addl -0x58(%rbp), %eax
movslq %eax, %rsi
callq 0x8db0
movq -0x88(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
movl -0x34(%rbp), %eax
movl -0x70(%rbp), %ecx
imull -0x30(%rbp), %ecx
addl -0x74(%rbp), %ecx
imull %ecx, %eax
addl $0x3, %eax
addl -0x58(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x8db0
movq %rax, %rcx
movl -0xa4(%rbp), %eax
subl (%rcx), %eax
cvtsi2sd %eax, %xmm0
subsd -0x28(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jge 0x5819
imull $0xfffffffe, -0x38(%rbp), %eax # imm = 0xFFFFFFFE
movl %eax, -0x38(%rbp)
jmp 0x582c
cmpl $0x0, -0x38(%rbp)
jle 0x582a
movl -0x38(%rbp), %eax
shll %eax
subl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x582c
movq -0x80(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x8d40
jmp 0x583b
movl -0x38(%rbp), %esi
movq 0x10793(%rip), %rdi     # 0x15fd8
callq 0x2370
movq %rax, -0xb0(%rbp)
jmp 0x5853
movq -0xb0(%rbp), %rdi
leaq 0xa843(%rip), %rsi      # 0x100a4
callq 0x2200
jmp 0x5868
jmp 0x586a
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x578b
jmp 0x587a
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x5778
movq 0x10749(%rip), %rdi     # 0x15fd8
movq 0x10732(%rip), %rsi     # 0x15fc8
callq 0x2240
jmp 0x589d
jmp 0x589f
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x55a5
movb $0x1, -0x19(%rbp)
testb $0x1, -0x19(%rbp)
jne 0x58c0
movq -0x80(%rbp), %rdi
callq 0x8f20
movq -0x90(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x2380
nopl (%rax)