// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq 0x10(%rbp), %rax
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq %r9, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x8(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
movl -0x6c(%rbp), %eax
andl $0x80000000, %eax       # imm = 0x80000000
orl $0x0, %eax
movl %eax, -0x6c(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x80(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x322490
movq -0x80(%rbp), %rax
movq (%rax), %rax
movl -0x34(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x38(%rbp)
movq -0x80(%rbp), %rax
movq (%rax), %rax
movl -0x34(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x80(%rbp), %rax
movq (%rax), %rdi
movl -0x34(%rbp), %eax
imulq $0x28, %rax, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x3c(%rbp), %ecx
movl (%rax,%rcx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq -0x18(%rbp), %rax
movl 0x40(%rax), %ecx
leaq 0xa23ea(%rip), %rax     # 0x3c47f8
movss 0x158(%rax), %xmm0
movq -0x18(%rbp), %rax
movl 0x44(%rax), %r8d
leaq -0x60(%rbp), %r9
callq 0x31a080
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jge 0x322432
jmp 0x322482
movq -0x60(%rbp), %rax
shrq $0x20, %rax
movl -0x8(%rbp), %ecx
cmpq %rcx, %rax
je 0x322444
jmp 0x322482
movl -0x54(%rbp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl -0x24(%rbp), %eax
jae 0x322453
jmp 0x322482
movl -0x54(%rbp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl -0x6c(%rbp), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jle 0x322480
movq -0x60(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x322482
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x322385
movl -0x6c(%rbp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x0, %eax
jne 0x3224a9
movl $0x0, -0x4(%rbp)
jmp 0x322759
movl -0x70(%rbp), %eax
shrl %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x3c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x3224d4
movq -0x20(%rbp), %rax
movl -0x3c(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
je 0x3224e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x322759
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdi
movl -0x3c(%rbp), %esi
leaq -0x40(%rbp), %rdx
leaq -0x44(%rbp), %rcx
callq 0xd3d50
cmpl $0x1, -0x44(%rbp)
jne 0x32250a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x322759
cmpl $-0x1, -0x40(%rbp)
je 0x32253f
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x40(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x322533
movq -0x20(%rbp), %rax
movl -0x40(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
je 0x32253f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x322759
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl -0x8(%rbp), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
cmpq %rcx, %rax
jb 0x32258f
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x8(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
movq 0x10(%rbp), %rax
movq (%rax), %rcx
subq %rdx, %rcx
movq %rcx, (%rax)
jmp 0x32259a
movq 0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x78(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl -0x70(%rbp), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addl %ecx, %eax
movl %eax, %eax
movl %eax, %ecx
movq 0x10(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movl -0x8(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x70(%rbp), %eax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movl -0x70(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x8(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movl -0x6c(%rbp), %eax
andl $0x80000000, %eax       # imm = 0x80000000
orl $0x0, %eax
movl %eax, -0x6c(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x80(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x32271e
movq -0x80(%rbp), %rax
movq (%rax), %rax
movl -0x34(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x38(%rbp)
movq -0x80(%rbp), %rax
movq (%rax), %rax
movl -0x34(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x80(%rbp), %rax
movq (%rax), %rdi
movl -0x34(%rbp), %eax
imulq $0x28, %rax, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x3c(%rbp), %ecx
movl (%rax,%rcx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq -0x18(%rbp), %rax
movl 0x40(%rax), %ecx
leaq 0xa215c(%rip), %rax     # 0x3c47f8
movss 0x158(%rax), %xmm0
movq -0x18(%rbp), %rax
movl 0x44(%rax), %r8d
leaq -0x60(%rbp), %r9
callq 0x31a080
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jge 0x3226c0
jmp 0x322710
movq -0x60(%rbp), %rax
shrq $0x20, %rax
movl -0x8(%rbp), %ecx
cmpq %rcx, %rax
je 0x3226d2
jmp 0x322710
movl -0x54(%rbp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl -0x24(%rbp), %eax
jae 0x3226e1
jmp 0x322710
movl -0x54(%rbp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl -0x6c(%rbp), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jle 0x32270e
movq -0x60(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x322710
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x322613
movl -0x70(%rbp), %eax
xorl $0x1, %eax
cmpl -0x84(%rbp), %eax
jne 0x322747
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
shrq %rcx
movb $0x1, (%rax,%rcx)
movl $0x1, -0x4(%rbp)
jmp 0x322759
movq -0x30(%rbp), %rax
movq $-0x1, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)