// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: gen_telo_end_t(All_reads*, char const*, long, long, long, unsigned long)
telo_end_t* gen_telo_end_t(All_reads *in, const char* motif, int64_t min_sc, int64_t penalty, int64_t max_drop, uint64_t n_thread)
{
    uint64_t j, k, c, x; int absent;
    telo_end_t* p = NULL; CALLOC(p, 1);
    p->tlen = strlen(motif); p->mask = ((1ULL)<<(p->tlen<<1))-1;

    for (j = 0, p->tm = 0; j < p->tlen; ++j) {
        c = seq_nt6_table[(uint8_t)motif[j]];
        assert(c >= 0 && c <= 3);
        p->tm = (p->tm<<2)|(c);
    }
    p->n = in->total_reads; CALLOC(p->hh, p->n);

    telo_end_pip_t *aux; CALLOC(aux, 1); 
    aux->min_sc = min_sc; aux->max_drop = max_drop;
    aux->penalty = penalty; if(aux->penalty < 0) aux->penalty = (aux->penalty)*-1;
    aux->Rinf = in; aux->u = p; CALLOC(aux->aux, n_thread); 
    for (k = 0; k < n_thread; k++) init_UC_Read(&(aux->aux[k]));
    aux->h = kh_init(64); kh_resize(64, aux->h, (p->tlen*2)); // hash table for all roations of the telomere motif
    for (k = 0, x = p->tm; k < p->tlen; k++) {
        kh_put(64, aux->h, x, &absent);
        x = (((x>>((p->tlen-1)<<1))&(3ULL))|(x<<2))&p->mask;
    }
    assert(x == p->tm);
    
    kt_for(n_thread, mark_telo_ends, aux, p->n);

    for (k = 0; k < n_thread; k++) destory_UC_Read(&(aux->aux[k]));
    free(aux->aux); kh_destroy(64, aux->h); free(aux);

    uint64_t t3, t5;
    for (k = t3 = t5 = 0; k < p->n; k++) {
        if(p->hh[k]&1) {
            // fprintf(stderr, "%.*s(+)\n", (int)Get_NAME_LENGTH((*in), k), Get_NAME((*in), k));
            t5++;
        }
        if(p->hh[k]&2) {
            // fprintf(stderr, "%.*s(-)\n", (int)Get_NAME_LENGTH((*in), k), Get_NAME((*in), k));
            t3++;
        }
    }
    

    fprintf(stderr, "[M::%s::] ==> # 5'-telomeres::%lu, # 3'-telomeres::%lu, # tot::%lu, motif::%s, motif_len::%lu\n", __func__, t5, t3, p->n, motif, p->tlen);
    // exit(1);

    return p;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r15
movq %r8, %r12
movq %rcx, %r13
movq %rdx, 0x8(%rsp)
movq %rsi, %rbx
movq %rdi, %rbp
pushq $0x1
popq %rdi
pushq $0x28
popq %rsi
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
callq 0x2090
movq %rax, 0x18(%r14)
leal (%rax,%rax), %ecx
pushq $-0x1
popq %rdx
shlq %cl, %rdx
notq %rdx
movq %rdx, 0x8(%r14)
xorl %ecx, %ecx
leaq 0x1eb4b6(%rip), %rdx    # 0x253630
xorl %esi, %esi
cmpq %rsi, %rax
je 0x681a0
movzbl (%rbx,%rsi), %edi
movzbl (%rdx,%rdi), %edi
cmpq $0x4, %rdi
jae 0x684f1
leaq (%rdi,%rcx,4), %rcx
movq %rcx, 0x20(%r14)
incq %rsi
jmp 0x6817c
movq %rbx, 0x18(%rsp)
movq 0x50(%rbp), %rdi
movq %rdi, (%r14)
movq %r14, 0x10(%rsp)
movq %rbp, 0x20(%rsp)
movq %r12, %rbp
pushq $0x1
popq %r12
movq %r12, %rsi
callq 0x2120
movq 0x10(%rsp), %rcx
movq %rax, 0x10(%rcx)
pushq $0x38
popq %rsi
movq %r12, %rdi
movq 0x10(%rsp), %r14
callq 0x2120
movq %rax, %rbx
movq 0x8(%rsp), %rax
movq %rax, 0x20(%rbx)
movq %r13, %rax
negq %rax
cmovsq %r13, %rax
movq %rbp, 0x30(%rbx)
movq %rax, 0x28(%rbx)
movq 0x20(%rsp), %rax
movq %rax, (%rbx)
movq %r14, 0x10(%rbx)
pushq $0x20
popq %rsi
movq %r15, %rdi
callq 0x2120
movq %rax, %r13
movq %rbx, 0x8(%rsp)
movq %rax, 0x8(%rbx)
movq %r15, %rbx
subq $0x1, %rbx
jb 0x68236
movq %r13, %rdi
callq 0x6bd1
addq $0x20, %r13
jmp 0x68222
xorl %ebp, %ebp
pushq $0x1
popq %rsi
pushq $0x30
popq %rdx
xorl %edi, %edi
callq 0x1a3848
movq %rax, %r13
andq $0x0, 0x28(%rax)
movq 0x8(%rsp), %rax
movq %r13, 0x18(%rax)
movl 0x18(%r14), %esi
addl %esi, %esi
movq %r13, %rdi
callq 0x6852f
movq 0x18(%r14), %rax
movq 0x20(%r14), %rbx
pushq $0x3
popq %r12
cmpq %rax, %rbp
jae 0x683f9
movl 0x8(%r13), %eax
cmpl 0xc(%r13), %eax
jb 0x682a7
movl (%r13), %esi
movl 0x4(%r13), %eax
addl %eax, %eax
cmpl %eax, %esi
jbe 0x68295
decl %esi
jmp 0x68297
incl %esi
movq %r13, %rdi
callq 0x6852f
testl %eax, %eax
js 0x683d6
movl (%r13), %edx
leal -0x1(%rdx), %r8d
movq %rbx, %rsi
shrq $0x21, %rsi
movl %ebx, %eax
shll $0xb, %eax
xorl %eax, %esi
xorl %ebx, %esi
andl %r8d, %esi
movq 0x10(%r13), %rax
movl %esi, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %edi
leal (%rsi,%rsi), %ecx
shrl %cl, %edi
testb $0x2, %dil
je 0x682e0
movl %esi, %edi
jmp 0x6836c
pushq $0x1
popq %r10
movl %esi, %r9d
movl %edx, %edi
movl %r9d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %r12d
leal (%r9,%r9), %ecx
andl $0x1e, %ecx
movl %r12d, %r11d
shrl %cl, %r11d
testb $0x2, %r11b
jne 0x68343
testb $0x1, %r11b
jne 0x68319
movq 0x18(%r13), %r11
movl %r9d, %r14d
cmpq %rbx, (%r11,%r14,8)
je 0x68343
btl %ecx, %r12d
cmovbl %r9d, %edi
addl %r10d, %r9d
andl %r8d, %r9d
incl %r10d
cmpl %esi, %r9d
jne 0x682e9
movb $0x1, %cl
movl %edx, %r8d
cmpl %edx, %edi
movq 0x10(%rsp), %r14
pushq $0x3
popq %r12
je 0x6835c
jmp 0x6836c
shrl %cl, %r12d
testb $0x2, %r12b
sete %cl
movl %edi, %r8d
movl %r9d, %esi
movq 0x10(%rsp), %r14
pushq $0x3
popq %r12
cmpl %edx, %r8d
cmovel %esi, %r8d
testb %cl, %cl
cmovnel %esi, %r8d
movl %r8d, %edi
movl %edi, %edx
shrl $0x4, %edx
movl (%rax,%rdx,4), %esi
leal (%rdi,%rdi), %ecx
andl $0x1e, %ecx
movl %esi, %r8d
shrl %cl, %r8d
movl %edi, %edi
testb $0x2, %r8b
jne 0x683ab
testb $0x1, %r8b
je 0x683d6
movl %r12d, %r8d
shll %cl, %r8d
movq 0x18(%r13), %rcx
movq %rbx, (%rcx,%rdi,8)
notl %r8d
andl %r8d, %esi
movl %esi, (%rax,%rdx,4)
incl 0x4(%r13)
jmp 0x683d6
movl %r12d, %r8d
shll %cl, %r8d
movq 0x18(%r13), %rcx
movq %rbx, (%rcx,%rdi,8)
notl %r8d
andl %r8d, %esi
movl %esi, (%rax,%rdx,4)
movq 0x4(%r13), %xmm0
psubd 0x17b0d0(%rip), %xmm0   # 0x1e34a0
movq %xmm0, 0x4(%r13)
movq 0x18(%r14), %rax
leal (%rax,%rax), %ecx
addb $-0x2, %cl
movq %rbx, %rdx
shrq %cl, %rdx
andl $0x3, %edx
leaq (%rdx,%rbx,4), %rbx
andq 0x8(%r14), %rbx
incq %rbp
jmp 0x68270
cmpq 0x20(%r14), %rbx
jne 0x68510
movq (%r14), %rcx
leaq 0x33e(%rip), %rsi       # 0x6874b
movl %r15d, %edi
movq 0x8(%rsp), %r12
movq %r12, %rdx
callq 0xc67c0
xorl %ebx, %ebx
movq 0x8(%r12), %rdi
subq $0x1, %r15
jb 0x68438
addq %rbx, %rdi
callq 0x6b07
addq $0x20, %rbx
jmp 0x6841f
callq 0x2220
movq 0x18(%r12), %r15
testq %r15, %r15
je 0x6847f
movq 0x18(%r15), %rsi
movq 0x28(%r15), %r13
movq %r13, %rdi
callq 0x1a38cf
movq 0x10(%r15), %rsi
movq %r13, %rdi
callq 0x1a38cf
movq 0x20(%r15), %rsi
movq %r13, %rdi
callq 0x1a38cf
movq %r13, %rdi
movq 0x8(%rsp), %r12
movq %r15, %rsi
callq 0x1a38cf
movq %r12, %rdi
callq 0x2220
movq (%r14), %r9
xorl %eax, %eax
xorl %r8d, %r8d
xorl %ecx, %ecx
movq 0x18(%rsp), %r10
cmpq %rax, %r9
je 0x684b8
movq 0x10(%r14), %rdx
movzbl (%rdx,%rax), %edx
movl %edx, %esi
andl $0x1, %esi
addq %rsi, %rcx
btl $0x1, %edx
adcq $0x0, %r8
incq %rax
jmp 0x68496
movq 0x1eab21(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x17f93b(%rip), %rsi    # 0x1e7e04
leaq 0x17f993(%rip), %rdx    # 0x1e7e63
xorl %eax, %eax
pushq 0x18(%r14)
pushq %r10
callq 0x22b0
popq %rcx
popq %rdx
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x17f888(%rip), %rdi    # 0x1e7d80
leaq 0x17f892(%rip), %rsi    # 0x1e7d91
leaq 0x17f898(%rip), %rcx    # 0x1e7d9e
movl $0x134, %edx            # imm = 0x134
callq 0x2110
leaq 0x17f8e2(%rip), %rdi    # 0x1e7df9
leaq 0x17f873(%rip), %rsi    # 0x1e7d91
leaq 0x17f879(%rip), %rcx    # 0x1e7d9e
movl $0x143, %edx            # imm = 0x143
callq 0x2110