// File: /workspace/github_cpp_makefile_star2_O1/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 $0x258, %rsp            # imm = 0x258
movq %r8, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, %rbp
movq %rsi, %r15
movq %rdi, %rsi
leaq 0x28(%rsp), %rbx
movl $0x230, %edx            # imm = 0x230
movq %rbx, %rdi
callq 0x3110
movslq 0xc(%rbx), %r14
leal -0x1(%r14), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0x14(%rsp)
movb 0x28(%rbx), %cl
addb %cl, %cl
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
shll %cl, %r12d
xorl %eax, %eax
cmpq %rbp, %r14
jg 0x4f3f
notl %r12d
addq %r15, %rbp
addq %r15, %r14
leaq 0x5a00(%rip), %rbx      # 0xa780
movq %rbp, (%rsp)
movq %r15, 0x8(%rsp)
movzbl -0x1(%r14), %eax
movswl 0x54(%rsp,%rax,2), %r8d
testl %r8d, %r8d
js 0x4e16
movzbl -0x2(%r14), %eax
movzwl 0x54(%rsp,%rax,2), %ecx
testw %cx, %cx
js 0x4e25
movl 0x50(%rsp), %edx
movq 0x38(%rsp), %r11
movl 0x34(%rsp), %r10d
leal -0x2(%r10), %ebx
movq %r14, %rsi
subq %rbx, %rsi
addq $-0x2, %rsi
decl %r10d
leaq -0x3(%r14), %r15
xorl %edi, %edi
xorl %eax, %eax
movl 0x14(%rsp), %r9d
movzwl %cx, %ebp
movl %edx, %ecx
shll %cl, %r8d
orl %ebp, %r8d
andl %r12d, %r8d
movzwl (%r11,%r8,2), %r13d
movl %r13d, %ebp
movl %eax, %ecx
shrl %cl, %ebp
orl %ebp, %r9d
btl %eax, %r13d
jae 0x4e0e
cmpl %eax, %ebx
je 0x4e37
incl %eax
movzbl (%r15), %ecx
movzwl 0x54(%rsp,%rcx,2), %ecx
decq %r15
testw %cx, %cx
jns 0x4dd4
incq %r15
movq %r15, %rsi
jmp 0x4e3a
movl $0x4, %edx
movl 0x34(%rsp), %r15d
jmp 0x4f0b
movl 0x34(%rsp), %r15d
decl %r15d
movl $0x4, %edx
jmp 0x4f0b
movl %r10d, %eax
movl %r9d, %ecx
sarl %ecx
bsfl %ecx, %r15d
incl %r15d
cmpl $0x2, %r9d
cmovbl %edi, %r15d
movl $0x4, %edx
testb $0x1, %bpl
leaq 0x5922(%rip), %rbx      # 0xa780
jne 0x4e69
movq (%rsp), %rbp
jmp 0x4f0b
movl 0x34(%rsp), %ecx
decl %ecx
cmpl %ecx, %eax
movq (%rsp), %rbp
jne 0x4f0b
movq 0x40(%rsp), %rdi
callq 0x5433
testq %rax, %rax
je 0x4e2d
movq %rax, %r13
movl (%rax), %ebp
xorl %edx, %edx
cmpl $-0x1, %ebp
movq 0x8(%rsp), %rsi
je 0x4e60
addq $0x4, %r13
movq 0x28(%rsp), %rax
movslq %ebp, %rcx
shlq $0x4, %rcx
movq 0x8(%rax,%rcx), %rdx
movq %r14, %rbx
subq %rdx, %rbx
cmpq %rsi, %rbx
jb 0x4ef1
addq %rcx, %rax
movq (%rax), %rdi
movslq 0x34(%rsp), %rax
subq %rax, %rdx
movq %rbx, %rsi
callq 0x3180
movq 0x8(%rsp), %rsi
testl %eax, %eax
jne 0x4ef1
movl %ebp, %edi
movq %rbx, %rsi
movq 0x20(%rsp), %rdx
callq *0x18(%rsp)
movq 0x8(%rsp), %rsi
testl %eax, %eax
jne 0x4f24
movl (%r13), %ebp
addq $0x4, %r13
cmpl $-0x1, %ebp
jne 0x4e9f
movq (%rsp), %rbp
leaq 0x5877(%rip), %rbx      # 0xa780
xorl %edx, %edx
testb $0x3, %dl
jne 0x4f3a
movslq %r15d, %rax
incl (%rbx,%rax,4)
addq %rax, %r14
cmpq %rbp, %r14
jbe 0x4d89
jmp 0x4f36
movl $0x1, %edx
movq (%rsp), %rbp
leaq 0x584c(%rip), %rbx      # 0xa780
jmp 0x4f0b
xorl %eax, %eax
jmp 0x4f3f
movl $0x1, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)