// File: /workspace/github_cpp_makefile_star2_O3/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 $0x18, %rsp
movq %r9, %r15
movq %r8, %r12
movq %rcx, %r13
movq %rdx, (%rsp)
movq %rsi, %rbx
movq %rdi, %rbp
movl $0x1, %edi
movl $0x28, %esi
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
callq 0x2090
leal (%rax,%rax), %ecx
movq $-0x1, %rdx
shlq %cl, %rdx
movq %rax, 0x18(%r14)
notq %rdx
movq %rdx, 0x8(%r14)
testq %rax, %rax
je 0x9f315
xorl %ecx, %ecx
leaq 0x1f633f(%rip), %rdx    # 0x295630
xorl %esi, %esi
movzbl (%rbx,%rsi), %edi
movzbl (%rdx,%rdi), %edi
cmpq $0x4, %rdi
jae 0x9f66a
leaq (%rdi,%rcx,4), %rcx
movq %rcx, 0x20(%r14)
incq %rsi
cmpq %rsi, %rax
jne 0x9f2f3
movq %rbx, 0x10(%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 (%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 0x9f398
movq %rax, %r13
movq %r15, %rbx
movq %r13, %rdi
callq 0x32e2d
addq $0x20, %r13
decq %rbx
jne 0x9f387
movl $0x1, %esi
movl $0x30, %edx
xorl %edi, %edi
callq 0x1edfb4
movq %rax, %r13
movq $0x0, 0x28(%rax)
movq %rax, 0x18(%r12)
movl 0x18(%r14), %esi
addl %esi, %esi
movq %rax, %rdi
callq 0x9f6a8
cmpq $0x0, 0x18(%r14)
je 0x9f574
movq %r12, (%rsp)
movq 0x20(%r14), %rbp
xorl %ebx, %ebx
movq %r14, 0x8(%rsp)
movl 0x8(%r13), %eax
cmpl 0xc(%r13), %eax
jb 0x9f40f
movl (%r13), %esi
movl 0x4(%r13), %eax
addl %eax, %eax
cmpl %eax, %esi
jbe 0x9f3fd
decl %esi
jmp 0x9f3ff
incl %esi
movq %r13, %rdi
callq 0x9f6a8
testl %eax, %eax
js 0x9f53f
movl (%r13), %esi
leal -0x1(%rsi), %r8d
movq %rbp, %rdi
shrq $0x21, %rdi
movl %ebp, %eax
shll $0xb, %eax
xorl %eax, %edi
xorl %ebp, %edi
andl %r8d, %edi
movq 0x10(%r13), %rax
movl %edi, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %edx
leal (%rdi,%rdi), %ecx
shrl %cl, %edx
testb $0x2, %dl
je 0x9f447
movl %edi, %edx
jmp 0x9f4cd
movl $0x1, %r10d
movl %edi, %r9d
movl %esi, %edx
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 0x9f4a8
testb $0x1, %r11b
jne 0x9f482
movq 0x18(%r13), %r11
movl %r9d, %r14d
cmpq %rbp, (%r11,%r14,8)
je 0x9f4a8
btl %ecx, %r12d
cmovbl %r9d, %edx
addl %r10d, %r9d
andl %r8d, %r9d
incl %r10d
cmpl %edi, %r9d
jne 0x9f452
movb $0x1, %cl
movl %esi, %r8d
cmpl %esi, %edx
movq 0x8(%rsp), %r14
je 0x9f4bd
jmp 0x9f4cd
shrl %cl, %r12d
testb $0x2, %r12b
sete %cl
movl %edx, %r8d
movl %r9d, %edi
movq 0x8(%rsp), %r14
cmpl %esi, %r8d
cmovel %edi, %r8d
testb %cl, %cl
cmovnel %edi, %r8d
movl %r8d, %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 0x9f50f
testb $0x1, %r8b
je 0x9f53f
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 0x9f53f
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)
movq 0x4(%r13), %xmm0
psubd 0x191fe7(%rip), %xmm0   # 0x231520
movq %xmm0, 0x4(%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 0x9f3e1
cmpq 0x20(%r14), %rbp
movq (%rsp), %r12
jne 0x9f689
movq (%r14), %rcx
leaq 0x34e(%rip), %rsi       # 0x9f8cc
movl %r15d, %edi
movq %r12, %rdx
callq 0x108f2c
testq %r15, %r15
je 0x9f5a6
xorl %ebx, %ebx
movq 0x8(%r12), %rdi
addq %rbx, %rdi
callq 0x32d68
addq $0x20, %rbx
decq %r15
jne 0x9f590
movq 0x8(%r12), %rdi
callq 0x2220
movq 0x18(%r12), %r15
testq %r15, %r15
movq 0x10(%rsp), %rbx
je 0x9f5f2
movq 0x18(%r15), %rsi
movq 0x28(%r15), %r13
movq %r13, %rdi
callq 0x1ee04f
movq 0x10(%r15), %rsi
movq %r13, %rdi
callq 0x1ee04f
movq 0x20(%r15), %rsi
movq %r13, %rdi
callq 0x1ee04f
movq %r13, %rdi
movq %r15, %rsi
callq 0x1ee04f
movq %r12, %rdi
callq 0x2220
movq (%r14), %r9
testq %r9, %r9
jne 0x9f609
xorl %r8d, %r8d
xorl %ecx, %ecx
jmp 0x9f630
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 0x9f614
movq 0x1f59a9(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
leaq 0x1968c3(%rip), %rsi    # 0x235f04
leaq 0x19691b(%rip), %rdx    # 0x235f63
xorl %eax, %eax
pushq 0x18(%r14)
pushq %rbx
callq 0x22b0
addq $0x10, %rsp
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x19680f(%rip), %rdi    # 0x235e80
leaq 0x196819(%rip), %rsi    # 0x235e91
leaq 0x19681f(%rip), %rcx    # 0x235e9e
movl $0x134, %edx            # imm = 0x134
callq 0x2110
leaq 0x196869(%rip), %rdi    # 0x235ef9
leaq 0x1967fa(%rip), %rsi    # 0x235e91
leaq 0x196800(%rip), %rcx    # 0x235e9e
movl $0x143, %edx            # imm = 0x143
callq 0x2110