// File: /workspace/github_cpp_makefile_star2_O1/sisong[P]ApkDiffPatch/HDiffPatch/libHDiffPatch/HPatch/patch.c
// Function: TDiffToSingleStream_read
static hpatch_BOOL _TDiffToSingleStream_read(const struct hpatch_TStreamInput* stream,hpatch_StreamPos_t readFromPos,
                                           unsigned char* out_data,unsigned char* out_data_end){
    //[                                                         |readedSize                  ]
    //     [     |cachedBufBegin   _TDiffToSingleStream_kBufSize]
    //                                 readFromPos[out_data       out_data_end]
    TDiffToSingleStream* self=(TDiffToSingleStream*)stream->streamImport;
    hpatch_StreamPos_t readedSize=self->readedSize;
    while (1){
        size_t rLen=out_data_end-out_data;
        if (readFromPos==readedSize){
            hpatch_BOOL result=self->diffStream->read(self->diffStream,readedSize,out_data,out_data_end);
            self->readedSize=readedSize+rLen;
            if ((self->isInSingleStream)||(rLen>_TDiffToSingleStream_kBufSize)){
                self->cachedBufBegin=_TDiffToSingleStream_kBufSize;
            }else{
                //cache
                if (rLen>=_TDiffToSingleStream_kBufSize){
                    memcpy(self->buf,out_data_end-_TDiffToSingleStream_kBufSize,_TDiffToSingleStream_kBufSize);
                    self->cachedBufBegin = 0;
                }else{
                    size_t new_cachedBufBegin;
                    if (self->cachedBufBegin>=rLen){
                        new_cachedBufBegin=self->cachedBufBegin-rLen;
                        memmove(self->buf+new_cachedBufBegin,self->buf+self->cachedBufBegin,_TDiffToSingleStream_kBufSize-self->cachedBufBegin);
                    }else{
                        new_cachedBufBegin=0;
                        memmove(self->buf,self->buf+rLen,_TDiffToSingleStream_kBufSize-rLen);
                    }
                    memcpy(self->buf+(_TDiffToSingleStream_kBufSize-rLen),out_data,rLen);
                    self->cachedBufBegin=new_cachedBufBegin;
                }
            }
            return result;
        }else{
            size_t cachedSize=_TDiffToSingleStream_kBufSize-self->cachedBufBegin;
            size_t bufSize=(size_t)(readedSize-readFromPos);
            if ((readFromPos<readedSize)&(bufSize<=cachedSize)){
                if (rLen>bufSize)
                    rLen=bufSize;
                memcpy(out_data,self->buf+(_TDiffToSingleStream_kBufSize-bufSize),rLen);
                out_data+=rLen;
                readFromPos+=rLen;
                if (out_data==out_data_end) 
                    return hpatch_TRUE;
                else
                    continue;
            }else{
                return _hpatch_FALSE;
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r13
movq %rdx, %r14
movq %rsi, %r15
movq (%rdi), %rbp
movq 0x28(%rbp), %r12
leaq 0x3c(%rbp), %rsi
leaq -0x1000(%rcx), %rax
movq %rax, 0x20(%rsp)
movq %rsi, 0x8(%rsp)
movq %rcx, 0x18(%rsp)
movq %r13, %rbx
subq %r14, %rbx
cmpq %r15, %r12
jne 0x475bf
movq 0x20(%rbp), %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r13, %rcx
callq *0x10(%rdi)
leaq (%rbx,%r12), %rcx
movq %rcx, 0x28(%rbp)
cmpl $0x0, 0x38(%rbp)
jne 0x4762f
cmpq $0x1001, %rbx           # imm = 0x1001
jae 0x4762f
cmpq $0x1000, %rbx           # imm = 0x1000
movq 0x8(%rsp), %rcx
jb 0x47644
movl $0x1000, %edx           # imm = 0x1000
movl %eax, %ebx
movq %rcx, %rdi
movq 0x20(%rsp), %rsi
callq 0x4290
movl %ebx, %eax
movq 0x8(%rsp), %rsi
movq $0x0, 0x30(%rbp)
xorl %edi, %edi
jmp 0x476b0
xorl %edi, %edi
movq %r12, %rcx
subq %r15, %rcx
jbe 0x47640
movl $0x1000, %edx           # imm = 0x1000
subq 0x30(%rbp), %rdx
cmpq %rdx, %rcx
ja 0x47640
cmpq %rcx, %rbx
cmovaeq %rcx, %rbx
subq %rcx, %rsi
addq $0x1000, %rsi           # imm = 0x1000
movq %rbp, %r13
movq %r12, %rbp
movq %r15, %r12
movl %eax, %r15d
movq %r14, %rdi
movq %rbx, %rdx
callq 0x4290
movl %r15d, %eax
movq %r12, %r15
movq %rbp, %r12
movq %r13, %rbp
movq 0x18(%rsp), %r13
movq 0x8(%rsp), %rsi
addq %rbx, %r14
addq %rbx, %r15
cmpq %r13, %r14
setne %dil
movl $0x1, %ecx
cmovel %ecx, %eax
jmp 0x476b0
movq $0x1000, 0x30(%rbp)     # imm = 0x1000
xorl %edi, %edi
movq 0x8(%rsp), %rsi
jmp 0x476b0
xorl %eax, %eax
jmp 0x476b0
movl %eax, 0x14(%rsp)
movq 0x30(%rbp), %rax
movq %rax, %r13
subq %rbx, %r13
jae 0x4766d
leaq (%rcx,%rbx), %rsi
movl $0x1000, %edx           # imm = 0x1000
subq %rbx, %rdx
movq %rcx, %rdi
callq 0x4510
xorl %r13d, %r13d
jmp 0x47682
leaq (%rcx,%r13), %rdi
leaq (%rcx,%rax), %rsi
movl $0x1000, %edx           # imm = 0x1000
subq %rax, %rdx
callq 0x4510
movq 0x8(%rsp), %rdi
subq %rbx, %rdi
addq $0x1000, %rdi           # imm = 0x1000
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4290
movq %r13, 0x30(%rbp)
xorl %edi, %edi
movq 0x18(%rsp), %r13
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %eax
testb %dil, %dil
jne 0x47549
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq