// File: sisong[P]ApkDiffPatch/HDiffPatch/libHDiffPatch/HPatch/patch.c
// Function: patch
hpatch_BOOL patch(TByte* out_newData,TByte* out_newData_end,
                  const TByte* oldData,const TByte* oldData_end,
                  const TByte* serializedDiff,const TByte* serializedDiff_end){
    const TByte *code_lengths, *code_lengths_end,
                *code_inc_newPos, *code_inc_newPos_end,
                *code_inc_oldPos, *code_inc_oldPos_end,
                *code_newDataDiff, *code_newDataDiff_end;
    hpatch_size_t  coverCount;

    assert(out_newData<=out_newData_end);
    assert(oldData<=oldData_end);
    assert(serializedDiff<=serializedDiff_end);
    unpackUIntTo(&coverCount,&serializedDiff, serializedDiff_end);
    {   //head
        hpatch_size_t lengthSize,inc_newPosSize,inc_oldPosSize,newDataDiffSize;
        unpackUIntTo(&lengthSize,&serializedDiff, serializedDiff_end);
        unpackUIntTo(&inc_newPosSize,&serializedDiff, serializedDiff_end);
        unpackUIntTo(&inc_oldPosSize,&serializedDiff, serializedDiff_end);
        unpackUIntTo(&newDataDiffSize,&serializedDiff, serializedDiff_end);
#ifdef __RUN_MEM_SAFE_CHECK
        if (lengthSize>(hpatch_size_t)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
#endif
        code_lengths=serializedDiff;     serializedDiff+=lengthSize;
        code_lengths_end=serializedDiff;
#ifdef __RUN_MEM_SAFE_CHECK
        if (inc_newPosSize>(hpatch_size_t)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
#endif
        code_inc_newPos=serializedDiff; serializedDiff+=inc_newPosSize;
        code_inc_newPos_end=serializedDiff;
#ifdef __RUN_MEM_SAFE_CHECK
        if (inc_oldPosSize>(hpatch_size_t)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
#endif
        code_inc_oldPos=serializedDiff; serializedDiff+=inc_oldPosSize;
        code_inc_oldPos_end=serializedDiff;
#ifdef __RUN_MEM_SAFE_CHECK
        if (newDataDiffSize>(hpatch_size_t)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
#endif
        code_newDataDiff=serializedDiff; serializedDiff+=newDataDiffSize;
        code_newDataDiff_end=serializedDiff;
    }

    //decode rle ; rle data begin==cur serializedDiff;
    _SAFE_CHECK_DO(_bytesRle_load(out_newData, out_newData_end, serializedDiff, serializedDiff_end));

    {   //patch
        const hpatch_size_t newDataSize=(hpatch_size_t)(out_newData_end-out_newData);
        hpatch_size_t oldPosBack=0;
        hpatch_size_t newPosBack=0;
        hpatch_size_t i;
        for (i=0; i<coverCount; ++i){
            hpatch_size_t copyLength,addLength, oldPos,inc_oldPos,inc_oldPos_sign;
            unpackUIntTo(&copyLength,&code_inc_newPos, code_inc_newPos_end);
            unpackUIntTo(&addLength,&code_lengths, code_lengths_end);
#ifdef __RUN_MEM_SAFE_CHECK
            if (code_inc_oldPos>=code_inc_oldPos_end) return _hpatch_FALSE;
#endif
            inc_oldPos_sign=(*code_inc_oldPos)>>(8-kSignTagBit);
            unpackUIntWithTagTo(&inc_oldPos,&code_inc_oldPos, code_inc_oldPos_end, kSignTagBit);
            if (inc_oldPos_sign==0)
                oldPos=oldPosBack+inc_oldPos;
            else
                oldPos=oldPosBack-inc_oldPos;
            if (copyLength>0){
#ifdef __RUN_MEM_SAFE_CHECK
                if (copyLength>(hpatch_size_t)(newDataSize-newPosBack)) return _hpatch_FALSE;
                if (copyLength>(hpatch_size_t)(code_newDataDiff_end-code_newDataDiff)) return _hpatch_FALSE;
#endif
                memcpy(out_newData+newPosBack,code_newDataDiff,copyLength);
                code_newDataDiff+=copyLength;
                newPosBack+=copyLength;
            }
#ifdef __RUN_MEM_SAFE_CHECK
            if ( (addLength>(hpatch_size_t)(newDataSize-newPosBack)) ) return _hpatch_FALSE;
            if ( (oldPos>(hpatch_size_t)(oldData_end-oldData)) ||
                 (addLength>(hpatch_size_t)(oldData_end-oldData-oldPos)) ) return _hpatch_FALSE;
#endif
            addData(out_newData+newPosBack,oldData+oldPos,addLength);
            oldPosBack=oldPos;
            newPosBack+=addLength;
        }

        if (newPosBack<newDataSize){
            hpatch_size_t copyLength=newDataSize-newPosBack;
#ifdef __RUN_MEM_SAFE_CHECK
            if (copyLength>(hpatch_size_t)(code_newDataDiff_end-code_newDataDiff)) return _hpatch_FALSE;
#endif
            memcpy(out_newData+newPosBack,code_newDataDiff,copyLength);
            code_newDataDiff+=copyLength;
            //newPosBack=newDataSize;
        }
    }

    if (  (code_lengths==code_lengths_end)
        &&(code_inc_newPos==code_inc_newPos_end)
        &&(code_inc_oldPos==code_inc_oldPos_end)
        &&(code_newDataDiff==code_newDataDiff_end))
        return hpatch_TRUE;
    else
        return _hpatch_FALSE;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rax
cmpq -0x18(%rbp), %rax
ja 0x85dff
jmp 0x85e1e
leaq 0x2ec85(%rip), %rdi     # 0xb4a8b
leaq 0x2ebcd(%rip), %rsi     # 0xb49da
movl $0xea, %edx
leaq 0x2ec8f(%rip), %rcx     # 0xb4aa8
callq 0x4270
movq -0x20(%rbp), %rax
cmpq -0x28(%rbp), %rax
ja 0x85e2a
jmp 0x85e49
leaq 0x29366(%rip), %rdi     # 0xaf197
leaq 0x2eba2(%rip), %rsi     # 0xb49da
movl $0xeb, %edx
leaq 0x2ec64(%rip), %rcx     # 0xb4aa8
callq 0x4270
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
ja 0x85e55
jmp 0x85e74
leaq 0x2ecac(%rip), %rdi     # 0xb4b08
leaq 0x2eb77(%rip), %rsi     # 0xb49da
movl $0xec, %edx
leaq 0x2ec39(%rip), %rcx     # 0xb4aa8
callq 0x4270
jmp 0x85e76
movq -0x38(%rbp), %rsi
leaq -0x30(%rbp), %rdi
leaq -0x80(%rbp), %rdx
xorl %ecx, %ecx
callq 0x863b0
cmpl $0x0, %eax
jne 0x85e9a
movl $0x0, -0x4(%rbp)
jmp 0x8639d
jmp 0x85e9c
jmp 0x85e9e
movq -0x38(%rbp), %rsi
leaq -0x30(%rbp), %rdi
leaq -0x88(%rbp), %rdx
xorl %ecx, %ecx
callq 0x863b0
cmpl $0x0, %eax
jne 0x85ec5
movl $0x0, -0x4(%rbp)
jmp 0x8639d
jmp 0x85ec7
jmp 0x85ec9
movq -0x38(%rbp), %rsi
leaq -0x30(%rbp), %rdi
leaq -0x90(%rbp), %rdx
xorl %ecx, %ecx
callq 0x863b0
cmpl $0x0, %eax
jne 0x85ef0
movl $0x0, -0x4(%rbp)
jmp 0x8639d
jmp 0x85ef2
jmp 0x85ef4
movq -0x38(%rbp), %rsi
leaq -0x30(%rbp), %rdi
leaq -0x98(%rbp), %rdx
xorl %ecx, %ecx
callq 0x863b0
cmpl $0x0, %eax
jne 0x85f1b
movl $0x0, -0x4(%rbp)
jmp 0x8639d
jmp 0x85f1d
jmp 0x85f1f
movq -0x38(%rbp), %rsi
leaq -0x30(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
xorl %ecx, %ecx
callq 0x863b0
cmpl $0x0, %eax
jne 0x85f46
movl $0x0, -0x4(%rbp)
jmp 0x8639d
jmp 0x85f48
movq -0x88(%rbp), %rax
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jbe 0x85f6b
movl $0x0, -0x4(%rbp)
jmp 0x8639d
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x88(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x90(%rbp), %rax
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jbe 0x85fad
movl $0x0, -0x4(%rbp)
jmp 0x8639d
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x90(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x98(%rbp), %rax
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jbe 0x85fef
movl $0x0, -0x4(%rbp)
jmp 0x8639d
movq -0x30(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x98(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0xa0(%rbp), %rax
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jbe 0x86031
movl $0x0, -0x4(%rbp)
jmp 0x8639d
movq -0x30(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0xa0(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x863f0
cmpl $0x0, %eax
jne 0x86076
movl $0x0, -0x4(%rbp)
jmp 0x8639d
jmp 0x86078
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq $0x0, -0xb0(%rbp)
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
cmpq -0x80(%rbp), %rax
jae 0x862f6
jmp 0x860be
movq -0x58(%rbp), %rsi
leaq -0x50(%rbp), %rdi
leaq -0xc8(%rbp), %rdx
xorl %ecx, %ecx
callq 0x863b0
cmpl $0x0, %eax
jne 0x860e5
movl $0x0, -0x4(%rbp)
jmp 0x8639d
jmp 0x860e7
jmp 0x860e9
movq -0x48(%rbp), %rsi
leaq -0x40(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
xorl %ecx, %ecx
callq 0x863b0
cmpl $0x0, %eax
jne 0x86110
movl $0x0, -0x4(%rbp)
jmp 0x8639d
jmp 0x86112
movq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
jb 0x86128
movl $0x0, -0x4(%rbp)
jmp 0x8639d
movq -0x60(%rbp), %rax
movzbl (%rax), %eax
sarl $0x7, %eax
cltq
movq %rax, -0xe8(%rbp)
movq -0x68(%rbp), %rsi
leaq -0x60(%rbp), %rdi
leaq -0xe0(%rbp), %rdx
movl $0x1, %ecx
callq 0x863b0
cmpl $0x0, %eax
jne 0x86165
movl $0x0, -0x4(%rbp)
jmp 0x8639d
jmp 0x86167
cmpq $0x0, -0xe8(%rbp)
jne 0x86188
movq -0xb0(%rbp), %rax
addq -0xe0(%rbp), %rax
movq %rax, -0xd8(%rbp)
jmp 0x8619d
movq -0xb0(%rbp), %rax
subq -0xe0(%rbp), %rax
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0xc8(%rbp)
jbe 0x86233
movq -0xc8(%rbp), %rax
movq -0xa8(%rbp), %rcx
subq -0xb8(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x861d1
movl $0x0, -0x4(%rbp)
jmp 0x8639d
movq -0xc8(%rbp), %rax
movq -0x78(%rbp), %rcx
movq -0x70(%rbp), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jbe 0x861f4
movl $0x0, -0x4(%rbp)
jmp 0x8639d
movq -0x10(%rbp), %rdi
addq -0xb8(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0xc8(%rbp), %rdx
callq 0x4320
movq -0xc8(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0xc8(%rbp), %rax
addq -0xb8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xd0(%rbp), %rax
movq -0xa8(%rbp), %rcx
subq -0xb8(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x86259
movl $0x0, -0x4(%rbp)
jmp 0x8639d
movq -0xd8(%rbp), %rax
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
ja 0x8628e
movq -0xd0(%rbp), %rax
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rdx
subq %rdx, %rcx
subq -0xd8(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x8629a
movl $0x0, -0x4(%rbp)
jmp 0x8639d
movq -0x10(%rbp), %rdi
addq -0xb8(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq -0xd8(%rbp), %rsi
movq -0xd0(%rbp), %rdx
callq 0x86620
movq -0xd8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xd0(%rbp), %rax
addq -0xb8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xc0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc0(%rbp)
jmp 0x860ab
movq -0xb8(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jae 0x86365
movq -0xa8(%rbp), %rax
subq -0xb8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movq -0x78(%rbp), %rcx
movq -0x70(%rbp), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jbe 0x8633b
movl $0x0, -0x4(%rbp)
jmp 0x8639d
movq -0x10(%rbp), %rdi
addq -0xb8(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0xf0(%rbp), %rdx
callq 0x4320
movq -0xf0(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
jne 0x86396
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
jne 0x86396
movq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
jne 0x86396
movq -0x70(%rbp), %rax
cmpq -0x78(%rbp), %rax
jne 0x86396
movl $0x1, -0x4(%rbp)
jmp 0x8639d
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax)