// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: get_enzyme_occ(char*, long long, char*, long long)
long long get_enzyme_occ(char* t, long long tlen, char* p, long long plen)
{
    long long i, c, s = 0, j, occ = 0;
    int badchar[5]; badchar[0] = badchar[1] = badchar[2] = badchar[3] = badchar[4] = -1;
    for (i = 0; i < plen; i++)
    {
        c = seq_nt4_table[(uint8_t)p[i]];
        badchar[c] = i; 
        if(c == 4) badchar[0] = badchar[1] = badchar[2] = badchar[3] = i; 
    } 
    badchar[4] = -1;

    while(s <= (tlen - plen)) 
    { 
        j = plen-1; 

        while(j >= 0)
        {
            if(seq_nt4_table[(uint8_t)t[s+j]] >= 4) break;
            if((p[j] != t[s+j]) && seq_nt4_table[(uint8_t)p[j]] < 4) break;
            j--; 
        } 
        
            
        if (j < 0) 
        { 
            occ++;
            ///s += (s+m < n)? m-badchar[txt[s+m]] : 1; 
            s++;
        } 
        else
        {
            /*******************************for debug************************************/
            // long long f, end = s + MAX(1, j - badchar[seq_nt4_table[(uint8_t)t[s+j]]]);
            // for (f = s+1; f < end; f++)
            // {
            //     if(check_exact_match(t+f, plen, p, plen))
            //     {
            //         fprintf(stderr, "s: %lld, end: %lld, s+j: %lld, t[s+j]: %c, badchar: %d, j: %lld\n", 
            //                     s, end, s+j, t[s+j], badchar[seq_nt4_table[(uint8_t)t[s+j]]], j);
            //     }
            // }            
            /*******************************for debug************************************/
            
            s += MAX(1, j - badchar[seq_nt4_table[(uint8_t)t[s+j]]]); 
        }
    } 

    return occ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, -0x20(%rsp)
movl $0xffffffff, -0x10(%rsp) # imm = 0xFFFFFFFF
testq %rcx, %rcx
jle 0x13bac8
xorl %eax, %eax
leaq 0xf1f75(%rip), %r8      # 0x22da10
movzbl (%rdx,%rax), %r9d
movzbl (%r8,%r9), %r9d
movl %eax, -0x20(%rsp,%r9,4)
cmpl $0x4, %r9d
jne 0x13bac0
movl %eax, -0x14(%rsp)
movl %eax, -0x18(%rsp)
movl %eax, -0x1c(%rsp)
movl %eax, -0x20(%rsp)
incq %rax
cmpq %rax, %rcx
jne 0x13ba9b
movl $0xffffffff, -0x10(%rsp) # imm = 0xFFFFFFFF
subq %rcx, %rsi
js 0x13bb77
leaq -0x1(%rcx), %r8
xorl %eax, %eax
leaq 0xf1f2a(%rip), %r9      # 0x22da10
movl $0x1, %r10d
xorl %r11d, %r11d
leaq (%rdi,%r11), %rbx
movb $0x1, %bpl
testq %rcx, %rcx
jle 0x13bb0c
movzbl (%rbx,%r8), %r15d
cmpb $0x3, (%r9,%r15)
movq %r8, %r14
jbe 0x13bb11
xorl %ebp, %ebp
movq %r8, %r14
jmp 0x13bb42
movzbl (%rdx,%r14), %r12d
cmpb %r15b, %r12b
je 0x13bb22
cmpb $0x4, (%r9,%r12)
jb 0x13bb37
testq %r14, %r14
jle 0x13bb3b
movzbl -0x1(%rbx,%r14), %r15d
decq %r14
cmpb $0x3, (%r9,%r15)
jbe 0x13bb11
xorl %ebp, %ebp
jmp 0x13bb42
movq $-0x1, %r14
testb %bpl, %bpl
je 0x13bb4f
incq %rax
incq %r11
jmp 0x13bb6c
movzbl (%r14,%rbx), %ebx
movzbl (%r9,%rbx), %ebx
movslq -0x20(%rsp,%rbx,4), %rbx
subq %rbx, %r14
cmpq $0x2, %r14
cmovlq %r10, %r14
addq %r14, %r11
cmpq %rsi, %r11
jle 0x13baef
jmp 0x13bb79
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq