// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_chimeric_reads_chain(ma_hit_t*, ma_hit_t_alloc*, ma_sub_t*, R_to_U*, ma_ug_t*, asg_t*, int, int, unsigned int, unsigned int, kvec_t_u32_warp*, kvec_asg_arc_t_warp*, unsigned int*, unsigned int*, unsigned int*, unsigned int, unsigned int, unsigned char*)
int get_chimeric_reads_chain(ma_hit_t *h, ma_hit_t_alloc* sources, ma_sub_t *coverage_cut, 
R_to_U* ruIndex, ma_ug_t *ug, asg_t *rg, int max_hang, int min_ovlp, uint32_t endRid, uint32_t uId,
kvec_t_u32_warp* chain_buffer, kvec_asg_arc_t_warp* chain_edges, uint32_t* return_ava_cur,
uint32_t* return_ava_ol, uint32_t* return_chainLen, uint32_t thresLen, uint32_t ignore_trio_flag, uint8_t *cmk)
{
    (*return_chainLen) = (*return_ava_cur) = (*return_ava_ol) = (uint32_t)-1;
    uint32_t trio_flag, non_trio_flag = (uint32_t)-1, contain_rId, is_Unitig;
    uint32_t chainLen = 0, ava_cur, test_oLen; int ql, tl; int32_t r;
    ma_sub_t *sq = NULL; ma_sub_t *st = NULL; asg_arc_t t; 
    chain_buffer->a.n = 0; kv_push(uint32_t, chain_buffer->a, uId);
    if(chain_edges) chain_edges->a.n = 0;
    

    ///continue
    ///need to update h, endRid, chainLen, chain_buffer and chain_edges
    while (h) {
        sq = &(coverage_cut[Get_qn(*h)]);
        st = &(coverage_cut[Get_tn(*h)]);
        trio_flag = R_INF.trio_flag[Get_qn(*h)];

        ///don't want to edges between different haps
        non_trio_flag = (uint32_t)-1;
        if(ignore_trio_flag == 0) {
            if(trio_flag == FATHER) non_trio_flag = MOTHER;
            if(trio_flag == MOTHER) non_trio_flag = FATHER;
            if(R_INF.trio_flag[Get_tn(*h)] == non_trio_flag) break;
        }

        ///just need deleted edges
        ///sq might be deleted or not
        if((!st->del) || (!h->del) || (!rg->seq[Get_tn(*h)].del)) break;
        // if(cmk[Get_tn(*h)] != ((uint8_t)-1)) break;
        // if(cmk[Get_tn(*h)] == ((uint8_t)-1)) break;

        ///tn must be contained in another existing read
        get_R_to_U(ruIndex, Get_tn(*h), &contain_rId, &is_Unitig);
        if(is_Unitig == 1) break;

        ql = sq->e - sq->s; tl = st->e - st->s;
        r = ma_hit2arc(h, ql, tl, max_hang, asm_opt.max_hang_rate, min_ovlp, &t);

        ///if st is contained in sq, or vice verse, skip
        if(r < 0) break;

        ///if sq and v are not in the same direction, skip
        ///endRid is (t.ul>>32), and t.v is a contained read
        if((t.ul>>32) != endRid) break;

        if(chain_edges) kv_push(asg_arc_t, chain_edges->a, t);
        chainLen++;
        ///endRid is (t.ul>>32), and t.v is a contained read
        ///find edges from t.v to existing unitigs
        ///chain_buffer->a.n always be 1
        ava_cur = get_num_edges2existing_nodes_advance(ug, rg, sources, coverage_cut, ruIndex, max_hang, min_ovlp, t.v, &test_oLen, chain_buffer->a.a, chain_buffer->a.n, ignore_trio_flag);
        //means find an aim
        if(ava_cur > 0) {
            (*return_ava_cur) = ava_cur;
            (*return_ava_ol) = test_oLen;
            (*return_chainLen) = chainLen;
            h = NULL;
            return 1;
        }

        if(chainLen >= thresLen) break;

        ///endRid is (t.ul>>32), and t.v is a contained read
        ///haven't found a existing unitig from t.v
        ///check if t.v can link to a new contained read
        endRid = t.v; 
        h = get_best_cmk_read(ug, rg, sources, coverage_cut, ruIndex, max_hang, min_ovlp, endRid, ignore_trio_flag, cmk);
    }

    return 0;    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xc8, %rsp
movq 0x68(%rbp), %rax
movl 0x60(%rbp), %eax
movl 0x58(%rbp), %eax
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movl 0x28(%rbp), %eax
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
movq %r8, -0x48(%rbp)
movq %r9, -0x50(%rbp)
movq 0x48(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x40(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x50(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0xffffffff, -0x58(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x64(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq 0x30(%rbp), %rax
movq $0x0, (%rax)
movq 0x30(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x1523ed
movq 0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1523ac
movq 0x30(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xa8(%rbp)
jmp 0x1523ba
movl $0x2, %eax
movq %rax, -0xa8(%rbp)
jmp 0x1523ba
movq -0xa8(%rbp), %rcx
movq 0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x30(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq 0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movl 0x28(%rbp), %edx
movq 0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x30(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movl %edx, (%rax,%rcx,4)
cmpq $0x0, 0x38(%rbp)
je 0x15241e
movq 0x38(%rbp), %rax
movq $0x0, (%rax)
jmp 0x152420
cmpq $0x0, -0x28(%rbp)
je 0x152766
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
shrq $0x20, %rcx
movl %ecx, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
movl 0xc(%rcx), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
leaq 0x272e69(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
shrq $0x20, %rcx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0x54(%rbp)
movl $0xffffffff, -0x58(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, 0x60(%rbp)
jne 0x1524c8
cmpl $0x1, -0x54(%rbp)
jne 0x152499
movl $0x2, -0x58(%rbp)
cmpl $0x2, -0x54(%rbp)
jne 0x1524a6
movl $0x1, -0x58(%rbp)
leaq 0x272e23(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movq -0x28(%rbp), %rcx
movl 0xc(%rcx), %ecx
movzbl (%rax,%rcx), %eax
cmpl -0x58(%rbp), %eax
jne 0x1524c6
jmp 0x152766
jmp 0x1524c8
movq -0x88(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x152504
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
shrq $0x3f, %rax
cmpl $0x0, %eax
je 0x152504
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
movl 0xc(%rcx), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x152509
jmp 0x152766
movq -0x40(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0xc(%rax), %esi
leaq -0x5c(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0xd3d50
cmpl $0x1, -0x60(%rbp)
jne 0x15252c
jmp 0x152766
movq -0x80(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x80(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, -0x70(%rbp)
movq -0x88(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x88(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, -0x74(%rbp)
movq -0x28(%rbp), %rdi
movl -0x70(%rbp), %esi
movl -0x74(%rbp), %edx
movl 0x10(%rbp), %ecx
leaq 0x272282(%rip), %rax    # 0x3c47f8
movss 0x158(%rax), %xmm0
movl 0x18(%rbp), %r8d
leaq -0xa0(%rbp), %r9
callq 0xd0300
movl %eax, -0x78(%rbp)
cmpl $0x0, -0x78(%rbp)
jge 0x15259c
jmp 0x152766
movq -0xa0(%rbp), %rax
shrq $0x20, %rax
movl 0x20(%rbp), %ecx
cmpq %rcx, %rax
je 0x1525b4
jmp 0x152766
cmpq $0x0, 0x38(%rbp)
je 0x152671
jmp 0x1525c1
movq 0x38(%rbp), %rax
movq (%rax), %rax
movq 0x38(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x15262f
movq 0x38(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1525f1
movq 0x38(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xb0(%rbp)
jmp 0x1525ff
movl $0x2, %eax
movq %rax, -0xb0(%rbp)
jmp 0x1525ff
movq -0xb0(%rbp), %rcx
movq 0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x38(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x38(%rbp), %rax
imulq $0x18, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq 0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq 0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x38(%rbp), %rdx
movq (%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, (%rdx)
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq -0xa0(%rbp), %rcx
movq %rcx, (%rax)
movq -0x98(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x90(%rbp), %rcx
movq %rcx, 0x10(%rax)
jmp 0x152671
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
movl 0x10(%rbp), %r9d
movl 0x18(%rbp), %r15d
movl -0x98(%rbp), %r14d
movq 0x30(%rbp), %rax
movq 0x10(%rax), %r11
movq 0x30(%rbp), %rax
movq (%rax), %rax
movl %eax, %r10d
movl 0x60(%rbp), %eax
leaq -0x6c(%rbp), %rbx
movl %r15d, (%rsp)
movl %r14d, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movl %r10d, 0x20(%rsp)
movl %eax, 0x28(%rsp)
callq 0x1692c0
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x68(%rbp)
jbe 0x15270c
movl -0x68(%rbp), %ecx
movq 0x40(%rbp), %rax
movl %ecx, (%rax)
movl -0x6c(%rbp), %ecx
movq 0x48(%rbp), %rax
movl %ecx, (%rax)
movl -0x64(%rbp), %ecx
movq 0x50(%rbp), %rax
movl %ecx, (%rax)
movq $0x0, -0x28(%rbp)
movl $0x1, -0x1c(%rbp)
jmp 0x15276d
movl -0x64(%rbp), %eax
cmpl 0x58(%rbp), %eax
jb 0x152716
jmp 0x152766
movl -0x98(%rbp), %eax
movl %eax, 0x20(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
movl 0x10(%rbp), %r9d
movl 0x18(%rbp), %ebx
movl 0x20(%rbp), %r11d
movl 0x60(%rbp), %r10d
movq 0x68(%rbp), %rax
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x151af0
movq %rax, -0x28(%rbp)
jmp 0x152420
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
addq $0xc8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop