// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r8, 0x10(%rsp)
cmpq %rsi, %rdi
ja 0x3abc2
movq %rcx, %r15
movq %rdx, %r12
cmpq %rcx, %rdx
ja 0x3abe1
movq %r9, %rbp
cmpq %r9, %r8
ja 0x3ac00
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x10(%rsp), %rdi
leaq 0x80(%rsp), %rdx
xorl %r13d, %r13d
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x3a5a8
testl %eax, %eax
je 0x3a9f8
leaq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %rdx
xorl %r13d, %r13d
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x3a5a8
testl %eax, %eax
je 0x3a9f8
leaq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rdx
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x3a5a8
testl %eax, %eax
je 0x3a9f8
leaq 0x10(%rsp), %rdi
leaq 0x38(%rsp), %rdx
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x3a5a8
testl %eax, %eax
je 0x3a9f8
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rdx
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x3a5a8
testl %eax, %eax
je 0x3a9f8
movq 0x28(%rsp), %rdx
movq 0x10(%rsp), %rax
movq %rbp, %rcx
subq %rax, %rcx
cmpq %rcx, %rdx
ja 0x3a9f8
movq %rax, 0x78(%rsp)
addq %rax, %rdx
movq 0x20(%rsp), %rcx
movq %rbp, %rax
subq %rdx, %rax
cmpq %rax, %rcx
ja 0x3a9f8
movq %rdx, 0x70(%rsp)
addq %rdx, %rcx
movq 0x38(%rsp), %rsi
movq %rbp, %rax
subq %rcx, %rax
cmpq %rax, %rsi
ja 0x3a9f8
movq %rcx, 0x58(%rsp)
addq %rcx, %rsi
movq 0x40(%rsp), %r8
movq %rbp, %rax
subq %rsi, %rax
cmpq %rax, %r8
ja 0x3a9f8
movq %rcx, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rsi, 0x30(%rsp)
addq %rsi, %r8
movq %r8, 0x10(%rsp)
leaq 0x28(%rsp), %rdi
movq %r8, 0x48(%rsp)
movq %r8, (%rdi)
leaq 0x38(%rsp), %rdx
xorl %r13d, %r13d
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x3a5a8
testl %eax, %eax
je 0x3a9f8
movq 0x38(%rsp), %rsi
movq 0x28(%rsp), %rax
movq %rbp, %rcx
subq %rax, %rcx
cmpq %rcx, %rsi
ja 0x3a9f8
leaq 0x20(%rsp), %rcx
movq %rax, (%rcx)
addq %rax, %rsi
movq %rsi, 0x28(%rsp)
movq %rsi, 0x8(%rsp)
movq %r14, %rdi
movq 0x30(%rsp), %rdx
movq %rsi, 0x50(%rsp)
movq %rsi, %rcx
subq %rax, %rcx
testq %rcx, %rcx
jle 0x3a9df
movzbl (%rax), %eax
movl %eax, (%rsp)
movq %rdi, 0x18(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rdx
pushq $0x2
popq %rcx
callq 0x3a5a8
movq 0x18(%rsp), %rdi
testl %eax, %eax
je 0x3a9f8
movq 0x40(%rsp), %rax
movq %rbx, %rcx
subq %rdi, %rcx
cmpq %rcx, %rax
jae 0x3a9f8
movl (%rsp), %ecx
shrl $0x6, %ecx
leaq 0x1(%rax), %rdx
movq %rdx, 0x40(%rsp)
movl %ecx, %ecx
leaq 0x1baa1(%rip), %rsi     # 0x56404
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
movq %rdx, (%rsp)
jmpq *%rcx
xorl %esi, %esi
jmp 0x3a9ba
cmpq %rbp, 0x8(%rsp)
je 0x3a9f8
movq 0x8(%rsp), %rax
movzbl (%rax), %esi
callq 0x41f0
movq (%rsp), %rax
incq 0x8(%rsp)
jmp 0x3a9c3
movq %rbp, %rcx
subq 0x8(%rsp), %rcx
cmpq %rcx, %rax
jae 0x3a9f8
movq 0x8(%rsp), %rsi
callq 0x42c0
movq (%rsp), %rax
addq %rax, 0x8(%rsp)
jmp 0x3a9c3
movl $0xff, %esi
callq 0x41f0
movq (%rsp), %rax
movq 0x18(%rsp), %rdi
addq %rax, %rdi
movq 0x20(%rsp), %rax
movq 0x30(%rsp), %rdx
movq 0x50(%rsp), %rsi
jmp 0x3a8fe
xorq %rsi, %rax
movq 0x8(%rsp), %rcx
xorq %rbp, %rcx
orq %rax, %rcx
xorq %rbx, %rdi
orq %rcx, %rdi
je 0x3aa0d
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
subq %r14, %rbx
movq 0x80(%rsp), %rax
subq %r12, %r15
movq $0x0, 0x18(%rsp)
movq %rdx, (%rsp)
xorl %ebp, %ebp
subq $0x1, %rax
movq %rax, 0x8(%rsp)
jb 0x3ab53
leaq 0x70(%rsp), %rdi
movq 0x68(%rsp), %rsi
leaq 0x28(%rsp), %rdx
xorl %ecx, %ecx
callq 0x3a5a8
testl %eax, %eax
je 0x3a9f5
leaq 0x78(%rsp), %rdi
movq 0x60(%rsp), %rsi
leaq 0x20(%rsp), %rdx
xorl %ecx, %ecx
callq 0x3a5a8
testl %eax, %eax
je 0x3a9f5
movq 0x58(%rsp), %rax
cmpq 0x30(%rsp), %rax
jae 0x3a9f5
movb (%rax), %r13b
leaq 0x58(%rsp), %rdi
movq 0x30(%rsp), %rsi
leaq 0x38(%rsp), %rdx
pushq $0x1
popq %rcx
callq 0x3a5a8
testl %eax, %eax
je 0x3a9f5
movq 0x38(%rsp), %rcx
testb %r13b, %r13b
jns 0x3aaac
negq %rcx
movq 0x28(%rsp), %r13
testq %r13, %r13
je 0x3aaf8
movq %rbx, %rax
subq %rbp, %rax
cmpq %rax, %r13
ja 0x3a9f5
movq 0x48(%rsp), %rax
subq (%rsp), %rax
cmpq %rax, %r13
ja 0x3a9f5
leaq (%r14,%rbp), %rdi
movq (%rsp), %rsi
movq %r13, %rdx
movq %rcx, 0x50(%rsp)
callq 0x42c0
movq 0x50(%rsp), %rcx
addq %r13, (%rsp)
addq %r13, %rbp
movq 0x20(%rsp), %r13
movq %rbx, %rax
subq %rbp, %rax
cmpq %rax, %r13
ja 0x3a9f5
movq 0x18(%rsp), %rdx
addq %rcx, %rdx
movq %r15, %rax
movq %rdx, 0x18(%rsp)
subq %rdx, %rax
jb 0x3a9f5
cmpq %rax, %r13
ja 0x3a9f5
movq %rbp, %rsi
leaq (%r14,%rsi), %rdi
movq 0x18(%rsp), %rax
leaq (%r12,%rax), %rsi
movq %r13, %rdx
callq 0x3ac1f
addq %r13, %rbp
movq 0x8(%rsp), %rax
jmp 0x3aa2a
subq %rbp, %rbx
jbe 0x3ab86
movq 0x48(%rsp), %rax
subq (%rsp), %rax
cmpq %rax, %rbx
ja 0x3a9f5
addq %rbp, %r14
movq %r14, %rdi
movq (%rsp), %r14
movq %r14, %rsi
movq %rbx, %rdx
callq 0x42c0
addq %rbx, %r14
movq %r14, (%rsp)
movq 0x60(%rsp), %rax
xorq 0x78(%rsp), %rax
movq 0x68(%rsp), %rcx
xorq 0x70(%rsp), %rcx
orq %rax, %rcx
movq 0x30(%rsp), %rax
xorq 0x58(%rsp), %rax
orq %rcx, %rax
movq (%rsp), %rbp
xorq 0x48(%rsp), %rbp
xorl %r13d, %r13d
orq %rax, %rbp
sete %r13b
jmp 0x3a9f8
leaq 0x1b916(%rip), %rdi     # 0x564df
leaq 0x1b85e(%rip), %rsi     # 0x5642e
leaq 0x1b925(%rip), %rcx     # 0x564fc
movl $0xea, %edx
callq 0x4230
leaq 0x16164(%rip), %rdi     # 0x50d4c
leaq 0x1b83f(%rip), %rsi     # 0x5642e
leaq 0x1b906(%rip), %rcx     # 0x564fc
movl $0xeb, %edx
callq 0x4230
leaq 0x1b955(%rip), %rdi     # 0x5655c
leaq 0x1b820(%rip), %rsi     # 0x5642e
leaq 0x1b8e7(%rip), %rcx     # 0x564fc
movl $0xec, %edx
callq 0x4230