// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rcx, %r12
movq %rdx, %rbp
movq %r9, %rax
subq %r8, %rax
xorl %ecx, %ecx
testq %r12, %r12
sete %cl
leaq (%rcx,%rcx,2), %rcx
addq $0x5, %rcx
cqto
idivq %rcx
testq %rdi, %rdi
je 0x4556a
movq %rdi, %r15
cmpq $0x0, 0x18(%rdi)
je 0x45589
testq %rsi, %rsi
je 0x455a8
cmpq $0x0, 0x10(%rsi)
je 0x455c7
cmpq $0x0, 0x10(%rbp)
je 0x455e6
movq %r8, %rbx
movq %rax, %r13
movq %rsi, 0x8(%rsp)
movq 0x8(%rbp), %r14
testq %r12, %r12
je 0x4539d
leaq 0xe0(%rsp), %rdi
movq %rbp, %rsi
callq 0x46e59
testl %eax, %eax
jne 0x453d0
jmp 0x45488
leaq (,%r13,4), %rcx
addq %r13, %rcx
addq %rbx, %rcx
leaq 0x40(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
movq %rbp, %rdx
movq %r9, %r8
callq 0x45bf3
testl %eax, %eax
je 0x45488
movq 0x40(%rsp), %r12
movq 0x110(%rsp), %rdx
movq 0x100(%rsp), %rax
addq %rdx, %rax
jb 0x4554b
movq 0x8(%rbp), %rcx
cmpq %rax, %rcx
jb 0x4554b
movq %rdx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq %rbp, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r13, 0x30(%rsp)
movq %r13, 0x38(%rsp)
cmpq $0xb, %r13
jb 0x45488
cmpq %r14, %rax
ja 0x4554b
cmpq %r14, %rcx
jb 0x4554b
addq %r13, %rbx
leaq 0x58(%rsp), %rdi
movq %rax, (%rdi)
movq %r14, 0x8(%rdi)
movq %rbp, 0x10(%rdi)
movq %rbx, 0x18(%rdi)
movl $0xb, %eax
movq %rax, 0x20(%rdi)
movq %rax, 0x28(%rdi)
leaq 0xb8(%rsp), %rsi
xorl %edx, %edx
callq 0x43f27
testl %eax, %eax
je 0x45488
movq 0x78(%rsp), %rcx
subq 0x80(%rsp), %rcx
addq 0x58(%rsp), %rcx
movq 0xb8(%rsp), %rax
movq %r14, %rdx
subq %rcx, %rdx
cmpq %rdx, %rax
jbe 0x4549c
xorl %eax, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movb $0x0, 0x50(%rsp)
addq %rcx, %rax
jb 0x4554b
movq 0x8(%rbp), %rdx
cmpq %rax, %rdx
jb 0x4554b
movq %rcx, 0x58(%rsp)
movq %rax, 0x60(%rsp)
movq %rbp, 0x68(%rsp)
movq %rbx, 0x70(%rsp)
movq %r13, 0x78(%rsp)
movq %r13, 0x80(%rsp)
cmpq %r14, %rax
ja 0x4554b
cmpq %r14, %rdx
jb 0x4554b
addq %r13, %rbx
leaq 0x40(%rsp), %r8
movq %rax, 0x48(%r8)
movq %r14, 0x50(%r8)
movq %rbp, 0x58(%r8)
movq %rbx, 0x60(%r8)
movq %r13, 0x68(%r8)
movq %r13, 0x70(%r8)
addq %r13, %rbx
xorl %eax, %eax
leaq 0xb8(%rsp), %rdi
movq %rax, (%rdi)
movq %rax, 0x18(%rdi)
movq %r15, 0x8(%rdi)
movq %rbx, 0x10(%rdi)
movq %r13, 0x20(%rdi)
addq %r13, %rbx
movq %r13, (%rsp)
leaq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rsi
movq %r12, %rdx
movq %rbx, %r9
callq 0x46f72
jmp 0x4548a
leaq 0x1c5cd(%rip), %rdi     # 0x61b1f
leaq 0x1c617(%rip), %rsi     # 0x61b70
leaq 0x1c640(%rip), %rcx     # 0x61ba0
movl $0x55, %edx
callq 0x4200
leaq 0x1c99f(%rip), %rdi     # 0x61f10
leaq 0x1be16(%rip), %rsi     # 0x6138e
leaq 0x1c9a0(%rip), %rcx     # 0x61f1f
movl $0x46c, %edx            # imm = 0x46C
callq 0x4200
leaq 0x1ca30(%rip), %rdi     # 0x61fc0
leaq 0x1bdf7(%rip), %rsi     # 0x6138e
leaq 0x1c981(%rip), %rcx     # 0x61f1f
movl $0x46d, %edx            # imm = 0x46D
callq 0x4200
leaq 0x1ca27(%rip), %rdi     # 0x61fd6
leaq 0x1bdd8(%rip), %rsi     # 0x6138e
leaq 0x1c962(%rip), %rcx     # 0x61f1f
movl $0x46e, %edx            # imm = 0x46E
callq 0x4200
leaq 0x1ca13(%rip), %rdi     # 0x61fe1
leaq 0x1bdb9(%rip), %rsi     # 0x6138e
leaq 0x1c943(%rip), %rcx     # 0x61f1f
movl $0x46f, %edx            # imm = 0x46F
callq 0x4200
leaq 0x1ca05(%rip), %rdi     # 0x61ff2
leaq 0x1bd9a(%rip), %rsi     # 0x6138e
leaq 0x1c924(%rip), %rcx     # 0x61f1f
movl $0x471, %edx            # imm = 0x471
callq 0x4200