// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq %rsi, %rbx
movq %rdi, %rbp
movl $0x1, %edi
movl $0x28, %esi
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
callq 0x2090
movq %rax, 0x18(%r14)
leal (%rax,%rax), %ecx
movq $-0x1, %rdx
shlq %cl, %rdx
notq %rdx
movq %rdx, 0x8(%r14)
movq $0x0, 0x20(%r14)
testq %rax, %rax
je 0x9ebea
xorl %ecx, %ecx
leaq 0x1eba6c(%rip), %rdx    # 0x28a630
movzbl (%rbx,%rcx), %esi
movzbl (%rdx,%rsi), %esi
cmpq $0x4, %rsi
jae 0x9ef35
movq 0x20(%r14), %rdi
leaq (%rsi,%rdi,4), %rsi
movq %rsi, 0x20(%r14)
incq %rcx
cmpq %rcx, %rax
jne 0x9ebc4
movq %rbx, 0x8(%rsp)
movq 0x50(%rbp), %rdi
movq %rdi, (%r14)
movl $0x1, %esi
callq 0x2120
movq %rax, 0x10(%r14)
movl $0x1, %edi
movl $0x38, %esi
callq 0x2120
movq %r12, %rcx
movq %rax, %r12
movq 0x10(%rsp), %rax
movq %rax, 0x20(%r12)
movq %r13, %rax
negq %rax
cmovsq %r13, %rax
movq %rcx, 0x30(%r12)
movq %rax, 0x28(%r12)
movq %rbp, (%r12)
movq %r14, 0x10(%r12)
movl $0x20, %esi
movq %r15, %rdi
callq 0x2120
movq %rax, 0x8(%r12)
testq %r15, %r15
je 0x9ec72
xorl %ebx, %ebx
movq %r15, %r13
movq 0x8(%r12), %rdi
addq %rbx, %rdi
callq 0x3372c
addq $0x20, %rbx
decq %r13
jne 0x9ec5c
movl $0x1, %esi
movl $0x30, %edx
xorl %edi, %edi
callq 0x1e3590
movq $0x0, 0x28(%rax)
movq %rax, 0x18(%r12)
movl 0x18(%r14), %esi
addl %esi, %esi
movq %rax, %rdi
callq 0x9ef73
movq 0x20(%r14), %rbp
cmpq $0x0, 0x18(%r14)
je 0x9ee34
xorl %ebx, %ebx
movq %r14, 0x20(%rsp)
movq %r12, 0x18(%rsp)
movq 0x18(%r12), %r13
movl 0x8(%r13), %eax
cmpl 0xc(%r13), %eax
jb 0x9ecec
movl (%r13), %esi
movl 0x4(%r13), %eax
addl %eax, %eax
cmpl %eax, %esi
jbe 0x9ecda
decl %esi
jmp 0x9ecdc
incl %esi
movq %r13, %rdi
callq 0x9ef73
testl %eax, %eax
js 0x9ee0d
movl (%r13), %esi
leal -0x1(%rsi), %r9d
movq %rbp, %rdx
shrq $0x21, %rdx
movl %ebp, %eax
shll $0xb, %eax
xorl %eax, %edx
xorl %ebp, %edx
andl %r9d, %edx
movq 0x10(%r13), %rax
movl %edx, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %edi
leal (%rdx,%rdx), %ecx
shrl %cl, %edi
testb $0x2, %dil
jne 0x9eda7
movl $0x1, %r10d
movl %edx, %r8d
movl %esi, %edi
movl %r8d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %r12d
leal (%r8,%r8), %ecx
andl $0x1e, %ecx
movl %r12d, %r11d
shrl %cl, %r11d
testb $0x2, %r11b
jne 0x9ed77
testb $0x1, %r11b
jne 0x9ed5d
movq 0x18(%r13), %r11
movl %r8d, %r14d
cmpq %rbp, (%r11,%r14,8)
je 0x9ed77
btl %ecx, %r12d
cmovbl %r8d, %edi
addl %r10d, %r8d
andl %r9d, %r8d
incl %r10d
movl %edi, %ecx
cmpl %edx, %r8d
jne 0x9ed2d
jmp 0x9ed79
movl %esi, %ecx
movl %ecx, %edx
cmpl %esi, %ecx
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %r12
jne 0x9eda7
movl %r8d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %edx
leal (%r8,%r8), %ecx
shrl %cl, %edx
cmpl %esi, %edi
cmovel %r8d, %edi
testb $0x2, %dl
cmovel %r8d, %edi
movl %edi, %edx
movl %edx, %esi
shrl $0x4, %esi
movl (%rax,%rsi,4), %edi
leal (%rdx,%rdx), %ecx
andl $0x1e, %ecx
movl %edi, %r8d
shrl %cl, %r8d
testb $0x2, %r8b
jne 0x9ede9
testb $0x1, %r8b
je 0x9ee0d
movq 0x18(%r13), %r8
movl $0x3, %r9d
shll %cl, %r9d
movl %edx, %ecx
movq %rbp, (%r8,%rcx,8)
notl %r9d
andl %r9d, %edi
movl %edi, (%rax,%rsi,4)
incl 0x4(%r13)
jmp 0x9ee0d
movq 0x18(%r13), %r8
movl $0x3, %r9d
shll %cl, %r9d
movl %edx, %ecx
movq %rbp, (%r8,%rcx,8)
notl %r9d
andl %r9d, %edi
movl %edi, (%rax,%rsi,4)
incl 0x4(%r13)
incl 0x8(%r13)
movq 0x18(%r14), %rax
leal (%rax,%rax), %ecx
addb $-0x2, %cl
movq %rbp, %rdx
shrq %cl, %rdx
andl $0x3, %edx
leaq (%rdx,%rbp,4), %rbp
andq 0x8(%r14), %rbp
incq %rbx
cmpq %rax, %rbx
jb 0x9ecb9
cmpq 0x20(%r14), %rbp
jne 0x9ef54
movq (%r14), %rcx
leaq 0x37a(%rip), %rsi       # 0x9f1c2
movl %r15d, %edi
movq %r12, %rdx
callq 0x1047b4
testq %r15, %r15
je 0x9ee70
xorl %ebx, %ebx
movq 0x8(%r12), %rdi
addq %rbx, %rdi
callq 0x33667
addq $0x20, %rbx
decq %r15
jne 0x9ee5a
movq 0x8(%r12), %rdi
callq 0x2220
movq 0x18(%r12), %r15
testq %r15, %r15
je 0x9eeb7
movq 0x18(%r15), %rsi
movq 0x28(%r15), %r13
movq %r13, %rdi
callq 0x1e3632
movq 0x10(%r15), %rsi
movq %r13, %rdi
callq 0x1e3632
movq 0x20(%r15), %rsi
movq %r13, %rdi
callq 0x1e3632
movq %r13, %rdi
movq %r15, %rsi
callq 0x1e3632
movq %r12, %rdi
callq 0x2220
movq (%r14), %r9
testq %r9, %r9
movq 0x8(%rsp), %r10
jne 0x9eed3
xorl %r8d, %r8d
xorl %ecx, %ecx
jmp 0x9eefa
movq 0x10(%r14), %rax
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %edx, %edx
movzbl (%rax,%rdx), %esi
movl %esi, %edi
andl $0x1, %edi
addq %rdi, %rcx
btl $0x1, %esi
adcq $0x0, %r8
incq %rdx
cmpq %rdx, %r9
jne 0x9eede
movq 0x1eb0df(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
leaq 0x18ae09(%rip), %rsi    # 0x229d14
leaq 0x18ae61(%rip), %rdx    # 0x229d73
xorl %eax, %eax
pushq 0x18(%r14)
pushq %r10
callq 0x22b0
addq $0x10, %rsp
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18ad54(%rip), %rdi    # 0x229c90
leaq 0x18ad5e(%rip), %rsi    # 0x229ca1
leaq 0x18ad64(%rip), %rcx    # 0x229cae
movl $0x134, %edx            # imm = 0x134
callq 0x2110
leaq 0x18adae(%rip), %rdi    # 0x229d09
leaq 0x18ad3f(%rip), %rsi    # 0x229ca1
leaq 0x18ad45(%rip), %rcx    # 0x229cae
movl $0x143, %edx            # imm = 0x143
callq 0x2110