// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movl %ecx, %ebx
movq %rdx, 0x18(%rsp)
callq 0xe7a6
movq %rax, 0x10(%rsp)
movl (%rax), %edi
movq %rdi, 0x30(%rsp)
callq 0x18f98
xorl %edx, %edx
testl %ebx, %ebx
cmovlel %edx, %ebx
movq %rbx, 0x20(%rsp)
movq %rax, %r15
xorl %r12d, %r12d
xorl %eax, %eax
movq %r15, 0x28(%rsp)
cmpq 0x30(%rsp), %rdx
je 0x192bd
movq %rax, 0x40(%rsp)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq %rdx, 0x48(%rsp)
imulq $0x18, %rdx, %rcx
movq 0x8(%rax,%rcx), %rbp
movl 0x10(%rax,%rcx), %eax
movq %rax, 0x8(%rsp)
xorl %r14d, %r14d
cmpq %r14, 0x8(%rsp)
je 0x190da
movsbq (%rbp,%r14), %rbx
callq 0x2570
movq (%rax), %rax
testb $0x4, 0x1(%rax,%rbx,2)
je 0x19275
leaq 0xf2b3(%rip), %rax      # 0x28380
movb (%rax,%rbx), %al
movb %al, (%rbp,%r14)
incq %r14
jmp 0x190a6
movq %r12, 0x38(%rsp)
xorl %r15d, %r15d
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r14d, %r14d
movq %rbp, (%rsp)
cmpq 0x20(%rsp), %r15
je 0x19151
movq 0x18(%rsp), %rax
movq (%rax,%r15,8), %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2540
testq %rax, %rax
je 0x19148
movq %rax, %rbp
cmpq %r12, %rbx
jne 0x19137
leaq (%rbx,%rbx), %r12
testq %rbx, %rbx
pushq $0x2
popq %rax
cmoveq %rax, %r12
leaq (,%r12,4), %rsi
movq %r14, %rdi
callq 0x2390
movq %rax, %r14
movl %ebp, %eax
subl (%rsp), %eax
movl %eax, (%r14,%rbx,4)
incq %rbx
incq %rbp
jmp 0x190fe
incq %r15
movq (%rsp), %rbp
jmp 0x190ee
movq 0x8(%rsp), %rcx
movl %ecx, %eax
shrl %eax
decl %ecx
xorl %edx, %edx
leaq 0xf19b(%rip), %r9       # 0x28300
cmpq %rdx, %rax
je 0x19191
movsbq (%rbp,%rdx), %rsi
movb (%r9,%rsi), %sil
movl %ecx, %edi
movsbq (%rbp,%rdi), %r8
movb (%r9,%r8), %r8b
movb %r8b, (%rbp,%rdx)
movb %sil, (%rbp,%rdi)
incq %rdx
decl %ecx
jmp 0x19165
testb $0x1, 0x8(%rsp)
je 0x191a6
movsbq (%rbp,%rax), %rcx
movb (%r9,%rcx), %cl
movb %cl, (%rbp,%rax)
movq 0x8(%rsp), %rax
leal -0x1(%rax), %r15d
xorl %ecx, %ecx
cmpq 0x20(%rsp), %rcx
je 0x19222
movq 0x18(%rsp), %rax
movq %rcx, 0x50(%rsp)
movq (%rax,%rcx,8), %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2540
testq %rax, %rax
je 0x19214
movq %rax, %rbp
cmpq %r12, %rbx
jne 0x191ff
leaq (%rbx,%rbx), %r12
testq %rbx, %rbx
pushq $0x2
popq %rax
cmoveq %rax, %r12
leaq (,%r12,4), %rsi
movq %r14, %rdi
callq 0x2390
movq %rax, %r14
movq (%rsp), %rax
subl %ebp, %eax
addl %r15d, %eax
movl %eax, (%r14,%rbx,4)
incq %rbx
incq %rbp
jmp 0x191c6
movq 0x50(%rsp), %rcx
incq %rcx
movq (%rsp), %rbp
jmp 0x191b1
movq %r14, %rdi
movq %rbx, %rsi
pushq $0x4
popq %rdx
leaq -0x210(%rip), %rcx      # 0x19022
callq 0x20e0
movq 0x28(%rsp), %r15
movq 0x10(%r15), %rax
movq 0x48(%rsp), %rdx
movq %rdx, %rcx
shlq $0x4, %rcx
movq %r14, 0x8(%rax,%rcx)
movl %ebx, 0x4(%rax,%rcx)
movq 0x8(%rsp), %rsi
movl %esi, (%rax,%rcx)
movq 0x40(%rsp), %rax
addq %rsi, %rax
movq 0x38(%rsp), %r12
addq %rbx, %r12
incq %rdx
jmp 0x19073
movq 0xed7c(%rip), %rax      # 0x27ff8
movq (%rax), %rdi
leaq 0x6e62(%rip), %rsi      # 0x200e8
leaq 0x6e8f(%rip), %rdx      # 0x2011c
movl %ebx, %ecx
xorl %eax, %eax
callq 0x22c0
movq %r15, %rdi
callq 0x18fde
xorl %r15d, %r15d
movq 0x10(%rsp), %rdi
callq 0xe20a
movq %r15, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cvtsi2sd %r12, %xmm1
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movsd %xmm1, 0x8(%r15)
movq 0xed1b(%rip), %r14      # 0x27ff8
movq (%r14), %rdi
leaq 0x6e47(%rip), %rsi      # 0x2012e
leaq 0x6e2e(%rip), %rbx      # 0x2011c
movq %rbx, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x22c0
movq (%r14), %rdi
leaq 0x6e72(%rip), %rsi      # 0x20177
movq %rbx, %rdx
movsd (%rsp), %xmm0
movb $0x1, %al
callq 0x22c0
jmp 0x192a1