// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: bub_complex(asg_t*, ma_ug_t*, bubble_type*, unsigned int, ma_hit_t_alloc*, ma_sub_t*, kvec_t_u32_warp*, int, int, unsigned char*, unsigned char*, kv_asg_arc_t*, buf_t*, unsigned long)
int bub_complex(asg_t *sg, ma_ug_t *ug, bubble_type* bub, uint32_t bid, ma_hit_t_alloc* sources, ma_sub_t *coverage_cut, kvec_t_u32_warp* stack, 
int max_hang, int min_ovlp, uint8_t* trio_flag, uint8_t* vis_flag, kv_asg_arc_t* e, buf_t *b, uint64_t tLen)
{
    if(bid >= bub->f_bub) return 0;
    uint32_t beg_utg, sink_utg, *a = NULL, n, begRid, sinkRid, i, k_i, k_j, k_v, rID/**, cur_flag, pre_flag, after_flag**/;
    int is_switch_0, is_switch_1;
    ma_utg_t* nsu = NULL;
    get_bubbles(bub, bid, &beg_utg, &sink_utg, &a, &n, NULL);
    if(beg_utg == (uint32_t)-1 || sink_utg == (uint32_t)-1) return 0;
    if(beg_utg&1)
    {
        begRid = ug->u.a[beg_utg>>1].start^1;
    }
    else
    {
        begRid = ug->u.a[beg_utg>>1].end^1;
    }

    if(sink_utg&1)
    {
        sinkRid = ug->u.a[sink_utg>>1].start;
    }
    else
    {
        sinkRid = ug->u.a[sink_utg>>1].end;
    }


    
    is_switch_0 = is_switch_1 = 1;
    asg_bub_pop1_primary_trio_switch_check(ug->g, ug, beg_utg, tLen, b, FATHER, DROP, 0, NULL, NULL, &is_switch_0);

    if(is_switch_0 == 0)
    {
        asg_bub_pop1_primary_trio_switch_check(ug->g, ug, beg_utg, tLen, b, MOTHER, DROP, 0, NULL, NULL, &is_switch_1);
    } 
    

    for (k_i = 0; k_i < n; k_i++)
    {
        nsu = &(ug->u.a[a[k_i]>>1]);
        for (k_j = 0; k_j < nsu->n; k_j++)
        {
            rID = nsu->a[k_j]>>33;
            if(R_INF.trio_flag[rID] == DROP) R_INF.trio_flag[rID] = AMBIGU;
        }
    }
    /*******************************for debug************************************/
    // for (i = 0; i < sg->n_seq; i++)
    // {
    //     if(R_INF.trio_flag[i] == DROP) fprintf(stderr, "ERROR-1\n");
    // }
    /*******************************for debug************************************/
    
    if(is_switch_0 == 0 && is_switch_1 == 0) return 0;

    asg_arc_t *acur = NULL;
    uint32_t cur, ncur, v, n_vx = sg->n_seq<<1;
    stack->a.n = 0; 
    memset(vis_flag, 0, n_vx);

    kv_push(uint32_t, stack->a, begRid);
    while (stack->a.n > 0)
    {
        stack->a.n--;
        cur = stack->a.a[stack->a.n];
        ncur = asg_arc_n(sg, cur);
        acur = asg_arc_a(sg, cur);
        vis_flag[cur] = 1;
        for (i = 0; i < ncur; i++)
        {
            if(acur[i].del) continue;
            if(vis_flag[acur[i].v]) continue;
            if(acur[i].v == sinkRid) continue;
            kv_push(uint32_t, stack->a, acur[i].v);
        }
    }
    vis_flag[sinkRid] = 1;


    ma_hit_t_alloc* x = NULL;
    ma_hit_t *h = NULL;
    ma_sub_t *sq = NULL;
    ma_sub_t *st = NULL;
    int32_t r;
    asg_arc_t t;
    

    for (k_i = 0; k_i < n; k_i++)
    {
        nsu = &(ug->u.a[a[k_i]>>1]);
        for (k_j = 0; k_j < nsu->n; k_j++)
        {
            rID = nsu->a[k_j]>>33;
            for (k_v = 0; k_v < 2; k_v++)
            {
                v = (rID<<1) + k_v;
                if(vis_flag[v] == 0) continue;
                x = &(sources[v>>1]);
                for (i = 0; i < x->length; i++)
                {
                    h = &(x->buffer[i]);
                    sq = &(coverage_cut[Get_qn(*h)]);
                    st = &(coverage_cut[Get_tn(*h)]);
                    if(st->del || sg->seq[Get_tn(*h)].del) continue;
                    r = ma_hit2arc(h, sq->e - sq->s, st->e - st->s, max_hang, 
                                        asm_opt.max_hang_rate, min_ovlp, &t);
                
                    ///if it is a contained read, skip
                    if(r < 0) continue;
                    if((t.ul>>32) != v) continue;
                    if(vis_flag[t.ul>>32] == 0 || vis_flag[t.v] == 0) continue;
                    kv_push(asg_arc_t, *e, t);
                    get_edge_from_source(sources, coverage_cut, NULL, max_hang, min_ovlp, 
                                (t.v^1), ((t.ul>>32)^1), &t);
                    kv_push(asg_arc_t, *e, t);
                }

            }
        }
    }

    return 1;
    
    /**
    for (v = 0; v < n_vx; v++)
    {
        if(vis_flag[v] == 0) continue;
        fprintf(stderr, "v: %u, n_vx: %u\n", v, n_vx);
        x = &(sources[v>>1]);
        for (i = 0; i < x->length; i++)
        {
            fprintf(stderr, "i: %u, x->length: %u\n", i, x->length);
            h = &(x->buffer[i]);
            sq = &(coverage_cut[Get_qn(*h)]);
            st = &(coverage_cut[Get_tn(*h)]);
            if(st->del || sg->seq[Get_tn(*h)].del) continue;
            r = ma_hit2arc(h, sq->e - sq->s, st->e - st->s, max_hang, 
                                asm_opt.max_hang_rate, min_ovlp, &t);
        
            ///if it is a contained read, skip
            if(r < 0) continue;
            if((t.ul>>32) != v) continue;
            if(vis_flag[t.ul>>32] == 0 || vis_flag[t.v] == 0) continue;
            kv_push(asg_arc_t, *e, t);
            get_edge_from_source(sources, coverage_cut, NULL, max_hang, min_ovlp, 
                        (t.v^1), ((t.ul>>32)^1), &t);
            kv_push(asg_arc_t, *e, t);
        }
    }
    **/
    

    /*******************************for debug************************************/
    // uint32_t utg_occ = 0, rtg_occ = 0;
    // for (i = 0; i < n; i++)
    // {
    //     utg_occ += ug->u.a[a[i]>>1].n;
    // }

    // for (i = 0; i < n_vx; i++)
    // {
    //     if(vis_flag[i]) rtg_occ++;
    // }
    
    // fprintf(stderr, "bid: %u, rtg_occ: %u, utg_occ: %u\n", bid, rtg_occ, utg_occ);
    // if(rtg_occ != utg_occ + 2) fprintf(stderr, "ERROR\n");
    /*******************************for debug************************************/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r8, 0x60(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, 0x48(%rsp)
movl %ecx, %esi
xorl %eax, %eax
cmpq %rsi, 0x80(%rdx)
jbe 0xbba55
movq %r9, %rbx
leaq 0x68(%rsp), %r8
andq $0x0, (%r8)
andq $0x0, (%rsp)
leaq 0x5c(%rsp), %r14
leaq 0xb4(%rsp), %r15
leaq 0x34(%rsp), %r9
movq %rdx, %rdi
movq %r14, %rdx
movq %r15, %rcx
callq 0xf6ab5
movl (%r14), %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rdx
sete %cl
movl (%r15), %eax
cmpl $-0x1, %eax
sete %sil
orb %cl, %sil
jne 0xbb686
movq 0x128(%rsp), %rcx
movq 0x120(%rsp), %r8
movl %eax, %eax
xorl %r9d, %r9d
testb $0x1, %dl
sete %r9b
movq 0x38(%rsp), %rsi
movq 0x10(%rsi), %r10
movl %edx, %edi
shrl %edi
imulq $0x28, %rdi, %rdi
addq %r10, %rdi
movl 0x4(%rdi,%r9,4), %ebp
pushq $0x1
popq %r15
movl %eax, %edi
shrl %edi
imulq $0x28, %rdi, %r9
movl %eax, %edi
shrl %edi
imulq $0x28, %rdi, %rdi
testb $0x1, %al
leaq 0x4(%r10,%r9), %rax
leaq 0x8(%r10,%rdi), %rdi
cmovneq %rax, %rdi
movl (%rdi), %r13d
movl %r15d, 0x30(%rsp)
leaq 0x58(%rsp), %r12
movl %r15d, (%r12)
movq 0x18(%rsi), %rdi
movq %r12, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
andl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
movq %rcx, %r14
movl %r15d, %r9d
callq 0xa58d0
cmpl $0x0, (%r12)
jne 0xbb618
movq 0x38(%rsp), %rsi
movq 0x18(%rsi), %rdi
movl 0x5c(%rsp), %edx
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
andl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
pushq $0x2
popq %r9
movq %r14, %rcx
movq 0x120(%rsp), %r8
callq 0xa58d0
movq 0xf0(%rsp), %r14
xorl %r15d, %ebp
xorl %eax, %eax
leaq 0x198c04(%rip), %rcx    # 0x254230
movl 0x34(%rsp), %edx
cmpq %rdx, %rax
jae 0xbb67c
movq 0x68(%rsp), %rdx
movl (%rdx,%rax,4), %edx
shrl %edx
imulq $0x28, %rdx, %rdx
movq 0x38(%rsp), %rsi
addq 0x10(%rsi), %rdx
xorl %esi, %esi
movl 0x10(%rdx), %edi
cmpq %rdi, %rsi
jae 0xbb677
movq 0x18(%rdx), %rdi
movq (%rdi,%rsi,8), %rdi
shrq $0x21, %rdi
movq 0x28(%rcx), %r8
cmpb $0x5, (%r8,%rdi)
jne 0xbb672
movb $0x0, (%r8,%rdi)
incq %rsi
jmp 0xbb64e
incq %rax
jmp 0xbb62c
movl 0x30(%rsp), %eax
orl 0x58(%rsp), %eax
jne 0xbb68d
xorl %eax, %eax
jmp 0xbba55
movq 0x48(%rsp), %rax
movl 0x14(%rax), %edx
addl %edx, %edx
andq $0x0, (%r14)
movq 0x110(%rsp), %rdi
xorl %esi, %esi
callq 0x20e0
movq (%r14), %rcx
pushq $0x2
popq %rax
cmpq 0x8(%r14), %rcx
jne 0xbb6db
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
jmp 0xbb6df
movq 0x10(%r14), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movl %ebp, (%rax,%rcx,4)
testq %rdx, %rdx
movq 0x110(%rsp), %rbp
je 0xbb7a9
leaq -0x1(%rdx), %rax
movq %rax, (%r14)
movq 0x10(%r14), %rax
movl -0x4(%rax,%rdx,4), %eax
movq 0x48(%rsp), %rdx
movq 0x8(%rdx), %rcx
movq 0x28(%rdx), %rdx
movq (%rdx,%rax,8), %rdx
movl %edx, %esi
shrq $0x20, %rdx
movb $0x1, (%rbp,%rax)
imulq $0x18, %rsi, %r15
imulq $0x18, %rdx, %rax
leaq (%rcx,%rax), %r12
addq $0x8, %r12
xorl %ebp, %ebp
cmpq %rbp, %r15
je 0xbb7a1
cmpl $0x0, 0x4(%r12,%rbp)
js 0xbb79b
movl (%r12,%rbp), %ecx
movq 0x110(%rsp), %rax
cmpb $0x0, (%rax,%rcx)
jne 0xbb79b
cmpl %r13d, %ecx
je 0xbb79b
movq (%r14), %rdx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rdx
jne 0xbb791
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%r12,%rbp), %ecx
movq (%r14), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r14)
movl %ecx, (%rax,%rdx,4)
addq $0x18, %rbp
jmp 0xbb737
movq (%r14), %rdx
jmp 0xbb6e9
movb $0x1, (%rbp,%r13)
xorl %ecx, %ecx
movl 0x34(%rsp), %eax
cmpq %rax, %rcx
jae 0xbba52
movq 0x68(%rsp), %rax
movq %rcx, 0x88(%rsp)
movl (%rax,%rcx,4), %eax
shrl %eax
imulq $0x28, %rax, %rcx
movq 0x38(%rsp), %rax
addq 0x10(%rax), %rcx
xorl %edx, %edx
movq %rcx, 0x90(%rsp)
movl 0x10(%rcx), %eax
cmpq %rax, %rdx
jae 0xbba42
movq 0x18(%rcx), %rax
movq %rdx, 0x98(%rsp)
movq (%rax,%rdx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
andl $-0x2, %ecx
movq %rcx, 0xa0(%rsp)
shrq $0x21, %rax
imulq $0x18, %rax, %r15
addq 0x60(%rsp), %r15
xorl %eax, %eax
cmpq $0x2, %rax
je 0xbba2a
movq %rax, 0xa8(%rsp)
orq 0xa0(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpb $0x0, (%rbp,%rax)
je 0xbba1a
movq 0x40(%rsp), %rax
xorl $0x1, %eax
movl %eax, 0x54(%rsp)
xorl %r14d, %r14d
xorl %r12d, %r12d
movl 0xc(%r15), %eax
cmpq %rax, %r12
jae 0xbba1a
movq (%r15), %rdi
movl 0xc(%rdi,%r14), %edx
imulq $0xc, %rdx, %rcx
movl (%rbx,%rcx), %eax
testl %eax, %eax
js 0xbba0e
movq 0x48(%rsp), %rsi
movq 0x20(%rsi), %rsi
cmpl $0x0, (%rsi,%rdx,8)
js 0xbba0e
movl 0x4(%rdi,%r14), %edx
addq %r14, %rdi
imulq $0xc, %rdx, %rdx
addq %rbx, %rdx
addq %rbx, %rcx
movl 0x4(%rdx), %esi
movl (%rdx), %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edx
subl %edx, %esi
movl 0x4(%rcx), %edx
subl %eax, %edx
leaq 0x197e93(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl 0xf8(%rsp), %ecx
movl 0x100(%rsp), %r8d
leaq 0x70(%rsp), %r9
callq 0x6a4a7
testl %eax, %eax
js 0xbba0e
movl 0x74(%rsp), %eax
cmpq 0x40(%rsp), %rax
jne 0xbba0e
movq 0x40(%rsp), %rax
cmpb $0x0, (%rbp,%rax)
je 0xbba0e
movl 0x78(%rsp), %r13d
cmpb $0x0, (%rbp,%r13)
je 0xbba0e
movq 0x118(%rsp), %rbp
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0xbb95a
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbp)
imulq $0x18, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
imulq $0x18, %rcx, %rcx
movq 0x80(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movups 0x70(%rsp), %xmm0
movups %xmm0, (%rax,%rcx)
xorl $0x1, %r13d
leaq 0x70(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x54(%rsp), %eax
movl %eax, (%rsp)
movq 0x60(%rsp), %rdi
movq %rbx, %rsi
movl 0xf8(%rsp), %ecx
movl 0x100(%rsp), %r8d
movl %r13d, %r9d
callq 0x79204
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0xbb9e4
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbp)
imulq $0x18, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
imulq $0x18, %rcx, %rcx
movq 0x80(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movups 0x70(%rsp), %xmm0
movups %xmm0, (%rax,%rcx)
movq 0x110(%rsp), %rbp
incq %r12
addq $0x28, %r14
jmp 0xbb860
movq 0xa8(%rsp), %rax
incq %rax
jmp 0xbb824
movq 0x98(%rsp), %rdx
incq %rdx
movq 0x90(%rsp), %rcx
jmp 0xbb7e7
movq 0x88(%rsp), %rcx
incq %rcx
jmp 0xbb7b1
pushq $0x1
popq %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq