// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movl %ecx, %ebp
movq %rdx, 0x38(%rsp)
callq 0x12893
movq %rax, 0x20(%rsp)
movl (%rax), %r14d
movl %r14d, %edi
callq 0x1d944
movq %rax, %r8
movq %r14, 0x40(%rsp)
testq %r14, %r14
jne 0x1dad8
xorl %r14d, %r14d
xorpd %xmm0, %xmm0
movq %r14, %rdi
movq %r8, %rsi
movq %r8, %rbx
callq 0x3744
jmp 0x1dda1
movl %ebp, %eax
movq %rax, 0x30(%rsp)
xorl %r15d, %r15d
leaq 0xe897(%rip), %r12      # 0x2c380
xorl %r13d, %r13d
xorl %r14d, %r14d
movq %r8, 0x28(%rsp)
movl %ebp, 0x4(%rsp)
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
leaq (%r15,%r15,2), %rcx
movq 0x8(%rax,%rcx,8), %rbx
movl 0x10(%rax,%rcx,8), %eax
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x1db4b
callq 0x2570
movq 0x28(%rsp), %r8
movq 0x10(%rsp), %rdi
xorl %edx, %edx
movsbq (%rbx,%rdx), %rcx
movq (%rax), %rsi
testb $0x4, 0x1(%rsi,%rcx,2)
je 0x1dd75
movb (%r12,%rcx), %cl
movb %cl, (%rbx,%rdx)
incq %rdx
cmpq %rdx, %rdi
jne 0x1db29
movq %rbx, 0x8(%rsp)
movq %r13, 0x48(%rsp)
movq %r15, 0x50(%rsp)
movq %r14, 0x58(%rsp)
testl %ebp, %ebp
jle 0x1dbfc
xorl %ecx, %ecx
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %r14d, %r14d
movq 0x38(%rsp), %rax
movq %rcx, 0x18(%rsp)
movq (%rax,%rcx,8), %rbx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x2540
testq %rax, %rax
je 0x1dbe7
movq %rax, %r13
cmpq %rbp, %r14
jne 0x1dbc3
leaq (,%rbp,2), %r15
testq %rbp, %rbp
movl $0x2, %eax
cmoveq %rax, %r15
leaq (,%r15,4), %rsi
movq %r12, %rdi
callq 0x2390
movq %rax, %r12
movq %r15, %rbp
movl %r13d, %eax
subl 0x8(%rsp), %eax
movl %eax, (%r12,%r14,4)
incq %r14
incq %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2540
movq %rax, %r13
testq %rax, %rax
jne 0x1db94
movq 0x18(%rsp), %rcx
incq %rcx
cmpq 0x30(%rsp), %rcx
jne 0x1db71
jmp 0x1dc04
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x10(%rsp), %r10
movl %r10d, %eax
shrl %eax
cmpl $0x2, %r10d
leaq 0xe6e7(%rip), %r9       # 0x2c300
movq 0x8(%rsp), %rbx
jb 0x1dc4c
leal -0x1(%r10), %ecx
xorl %edx, %edx
movsbq (%rbx,%rdx), %rsi
movb (%r9,%rsi), %sil
movl %ecx, %edi
movsbq (%rbx,%rdi), %r8
movb (%r9,%r8), %r8b
movb %r8b, (%rbx,%rdx)
movb %sil, (%rbx,%rdi)
incq %rdx
decl %ecx
cmpq %rdx, %rax
jne 0x1dc26
testb $0x1, %r10b
je 0x1dc5e
movsbq (%rbx,%rax), %rcx
movb (%r9,%rcx), %cl
movb %cl, (%rbx,%rax)
cmpl $0x0, 0x4(%rsp)
jle 0x1dd01
leal -0x1(%r10), %eax
movl %eax, 0x18(%rsp)
xorl %ecx, %ecx
movq 0x38(%rsp), %rax
movq %rcx, 0x60(%rsp)
movq (%rax,%rcx,8), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2540
testq %rax, %rax
je 0x1dcee
movq %rax, %r13
cmpq %rbp, %r14
jne 0x1dcc8
leaq (,%rbp,2), %rbx
testq %rbp, %rbp
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,4), %rsi
movq %r12, %rdi
callq 0x2390
movq %rax, %r12
movq %rbx, %rbp
movq 0x8(%rsp), %rbx
movl %ebx, %eax
subl %r13d, %eax
addl 0x18(%rsp), %eax
movl %eax, (%r12,%r14,4)
incq %r14
incq %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x2540
movq %rax, %r13
testq %rax, %rax
jne 0x1dc94
movq 0x60(%rsp), %rcx
incq %rcx
cmpq 0x30(%rsp), %rcx
jne 0x1dc73
movl $0x4, %edx
movq %r12, %rdi
movq %r14, %rsi
leaq -0x299(%rip), %rcx      # 0x1da7a
callq 0x20e0
movq 0x28(%rsp), %r8
movq 0x10(%r8), %rax
movq 0x50(%rsp), %r15
movq %r15, %rcx
shlq $0x4, %rcx
movq %r12, 0x8(%rax,%rcx)
movl %r14d, 0x4(%rax,%rcx)
movq 0x10(%rsp), %rdx
movl %edx, (%rax,%rcx)
movq 0x48(%rsp), %r13
addq %rdx, %r13
movq 0x58(%rsp), %rax
addq %r14, %rax
movq %rax, %r14
incq %r15
cmpq 0x40(%rsp), %r15
movl 0x4(%rsp), %ebp
leaq 0xe61b(%rip), %r12      # 0x2c380
jne 0x1daf8
cvtsi2sd %r13, %xmm0
jmp 0x1dac5
movq 0xe27c(%rip), %rax      # 0x2bff8
movq (%rax), %rdi
leaq 0x64b2(%rip), %rsi      # 0x24238
leaq 0x64df(%rip), %rdx      # 0x2426c
xorl %eax, %eax
movq %r8, %rbx
callq 0x22c0
movq %rbx, %rdi
callq 0x1da25
xorl %ebx, %ebx
movq 0x20(%rsp), %rdi
callq 0x12233
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq