// File: /workspace/github_cpp_makefile_star2_O1/ctSkennerton[P]fxtract/util/ssearch.c
// Function: ssearch_create
SSEARCH*
ssearch_create(const MEMREF strv[], int nstrs)
{
    int         i, j, cum, nsyms, nsuffixes, suflen;
    const char  *suffix;

    for (i = 0, suflen = 8 * sizeof(MASK); i < nstrs; ++i)
        if (suflen > (int)strv[i].len)
            suflen = strv[i].len;

    if (suflen < 2)
        return NULL;

    SSEARCH        *ssp = calloc(1, sizeof(*ssp));
    ssp->strv   = strv;
    ssp->nstrs  = nstrs;
    ssp->suflen = suflen;

    // Find all distinct bytevals that occur in suffixes
    for (i = nstrs; --i >= 0;)
        for (j = suflen + 1; --j > 0;)
            ssp->symv[(uint8_t)strv[i].ptr[strv[i].len - j]]=1;

    // Create a map from [0..255] to [NONSYM,0..nsyms-1]
    for (i = nsyms = 0; i < 256; ++i)
        ssp->symv[i] = ssp->symv[i] ? nsyms++ : NONSYM;

    // Calculate column count of mapv[][]: a power of 2
    for (ssp->symwid = 1; (1 << ssp->symwid) < nsyms;)
         ++ssp->symwid;

    // Set mapv[][] for each adj pair of bytes in suffixes
    const MASK bits        = (1 << (ssp->suflen - 1)) - 1;
    const int  mapmask  = (1 << 2*ssp->symwid) - 1;

    ssp->mapv   = calloc(nsyms << ssp->symwid, sizeof(MASK));
    for (i = 0 ; i < nstrs; ++i) {
        const char *cp = strv[i].ptr + strv[i].len;
        int            symsym = ssp->symv[(uint8_t)*--cp];
        MASK        mask;

        for (mask = 1; mask & bits; mask <<= 1) {
            int            sym = ssp->symv[(uint8_t)*--cp];
            symsym = ((symsym << ssp->symwid) | sym) & mapmask;
            ssp->mapv[symsym] |= mask;
        }
    }

        // Map each unique suffix to a set of strings

    // Count #(unique suffixes) and #(strings per suffix)
    ssp->suffixh = map_create( (map_diff*)memcmp, 
                                (map_hash*)fnv04, 
                                    VOIDP(ssp->suflen));
    int                *locp, *locv = calloc(nstrs, sizeof(int));
    for (i = nsuffixes = 0; i < nstrs; ++i) {
        suffix        = strv[i].ptr + strv[i].len - ssp->suflen;
        locp = map_get(ssp->suffixh, suffix);
        if (!locp) {
            locp = &locv[nsuffixes++];
            map_set(ssp->suffixh, suffix, locp);
        }
        ++*locp;
    }

    // Convert sublist lengths to sublist offsets in prefixv[].
    // Pre-mark list-ends.
    ssp->prefixv = calloc(nstrs + nsuffixes, sizeof(int));
    for (i = cum = 0; i < nsuffixes; ++i) {
        locv[i] = cum += locv[i] + 1;
        ssp->prefixv[--locv[i]] = NONSTR;
    }

    // Populate lists in prefixv[]
    for (i = nstrs; --i >= 0;) {
        suffix = strv[i].ptr + strv[i].len - ssp->suflen;
        locp = map_get(ssp->suffixh, suffix);
        ssp->prefixv[--*locp] = i;
    }

    // Convert map values from (list**) to (list*)
    map_start(ssp->suffixh); 
    while (map_next(ssp->suffixh, VOIDP(&suffix),
                        VOIDP(&locp))) {
        map_set(ssp->suffixh, suffix, &ssp->prefixv[*locp]);
    }

    free(locv);
    return    ssp;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %esi, %ebp
movq %rdi, %r14
movl %esi, %r8d
testl %esi, %esi
jle 0x499c
movq %r8, %rax
shlq $0x4, %rax
movl $0x10, %r15d
xorl %ecx, %ecx
movl 0x8(%r14,%rcx), %edx
cmpl %edx, %r15d
cmovgel %edx, %r15d
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x4985
jmp 0x49a2
movl $0x10, %r15d
cmpl $0x2, %r15d
jge 0x49af
xorl %ebx, %ebx
jmp 0x4cce
movl $0x1, %r12d
movl $0x1, %edi
movl $0x230, %esi            # imm = 0x230
movq %r8, 0x10(%rsp)
callq 0x30f0
movq 0x10(%rsp), %rdx
movq %rax, %rbx
movq %r14, (%rax)
movl %ebp, 0x8(%rax)
movl %r15d, 0xc(%rax)
testl %ebp, %ebp
jle 0x4a24
leal 0x1(%r15), %ecx
leal 0x2(%r15), %eax
subq %rcx, %r12
movq %rdx, %rcx
movq %rcx, %rdx
decq %rcx
movq %rcx, %rdi
shlq $0x4, %rdi
movq (%r14,%rdi), %rsi
addq %r12, %rsi
addq 0x8(%r14,%rdi), %rsi
movl %eax, %edi
movzbl (%rsi), %r8d
movw $0x1, 0x2c(%rbx,%r8,2)
decl %edi
incq %rsi
cmpl $0x2, %edi
jg 0x4a08
cmpq $0x1, %rdx
jg 0x49ed
movq %rbp, 0x18(%rsp)
xorl %edx, %edx
movl $0x16, %eax
movl %edx, %ecx
movzwl (%rbx,%rax,2), %esi
cmpw $0x1, %si
sbbl $-0x1, %edx
cmpw $0x1, %si
movl $0x0, %esi
sbbl %esi, %esi
orl %ecx, %esi
movw %si, (%rbx,%rax,2)
incq %rax
cmpq $0x116, %rax            # imm = 0x116
jne 0x4a30
xorl %eax, %eax
xorl %ebp, %ebp
incl %ebp
movl $0x1, %esi
movl %ebp, %ecx
shll %cl, %esi
addl $0x2, %eax
cmpl %edx, %esi
jl 0x4a5d
movl %ebp, 0x28(%rbx)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %eax, %ecx
shll %cl, %r12d
movl %ebp, %ecx
shll %cl, %edx
movslq %edx, %rdi
movl $0x2, %esi
callq 0x30f0
movq %rax, 0x10(%rbx)
cmpl $0x0, 0x18(%rsp)
movq 0x10(%rsp), %r11
jle 0x4b1b
leal -0x1(%r15), %ecx
shll %cl, %r13d
notl %r12d
notl %r13d
movzwl %r13w, %edx
xorl %esi, %esi
testb $0x1, %dl
je 0x4b13
movq %rsi, %rcx
shlq $0x4, %rcx
movq (%r14,%rcx), %rdi
movq 0x8(%r14,%rcx), %rcx
addq %rcx, %rdi
addq $-0x2, %rdi
movzbl 0x1(%rdi), %ecx
movswl 0x2c(%rbx,%rcx,2), %r8d
movl $0x1, %r9d
movzbl (%rdi), %ecx
movswl 0x2c(%rbx,%rcx,2), %r10d
movl %ebp, %ecx
shll %cl, %r8d
orl %r10d, %r8d
andl %r12d, %r8d
orw %r9w, (%rax,%r8,2)
addl %r9d, %r9d
movl %r9d, %ecx
andl $0x1fffe, %ecx          # imm = 0x1FFFE
decq %rdi
testl %edx, %r9d
movl %ecx, %r9d
jne 0x4ae3
incq %rsi
cmpq %r11, %rsi
jne 0x4ab7
movl %r15d, %ebp
movq 0x54a3(%rip), %rdi      # 0x9fc8
leaq 0x58c(%rip), %rsi       # 0x50b8
movq %rbp, %rdx
callq 0x5240
movq %rax, %r12
movq %rax, 0x18(%rbx)
movq 0x18(%rsp), %r15
movslq %r15d, %rdi
movl $0x4, %esi
callq 0x30f0
movq %rax, 0x20(%rsp)
testl %r15d, %r15d
movq %rbp, 0x28(%rsp)
jle 0x4bc8
negq %rbp
movq %rbp, 0x30(%rsp)
movq 0x10(%rsp), %rbp
shlq $0x4, %rbp
xorl %r15d, %r15d
xorl %r13d, %r13d
movq (%r14,%r15), %rsi
addq 0x8(%r14,%r15), %rsi
addq 0x30(%rsp), %rsi
movq %rsi, 0x8(%rsp)
movq %r12, %rdi
callq 0x5433
movq %rax, (%rsp)
testq %rax, %rax
jne 0x4bb7
movslq %r13d, %rax
incl %r13d
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,4), %rdx
movq %rdx, (%rsp)
movq 0x8(%rsp), %rsi
movq %r12, %rdi
callq 0x5607
movq (%rsp), %rax
incl (%rax)
addq $0x10, %r15
cmpq %r15, %rbp
jne 0x4b73
jmp 0x4bcb
xorl %r13d, %r13d
movq 0x18(%rsp), %rbp
leal (%r13,%rbp), %eax
movslq %eax, %rdi
movl $0x4, %esi
callq 0x30f0
movq %rax, %r15
movq %rax, 0x20(%rbx)
testl %r13d, %r13d
movq 0x20(%rsp), %r8
jle 0x4c1e
movl %r13d, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movl (%r8,%rcx,4), %esi
leal (%rsi,%rdx), %edi
incl %edi
addl %edx, %esi
movl %esi, (%r8,%rcx,4)
movslq %esi, %rdx
movl $0xffffffff, (%r15,%rdx,4) # imm = 0xFFFFFFFF
incq %rcx
movl %edi, %edx
cmpq %rcx, %rax
jne 0x4bfa
testl %ebp, %ebp
movq 0x10(%rsp), %r13
jle 0x4c76
negq 0x28(%rsp)
movq %r13, %rax
shlq $0x4, %rax
addq %rax, %r14
addq $-0x8, %r14
leaq -0x1(%r13), %rbp
movq -0x8(%r14), %rsi
addq (%r14), %rsi
addq 0x28(%rsp), %rsi
movq %rsi, 0x8(%rsp)
movq %r12, %rdi
callq 0x5433
movq %rax, (%rsp)
movslq (%rax), %rcx
leaq -0x1(%rcx), %rdx
movl %edx, (%rax)
movl %ebp, -0x4(%r15,%rcx,4)
addq $-0x10, %r14
cmpq $0x1, %r13
movq %rbp, %r13
jg 0x4c3a
movq %r12, %rdi
callq 0x56bc
leaq 0x8(%rsp), %rsi
movq %rsp, %rdx
movq %r12, %rdi
callq 0x5460
testl %eax, %eax
je 0x4cc4
leaq 0x8(%rsp), %r14
movq %rsp, %r13
movq 0x8(%rsp), %rsi
movq (%rsp), %rax
movslq (%rax), %rax
leaq (%r15,%rax,4), %rdx
movq %r12, %rdi
callq 0x5607
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x5460
testl %eax, %eax
jne 0x4c9a
movq 0x20(%rsp), %rdi
callq 0x3200
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq