// File: sisong[P]ApkDiffPatch/HDiffPatch/libHDiffPatch/HPatch/patch.c
// Function: patch_stream_with_cache
static hpatch_BOOL _patch_stream_with_cache(const hpatch_TStreamOutput* out_newData,
                                            const hpatch_TStreamInput*  oldData,
                                            const hpatch_TStreamInput*  serializedDiff,
                                            hpatch_TCovers*  cached_covers,
                                            TByte*   temp_cache,TByte* temp_cache_end){
    struct _THDiffHead              diffHead;
    TStreamCacheClip                code_newDataDiffClip;
    struct _TBytesRle_load_stream   rle_loader;
    hpatch_TCovers*                 pcovers=0;
    hpatch_StreamPos_t       diffPos0;
    const hpatch_StreamPos_t diffPos_end=serializedDiff->streamSize;
    const hpatch_size_t cacheSize=(temp_cache_end-temp_cache)/(cached_covers?(_kCachePatCount-3):_kCachePatCount);

    assert(out_newData!=0);
    assert(out_newData->write!=0);
    assert(oldData!=0);
    assert(oldData->read!=0);
    assert(serializedDiff!=0);
    assert(serializedDiff->read!=0);
    
    //covers
    if (cached_covers==0){
        struct _TPackedCovers* packedCovers;
        if (!_packedCovers_open(&packedCovers,&diffHead,serializedDiff,temp_cache+cacheSize*(_kCachePatCount-3),
                                temp_cache_end)) return _hpatch_FALSE;
            pcovers=&packedCovers->base.ICovers; //not need close before return
    }else{
        pcovers=cached_covers;
        if (!read_diff_head(&diffHead,serializedDiff)) return _hpatch_FALSE;
    }
    //newDataDiff
    diffPos0=diffHead.coverEndPos;
    _TStreamCacheClip_init(&code_newDataDiffClip,serializedDiff,diffPos0,
                           diffPos0+diffHead.newDataDiffSize,temp_cache,cacheSize);
    temp_cache+=cacheSize;
    diffPos0+=diffHead.newDataDiffSize;
        
    {//rle
        hpatch_StreamPos_t rleCtrlSize;
        hpatch_StreamPos_t rlePos0;
        TStreamCacheClip*  rleHeadClip=&rle_loader.ctrlClip;//rename, share address
#ifdef __RUN_MEM_SAFE_CHECK
        if (cacheSize<hpatch_kMaxPackedUIntBytes) return _hpatch_FALSE;
#endif
        _TStreamCacheClip_init(rleHeadClip,serializedDiff,diffPos0,diffPos_end,
                               temp_cache,hpatch_kMaxPackedUIntBytes);
        _clip_unpackUIntTo(&rleCtrlSize,rleHeadClip);
        rlePos0=(hpatch_StreamPos_t)(_TStreamCacheClip_readPosOfSrcStream(rleHeadClip));
#ifdef __RUN_MEM_SAFE_CHECK
        if (rleCtrlSize>(hpatch_StreamPos_t)(diffPos_end-rlePos0)) return _hpatch_FALSE;
#endif
        _TBytesRle_load_stream_init(&rle_loader);
        _TStreamCacheClip_init(&rle_loader.ctrlClip,serializedDiff,rlePos0,rlePos0+rleCtrlSize,
                               temp_cache,cacheSize);
        temp_cache+=cacheSize;
        _TStreamCacheClip_init(&rle_loader.rleCodeClip,serializedDiff,rlePos0+rleCtrlSize,diffPos_end,
                               temp_cache,cacheSize);
        temp_cache+=cacheSize;
    }
    {
        _TOutStreamCache outCache;
        _TOutStreamCache_init(&outCache,out_newData,temp_cache,cacheSize);
        temp_cache+=cacheSize;
        return patchByClip(&outCache,oldData,pcovers,&code_newDataDiffClip,
                           &rle_loader,temp_cache,cacheSize);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
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 $0x0, -0x120(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x130(%rbp)
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
subq %rcx, %rax
movq -0x28(%rbp), %rsi
movl $0x8, %ecx
movl $0x5, %edx
cmpq $0x0, %rsi
cmovnel %edx, %ecx
movslq %ecx, %rcx
cqto
idivq %rcx
movq %rax, -0x138(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x88695
jmp 0x886b4
leaq 0x2cf74(%rip), %rdi     # 0xb5610
leaq 0x2c337(%rip), %rsi     # 0xb49da
movl $0x46c, %edx            # imm = 0x46C
leaq 0x2cf70(%rip), %rcx     # 0xb561f
callq 0x4270
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x886c1
jmp 0x886e0
leaq 0x2cff8(%rip), %rdi     # 0xb56c0
leaq 0x2c30b(%rip), %rsi     # 0xb49da
movl $0x46d, %edx            # imm = 0x46D
leaq 0x2cf44(%rip), %rcx     # 0xb561f
callq 0x4270
cmpq $0x0, -0x18(%rbp)
je 0x886e9
jmp 0x88708
leaq 0x2cfe6(%rip), %rdi     # 0xb56d6
leaq 0x2c2e3(%rip), %rsi     # 0xb49da
movl $0x46e, %edx            # imm = 0x46E
leaq 0x2cf1c(%rip), %rcx     # 0xb561f
callq 0x4270
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x88715
jmp 0x88734
leaq 0x2cfc5(%rip), %rdi     # 0xb56e1
leaq 0x2c2b7(%rip), %rsi     # 0xb49da
movl $0x46f, %edx            # imm = 0x46F
leaq 0x2cef0(%rip), %rcx     # 0xb561f
callq 0x4270
cmpq $0x0, -0x20(%rbp)
je 0x8873d
jmp 0x8875c
leaq 0x2cfae(%rip), %rdi     # 0xb56f2
leaq 0x2c28f(%rip), %rsi     # 0xb49da
movl $0x470, %edx            # imm = 0x470
leaq 0x2cec8(%rip), %rcx     # 0xb561f
callq 0x4270
movq -0x20(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x88769
jmp 0x88788
leaq 0x2cf94(%rip), %rdi     # 0xb5704
leaq 0x2c263(%rip), %rsi     # 0xb49da
movl $0x471, %edx            # imm = 0x471
leaq 0x2ce9c(%rip), %rcx     # 0xb561f
callq 0x4270
cmpq $0x0, -0x28(%rbp)
jne 0x887d7
movq -0x20(%rbp), %rdx
movq -0x30(%rbp), %rcx
imulq $0x5, -0x138(%rbp), %rax
addq %rax, %rcx
movq -0x38(%rbp), %r8
leaq -0x140(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x89270
cmpl $0x0, %eax
jne 0x887c7
movl $0x0, -0x4(%rbp)
jmp 0x88a14
movq -0x140(%rbp), %rax
movq %rax, -0x120(%rbp)
jmp 0x88802
movq -0x28(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x8bdb0
cmpl $0x0, %eax
jne 0x88800
movl $0x0, -0x4(%rbp)
jmp 0x88a14
jmp 0x88802
movq -0x40(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x20(%rbp), %rsi
movq -0x128(%rbp), %rdx
movq -0x128(%rbp), %rcx
addq -0x50(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x138(%rbp), %r9
leaq -0xa0(%rbp), %rdi
callq 0x871e0
movq -0x138(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x50(%rbp), %rax
addq -0x128(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0x118(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x158(%rbp)
cmpq $0xb, -0x138(%rbp)
jae 0x88883
movl $0x0, -0x4(%rbp)
jmp 0x88a14
movq -0x158(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x128(%rbp), %rdx
movq -0x130(%rbp), %rcx
movq -0x30(%rbp), %r8
movl $0xb, %r9d
callq 0x871e0
movq -0x158(%rbp), %rdi
leaq -0x148(%rbp), %rsi
xorl %edx, %edx
callq 0x86d00
cmpl $0x0, %eax
jne 0x888d1
movl $0x0, -0x4(%rbp)
jmp 0x88a14
movq -0x158(%rbp), %rax
movq (%rax), %rax
movq -0x158(%rbp), %rcx
movq 0x28(%rcx), %rcx
movq -0x158(%rbp), %rdx
subq 0x20(%rdx), %rcx
subq %rcx, %rax
movq %rax, -0x150(%rbp)
movq -0x148(%rbp), %rax
movq -0x130(%rbp), %rcx
subq -0x150(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x88921
movl $0x0, -0x4(%rbp)
jmp 0x88a14
leaq -0x118(%rbp), %rdi
callq 0x8bfb0
leaq -0x118(%rbp), %rdi
addq $0x18, %rdi
movq -0x20(%rbp), %rsi
movq -0x150(%rbp), %rdx
movq -0x150(%rbp), %rcx
addq -0x148(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x138(%rbp), %r9
callq 0x871e0
movq -0x138(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x118(%rbp), %rdi
addq $0x48, %rdi
movq -0x20(%rbp), %rsi
movq -0x150(%rbp), %rdx
addq -0x148(%rbp), %rdx
movq -0x130(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x138(%rbp), %r9
callq 0x871e0
movq -0x138(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x138(%rbp), %rcx
leaq -0x180(%rbp), %rdi
callq 0x8a220
movq -0x138(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rsi
movq -0x120(%rbp), %rdx
movq -0x30(%rbp), %r9
movq -0x138(%rbp), %rax
leaq -0x180(%rbp), %rdi
leaq -0xa0(%rbp), %rcx
leaq -0x118(%rbp), %r8
movq %rax, (%rsp)
callq 0x8c020
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq