// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movq %r9, 0x58(%rsp)
movl %r8d, 0x2c(%rsp)
movq %rcx, %r15
movl %edi, %r13d
movq 0x8(%rdx), %rax
movl %edi, %ecx
shrl %ecx
movq %rcx, 0x78(%rsp)
leaq (%rcx,%rcx,2), %rcx
movl 0xc(%rax,%rcx,8), %r12d
xorl %ebp, %ebp
testq %r12, %r12
je 0x1f02d0
movq %r15, 0x30(%rsp)
leaq (%rax,%rcx,8), %rax
movq (%rax), %r14
movl %r13d, %ecx
movq %rsi, 0x40(%rsp)
movq 0x20(%rsi), %r13
movl 0x40(%rdx), %ebx
movq %rdx, 0x38(%rsp)
movl 0x44(%rdx), %ebp
leaq 0x9a576(%rip), %rax     # 0x28a758
movss 0x158(%rax), %xmm0
movss %xmm0, 0x14(%rsp)
movl %ecx, 0x10(%rsp)
movl %ecx, %eax
movq %rax, 0x48(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl $0x0, 0xc(%rsp)
movq $0x0, 0x20(%rsp)
movl 0x4(%r14), %eax
movl 0xc(%r14), %ecx
movl (%r13,%rax,8), %esi
andl %r15d, %esi
movl (%r13,%rcx,8), %edx
andl %r15d, %edx
movq %r14, %rdi
movl %ebx, %ecx
movss 0x14(%rsp), %xmm0
movl %ebp, %r8d
leaq 0x60(%rsp), %r9
callq 0x1eb21b
testl %eax, %eax
js 0x1f029b
movq 0x60(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
cmpl 0x48(%rsp), %ecx
jne 0x1f029b
movl 0x6c(%rsp), %ecx
movl %ecx, %edx
andl %r15d, %edx
cmpl 0x2c(%rsp), %edx
setae %sil
movq 0x18(%rsp), %rdi
andl %r15d, %edi
cmpl %edi, %edx
seta %dl
andb %sil, %dl
cmpb $0x1, %dl
jne 0x1f029b
movl 0x68(%rsp), %edx
movl %edx, 0xc(%rsp)
movq %rax, 0x20(%rsp)
movl %ecx, %eax
movq %rax, 0x18(%rsp)
addq $0x28, %r14
decq %r12
jne 0x1f021b
testl $0x7fffffff, 0x18(%rsp) # imm = 0x7FFFFFFF
sete %al
xorl %ebp, %ebp
movq 0x30(%rsp), %r15
movl 0x10(%rsp), %r13d
movl 0xc(%rsp), %r12d
movq 0x38(%rsp), %rdx
movq 0x40(%rsp), %rsi
jmp 0x1f02de
movb $0x1, %al
movq $0x0, 0x20(%rsp)
xorl %r12d, %r12d
testb %al, %al
jne 0x1f04bd
movl %r12d, %ebx
shrl %ebx
movq 0x20(%rsi), %rax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x0, (%rax,%rbx,8)
jns 0x1f04bd
cmpb $0x0, (%r15,%rbx)
jne 0x1f04bd
movq %rsi, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq 0x30(%rdx), %rdi
leaq 0x54(%rsp), %rdx
leaq 0x84(%rsp), %r14
movl %ebx, %esi
movq %r14, %rcx
callq 0xa306f
cmpl $0x1, (%r14)
je 0x1f04bd
movl 0x54(%rsp), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
movq 0x40(%rsp), %rdx
je 0x1f0364
movq 0x20(%rdx), %rcx
cmpl $0x0, (%rcx,%rax,8)
jns 0x1f04bd
cmpb $0x0, (%r15,%rax)
jne 0x1f04bd
movl %r13d, 0x10(%rsp)
movq %r15, 0x30(%rsp)
movq 0xc0(%rsp), %rax
movq (%rax), %rcx
movq 0x20(%rdx), %r15
movq 0x78(%rsp), %rdx
movl (%r15,%rdx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq $0x0, 0x18(%rsp)
subq %rdx, %rcx
movl $0x0, %edx
cmovaeq %rcx, %rdx
movq %rdx, (%rax)
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl (%r15,%rbx,8), %ecx
andl %ebp, %ecx
addl 0x20(%rsp), %ecx
addq %rdx, %rcx
movq %rcx, (%rax)
movl %r12d, %ecx
movq 0x58(%rsp), %rax
movq %rcx, 0x20(%rsp)
movq %rcx, (%rax)
movq 0x38(%rsp), %rsi
movq 0x8(%rsi), %rax
leaq (%rbx,%rbx,2), %rcx
movl %r12d, %edx
movl 0xc(%rax,%rcx,8), %r12d
testq %r12, %r12
movl %edx, 0xc(%rsp)
je 0x1f0488
leaq (%rax,%rcx,8), %rax
movl %edx, %ecx
xorl $0x1, %ecx
movq %rcx, 0x48(%rsp)
movq (%rax), %rbx
movl 0x40(%rsi), %r13d
movl 0x44(%rsi), %r14d
leaq 0x9a34d(%rip), %rax     # 0x28a758
movss 0x158(%rax), %xmm0
movss %xmm0, 0x14(%rsp)
movl 0x4(%rbx), %eax
movl 0xc(%rbx), %ecx
movl (%r15,%rax,8), %esi
andl %ebp, %esi
movl (%r15,%rcx,8), %edx
andl %ebp, %edx
movq %rbx, %rdi
movl %r13d, %ecx
movss 0x14(%rsp), %xmm0
movl %r14d, %r8d
leaq 0x60(%rsp), %r9
callq 0x1eb21b
testl %eax, %eax
js 0x1f047f
movq 0x48(%rsp), %rax
cmpl %eax, 0x64(%rsp)
jne 0x1f047f
movl 0x6c(%rsp), %eax
movl %eax, %ecx
andl %ebp, %ecx
cmpl 0x2c(%rsp), %ecx
setae %dl
movq 0x18(%rsp), %rsi
andl %ebp, %esi
cmpl %esi, %ecx
seta %cl
testb %cl, %dl
je 0x1f047f
movl 0x68(%rsp), %ecx
movl %ecx, 0xc(%rsp)
movq %rax, 0x18(%rsp)
addq $0x28, %rbx
decq %r12
jne 0x1f0419
movl 0xc(%rsp), %eax
xorl 0x10(%rsp), %eax
cmpl $0x1, %eax
jne 0x1f04ac
movq 0x20(%rsp), %rcx
shrl %ecx
movq 0x30(%rsp), %rax
movb $0x1, (%rax,%rcx)
movl $0x1, %ebp
jmp 0x1f04bd
movq 0x58(%rsp), %rax
movq $-0x1, (%rax)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq