// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x17050
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x50(%rbp)
movl -0x40(%rbp), %edi
callq 0x296b0
movq %rax, -0x80(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x29cb6
leaq -0x98(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x21f0
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x28(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x28(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0x2995a
movq -0x68(%rbp), %rax
movl -0x44(%rbp), %ecx
movsbl (%rax,%rcx), %eax
movl %eax, -0x30(%rbp)
callq 0x2570
movq (%rax), %rax
movslq -0x30(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x400, %eax            # imm = 0x400
cmpl $0x0, %eax
jne 0x29934
movq 0xf6fe(%rip), %rax      # 0x38ff8
movq (%rax), %rdi
movl -0x30(%rbp), %ecx
leaq 0x9221(%rip), %rsi      # 0x32b28
leaq 0x924e(%rip), %rdx      # 0x32b5c
movb $0x0, %al
callq 0x22c0
movq -0x80(%rbp), %rdi
callq 0x29740
movq -0x78(%rbp), %rdi
callq 0x16650
movq $0x0, -0x8(%rbp)
jmp 0x29d2b
movslq -0x30(%rbp), %rcx
leaq 0xfa51(%rip), %rax      # 0x39390
movb (%rax,%rcx), %dl
movq -0x68(%rbp), %rax
movl -0x44(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x298bf
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x29a58
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x2550
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x29a48
jmp 0x2999b
movq -0x98(%rbp), %rax
cmpq -0x90(%rbp), %rax
jne 0x29a02
cmpq $0x0, -0x90(%rbp)
je 0x299c8
movq -0x90(%rbp), %rax
shlq %rax
movq %rax, -0xa0(%rbp)
jmp 0x299d6
movl $0x2, %eax
movq %rax, -0xa0(%rbp)
jmp 0x299d6
movq -0xa0(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
movq -0x90(%rbp), %rsi
shlq $0x2, %rsi
callq 0x23a0
movq %rax, -0x88(%rbp)
movq -0x60(%rbp), %rax
movq -0x68(%rbp), %rcx
subq %rcx, %rax
movl %eax, %edx
movq -0x88(%rbp), %rax
movq -0x98(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x98(%rbp)
movl %edx, (%rax,%rcx,4)
movq -0x60(%rbp), %rdi
addq $0x1, %rdi
movq -0x70(%rbp), %rsi
callq 0x2550
movq %rax, -0x60(%rbp)
jmp 0x2998e
jmp 0x29a4a
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x29961
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl -0x3c(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jae 0x29adc
movq -0x68(%rbp), %rax
movl -0x44(%rbp), %ecx
movsbl (%rax,%rcx), %eax
movslq %eax, %rcx
leaq 0xf890(%rip), %rax      # 0x39310
movsbl (%rax,%rcx), %eax
movl %eax, -0x34(%rbp)
movq -0x68(%rbp), %rax
movl -0x3c(%rbp), %ecx
subl $0x1, %ecx
subl -0x44(%rbp), %ecx
movl %ecx, %ecx
movsbl (%rax,%rcx), %eax
movslq %eax, %rcx
leaq 0xf86c(%rip), %rax      # 0x39310
movsbl (%rax,%rcx), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movb %al, %dl
movq -0x68(%rbp), %rax
movl -0x44(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x34(%rbp), %eax
movb %al, %dl
movq -0x68(%rbp), %rax
movl -0x3c(%rbp), %ecx
subl $0x1, %ecx
subl -0x44(%rbp), %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x29a5f
movl -0x3c(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x29b11
movq -0x68(%rbp), %rax
movl -0x3c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movsbl (%rax,%rcx), %eax
movslq %eax, %rcx
leaq 0xf810(%rip), %rax      # 0x39310
movb (%rax,%rcx), %dl
movq -0x68(%rbp), %rax
movl -0x3c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x29c1a
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x2550
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x29c0a
jmp 0x29b52
movq -0x98(%rbp), %rax
cmpq -0x90(%rbp), %rax
jne 0x29bb9
cmpq $0x0, -0x90(%rbp)
je 0x29b7f
movq -0x90(%rbp), %rax
shlq %rax
movq %rax, -0xa8(%rbp)
jmp 0x29b8d
movl $0x2, %eax
movq %rax, -0xa8(%rbp)
jmp 0x29b8d
movq -0xa8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
movq -0x90(%rbp), %rsi
shlq $0x2, %rsi
callq 0x23a0
movq %rax, -0x88(%rbp)
movl -0x3c(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movq -0x60(%rbp), %rcx
movq -0x68(%rbp), %rdx
subq %rdx, %rcx
subq %rcx, %rax
movl %eax, %edx
movq -0x88(%rbp), %rax
movq -0x98(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x98(%rbp)
movl %edx, (%rax,%rcx,4)
movq -0x60(%rbp), %rdi
addq $0x1, %rdi
movq -0x70(%rbp), %rsi
callq 0x2550
movq %rax, -0x60(%rbp)
jmp 0x29b45
jmp 0x29c0c
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x29b18
movq -0x88(%rbp), %rdi
movq -0x98(%rbp), %rsi
movl $0x4, %edx
leaq -0x464(%rip), %rcx      # 0x297d0
callq 0x20e0
movq -0x88(%rbp), %rcx
movq -0x80(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x98(%rbp), %rax
movl %eax, %ecx
movq -0x80(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl -0x3c(%rbp), %ecx
movq -0x80(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq -0x98(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x3c(%rbp), %eax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x29865
cvtsi2sdq -0x50(%rbp), %xmm0
cvtsi2sdq -0x58(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0x80(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movq 0xf322(%rip), %rax      # 0x38ff8
movq (%rax), %rdi
movq -0x50(%rbp), %rcx
leaq 0x8e8a(%rip), %rsi      # 0x32b6e
leaq 0x8e71(%rip), %rdx      # 0x32b5c
movb $0x0, %al
callq 0x22c0
movq 0xf2ff(%rip), %rax      # 0x38ff8
movq (%rax), %rdi
movq -0x80(%rbp), %rax
movsd 0x8(%rax), %xmm0
leaq 0x8eab(%rip), %rsi      # 0x32bb7
leaq 0x8e49(%rip), %rdx      # 0x32b5c
movb $0x1, %al
callq 0x22c0
movq -0x78(%rbp), %rdi
callq 0x16650
movq -0x80(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax,%rax)