// File: /workspace/github_cpp_makefile_star2_O2/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 $0x128, %rsp            # imm = 0x128
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 0x3c4ee
movq %rdi, %r15
cmpq $0x0, 0x18(%rdi)
je 0x3c50d
movq %rsi, %r14
testq %rsi, %rsi
je 0x3c52c
cmpq $0x0, 0x10(%r14)
je 0x3c54b
cmpq $0x0, 0x10(%rbp)
je 0x3c56a
movq %r8, %rbx
movq %rax, %r13
movq 0x8(%rbp), %rax
movq %rax, 0x8(%rsp)
testq %r12, %r12
je 0x3c363
leaq 0xc0(%rsp), %rdi
movq %rbp, %rsi
callq 0x3dbbe
testl %eax, %eax
jne 0x3c396
jmp 0x3c433
leaq (,%r13,4), %rcx
addq %r13, %rcx
addq %rbx, %rcx
leaq 0x20(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
movq %rbp, %rdx
movq %r9, %r8
callq 0x3cb43
testl %eax, %eax
je 0x3c433
movq 0x20(%rsp), %r12
movq %r14, 0x18(%rsp)
movq 0xf0(%rsp), %rdx
movq 0xe0(%rsp), %r14
addq %rdx, %r14
leaq 0xf8(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rcx
movq %rbx, %r8
movq %r13, %r9
callq 0x3b1fb
cmpq $0xb, %r13
jb 0x3c433
addq %r13, %rbx
movq %r15, 0x10(%rsp)
leaq 0x38(%rsp), %r15
pushq $0xb
popq %r9
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq 0x8(%rsp), %rcx
movq %rbx, %r8
callq 0x3b1fb
leaq 0x98(%rsp), %rsi
movq %r15, %rdi
movq 0x10(%rsp), %r15
xorl %edx, %edx
callq 0x3af7d
testl %eax, %eax
je 0x3c433
movq 0x58(%rsp), %rdx
subq 0x60(%rsp), %rdx
addq 0x38(%rsp), %rdx
movq 0x98(%rsp), %r14
movq 0x8(%rsp), %rax
subq %rdx, %rax
cmpq %rax, %r14
jbe 0x3c447
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorps %xmm0, %xmm0
leaq 0x68(%rsp), %rax
movaps %xmm0, -0x48(%rax)
movb $0x0, -0x38(%rax)
leaq 0x38(%rsp), %rdi
movups %xmm0, 0x50(%rdi)
movups %xmm0, 0x40(%rdi)
movups %xmm0, 0x30(%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movups %xmm0, (%rdi)
addq %rdx, %r14
movq %rbp, %rsi
movq %r14, %rcx
movq %rbx, %r8
movq %r13, %r9
callq 0x3b1fb
addq %r13, %rbx
leaq 0x68(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq 0x8(%rsp), %rcx
movq %rbx, %r8
movq %r13, %r9
callq 0x3b1fb
leaq 0x98(%rsp), %rdi
andq $0x0, (%rdi)
addq %r13, %rbx
andq $0x0, 0x18(%rdi)
movq %r15, 0x8(%rdi)
movq %rbx, 0x10(%rdi)
movq %r13, 0x20(%rdi)
addq %r13, %rbx
movq %r13, (%rsp)
leaq 0xf8(%rsp), %rcx
leaq 0x20(%rsp), %r8
movq 0x18(%rsp), %rsi
movq %r12, %rdx
movq %rbx, %r9
callq 0x3dcd8
jmp 0x3c435
leaq 0x1aabb(%rip), %rdi     # 0x56fb0
leaq 0x19f32(%rip), %rsi     # 0x5642e
leaq 0x1aabc(%rip), %rcx     # 0x56fbf
movl $0x46c, %edx            # imm = 0x46C
callq 0x4230
leaq 0x1ab4c(%rip), %rdi     # 0x57060
leaq 0x19f13(%rip), %rsi     # 0x5642e
leaq 0x1aa9d(%rip), %rcx     # 0x56fbf
movl $0x46d, %edx            # imm = 0x46D
callq 0x4230
leaq 0x1ab43(%rip), %rdi     # 0x57076
leaq 0x19ef4(%rip), %rsi     # 0x5642e
leaq 0x1aa7e(%rip), %rcx     # 0x56fbf
movl $0x46e, %edx            # imm = 0x46E
callq 0x4230
leaq 0x1ab2f(%rip), %rdi     # 0x57081
leaq 0x19ed5(%rip), %rsi     # 0x5642e
leaq 0x1aa5f(%rip), %rcx     # 0x56fbf
movl $0x46f, %edx            # imm = 0x46F
callq 0x4230
leaq 0x1ab21(%rip), %rdi     # 0x57092
leaq 0x19eb6(%rip), %rsi     # 0x5642e
leaq 0x1aa40(%rip), %rcx     # 0x56fbf
movl $0x471, %edx            # imm = 0x471
callq 0x4230