// File: ctSkennerton[P]fxtract/util/ssearch.c
// Function: ssearch_scan
int
ssearch_scan(const SSEARCH*ssp, const MEMREF text,
                SSEARCH_CB cb, void *context)
{
    const SSEARCH ss     = *ssp;
    const char        *tp      = text.ptr + ss.suflen;
    const int        skipinit = 1 << (ss.suflen - 1);
    const int        mapmask  = (1 << 2*ss.symwid) - 1;
    int                skip;

    // "window" means the portion of the target (text) being
    // being matched. When match fails, the window is moved
    // forward by (skip) bytes.
    
    for (; tp <= text.ptr + text.len; ++ssearch_skip[skip], tp += skip) {
        const uint8_t *cp = (const uint8_t*)tp;

        // symsym: the mapv[] matrix index created from
        // the symbol values for two adjacent target chars.
        int        symsym = ss.symv[cp[-1]];
        if (!SYMVALID(symsym)) {
            skip = ss.suflen;
            continue;
        }

        int        sym = ss.symv[cp[-2]];
        if (!SYMVALID(sym)) {
            skip = ss.suflen - 1;
            continue;
        }

        // i: (backward) offset from end of window (0,1,2, ...)
        // skipmask: bits indicate where search should resume
        // (how much to shift forward). skipmask is initialized
        // to a bit that skips (suflen - 1) ahead,
        // since the initial value of symsym is the only place
        // that can skip (suflen) ahead.

        int        i = 0, matchmask, skipmask = skipinit;
        cp -= 2;

        // As long as matchmask & 1 == 1, the suffix matches
        // so far. matchmask acts like the and... of
        // mapv[] bitmasks for overlapping byte pairs.

        do {
            symsym = ((symsym << ss.symwid) | sym) & mapmask;
            skipmask |= matchmask = ss.mapv[symsym] >> i;

        } while (matchmask & 1 
                 && ++i != ss.suflen - 1
                 && SYMVALID(sym = ss.symv[*--cp]));

        skip = ffs(skipmask >> 1);
        if (!(matchmask & 1 && i == ss.suflen - 1))
            continue;

        int        *set = map_get(ss.suffixh, cp);
        if (!set)
            continue;

        //TODO: replace this with a faster (binary?) search
        for (; *set != NONSTR; ++set) {
            MEMREF s = ss.strv[*set];

            if (tp - s.len >= text.ptr
                && !memcmp(s.ptr, tp - s.len, s.len - ss.suflen) 
                && cb(*set, tp - s.len, context))
                return 1;
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2c0, %rsp            # imm = 0x2C0
movq %rsi, -0x18(%rbp)
movq %rdx, -0x10(%rbp)
movq %rdi, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x260(%rbp), %rdi
movl $0x230, %edx            # imm = 0x230
callq 0x3120
movq -0x18(%rbp), %rax
movslq -0x254(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x268(%rbp)
movl -0x254(%rbp), %ecx
subl $0x1, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x26c(%rbp)
movl -0x238(%rbp), %ecx
shll %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, -0x270(%rbp)
movq -0x268(%rbp), %rax
movq -0x18(%rbp), %rcx
addq -0x10(%rbp), %rcx
cmpq %rcx, %rax
ja 0x64c0
movq -0x268(%rbp), %rax
movq %rax, -0x280(%rbp)
movq -0x280(%rbp), %rax
movzbl -0x1(%rax), %eax
movswl -0x234(%rbp,%rax,2), %eax
movl %eax, -0x284(%rbp)
cmpl $0x0, -0x284(%rbp)
jge 0x6220
movl -0x254(%rbp), %eax
movl %eax, -0x274(%rbp)
jmp 0x6483
movq -0x280(%rbp), %rax
movzbl -0x2(%rax), %eax
movswl -0x234(%rbp,%rax,2), %eax
movl %eax, -0x288(%rbp)
cmpl $0x0, -0x288(%rbp)
jge 0x6256
movl -0x254(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x274(%rbp)
jmp 0x6483
movl $0x0, -0x28c(%rbp)
movl -0x26c(%rbp), %eax
movl %eax, -0x294(%rbp)
movq -0x280(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x280(%rbp)
movl -0x284(%rbp), %eax
movl -0x238(%rbp), %ecx
shll %cl, %eax
orl -0x288(%rbp), %eax
andl -0x270(%rbp), %eax
movl %eax, -0x284(%rbp)
movq -0x250(%rbp), %rax
movslq -0x284(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
movl -0x28c(%rbp), %ecx
sarl %cl, %eax
movl %eax, -0x290(%rbp)
orl -0x294(%rbp), %eax
movl %eax, -0x294(%rbp)
movl -0x290(%rbp), %ecx
andl $0x1, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x2b1(%rbp)
je 0x6337
movl -0x28c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x28c(%rbp)
movl -0x254(%rbp), %edx
subl $0x1, %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x2b1(%rbp)
je 0x6337
movq -0x280(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x280(%rbp)
movzbl -0x1(%rax), %eax
movswl -0x234(%rbp,%rax,2), %eax
movl %eax, -0x288(%rbp)
cmpl $0x0, %eax
setge %al
movb %al, -0x2b1(%rbp)
movb -0x2b1(%rbp), %al
testb $0x1, %al
jne 0x627e
movl -0x294(%rbp), %edi
sarl %edi
callq 0x3290
movl %eax, -0x274(%rbp)
movl -0x290(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x6379
movl -0x28c(%rbp), %eax
movl -0x254(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0x637e
jmp 0x6483
movq -0x248(%rbp), %rdi
movq -0x280(%rbp), %rsi
callq 0x6f30
movq %rax, -0x2a0(%rbp)
cmpq $0x0, -0x2a0(%rbp)
jne 0x63a7
jmp 0x6483
jmp 0x63a9
movq -0x2a0(%rbp), %rax
cmpl $-0x1, (%rax)
je 0x6481
movq -0x260(%rbp), %rax
movq -0x2a0(%rbp), %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x2b0(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x2a8(%rbp)
movq -0x268(%rbp), %rax
xorl %ecx, %ecx
subq -0x2a8(%rbp), %rcx
addq %rcx, %rax
cmpq -0x18(%rbp), %rax
jb 0x6468
movq -0x2b0(%rbp), %rdi
movq -0x268(%rbp), %rsi
xorl %eax, %eax
subq -0x2a8(%rbp), %rax
addq %rax, %rsi
movq -0x2a8(%rbp), %rdx
movslq -0x254(%rbp), %rax
subq %rax, %rdx
callq 0x3358
cmpl $0x0, %eax
jne 0x6468
movq -0x28(%rbp), %rax
movq -0x2a0(%rbp), %rcx
movl (%rcx), %edi
movq -0x268(%rbp), %rsi
xorl %ecx, %ecx
subq -0x2a8(%rbp), %rcx
addq %rcx, %rsi
movq -0x30(%rbp), %rdx
callq *%rax
cmpl $0x0, %eax
je 0x6468
movl $0x1, -0x4(%rbp)
jmp 0x64c7
jmp 0x646a
movq -0x2a0(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x2a0(%rbp)
jmp 0x63a9
jmp 0x6483
movslq -0x274(%rbp), %rcx
leaq 0x630f(%rip), %rax      # 0xc7a0
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
leaq 0x6302(%rip), %rax      # 0xc7a0
movl %edx, (%rax,%rcx,4)
movl -0x274(%rbp), %ecx
movq -0x268(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x268(%rbp)
jmp 0x61c7
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2c0, %rsp            # imm = 0x2C0
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)