// File: /workspace/github_cpp_makefile_star2_O3/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), %rax
movl (%rax), %ecx
leaq 0x18(%rsp), %rdx
movl %ecx, (%rdx)
movl 0x4(%rax), %ecx
movl %ecx, 0x10(%rsp)
movl 0x8(%rax), %eax
movl %eax, 0x14(%rsp)
xorl %esi, %esi
callq 0x652a
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rax
cmpq %rax, %rsi
je 0x4738
movl 0x10(%rsp), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x474d
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x652a
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rax
cmpq %rax, %rsi
je 0x4762
movl 0x14(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x476f
leaq 0x14(%rsp), %rdx
movq %rbx, %rdi
callq 0x652a
movl 0x18(%rsp), %ecx
testl %ecx, %ecx
jle 0x4856
xorl %ebp, %ebp
movl 0x10(%rsp), %eax
leaq 0xc(%rsp), %r15
testl %eax, %eax
jle 0x484c
xorl %r12d, %r12d
movslq 0x14(%rsp), %rcx
imull %ebp, %eax
addl %r12d, %eax
movslq %eax, %rdx
imulq %rcx, %rdx
movq (%r14), %rax
movl 0xc(%rax,%rdx,4), %ecx
leal 0xff(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
sarl $0x8, %edx
movl %edx, 0xc(%rsp)
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rcx
cmpq %rcx, %rsi
je 0x47d5
movl %edx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x47eb
movq %rbx, %rdi
movq %r15, %rdx
callq 0x664a
movq (%r14), %rax
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rcx
movslq 0x14(%rsp), %rdx
movl 0x10(%rsp), %edi
imull %ebp, %edi
addl %r12d, %edi
movslq %edi, %rdi
imulq %rdx, %rdi
movl 0xc(%rax,%rdi,4), %eax
leal 0xff(%rax), %edx
testl %eax, %eax
cmovnsl %eax, %edx
andl $0xffffff00, %edx       # imm = 0xFFFFFF00
subl %edx, %eax
movl %eax, 0xc(%rsp)
cmpq %rcx, %rsi
je 0x482d
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x4838
movq %rbx, %rdi
movq %r15, %rdx
callq 0x664a
incl %r12d
movl 0x10(%rsp), %eax
cmpl %eax, %r12d
jl 0x4791
movl 0x18(%rsp), %ecx
incl %ebp
cmpl %ecx, %ebp
jl 0x4786
movl 0x14(%rsp), %eax
cmpl $0x2, %eax
jl 0x4ad5
movq $0x0, 0x20(%rsp)
movl $0x3, %r15d
movq 0x675f(%rip), %r12      # 0xafd8
movslq 0x18(%rsp), %rcx
movl 0x10(%rsp), %edx
xorpd %xmm0, %xmm0
testq %rcx, %rcx
jle 0x48cc
movl %eax, %esi
imull %edx, %esi
movl %r15d, %edi
xorl %r8d, %r8d
testl %edx, %edx
jle 0x48c2
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 0x48a3
incq %r8
addl %esi, %edi
cmpq %rcx, %r8
jne 0x4896
imull %edx, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
addsd 0x3736(%rip), %xmm0     # 0x8018
addsd 0x3726(%rip), %xmm0     # 0x8010
callq 0x22a0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
movsd %xmm0, 0x30(%rsp)
jbe 0x4928
mulsd 0x3721(%rip), %xmm0     # 0x8028
cvttsd2si %xmm0, %eax
movl %eax, 0xc(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
jne 0x494c
movq %rbx, %rdi
leaq 0xc(%rsp), %rdx
callq 0x664a
jmp 0x4992
ucomisd %xmm1, %xmm0
jbe 0x4958
addsd %xmm0, %xmm0
addsd 0x36e6(%rip), %xmm0     # 0x8020
cvttsd2si %xmm0, %eax
movl %eax, 0xc(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x4976
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x4992
jne 0x4992
jp 0x4992
movl $0x0, 0xc(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x4985
movl $0x0, (%rsi)
jmp 0x494e
movq %rbx, %rdi
leaq 0xc(%rsp), %rdx
callq 0x664a
jmp 0x4992
movq %rbx, %rdi
leaq 0xc(%rsp), %rdx
callq 0x664a
movl %r15d, 0x2c(%rsp)
movq %r12, %rdi
movsd 0x30(%rsp), %xmm0
callq 0x2270
movl $0x1, %edx
movq %rax, %rdi
leaq 0x3710(%rip), %rsi      # 0x80c4
callq 0x21e0
movl 0x18(%rsp), %ecx
testl %ecx, %ecx
jle 0x4a8a
movq 0x20(%rsp), %rax
leal 0x3(%rax), %r15d
xorl %ebp, %ebp
movl 0x10(%rsp), %eax
testl %eax, %eax
jle 0x4a80
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 0x4a1d
je 0x4a27
leal -0x1(,%rax,2), %eax
jmp 0x4a21
addl %eax, %eax
negl %eax
movl %eax, 0x1c(%rsp)
jmp 0x4a29
xorl %eax, %eax
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x4a3f
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x4a4c
movq %rbx, %rdi
leaq 0x1c(%rsp), %rdx
callq 0x652a
movl 0x1c(%rsp), %esi
movq %r12, %rdi
callq 0x22b0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x365d(%rip), %rsi      # 0x80c4
callq 0x21e0
incl %r13d
movl 0x10(%rsp), %eax
cmpl %eax, %r13d
jl 0x49df
movl 0x18(%rsp), %ecx
incl %ebp
cmpl %ecx, %ebp
jl 0x49d4
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 0x4879
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4af5
jmp 0x4af5
jmp 0x4af5
jmp 0x4af5
jmp 0x4af5
jmp 0x4af5
jmp 0x4af5
movq %rax, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x4b0c
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x21b0
movq %r14, %rdi
callq 0x22c0