// File: dgboy2000[P]compress/compress.cpp
// Function: DATCompression::diff_compress(std::vector<int, std::allocator<int>>)
vector<int> DATCompression::diff_compress(vector<int> data) {
    vector<int> compressed;
    int x, y, L;
    
    x = data[0];
    y = data[1];
    L = data[2];
    
    compressed.push_back(x);
    compressed.push_back(y);
    compressed.push_back(L);
    
    for (int x_ind=0; x_ind < x; ++x_ind) {
        for (int y_ind=0; y_ind < y; ++y_ind) {
            int val = data[3 + L * (x_ind*y + y_ind)];

            compressed.push_back(val / 256);
            compressed.push_back(val % 256);
            
            for (int L_ind=1; L_ind < L; ++L_ind) {
                int diff = data[3 + L * (x_ind*y + y_ind) + L_ind] - val;
                val = diff + val;
                if (abs(diff) < 128) {
                    if (diff > 0) compressed.push_back(2*diff-1);
                    else compressed.push_back(-2*diff);
                    // compressed.push_back(diff + 127);
                    continue;
                }
                
                compressed.push_back(255);
                compressed.push_back(val / 256);
                compressed.push_back(val % 256);
            }
        }
    }
    
    return compressed;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdx, -0x78(%rbp)
movq %rdi, -0x70(%rbp)
movq %rdi, %rax
movq %rax, -0x80(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
callq 0x8d20
movq -0x78(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x8db0
movq -0x78(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x20(%rbp)
movl $0x1, %esi
callq 0x8db0
movq -0x78(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x24(%rbp)
movl $0x2, %esi
callq 0x8db0
movq -0x70(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x28(%rbp)
leaq -0x20(%rbp), %rsi
callq 0x8d40
jmp 0x2c36
movq -0x70(%rbp), %rdi
leaq -0x24(%rbp), %rsi
callq 0x8d40
jmp 0x2c45
movq -0x70(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x8d40
jmp 0x2c54
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x2e3b
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x2e2b
movq -0x78(%rbp), %rdi
movl -0x28(%rbp), %eax
movl -0x38(%rbp), %ecx
movl -0x24(%rbp), %edx
imull %edx, %ecx
movl -0x3c(%rbp), %edx
addl %edx, %ecx
imull %ecx, %eax
addl $0x3, %eax
movslq %eax, %rsi
callq 0x8db0
movq -0x70(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movl %eax, %ecx
addl $0xff, %ecx
testl %eax, %eax
cmovsl %ecx, %eax
sarl $0x8, %eax
movl %eax, -0x44(%rbp)
leaq -0x44(%rbp), %rsi
callq 0x8ed0
jmp 0x2cc7
movq -0x70(%rbp), %rdi
movl -0x40(%rbp), %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, -0x48(%rbp)
leaq -0x48(%rbp), %rsi
callq 0x8ed0
jmp 0x2cf3
movl $0x1, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x2e1b
movq -0x78(%rbp), %rdi
movl -0x28(%rbp), %eax
movl -0x38(%rbp), %ecx
imull -0x24(%rbp), %ecx
addl -0x3c(%rbp), %ecx
imull %ecx, %eax
addl $0x3, %eax
addl -0x4c(%rbp), %eax
movslq %eax, %rsi
callq 0x8db0
movl (%rax), %eax
subl -0x40(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x50(%rbp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
cmpl $0x80, %eax
jge 0x2da4
cmpl $0x0, -0x50(%rbp)
jle 0x2d87
movq -0x70(%rbp), %rdi
movl -0x50(%rbp), %ecx
movl %ecx, %eax
leal -0x1(%rax,%rax), %eax
movl %eax, -0x54(%rbp)
leaq -0x54(%rbp), %rsi
callq 0x8ed0
jmp 0x2d6b
jmp 0x2da2
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x8f20
jmp 0x2e5b
movq -0x70(%rbp), %rdi
movl -0x50(%rbp), %eax
addl %eax, %eax
negl %eax
movl %eax, -0x58(%rbp)
leaq -0x58(%rbp), %rsi
callq 0x8ed0
jmp 0x2da0
jmp 0x2da2
jmp 0x2e0d
movq -0x70(%rbp), %rdi
movl $0xff, -0x5c(%rbp)
leaq -0x5c(%rbp), %rsi
callq 0x8ed0
jmp 0x2dba
movq -0x70(%rbp), %rdi
movl -0x40(%rbp), %eax
movl %eax, %ecx
addl $0xff, %ecx
testl %eax, %eax
cmovsl %ecx, %eax
sarl $0x8, %eax
movl %eax, -0x60(%rbp)
leaq -0x60(%rbp), %rsi
callq 0x8ed0
jmp 0x2ddf
movq -0x70(%rbp), %rdi
movl -0x40(%rbp), %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, -0x64(%rbp)
leaq -0x64(%rbp), %rsi
callq 0x8ed0
jmp 0x2e0b
jmp 0x2e0d
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x2cfa
jmp 0x2e1d
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2c6e
jmp 0x2e2d
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2c5b
movb $0x1, -0x19(%rbp)
testb $0x1, -0x19(%rbp)
jne 0x2e4e
movq -0x70(%rbp), %rdi
callq 0x8f20
movq -0x80(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x2380
nopw %cs:(%rax,%rax)