// File: /workspace/github_cpp_makefile_star2_O1/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)
movq $0x0, 0x10(%rdi)
movq (%rdx), %rcx
movl (%rcx), %eax
movl %eax, 0x18(%rsp)
movl 0x4(%rcx), %edx
movl %edx, 0xc(%rsp)
movl 0x8(%rcx), %ecx
movl %ecx, 0x14(%rsp)
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x45fd
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x460a
leaq 0x18(%rsp), %rdx
movq %rbx, %rdi
callq 0x62c2
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x4624
movl 0xc(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x4631
leaq 0xc(%rsp), %rdx
movq %rbx, %rdi
callq 0x62c2
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x464b
movl 0x14(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x4658
leaq 0x14(%rsp), %rdx
movq %rbx, %rdi
callq 0x62c2
cmpl $0x0, 0x18(%rsp)
jle 0x4735
xorl %ebp, %ebp
leaq 0x10(%rsp), %r15
movl 0xc(%rsp), %eax
testl %eax, %eax
jle 0x4729
xorl %r12d, %r12d
movslq 0x14(%rsp), %rcx
imull %ebp, %eax
addl %r12d, %eax
cltq
imulq %rcx, %rax
movq (%r14), %rcx
movl 0xc(%rcx,%rax,4), %ecx
leal 0xff(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
sarl $0x8, %eax
movl %eax, 0x10(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x46b9
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x46c4
movq %rbx, %rdi
movq %r15, %rdx
callq 0x63e2
movslq 0x14(%rsp), %rax
movl 0xc(%rsp), %ecx
imull %ebp, %ecx
addl %r12d, %ecx
movslq %ecx, %rcx
imulq %rax, %rcx
movq (%r14), %rax
movl 0xc(%rax,%rcx,4), %eax
leal 0xff(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $0xffffff00, %ecx       # imm = 0xFFFFFF00
subl %ecx, %eax
movl %eax, 0x10(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x470e
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x4719
movq %rbx, %rdi
movq %r15, %rdx
callq 0x63e2
incl %r12d
movl 0xc(%rsp), %eax
cmpl %eax, %r12d
jl 0x4679
incl %ebp
cmpl 0x18(%rsp), %ebp
jl 0x466a
movl 0x14(%rsp), %eax
cmpl $0x2, %eax
jl 0x49b7
movq $0x0, 0x20(%rsp)
movl $0x3, %r15d
movq 0x6880(%rip), %r12      # 0xafd8
movslq 0x18(%rsp), %rcx
xorpd %xmm0, %xmm0
testq %rcx, %rcx
jle 0x47ab
movl 0xc(%rsp), %edx
movl %eax, %esi
imull %edx, %esi
movl %r15d, %edi
xorl %r8d, %r8d
testl %edx, %edx
jle 0x47a1
movq (%r14), %r9
movq %rdx, %r10
movl %edi, %r11d
movslq %r11d, %r11
movl (%r9,%r11,4), %ebp
subl 0x4(%r9,%r11,4), %ebp
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
addsd %xmm1, %xmm0
addl %eax, %r11d
decq %r10
jne 0x4782
incq %r8
addl %esi, %edi
cmpq %rcx, %r8
jne 0x4775
imull 0xc(%rsp), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
addsd 0x3855(%rip), %xmm0     # 0x8018
addsd 0x3845(%rip), %xmm0     # 0x8010
callq 0x22a0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
movsd %xmm0, 0x30(%rsp)
jbe 0x4809
mulsd 0x3840(%rip), %xmm0     # 0x8028
cvttsd2si %xmm0, %eax
movl %eax, 0x10(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
jne 0x482d
movq %rbx, %rdi
leaq 0x10(%rsp), %rdx
callq 0x63e2
jmp 0x4873
ucomisd %xmm1, %xmm0
jbe 0x4839
addsd %xmm0, %xmm0
addsd 0x3805(%rip), %xmm0     # 0x8020
cvttsd2si %xmm0, %eax
movl %eax, 0x10(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x4857
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x4873
jne 0x4873
jp 0x4873
movl $0x0, 0x10(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x4866
movl $0x0, (%rsi)
jmp 0x482f
movq %rbx, %rdi
leaq 0x10(%rsp), %rdx
callq 0x63e2
jmp 0x4873
movq %rbx, %rdi
leaq 0x10(%rsp), %rdx
callq 0x63e2
movl %r15d, 0x2c(%rsp)
movq %r12, %rdi
movsd 0x30(%rsp), %xmm0
callq 0x2270
movl $0x1, %edx
movq %rax, %rdi
leaq 0x380f(%rip), %rsi      # 0x80a4
callq 0x21e0
cmpl $0x0, 0x18(%rsp)
jle 0x496c
movq 0x20(%rsp), %rax
leal 0x3(%rax), %r15d
xorl %ebp, %ebp
movl 0xc(%rsp), %eax
testl %eax, %eax
jle 0x4960
xorl %r13d, %r13d
imull %ebp, %eax
addl %r13d, %eax
imull 0x14(%rsp), %eax
addl %r15d, %eax
cltq
movq (%r14), %rcx
movl (%rcx,%rax,4), %edx
subl 0x4(%rcx,%rax,4), %edx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
subsd 0x30(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x1c(%rsp)
testl %eax, %eax
js 0x4901
je 0x4909
leal -0x1(,%rax,2), %eax
movl %eax, 0x1c(%rsp)
jmp 0x4909
addl %eax, %eax
negl %eax
movl %eax, 0x1c(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x4923
movl 0x1c(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x4930
movq %rbx, %rdi
leaq 0x1c(%rsp), %rdx
callq 0x62c2
movl 0x1c(%rsp), %esi
movq %r12, %rdi
callq 0x22b0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x3759(%rip), %rsi      # 0x80a4
callq 0x21e0
incl %r13d
movl 0xc(%rsp), %eax
cmpl %eax, %r13d
jl 0x48bf
incl %ebp
cmpl 0x18(%rsp), %ebp
jl 0x48b0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq 0x20(%rsp), %rdx
incl %edx
movl 0x14(%rsp), %eax
leal -0x1(%rax), %ecx
movl 0x2c(%rsp), %r15d
incl %r15d
movq %rdx, 0x20(%rsp)
cmpl %ecx, %edx
jl 0x4758
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x49d7
jmp 0x49d7
jmp 0x49d7
jmp 0x49d7
jmp 0x49d7
jmp 0x49d7
jmp 0x49d7
movq %rax, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x49ee
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x21b0
movq %r14, %rdi
callq 0x22c0