// File: /workspace/github_c_makefile_star2_O1/c-zhou[P]yahs/enzyme.c
// Function: find_re_from_seqs
re_cuts_t *find_re_from_seqs(const char *f, uint32_t ml, char **enz_cs, int enz_n)
{
    // now find all RE cutting sites
    int i, j, c, c0, c1;
    uint32_t len, n, p;
    int64_t n_re, genome_size;
    char *pch, *seq, *re;
    sdict_t *sdict;
    re_cuts_t *re_cuts;

    sdict = make_sdict_from_fa(f, ml);
    n = sdict->n;
    n_re = genome_size = 0;
    re_cuts = re_cuts_init(n);
    for (i = 0; i < n; ++i) {
        u32_v enz_cs_pos = {0, 0, 0};

        seq = sdict->s[i].seq;
        len = sdict->s[i].len;
        for (p = 0; p < len; ++p) {
            c = seq[p];
            if (!isalpha(c)) {
                fprintf(stderr, "[E::%s] non-alphabetic chacrater in FASTA file: %c\n", __func__, c);
                re_cuts_destroy(re_cuts);
                sd_destroy(sdict);
                return 0;
            }
            seq[p] = nucl_toupper[c];
        }
        
        for (j = 0; j < enz_n; ++j) {
            re = enz_cs[j];
            pch = strstr(seq, re);
            while (pch != NULL) {
                kv_push(uint32_t, enz_cs_pos, pch - seq);
                pch = strstr(pch + 1, re);
            }
        }

        // reverse complement sequence
        for (p = 0; p < len>>1; ++p) {
            c0 = comp_table[(int) seq[p]];
            c1 = comp_table[(int) seq[len - 1 - p]];
            seq[p] = c1;
            seq[len - 1 - p] = c0;
        }
        if (len & 1) // complement the remaining base
            seq[len>>1] = comp_table[(int) seq[len>>1]];
        
        for (j = 0; j < enz_n; ++j) {
            re = enz_cs[j];
            pch = strstr(seq, re);
            while (pch != NULL) {
                kv_push(uint32_t, enz_cs_pos, len - 1 - (pch - seq));
                pch = strstr(pch + 1, re);
            }
        }

        qsort(enz_cs_pos.a, enz_cs_pos.n, sizeof(uint32_t), u32_cmp);
        re_cuts->re[i].sites = enz_cs_pos.a;
        re_cuts->re[i].n = enz_cs_pos.n;
        re_cuts->re[i].l = len;
        
        n_re += enz_cs_pos.n;
        genome_size += len;
    }

    re_cuts->density = (double) n_re / genome_size;

    fprintf(stderr, "[I::%s] number restriction enzyme cutting sites found in sequences: %ld\n", __func__, n_re);
    fprintf(stderr, "[I::%s] restriction enzyme cutting sites density: %.6f\n", __func__, re_cuts->density);
#ifdef DEBUG
    fprintf(stderr, "[DEBUG::%s] restriction enzyme cutting sites for individual sequences (n = %d)\n", __func__, n);
    for (i = 0; i < n; ++i)
        fprintf(stderr, "[DEBUG::%s] %s %u %u %.6f\n", __func__, sdict->s[i].name, sdict->s[i].len, re_cuts->re[i].n, (double) re_cuts->re[i].n / sdict->s[i].len);
#endif

    sd_destroy(sdict);

    return re_cuts;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %ecx, %ebx
movq %rdx, 0x48(%rsp)
callq 0x11ad4
movq %rax, 0x20(%rsp)
movl (%rax), %r15d
movl %r15d, %edi
callq 0x1cbc8
movq %rax, %r14
movq %r15, 0x58(%rsp)
testq %r15, %r15
je 0x1cfe0
movl %ebx, %eax
movq %rax, 0x40(%rsp)
leaq 0xe6bc(%rip), %r12      # 0x2b380
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %edi, %edi
movq %r14, 0x50(%rsp)
movl %ebx, 0x1c(%rsp)
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
movq 0x8(%rax,%rcx,8), %r15
movl 0x10(%rax,%rcx,8), %eax
movq %rax, 0x28(%rsp)
testq %rax, %rax
setne %al
movq %r15, 0x10(%rsp)
movq %rdi, 0x38(%rsp)
je 0x1cda3
movl %eax, 0xc(%rsp)
callq 0x2570
movb (%r15), %cl
movq (%rax), %rsi
movsbq %cl, %rdx
testb $0x4, 0x1(%rsi,%rdx,2)
je 0x1cd63
movq 0x28(%rsp), %r9
leal -0x1(%r9), %esi
xorl %edi, %edi
movq 0x10(%rsp), %r10
movb (%r12,%rdx), %cl
movb %cl, (%r10,%rdi)
cmpl %edi, %esi
je 0x1cda1
movb 0x1(%r10,%rdi), %cl
movq (%rax), %r8
movsbq %cl, %rdx
incq %rdi
testb $0x4, 0x1(%r8,%rdx,2)
jne 0x1cd36
cmpl %r9d, %edi
setb %al
movl %eax, 0xc(%rsp)
movsbl %cl, %ecx
movq 0xe28b(%rip), %rax      # 0x2aff8
movq (%rax), %rdi
leaq 0x6381(%rip), %rsi      # 0x230f8
leaq 0x63ae(%rip), %rdx      # 0x2312c
xorl %eax, %eax
callq 0x22c0
movq %r14, %rdi
callq 0x1cc1c
movq 0x20(%rsp), %rdi
callq 0x1148c
movq 0x38(%rsp), %rdi
jmp 0x1cfaf
xorl %eax, %eax
movq %rbp, 0x60(%rsp)
movq %r13, 0x68(%rsp)
testl %ebx, %ebx
movl %eax, 0xc(%rsp)
jle 0x1ce4d
xorl %ecx, %ecx
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x48(%rsp), %rax
movq %rcx, 0x30(%rsp)
movq (%rax,%rcx,8), %r14
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x2540
testq %rax, %rax
je 0x1ce38
movq %rax, %r13
cmpq %rbp, %r12
jne 0x1ce14
leaq (,%rbp,2), %r15
testq %rbp, %rbp
movl $0x2, %eax
cmoveq %rax, %r15
leaq (,%r15,4), %rsi
movq %rbx, %rdi
callq 0x2390
movq %rax, %rbx
movq %r15, %rbp
movl %r13d, %eax
subl 0x10(%rsp), %eax
movl %eax, (%rbx,%r12,4)
incq %r12
incq %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x2540
movq %rax, %r13
testq %rax, %rax
jne 0x1cde5
movq 0x30(%rsp), %rcx
incq %rcx
cmpq 0x40(%rsp), %rcx
jne 0x1cdc2
jmp 0x1ce54
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %ebx, %ebx
movq 0x28(%rsp), %r10
movl %r10d, %eax
shrl %eax
cmpl $0x2, %r10d
leaq 0xe497(%rip), %r9       # 0x2b300
movq 0x10(%rsp), %r14
jb 0x1ce9c
leal -0x1(%r10), %ecx
xorl %edx, %edx
movsbq (%r14,%rdx), %rsi
movb (%r9,%rsi), %sil
movl %ecx, %edi
movsbq (%r14,%rdi), %r8
movb (%r9,%r8), %r8b
movb %r8b, (%r14,%rdx)
movb %sil, (%r14,%rdi)
incq %rdx
decl %ecx
cmpq %rdx, %rax
jne 0x1ce76
testb $0x1, %r10b
je 0x1ceaf
movsbq (%r14,%rax), %rcx
movb (%r9,%rcx), %cl
movb %cl, (%r14,%rax)
cmpl $0x0, 0x1c(%rsp)
jle 0x1cf53
leal -0x1(%r10), %eax
movl %eax, 0x30(%rsp)
xorl %ecx, %ecx
movq 0x48(%rsp), %rax
movq %rcx, 0x70(%rsp)
movq (%rax,%rcx,8), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x2540
testq %rax, %rax
je 0x1cf40
movq %rax, %r13
cmpq %rbp, %r12
jne 0x1cf19
leaq (,%rbp,2), %r14
testq %rbp, %rbp
movl $0x2, %eax
cmoveq %rax, %r14
leaq (,%r14,4), %rsi
movq %rbx, %rdi
callq 0x2390
movq %rax, %rbx
movq %r14, %rbp
movq 0x10(%rsp), %r14
movl %r14d, %eax
subl %r13d, %eax
addl 0x30(%rsp), %eax
movl %eax, (%rbx,%r12,4)
incq %r12
incq %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x2540
movq %rax, %r13
testq %rax, %rax
jne 0x1cee5
movq 0x70(%rsp), %rcx
incq %rcx
cmpq 0x40(%rsp), %rcx
jne 0x1cec4
movl $0x4, %edx
movq %rbx, %rdi
movq %r12, %rsi
leaq -0x2f7(%rip), %rcx      # 0x1cc6e
callq 0x20e0
movq 0x50(%rsp), %r14
movq 0x10(%r14), %rax
movq 0x60(%rsp), %rbp
movq %rbp, %rcx
shlq $0x4, %rcx
movq %rbx, 0x8(%rax,%rcx)
movl %r12d, 0x4(%rax,%rcx)
movq 0x28(%rsp), %rdx
movl %edx, (%rax,%rcx)
movq 0x38(%rsp), %r15
addq %r12, %r15
movq %r15, %rdi
movq 0x68(%rsp), %r13
addq %rdx, %r13
movl 0x1c(%rsp), %ebx
leaq 0xe3d1(%rip), %r12      # 0x2b380
cmpb $0x0, 0xc(%rsp)
jne 0x1cfcb
incq %rbp
cmpq 0x58(%rsp), %rbp
jne 0x1ccd4
cvtsi2sd %r13, %xmm0
jmp 0x1cfe6
xorl %r14d, %r14d
movq %r14, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
xorpd %xmm0, %xmm0
movq %r14, %rsi
movq 0x20(%rsp), %rdx
callq 0x37ba
jmp 0x1cfce