// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: gen_ext_tip(unsigned int, asg_t*, ug_opt_t*, unsigned char*, unsigned int, unsigned long*, unsigned long*)
int32_t gen_ext_tip(uint32_t v, asg_t *sg, ug_opt_t *uopt, uint8_t *ff, uint32_t min_ovlp, uint64_t *res, uint64_t *path_len)
{
    uint32_t k, qn, tn, cc, ccu; int32_t r; asg_arc_t p, pmax;
    ma_hit_t_alloc *s = &(uopt->sources[v>>1]); memset(&pmax, 0, sizeof(pmax)); pmax.ol = 0;
    for (k = 0; k < s->length; k++) {
        qn = Get_qn(s->buffer[k]); tn = Get_tn(s->buffer[k]);
        r = ma_hit2arc(&(s->buffer[k]), sg->seq[qn].len, sg->seq[tn].len, uopt->max_hang, asm_opt.max_hang_rate, uopt->min_ovlp, &p);
        if(r < 0) continue;
        if((p.ul>>32) != v) continue;
        if(p.ol < min_ovlp) continue;
        if(p.ol > pmax.ol) pmax = p;
    }
    if(pmax.ol == 0) return 0;

    tn = pmax.v>>1;
    if((!(sg->seq[tn].del)) || (ff[tn])) return -1;///not a tip
    get_R_to_U(uopt->ruIndex, tn, &cc, &ccu);
    if(ccu == 1) return -1;
    if((cc != (uint32_t)-1) && 
                    ((!(sg->seq[cc].del)) || (ff[cc]))) {
        return -1;///contained in an existing read 
    }

    if((*path_len) >= sg->seq[v>>1].len) (*path_len) -= sg->seq[v>>1].len;
    else (*path_len) = 0;
    (*path_len) += (uint32_t)(pmax.ul) + sg->seq[pmax.v>>1].len;
    
    uint32_t v0 = v; (*res) = pmax.v;
    v = (pmax.v^1); s = &(uopt->sources[v>>1]); pmax.ol = 0;
    for (k = 0; k < s->length; k++) {
        qn = Get_qn(s->buffer[k]); tn = Get_tn(s->buffer[k]);
        r = ma_hit2arc(&(s->buffer[k]), sg->seq[qn].len, sg->seq[tn].len, uopt->max_hang, asm_opt.max_hang_rate, uopt->min_ovlp, &p);
        if(r < 0) continue;
        if((p.ul>>32) != v) continue;
        if(p.ol < min_ovlp) continue;
        if(p.ol > pmax.ol) pmax = p;
    }
    if((pmax.v^1) == v0) {
        ff[(*res)>>1] = 1; return 1;
    }
    (*res) = (uint64_t)-1; return -1;///not the longest overlap
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x38(%rsp)
movl %r8d, 0x14(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, %r12
movq %rsi, %r14
movq 0x8(%rdx), %rax
movl %edi, %edx
shrl %edx
movq %rdx, 0x60(%rsp)
imulq $0x18, %rdx, %rcx
leaq (%rax,%rcx), %rbx
movl 0xc(%rax,%rcx), %eax
leaq 0xa3bf0(%rip), %rcx     # 0x253758
movss 0x158(%rcx), %xmm0
movss %xmm0, 0x10(%rsp)
movl %edi, 0x2c(%rsp)
movl %edi, %ecx
movq %rcx, 0x20(%rsp)
imulq $0x28, %rax, %rbp
xorl %r13d, %r13d
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq $0x0, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
cmpq %r13, %rbp
je 0x1afc40
movq (%rbx), %rax
leaq (%rax,%r13), %rdi
movl 0x4(%rax,%r13), %ecx
movl 0xc(%rax,%r13), %eax
movq 0x20(%r14), %rdx
movl (%rdx,%rcx,8), %esi
andl %r15d, %esi
movl (%rdx,%rax,8), %edx
andl %r15d, %edx
movl 0x40(%r12), %ecx
movl 0x44(%r12), %r8d
movss 0x10(%rsp), %xmm0
leaq 0x48(%rsp), %r9
callq 0x1aac6d
testl %eax, %eax
js 0x1afc37
movq 0x48(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
cmpl 0x20(%rsp), %ecx
jne 0x1afc37
movl 0x54(%rsp), %ecx
movl %ecx, %edx
andl %r15d, %edx
cmpl 0x14(%rsp), %edx
setae %sil
movl 0x8(%rsp), %edi
andl %r15d, %edi
cmpl %edi, %edx
seta %dl
andb %sil, %dl
cmpb $0x1, %dl
jne 0x1afc37
movl 0x50(%rsp), %edx
movl %edx, 0xc(%rsp)
movq %rax, 0x18(%rsp)
movl %ecx, 0x8(%rsp)
addq $0x28, %r13
jmp 0x1afba7
testl $0x7fffffff, 0x8(%rsp)  # imm = 0x7FFFFFFF
je 0x1afde2
movl 0xc(%rsp), %ebp
movl %ebp, %ebx
shrl %ebx
movq 0x20(%r14), %rax
pushq $-0x1
popq %r13
cmpl $0x0, (%rax,%rbx,8)
jns 0x1afde5
movq 0x40(%rsp), %r15
cmpb $0x0, (%r15,%rbx)
jne 0x1afde5
movq 0x30(%r12), %rdi
leaq 0x34(%rsp), %rdx
leaq 0x30(%rsp), %rcx
movl %ebx, %esi
callq 0x6c2a2
leaq 0x30(%rsp), %rax
cmpl $0x1, (%rax)
je 0x1afde5
movl 0x34(%rsp), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x20(%r14), %r8
cmpq %rcx, %rax
je 0x1afcc4
cmpl $0x0, (%r8,%rax,8)
jns 0x1afde5
cmpb $0x0, (%r15,%rax)
jne 0x1afde5
movq 0xa0(%rsp), %rdx
movq (%rdx), %rax
movq 0x60(%rsp), %rcx
movl (%r8,%rcx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %r14d, %r14d
subq %rcx, %rax
cmovbq %r14, %rax
movq %rax, (%rdx)
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl (%r8,%rbx,8), %ecx
andl %esi, %ecx
addl 0x18(%rsp), %ecx
addq %rax, %rcx
movq %rcx, (%rdx)
movl %ebp, %ecx
movq 0x38(%rsp), %rax
movq %rcx, 0x18(%rsp)
movq %rcx, (%rax)
movl %ebp, %eax
xorl $0x1, %eax
movq %rax, 0x20(%rsp)
movq 0x8(%r12), %rax
imulq $0x18, %rbx, %rcx
leaq (%rax,%rcx), %r15
movl 0xc(%rax,%rcx), %eax
leaq 0xa3a27(%rip), %rcx     # 0x253758
movss 0x158(%rcx), %xmm0
movss %xmm0, 0x10(%rsp)
imulq $0x28, %rax, %rbx
movl $0x0, 0x8(%rsp)
cmpq %r14, %rbx
je 0x1afdf7
movq (%r15), %rax
leaq (%rax,%r14), %rdi
movl 0x4(%rax,%r14), %ecx
movl 0xc(%rax,%r14), %eax
movl (%r8,%rcx,8), %esi
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %esi
movq %r8, %rbp
movl (%r8,%rax,8), %edx
andl %ecx, %edx
movl 0x40(%r12), %ecx
movl 0x44(%r12), %r8d
movss 0x10(%rsp), %xmm0
leaq 0x48(%rsp), %r9
callq 0x1aac6d
testl %eax, %eax
js 0x1afdd6
movq 0x20(%rsp), %rax
cmpl %eax, 0x4c(%rsp)
jne 0x1afdd6
movl 0x54(%rsp), %eax
movl %eax, %ecx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %ecx
cmpl 0x14(%rsp), %ecx
setae %dl
movl 0x8(%rsp), %esi
andl %edi, %esi
cmpl %esi, %ecx
seta %cl
testb %cl, %dl
movq %rbp, %r8
je 0x1afdd9
movl 0x50(%rsp), %ecx
movl %ecx, 0xc(%rsp)
movl %eax, 0x8(%rsp)
jmp 0x1afdd9
movq %rbp, %r8
addq $0x28, %r14
jmp 0x1afd4b
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%rsp), %eax
xorl 0x2c(%rsp), %eax
cmpl $0x1, %eax
jne 0x1afe1a
movq 0x18(%rsp), %rcx
shrl %ecx
movq 0x40(%rsp), %rax
movb $0x1, (%rax,%rcx)
pushq $0x1
popq %r13
jmp 0x1afde5
movq 0x38(%rsp), %rax
orq $-0x1, (%rax)
jmp 0x1afde5