// File: /workspace/github_cpp_makefile_star2_O3/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 $0x298, %rsp            # imm = 0x298
movq %r8, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %rsi
leaq 0x68(%rsp), %rbx
movl $0x230, %edx            # imm = 0x230
movq %rbx, %rdi
callq 0x3110
movl 0xc(%rbx), %eax
movl %eax, 0xc(%rsp)
cltq
leal -0x1(%rax), %ecx
movl $0x1, %edx
movl %ecx, 0x8(%rsp)
shll %cl, %edx
movl %edx, 0x4(%rsp)
movl 0x28(%rbx), %edx
leal (%rdx,%rdx), %ecx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
shll %cl, %esi
cmpq %rbp, %rax
jg 0x4fb8
movq %rdx, 0x50(%rsp)
notl %esi
movl %esi, 0x24(%rsp)
addq %r14, %rbp
movq %r14, 0x18(%rsp)
leaq (%r14,%rax), %r12
movq 0x68(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq 0x78(%rsp), %rcx
movq %rcx, 0x60(%rsp)
movq %rax, 0x58(%rsp)
leal -0x2(%rax), %r13d
movq 0x80(%rsp), %rax
movq %rax, 0x30(%rsp)
movq $-0x2, %rax
subq %r13, %rax
movq %rax, 0x48(%rsp)
leaq 0x5976(%rip), %rsi      # 0xa780
movl 0xc(%rsp), %r14d
movl 0x8(%rsp), %r11d
movl 0x4(%rsp), %ebx
movq %rbp, 0x28(%rsp)
movzbl -0x1(%r12), %eax
movswl 0x94(%rsp,%rax,2), %edx
movl %r14d, %r15d
testl %edx, %edx
js 0x4fa6
movzbl -0x2(%r12), %eax
movzwl 0x94(%rsp,%rax,2), %ecx
movl %r11d, %r15d
testw %cx, %cx
js 0x4fa6
movq 0x48(%rsp), %rax
leaq (%r12,%rax), %rsi
leaq -0x3(%r12), %rdi
xorl %eax, %eax
movl %ebx, %r8d
movq 0x50(%rsp), %r15
movl %r8d, %r9d
movzwl %cx, %r8d
movl %r15d, %ecx
shll %cl, %edx
orl %r8d, %edx
andl 0x24(%rsp), %edx
movq 0x60(%rsp), %rcx
movzwl (%rcx,%rdx,2), %r10d
movl %r10d, %r8d
movl %eax, %ecx
shrl %cl, %r8d
orl %r9d, %r8d
btl %eax, %r10d
jae 0x4eba
cmpl %eax, %r13d
je 0x4ed8
incl %eax
movzbl (%rdi), %ecx
movzwl 0x94(%rsp,%rcx,2), %ecx
decq %rdi
testw %cx, %cx
jns 0x4e68
incq %rdi
decl %eax
movq %rdi, %rsi
jmp 0x4edb
movl %r8d, %eax
sarl %eax
bsfl %eax, %r15d
incl %r15d
cmpl $0x2, %r8d
movl $0x0, %eax
cmovbl %eax, %r15d
jmp 0x4f9f
movl %r13d, %eax
movl %r8d, %ecx
sarl %ecx
bsfl %ecx, %r15d
incl %r15d
cmpl $0x2, %r8d
movl $0x0, %ecx
cmovbl %ecx, %r15d
cmpl %r13d, %eax
jne 0x4f9f
movq 0x30(%rsp), %rdi
callq 0x554f
testq %rax, %rax
je 0x4f96
movq %rax, %rbx
movl (%rax), %r14d
cmpl $-0x1, %r14d
je 0x4f91
addq $0x4, %rbx
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rsi
movslq %r14d, %rax
shlq $0x4, %rax
movq 0x8(%rsi,%rax), %rdx
movq %r12, %rbp
subq %rdx, %rbp
cmpq %rcx, %rbp
jb 0x4f7f
addq %rsi, %rax
movq (%rax), %rdi
subq 0x58(%rsp), %rdx
movq %rbp, %rsi
callq 0x3180
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rcx
testl %eax, %eax
jne 0x4f7f
movl %r14d, %edi
movq %rbp, %rsi
movq 0x40(%rsp), %rdx
callq *0x38(%rsp)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rcx
testl %eax, %eax
jne 0x4fcc
movl (%rbx), %r14d
addq $0x4, %rbx
cmpl $-0x1, %r14d
jne 0x4f2a
movq 0x28(%rsp), %rbp
movl 0xc(%rsp), %r14d
movl 0x8(%rsp), %r11d
movl 0x4(%rsp), %ebx
leaq 0x57da(%rip), %rsi      # 0xa780
movslq %r15d, %rax
incl (%rsi,%rax,4)
addq %rax, %r12
cmpq %rbp, %r12
jbe 0x4e1d
xorl %eax, %eax
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %eax
jmp 0x4fba
nop