// File: /workspace/github_cpp_makefile_star2_O2/sisong[P]ApkDiffPatch/HDiffPatch/libHDiffPatch/HPatch/patch.c
// Function: TOutStreamCache_copyFromSelf
hpatch_BOOL _TOutStreamCache_copyFromSelf(_TOutStreamCache* self,hpatch_StreamPos_t aheadLength,hpatch_StreamPos_t copyLength){
    //      [          writed                 ]
    //                                        [    cached buf    |        empty buf         ]
    const hpatch_TStreamInput* src=(const hpatch_TStreamInput*)self->dstStream;
    hpatch_StreamPos_t srcPos=self->writeToPos+self->cacheCur-aheadLength;
    if (src->read==0) //can't read
        return _hpatch_FALSE;
    if ((aheadLength<1)|(aheadLength>self->writeToPos+self->cacheCur))
            return _hpatch_FALSE;
    
    if (srcPos+copyLength<=self->writeToPos){//copy from stream
        //    [        copyLength          ]
__copy_in_stream:
        return _TOutStreamCache_copyFromStream(self,src,srcPos,copyLength);
    }else if (srcPos>=self->writeToPos){ //copy in mem
        //                                      [        copyLength        ]
__copy_in_mem:
        while (copyLength>0){
            hpatch_byte* dstBuf=self->cacheBuf+self->cacheCur;
            hpatch_byte* srcBuf=dstBuf-(hpatch_size_t)aheadLength;
            hpatch_size_t runLen=(self->cacheCur+copyLength<=self->cacheEnd)?(hpatch_size_t)copyLength:(self->cacheEnd-self->cacheCur);
            hpatch_size_t i;
            for (i=0;i<runLen;i++)
                dstBuf[i]=srcBuf[i];
            copyLength-=runLen;
            self->cacheCur+=runLen;
            if (self->cacheCur==self->cacheEnd){
                if (!_TOutStreamCache_flush(self))
                    return _hpatch_FALSE;
                runLen=(hpatch_size_t)((aheadLength<=copyLength)?aheadLength:copyLength);
                memmove(self->cacheBuf,self->cacheBuf+self->cacheEnd-(hpatch_size_t)aheadLength,runLen);
                self->cacheCur=runLen;
                copyLength-=runLen;
            }else{
                assert(copyLength==0);
            }
        }
        return hpatch_TRUE;
    }else if (self->writeToPos+self->cacheCur<=srcPos+self->cacheEnd){ 
        // small data in stream，can as copy in mem
        hpatch_byte* dstBuf=self->cacheBuf+self->cacheCur;
        hpatch_size_t runLen=(hpatch_size_t)(self->writeToPos-srcPos);
        if (!src->read(src,srcPos,dstBuf,dstBuf+runLen))
            return _hpatch_FALSE;
        //srcPos+=runLen; //not used
        copyLength-=runLen;
        self->cacheCur+=runLen;
        if (self->cacheCur==self->cacheEnd){
            while (hpatch_TRUE){
                if (self->cacheCur==self->cacheEnd){
                    if (!_TOutStreamCache_flush(self))
                        return _hpatch_FALSE;
                }
                if (copyLength>0){
                    runLen=(self->cacheEnd<=copyLength)?self->cacheEnd:(hpatch_size_t)copyLength;
                    //srcPos+=runLen; //not used
                    copyLength-=runLen;
                    self->cacheCur=runLen;
                }else{
                    return hpatch_TRUE;
                }
            }
        }else{
            goto __copy_in_mem;
        }
    }else{
        goto __copy_in_stream;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movq (%rdi), %r13
movq 0x8(%rdi), %rsi
movq 0x18(%rdi), %r12
leaq (%r12,%r13), %rcx
movq 0x10(%rsi), %rax
testq %rax, %rax
setne %dl
leaq -0x1(%r15), %rdi
cmpq %rcx, %rdi
setb %dil
andb %dl, %dil
cmpb $0x1, %dil
jne 0x3b668
movq %rcx, %rdx
subq %r15, %rdx
leaq (%rdx,%rbx), %rdi
cmpq %r13, %rdi
jbe 0x3b52a
subq %rdx, %r13
jbe 0x3b5ad
movq 0x20(%r14), %rdi
addq %rdx, %rdi
cmpq %rdi, %rcx
jbe 0x3b543
movq %r14, %rdi
movq %rbx, %rcx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3b3d8
addq 0x10(%r14), %r12
leaq (%r12,%r13), %rcx
movq %rsi, %rdi
movq %rdx, %rsi
movq %r12, %rdx
callq *%rax
testl %eax, %eax
je 0x3b668
subq %r13, %rbx
addq 0x18(%r14), %r13
movq %r13, 0x18(%r14)
cmpq 0x20(%r14), %r13
jne 0x3b5aa
pushq $0x1
popq %rbp
movq %r13, %rax
cmpq %r13, %rax
jne 0x3b58a
movq %r14, %rdi
callq 0x3b25f
testl %eax, %eax
je 0x3b668
testq %rbx, %rbx
je 0x3b67e
movq 0x20(%r14), %r13
cmpq %rbx, %r13
movq %rbx, %rax
cmovbq %r13, %rax
subq %rax, %rbx
movq %rax, 0x18(%r14)
jmp 0x3b575
movq %r13, %r12
movq %r15, %r13
negq %r13
testq %rbx, %rbx
je 0x3b679
movq 0x10(%r14), %rax
movq 0x20(%r14), %rdx
leaq (%r12,%rbx), %rcx
movq %rdx, %rbp
subq %r12, %rbp
cmpq %rdx, %rcx
cmovbeq %rbx, %rbp
cmovaq %rdx, %rcx
leaq (%rax,%r13), %rdx
cmpq %r12, %rcx
je 0x3b5ef
movb (%rdx,%r12), %sil
movb %sil, (%rax,%r12)
incq %r12
jmp 0x3b5dd
movq 0x18(%r14), %r12
addq %rbp, %r12
movq %r12, 0x18(%r14)
cmpq 0x20(%r14), %r12
jne 0x3b63b
movq %r14, %rdi
callq 0x3b25f
testl %eax, %eax
je 0x3b668
subq %rbp, %rbx
cmpq %r15, %rbx
movq %r15, %r12
cmovbq %rbx, %r12
movq 0x10(%r14), %rdi
movq 0x20(%r14), %rsi
addq %rdi, %rsi
addq %r13, %rsi
movq %r12, %rdx
callq 0x4540
movq %r12, 0x18(%r14)
subq %r12, %rbx
jmp 0x3b5b3
cmpq %rbp, %rbx
movl $0x0, %ebx
je 0x3b5b3
leaq 0x1b051(%rip), %rdi     # 0x566a1
leaq 0x1add7(%rip), %rsi     # 0x5642e
leaq 0x1b051(%rip), %rcx     # 0x566af
movl $0x2cc, %edx            # imm = 0x2CC
callq 0x4230
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rax
jmp 0x3b66a
movl %ebp, %eax
jmp 0x3b66a