// File: sisong[P]ApkDiffPatch/HDiffPatch/libHDiffPatch/HDiff/private_diff/limit_mem_diff/digest_matcher.cpp
// Function: bool hdiff_private::getBestMatch<unsigned long>(unsigned long const*, unsigned long const*, unsigned long const*, hdiff_private::TOldStreamCache&, hdiff_private::TNewStreamCache&, hpatch_TCover const&, unsigned long, hpatch_TCover*)
static bool getBestMatch(const TIndex* left,const TIndex* right,const TIndex* best,
                         TOldStreamCache& oldStream,TNewStreamCache& newStream,
                         const TCover& lastCover,size_t digests_eq_n,TCover* out_curCover){
    assert(best!=0);
    const size_t kMatchBlockSize=newStream.kMatchBlockSize;
    const hpatch_StreamPos_t newPos=newStream.pos();
    bool isMatched=false;
    hpatch_StreamPos_t  bestLen=0;
    const size_t kMaxFindCount=5; //周围距离2;
    size_t findCount=(right-left)*2+1;
    if (findCount>kMaxFindCount) findCount=kMaxFindCount;
    for (size_t inc=1;(inc<=findCount);++inc) { //best附近找;
        const TIndex* fi;  size_t s=(inc>>1);
        if (inc&1){
            if (best<left+s) continue;
            fi=best-s;
        }else{
            if (best+s>=right) continue;
            fi=best+s;
        }
        if (inc>1)
            newStream.TBlockStreamCache::resetPos(newPos);
        
        hpatch_StreamPos_t oldPos=blockIndexToPos(*fi,kMatchBlockSize,oldStream.streamSize());
        hpatch_StreamPos_t matchedOldPos=oldPos;
        hpatch_StreamPos_t curEqLen=getMatchLength(oldStream,newStream,
                                                   &matchedOldPos,kMatchBlockSize,lastCover);
        if (curEqLen>bestLen){
            isMatched=true;
            bestLen=curEqLen;
            out_curCover->length=curEqLen;
            out_curCover->oldPos=matchedOldPos;
            out_curCover->newPos=newPos-(oldPos-matchedOldPos);
            if (curEqLen>=digests_eq_n*kMatchBlockSize)
                break;//matched maybe best
        }
    }
    return isMatched;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x413f4
jmp 0x41413
leaq 0x6f1fb(%rip), %rdi     # 0xb05f6
leaq 0x6ef2c(%rip), %rsi     # 0xb032e
movl $0x22d, %edx            # imm = 0x22D
leaq 0x6f1f0(%rip), %rcx     # 0xb05fe
callq 0x4270
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rdi
callq 0x497d0
movq %rax, -0x40(%rbp)
movb $0x0, -0x41(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x5, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rcx
subq %rcx, %rax
sarq $0x3, %rax
shlq %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
cmpq $0x5, -0x60(%rbp)
jbe 0x41469
movq $0x5, -0x60(%rbp)
movq $0x1, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x60(%rbp), %rax
ja 0x415f6
movq -0x68(%rbp), %rax
shrq %rax
movq %rax, -0x78(%rbp)
movq -0x68(%rbp), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x414cc
movq -0x18(%rbp), %rax
movq -0x8(%rbp), %rcx
movq -0x78(%rbp), %rdx
shlq $0x3, %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jae 0x414b5
jmp 0x415e5
movq -0x18(%rbp), %rax
xorl %ecx, %ecx
subq -0x78(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
jmp 0x414f9
movq -0x18(%rbp), %rax
movq -0x78(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
cmpq -0x10(%rbp), %rax
jb 0x414e6
jmp 0x415e5
movq -0x18(%rbp), %rax
movq -0x78(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
cmpq $0x1, -0x68(%rbp)
jbe 0x4150d
movq -0x28(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x43c10
movq -0x70(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x20(%rbp), %rdi
callq 0x4aa50
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq %rax, %rdx
callq 0x3ef10
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %r8
leaq -0x88(%rbp), %rdx
callq 0x41830
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x50(%rbp), %rax
jbe 0x415e3
movb $0x1, -0x41(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x90(%rbp), %rcx
movq 0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x88(%rbp), %rcx
movq 0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rcx
movq -0x80(%rbp), %rax
subq -0x88(%rbp), %rax
subq %rax, %rcx
movq 0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x90(%rbp), %rax
movq 0x10(%rbp), %rcx
imulq -0x38(%rbp), %rcx
cmpq %rcx, %rax
jb 0x415e1
jmp 0x415f6
jmp 0x415e3
jmp 0x415e5
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x41471
movb -0x41(%rbp), %al
andb $0x1, %al
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)