// File: /workspace/github_cpp_makefile_star2_O3/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
xorl %ebp, %ebp
cmpb $0x1, %dil
jne 0x4461d
movq %rcx, %rdx
subq %r15, %rdx
leaq (%rdx,%rbx), %rdi
cmpq %r13, %rdi
jbe 0x444d8
subq %rdx, %r13
jbe 0x44558
movq 0x20(%r14), %rdi
addq %rdx, %rdi
cmpq %rdi, %rcx
jbe 0x444f1
movq %r14, %rdi
movq %rbx, %rcx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x44356
addq 0x10(%r14), %r12
leaq (%r12,%r13), %rcx
movq %rsi, %rdi
movq %rdx, %rsi
movq %r12, %rdx
callq *%rax
testl %eax, %eax
je 0x4461d
subq %r13, %rbx
addq 0x18(%r14), %r13
movq %r13, 0x18(%r14)
cmpq 0x20(%r14), %r13
jne 0x44555
movq %r13, %rax
cmpq %r13, %rax
jne 0x44535
movq %r14, %rdi
callq 0x441f9
testl %eax, %eax
je 0x4461d
testq %rbx, %rbx
je 0x445eb
movq 0x20(%r14), %r13
cmpq %rbx, %r13
movq %rbx, %rax
cmovbq %r13, %rax
subq %rax, %rbx
movq %rax, 0x18(%r14)
jmp 0x44520
movq %r13, %r12
movl $0x1, %ebp
testq %rbx, %rbx
je 0x4461d
movq %r15, %r13
negq %r13
leaq (%r12,%rbx), %rcx
movq 0x20(%r14), %rax
movq %rax, %rbp
subq %r12, %rbp
cmpq %rax, %rcx
cmovbeq %rbx, %rbp
testq %rbp, %rbp
je 0x445a7
addq 0x10(%r14), %r12
leaq (%r12,%r13), %rax
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
movb %dl, (%r12,%rcx)
incq %rcx
cmpq %rbp, %rcx
jb 0x44590
movq 0x18(%r14), %r12
movq 0x20(%r14), %rax
addq %rbp, %r12
movq %r12, 0x18(%r14)
cmpq %rax, %r12
jne 0x445f2
movq %r14, %rdi
callq 0x441f9
testl %eax, %eax
je 0x4461b
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 0x4510
movq %r12, 0x18(%r14)
subq %r12, %rbx
jne 0x4456c
movl $0x1, %ebp
jmp 0x4461d
cmpq %rbp, %rbx
movl $0x1, %ebp
je 0x4461d
leaq 0x1cffe(%rip), %rdi     # 0x61601
leaq 0x1cd84(%rip), %rsi     # 0x6138e
leaq 0x1cffe(%rip), %rcx     # 0x6160f
movl $0x2cc, %edx            # imm = 0x2CC
callq 0x4200
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq