// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
movq %r8, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rsi
leaq 0x58(%rsp), %rbx
movl $0x230, %edx            # imm = 0x230
movq %rbx, %rdi
callq 0x3110
movl 0xc(%rbx), %eax
movl %eax, 0x10(%rsp)
cltq
leal -0x1(%rax), %r11d
pushq $0x1
popq %rdx
movl %r11d, %ecx
shll %cl, %edx
leaq (%r15,%rax), %rbp
movl 0x28(%rbx), %esi
leal (%rsi,%rsi), %ecx
pushq $-0x1
popq %rdi
shll %cl, %edi
notl %edi
movq %r15, 0x48(%rsp)
addq %r15, %r14
movl %edx, %r15d
movq 0x10(%rbx), %rcx
movq %rcx, 0x50(%rsp)
movq %rax, 0x40(%rsp)
addl $-0x2, %eax
movl %eax, 0x14(%rsp)
movq 0x18(%rbx), %rax
movq %rax, 0x28(%rsp)
movq (%rbx), %r13
movq %rsi, %rbx
movq %r14, 0x20(%rsp)
movl %r11d, 0x4(%rsp)
movl %edx, 0x8(%rsp)
movq %rsi, 0x18(%rsp)
movl %edi, 0xc(%rsp)
cmpq %r14, %rbp
ja 0x4d28
movzbl -0x1(%rbp), %eax
movswl 0x84(%rsp,%rax,2), %edi
movl 0x10(%rsp), %r12d
testl %edi, %edi
js 0x4d13
movzbl -0x2(%rbp), %eax
movzwl 0x84(%rsp,%rax,2), %ecx
movl %r11d, %r12d
testw %cx, %cx
js 0x4d13
leaq -0x2(%rbp), %rsi
leaq -0x3(%rbp), %r8
xorl %eax, %eax
movl %r15d, %edx
movl 0xc(%rsp), %r12d
movzwl %cx, %r9d
movl %ebx, %ecx
shll %cl, %edi
orl %r9d, %edi
andl %r12d, %edi
movq 0x50(%rsp), %rcx
movzwl (%rcx,%rdi,2), %r10d
movl %r10d, %r9d
movl %eax, %ecx
shrl %cl, %r9d
orl %r9d, %edx
btl %eax, %r10d
jae 0x4c6e
cmpl %eax, 0x14(%rsp)
je 0x4c65
incl %eax
decq %rsi
movzbl (%r8), %ecx
movzwl 0x84(%rsp,%rcx,2), %ecx
decq %r8
testw %cx, %cx
jns 0x4c15
incq %r8
movq %r8, %rsi
jmp 0x4c6e
incq %r8
movq %r8, %rsi
movl %r11d, %eax
movl %edx, %ecx
sarl %ecx
bsfl %ecx, %r12d
incl %r12d
cmpl $0x2, %edx
movl $0x0, %ecx
cmovbl %ecx, %r12d
testb $0x1, %r9b
je 0x4d13
cmpl %r11d, %eax
jne 0x4d13
movq 0x28(%rsp), %rdi
callq 0x51a4
testq %rax, %rax
je 0x4d09
movq %rax, %rbx
movslq (%rbx), %r14
cmpq $-0x1, %r14
je 0x4cf8
movq %r14, %rax
shlq $0x4, %rax
movq 0x8(%r13,%rax), %rdx
movq %rbp, %r15
subq %rdx, %r15
cmpq 0x48(%rsp), %r15
jb 0x4cf2
addq %r13, %rax
movq (%rax), %rdi
subq 0x40(%rsp), %rdx
movq %r15, %rsi
callq 0x3180
testl %eax, %eax
jne 0x4cf2
movl %r14d, %edi
movq %r15, %rsi
movq 0x38(%rsp), %rdx
callq *0x30(%rsp)
testl %eax, %eax
jne 0x4d2c
addq $0x4, %rbx
jmp 0x4ca6
movq 0x20(%rsp), %r14
movl 0x4(%rsp), %r11d
movl 0x8(%rsp), %r15d
jmp 0x4d0e
movl 0x4(%rsp), %r11d
movq 0x18(%rsp), %rbx
movslq %r12d, %rax
leaq 0x5a63(%rip), %rcx      # 0xa780
incl (%rcx,%rax,4)
addq %rax, %rbp
jmp 0x4bc9
xorl %eax, %eax
jmp 0x4d2f
pushq $0x1
popq %rax
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)