// File: sisong[P]ApkDiffPatch/HDiffPatch/libHDiffPatch/HDiff/diff.cpp
// Function: (anonymous namespace)::serialize_compressed_diff((anonymous namespace)::TDiffData const&, std::vector<(anonymous namespace)::TOldCover, std::allocator<(anonymous namespace)::TOldCover>>&, std::vector<unsigned char, std::allocator<unsigned char>>&, hdiff_TCompress const*)
static void serialize_compressed_diff(const TDiffData& diff,std::vector<TOldCover>& covers,std::vector<TByte>& out_diff,
                                      const hdiff_TCompress* compressPlugin){
    const TUInt coverCount=(TUInt)covers.size();
    std::vector<TByte> cover_buf;
    {
        TInt lastOldEnd=0;
        TInt lastNewEnd=0;
        for (TUInt i=0; i<coverCount; ++i) {
            if (covers[i].oldPos>=lastOldEnd){ //save inc_oldPos
                packUIntWithTag(cover_buf,(TUInt)(covers[i].oldPos-lastOldEnd), 0, 1);
            }else{
                packUIntWithTag(cover_buf,(TUInt)(lastOldEnd-covers[i].oldPos), 1, 1);//sub safe
            }
            assert(covers[i].newPos>=lastNewEnd);
            packUInt(cover_buf,(TUInt)(covers[i].newPos-lastNewEnd)); //save inc_newPos
            packUInt(cover_buf,(TUInt)covers[i].length);
            lastOldEnd=covers[i].oldPos+covers[i].length;//! +length
            lastNewEnd=covers[i].newPos+covers[i].length;
        }
    }
    
    std::vector<TByte> rle_ctrlBuf;
    std::vector<TByte> rle_codeBuf;
    {
        const TCovers _covers((void*)covers.data(),covers.size(),
                              sizeof(*covers.data())==sizeof(hpatch_TCover32));
        TNewDataSubDiffStream_mem newDataSubDiff(diff.newData,diff.newData_end,
                                                 diff.oldData,diff.oldData_end,_covers);
        bytesRLE_save(rle_ctrlBuf,rle_codeBuf,&newDataSubDiff,kRle_bestSize);
    }
    
    std::vector<TByte> compress_cover_buf;
    std::vector<TByte> compress_rle_ctrlBuf;
    std::vector<TByte> compress_rle_codeBuf;
    std::vector<TByte> compress_newDataDiff;
    do_compress(compress_cover_buf,cover_buf,compressPlugin);
    do_compress(compress_rle_ctrlBuf,rle_ctrlBuf,compressPlugin);
    do_compress(compress_rle_codeBuf,rle_codeBuf,compressPlugin);
    
    const TCovers _covers((void*)covers.data(),covers.size(),
                          sizeof(*covers.data())==sizeof(hpatch_TCover32));
    hpatch_TStreamInput _newDataStream;
    mem_as_hStreamInput(&_newDataStream,diff.newData,diff.newData_end);
    TNewDataDiffStream newDataDiffStream(_covers,&_newDataStream);
    do_compress(compress_newDataDiff,&newDataDiffStream,compressPlugin);

    _outType(out_diff,compressPlugin);
    const TUInt newDataSize=(size_t)(diff.newData_end-diff.newData);
    const TUInt oldDataSize=(size_t)(diff.oldData_end-diff.oldData);
    packUInt(out_diff, newDataSize);
    packUInt(out_diff, oldDataSize);
    packUInt(out_diff, coverCount);
    packUInt(out_diff, (TUInt)cover_buf.size());
    packUInt(out_diff, (TUInt)compress_cover_buf.size());
    packUInt(out_diff, (TUInt)rle_ctrlBuf.size());
    packUInt(out_diff, (TUInt)compress_rle_ctrlBuf.size());
    packUInt(out_diff, (TUInt)rle_codeBuf.size());
    packUInt(out_diff, (TUInt)compress_rle_codeBuf.size());
    packUInt(out_diff, newDataDiffStream.streamSize);
    packUInt(out_diff, (TUInt)compress_newDataDiff.size());
            
    pushCompressCode(out_diff,compress_cover_buf,cover_buf);
    pushCompressCode(out_diff,compress_rle_ctrlBuf,rle_ctrlBuf);
    pushCompressCode(out_diff,compress_rle_codeBuf,rle_codeBuf);
    pushCompressCode(out_diff,compress_newDataDiff,&newDataDiffStream);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x320, %rsp            # imm = 0x320
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1a300
movq %rax, -0x28(%rbp)
leaq -0x40(%rbp), %rdi
callq 0xbe70
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x19a87
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1fb50
movq (%rax), %rax
cmpq -0x48(%rbp), %rax
jl 0x19959
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1fb50
movq (%rax), %rsi
movq -0x48(%rbp), %rax
subq %rax, %rsi
leaq -0x40(%rbp), %rdi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x284d0
jmp 0x19946
jmp 0x19992
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x19efc
movq -0x48(%rbp), %rax
movq %rax, -0x2a8(%rbp)
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1fb50
movq -0x2a8(%rbp), %rsi
movq (%rax), %rax
subq %rax, %rsi
leaq -0x40(%rbp), %rdi
movl $0x1, %ecx
movl %ecx, %edx
callq 0x284d0
jmp 0x19990
jmp 0x19992
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1fb50
movq 0x8(%rax), %rax
cmpq -0x50(%rbp), %rax
jl 0x199ab
jmp 0x199ca
leaq 0x95c09(%rip), %rdi     # 0xaf5bb
leaq 0x953c9(%rip), %rsi     # 0xaed82
movl $0x29d, %edx            # imm = 0x29D
leaq 0x95d5a(%rip), %rcx     # 0xaf71f
callq 0x4270
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1fb50
movq 0x8(%rax), %rsi
movq -0x50(%rbp), %rax
subq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0x28480
jmp 0x199ed
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1fb50
movq 0x10(%rax), %rsi
leaq -0x40(%rbp), %rdi
callq 0x28480
jmp 0x19a09
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1fb50
movq (%rax), %rax
movq %rax, -0x2b8(%rbp)
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1fb50
movq %rax, %rcx
movq -0x2b8(%rbp), %rax
addq 0x10(%rcx), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1fb50
movq 0x8(%rax), %rax
movq %rax, -0x2b0(%rbp)
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1fb50
movq %rax, %rcx
movq -0x2b0(%rbp), %rax
addq 0x10(%rcx), %rax
movq %rax, -0x50(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x198f9
leaq -0x80(%rbp), %rdi
callq 0xbe70
leaq -0x98(%rbp), %rdi
callq 0xbe70
movq -0x10(%rbp), %rdi
callq 0x1a2e0
movq %rax, -0x2c0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1a300
movq -0x2c0(%rbp), %rsi
movq %rax, %rdx
leaq -0xc0(%rbp), %rdi
xorl %ecx, %ecx
callq 0x29b00
jmp 0x19acf
movq -0x8(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq 0x10(%rax), %rcx
movq 0x18(%rax), %r8
movq %rsp, %rax
movl $0x0, 0x8(%rax)
movl $0x0, (%rax)
leaq -0x180(%rbp), %rdi
leaq -0xc0(%rbp), %r9
callq 0x4d890
jmp 0x19b07
leaq -0x80(%rbp), %rdi
leaq -0x98(%rbp), %rsi
leaq -0x180(%rbp), %rdx
movl $0x1, %ecx
callq 0x37d80
jmp 0x19b25
leaq -0x180(%rbp), %rdi
callq 0x2b080
leaq -0x198(%rbp), %rdi
movq %rdi, -0x2c8(%rbp)
callq 0xbe70
leaq -0x1b0(%rbp), %rdi
callq 0xbe70
leaq -0x1c8(%rbp), %rdi
callq 0xbe70
leaq -0x1e0(%rbp), %rdi
callq 0xbe70
movq -0x2c8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq -0x40(%rbp), %rsi
xorl %ecx, %ecx
callq 0x28830
jmp 0x19b80
movq -0x20(%rbp), %rdx
leaq -0x1b0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
xorl %ecx, %ecx
callq 0x28830
jmp 0x19b98
movq -0x20(%rbp), %rdx
leaq -0x1c8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
xorl %ecx, %ecx
callq 0x28830
jmp 0x19bb3
movq -0x10(%rbp), %rdi
callq 0x1a2e0
movq %rax, -0x2d0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1a300
movq -0x2d0(%rbp), %rsi
movq %rax, %rdx
leaq -0x208(%rbp), %rdi
xorl %ecx, %ecx
callq 0x29b00
jmp 0x19be6
movq -0x8(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
leaq -0x228(%rbp), %rdi
callq 0x85b30
jmp 0x19bff
leaq -0x290(%rbp), %rdi
leaq -0x208(%rbp), %rsi
leaq -0x228(%rbp), %rdx
callq 0x2b030
jmp 0x19c1b
movq -0x20(%rbp), %rdx
leaq -0x1e0(%rbp), %rdi
leaq -0x290(%rbp), %rsi
xorl %ecx, %ecx
callq 0x50620
jmp 0x19c36
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq 0xcc933(%rip), %rdx     # 0xe6578
callq 0x1c330
jmp 0x19c4c
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
subq %rcx, %rax
movq %rax, -0x298(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rcx
movq 0x18(%rax), %rax
subq %rcx, %rax
movq %rax, -0x2a0(%rbp)
movq -0x18(%rbp), %rdi
movq -0x298(%rbp), %rsi
callq 0x28480
jmp 0x19c89
movq -0x18(%rbp), %rdi
movq -0x2a0(%rbp), %rsi
callq 0x28480
jmp 0x19c9b
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x28480
jmp 0x19caa
movq -0x18(%rbp), %rax
movq %rax, -0x2d8(%rbp)
leaq -0x40(%rbp), %rdi
callq 0xbe90
movq -0x2d8(%rbp), %rdi
movq %rax, %rsi
callq 0x28480
jmp 0x19ccf
movq -0x18(%rbp), %rax
movq %rax, -0x2e0(%rbp)
leaq -0x198(%rbp), %rdi
callq 0xbe90
movq -0x2e0(%rbp), %rdi
movq %rax, %rsi
callq 0x28480
jmp 0x19cf7
movq -0x18(%rbp), %rax
movq %rax, -0x2e8(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xbe90
movq -0x2e8(%rbp), %rdi
movq %rax, %rsi
callq 0x28480
jmp 0x19d1c
movq -0x18(%rbp), %rax
movq %rax, -0x2f0(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0xbe90
movq -0x2f0(%rbp), %rdi
movq %rax, %rsi
callq 0x28480
jmp 0x19d44
movq -0x18(%rbp), %rax
movq %rax, -0x2f8(%rbp)
leaq -0x98(%rbp), %rdi
callq 0xbe90
movq -0x2f8(%rbp), %rdi
movq %rax, %rsi
callq 0x28480
jmp 0x19d6c
movq -0x18(%rbp), %rax
movq %rax, -0x300(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0xbe90
movq -0x300(%rbp), %rdi
movq %rax, %rsi
callq 0x28480
jmp 0x19d94
movq -0x18(%rbp), %rdi
movq -0x288(%rbp), %rsi
callq 0x1c920
jmp 0x19da6
movq -0x18(%rbp), %rax
movq %rax, -0x308(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0xbe90
movq -0x308(%rbp), %rdi
movq %rax, %rsi
callq 0x28480
jmp 0x19dce
movq -0x18(%rbp), %rdi
leaq -0x198(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x288b0
jmp 0x19de4
movq -0x18(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
leaq -0x80(%rbp), %rdx
callq 0x288b0
jmp 0x19dfa
movq -0x18(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
leaq -0x98(%rbp), %rdx
callq 0x288b0
jmp 0x19e13
movq -0x18(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
leaq -0x290(%rbp), %rdx
callq 0x28900
jmp 0x19e2c
leaq -0x1e0(%rbp), %rdi
callq 0xbb80
leaq -0x1c8(%rbp), %rdi
callq 0xbb80
leaq -0x1b0(%rbp), %rdi
callq 0xbb80
leaq -0x198(%rbp), %rdi
callq 0xbb80
leaq -0x98(%rbp), %rdi
callq 0xbb80
leaq -0x80(%rbp), %rdi
callq 0xbb80
leaq -0x40(%rbp), %rdi
callq 0xbb80
addq $0x320, %rsp            # imm = 0x320
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x19ee7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x180(%rbp), %rdi
callq 0x2b080
jmp 0x19ee7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0xbb80
leaq -0x1c8(%rbp), %rdi
callq 0xbb80
leaq -0x1b0(%rbp), %rdi
callq 0xbb80
leaq -0x198(%rbp), %rdi
callq 0xbb80
leaq -0x98(%rbp), %rdi
callq 0xbb80
leaq -0x80(%rbp), %rdi
callq 0xbb80
leaq -0x40(%rbp), %rdi
callq 0xbb80
movq -0x60(%rbp), %rdi
callq 0x4680
nop