// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movl %esi, %ebp
xorl %eax, %eax
testl %esi, %esi
movl $0x0, %esi
cmovgl %ebp, %esi
movq %rdi, %r14
movq %rsi, %rdx
shlq $0x4, %rdx
pushq $0x10
popq %r13
cmpq %rax, %rdx
je 0x47f9
movl 0x8(%r14,%rax), %ecx
cmpl %ecx, %r13d
cmovgel %ecx, %r13d
addq $0x10, %rax
jmp 0x47e2
cmpl $0x2, %r13d
jge 0x4806
xorl %ebx, %ebx
jmp 0x4ae7
movq %rsi, (%rsp)
movq %rdx, 0x30(%rsp)
pushq $0x1
popq %r15
movl $0x230, %esi            # imm = 0x230
movq %r15, %rdi
callq 0x30f0
movq %rax, %rbx
movq %r14, (%rax)
movl %ebp, 0x8(%rax)
movl %r13d, 0xc(%rax)
leal 0x1(%r13), %eax
movl %ebp, %ecx
subq %rax, %r15
movq %rcx, 0x28(%rsp)
testl %ecx, %ecx
jle 0x4871
decq %rcx
movq %rcx, %rdx
shlq $0x4, %rdx
addq %r14, %rdx
movq %r15, %rsi
movl %eax, %edi
cmpl $0x2, %edi
jl 0x483b
movq (%rdx), %r8
addq 0x8(%rdx), %r8
movzbl (%rsi,%r8), %r8d
movw $0x1, 0x2c(%rbx,%r8,2)
decl %edi
incq %rsi
jmp 0x4851
xorl %edx, %edx
pushq $0x16
popq %rax
cmpq $0x116, %rax            # imm = 0x116
je 0x48a3
movzwl (%rbx,%rax,2), %ecx
cmpw $0x1, %cx
movl %edx, %esi
sbbl $-0x1, %esi
cmpw $0x1, %cx
movl $0x0, %ecx
sbbl %ecx, %ecx
orl %edx, %ecx
movw %cx, (%rbx,%rax,2)
incq %rax
movl %esi, %edx
jmp 0x4876
movl %ebp, 0xc(%rsp)
xorl %eax, %eax
pushq $0x1
popq %rdi
xorl %ebp, %ebp
incl %ebp
movl %edi, %esi
movl %ebp, %ecx
shll %cl, %esi
addl $0x2, %eax
cmpl %edx, %esi
jl 0x48ae
leal -0x1(%r13), %ecx
pushq $-0x1
popq %r15
movl %r15d, %r12d
shll %cl, %r12d
movl %eax, %ecx
shll %cl, %r15d
movl %ebp, 0x28(%rbx)
notl %r12d
notl %r15d
movl %ebp, %ecx
shll %cl, %edx
movslq %edx, %rdi
pushq $0x2
popq %rsi
callq 0x30f0
movq %rax, 0x10(%rbx)
movzwl %r12w, %edx
pushq $0x1
popq %r12
xorl %esi, %esi
movq (%rsp), %r11
cmpq %r11, %rsi
je 0x4952
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 %r12d, %r9d
testl %edx, %r9d
je 0x494d
movzbl (%rdi), %ecx
movswl 0x2c(%rbx,%rcx,2), %r10d
movl %ebp, %ecx
shll %cl, %r8d
orl %r10d, %r8d
andl %r15d, %r8d
orw %r9w, (%rax,%r8,2)
movzwl %r9w, %r9d
addl %r9d, %r9d
decq %rdi
jmp 0x4923
incq %rsi
jmp 0x48fa
movl %r13d, %r15d
movq 0x566c(%rip), %rdi      # 0x9fc8
leaq 0x551(%rip), %rsi       # 0x4eb4
movq %r15, %rdx
callq 0x5008
movq %rax, (%rsp)
movq %rax, 0x18(%rbx)
movslq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
callq 0x30f0
movq %rax, 0x18(%rsp)
negq %r15
movq %r15, 0x20(%rsp)
xorl %r12d, %r12d
movq $0x0, 0x10(%rsp)
movq (%rsp), %r15
cmpq %r12, 0x30(%rsp)
je 0x49f7
movq (%r14,%r12), %rbp
addq 0x8(%r14,%r12), %rbp
addq 0x20(%rsp), %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x51a4
testq %rax, %rax
je 0x49c7
movq %rax, %r13
jmp 0x49ed
movq 0x10(%rsp), %rcx
movslq %ecx, %rax
incl %ecx
movq %rcx, 0x10(%rsp)
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax,4), %r13
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x533f
incl (%r13)
addq $0x10, %r12
jmp 0x499d
movl 0xc(%rsp), %eax
movq 0x10(%rsp), %r15
addl %r15d, %eax
movslq %eax, %rdi
pushq $0x4
popq %rsi
callq 0x30f0
movq %rax, %r12
movq %rax, 0x20(%rbx)
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
movq %r15, %r8
xorl %ecx, %ecx
movq (%rsp), %r15
movq 0x18(%rsp), %rdi
cmpq %rax, %r8
je 0x4a4d
movl (%rdi,%rax,4), %edx
leal (%rdx,%rcx), %esi
incl %esi
addl %ecx, %edx
movslq %edx, %rcx
orl $-0x1, (%r12,%rcx,4)
movl %edx, (%rdi,%rax,4)
incq %rax
movl %esi, %ecx
jmp 0x4a2c
movq 0x28(%rsp), %rcx
testl %ecx, %ecx
jle 0x4a97
decq %rcx
movl %ecx, %eax
shlq $0x4, %rax
movq (%r14,%rax), %rbp
addq 0x8(%r14,%rax), %rbp
addq 0x20(%rsp), %rbp
movq %r15, %rdi
movq %rbp, %rsi
movq %rcx, %r15
callq 0x51a4
movq %rax, %r13
movslq (%rax), %rax
leaq -0x1(%rax), %rcx
movl %ecx, (%r13)
movq %r15, %rcx
movq (%rsp), %r15
movl %ecx, -0x4(%r12,%rax,4)
jmp 0x4a52
leaq 0x40(%rsp), %r14
movq %rbp, (%r14)
leaq 0x38(%rsp), %rbp
movq %r13, (%rbp)
movq %r15, %rdi
callq 0x53f2
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x51d1
testl %eax, %eax
je 0x4add
movq 0x40(%rsp), %rsi
movq 0x38(%rsp), %rax
movslq (%rax), %rax
leaq (%r12,%rax,4), %rdx
movq %r15, %rdi
callq 0x533f
jmp 0x4ab0
movq 0x18(%rsp), %rdi
callq 0x3200
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq