// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %r14
movq %rdi, %rbx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movq (%rdx), %rax
movl (%rax), %ecx
leaq 0x1c(%rsp), %rsi
movl %ecx, (%rsi)
movl 0x4(%rax), %ecx
movl %ecx, 0x14(%rsp)
movl 0x8(%rax), %eax
movl %eax, 0x10(%rsp)
callq 0x51de
leaq 0x14(%rsp), %rsi
movq %rbx, %rdi
callq 0x51de
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x51de
xorl %ebp, %ebp
movl $0x100, %r12d           # imm = 0x100
leaq 0xc(%rsp), %r15
cmpl 0x1c(%rsp), %ebp
jge 0x3d89
xorl %r13d, %r13d
movl 0x14(%rsp), %eax
cmpl %eax, %r13d
jge 0x3d85
movslq 0x10(%rsp), %rcx
imull %ebp, %eax
addl %r13d, %eax
cltq
imulq %rcx, %rax
movq (%r14), %rcx
movl 0xc(%rcx,%rax,4), %eax
cltd
idivl %r12d
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5602
movslq 0x10(%rsp), %rax
movl 0x14(%rsp), %ecx
imull %ebp, %ecx
addl %r13d, %ecx
movslq %ecx, %rcx
imulq %rax, %rcx
movq (%r14), %rax
movl 0xc(%rax,%rcx,4), %eax
cltd
idivl %r12d
movl %edx, 0xc(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5602
incl %r13d
jmp 0x3d1c
incl %ebp
jmp 0x3d13
xorl %r11d, %r11d
pushq $0x3
popq %rax
movl %eax, 0x2c(%rsp)
leaq 0x18(%rsp), %rbp
movl 0x10(%rsp), %eax
leal -0x1(%rax), %ecx
cmpl %ecx, %r11d
jge 0x3f81
movl 0x1c(%rsp), %ecx
movl 0x14(%rsp), %edx
leal 0x3(%r11), %r15d
testl %edx, %edx
movl $0x0, %esi
cmovgl %edx, %esi
testl %ecx, %ecx
movl $0x0, %edi
movl %ecx, 0x20(%rsp)
cmovgl %ecx, %edi
movl %edx, %r8d
imull %eax, %r8d
xorpd %xmm0, %xmm0
movl 0x2c(%rsp), %r9d
xorl %r10d, %r10d
movq %r11, 0x30(%rsp)
cmpq %rdi, %r10
je 0x3e1a
movl %r9d, %r11d
movq %rsi, %r12
subq $0x1, %r12
jb 0x3e12
movslq %r11d, %r11
movq (%r14), %r13
movl (%r13,%r11,4), %ecx
subl 0x4(%r13,%r11,4), %ecx
cvtsi2sd %ecx, %xmm1
addsd %xmm1, %xmm0
addl %eax, %r11d
jmp 0x3def
incq %r10
addl %r8d, %r9d
jmp 0x3de4
imull 0x20(%rsp), %edx
cvtsi2sd %edx, %xmm1
divsd %xmm1, %xmm0
addsd 0x31e9(%rip), %xmm0     # 0x7018
addsd 0x31d9(%rip), %xmm0     # 0x7010
callq 0x2280
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
movsd %xmm0, 0x20(%rsp)
jbe 0x3e6b
mulsd 0x31d4(%rip), %xmm0     # 0x7028
cvttsd2si %xmm0, %eax
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
leaq 0xc(%rsp), %rsi
callq 0x5602
jmp 0x3eaa
ucomisd %xmm1, %xmm0
jbe 0x3e94
addsd %xmm0, %xmm0
addsd 0x31a3(%rip), %xmm0     # 0x7020
cvttsd2si %xmm0, %eax
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
leaq 0xc(%rsp), %rsi
callq 0x5602
jmp 0x3eaa
jne 0x3eb0
jp 0x3eb0
andl $0x0, 0xc(%rsp)
movq %rbx, %rdi
leaq 0xc(%rsp), %rsi
callq 0x5602
movsd 0x20(%rsp), %xmm0
movq 0x7121(%rip), %rdi      # 0xafd8
callq 0x2250
movq %rax, %rdi
leaq 0x31de(%rip), %rsi      # 0x70a4
callq 0x2180
xorl %r12d, %r12d
cmpl 0x1c(%rsp), %r12d
jge 0x3f64
xorl %r13d, %r13d
movl 0x14(%rsp), %eax
cmpl %eax, %r13d
jge 0x3f5c
imull %r12d, %eax
addl %r13d, %eax
imull 0x10(%rsp), %eax
addl %r15d, %eax
cltq
movq (%r14), %rcx
movl (%rcx,%rax,4), %edx
subl 0x4(%rcx,%rax,4), %edx
cvtsi2sd %edx, %xmm0
subsd 0x20(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x18(%rsp)
testl %eax, %eax
js 0x3f25
je 0x3f2d
leal -0x1(,%rax,2), %eax
movl %eax, 0x18(%rsp)
jmp 0x3f2d
addl %eax, %eax
negl %eax
movl %eax, 0x18(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x51de
movl 0x18(%rsp), %esi
movq 0x7095(%rip), %rdi      # 0xafd8
callq 0x2290
movq %rax, %rdi
leaq 0x3152(%rip), %rsi      # 0x70a4
callq 0x2180
incl %r13d
jmp 0x3edc
incl %r12d
jmp 0x3ece
movq 0x706d(%rip), %rdi      # 0xafd8
callq 0x20d0
movq 0x30(%rsp), %r11
incl %r11d
incl 0x2c(%rsp)
jmp 0x3d98
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3fa1
jmp 0x3fa1
jmp 0x3fa1
jmp 0x3fa1
jmp 0x3fa1
jmp 0x3fa1
jmp 0x3fa1
movq %rax, %r14
movq %rbx, %rdi
callq 0x5466
movq %r14, %rdi
callq 0x22a0