// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa8, %rsp
movq %r8, 0x48(%rsp)
movq %rsi, %rax
movq %rdi, 0x38(%rsp)
movl %ecx, %esi
xorl %r14d, %r14d
cmpq %rsi, 0x80(%rdx)
jbe 0xf9f1b
movq %r9, %rbx
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
leaq 0x50(%rsp), %r8
movq %r14, (%r8)
movq %r14, (%rsp)
leaq 0x44(%rsp), %r15
leaq 0xa4(%rsp), %r13
leaq 0x28(%rsp), %r9
movq %rdx, %rdi
movq %r15, %rdx
movq %r13, %rcx
callq 0x13812e
movl (%r15), %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rdx
sete %cl
movl (%r13), %eax
cmpl $-0x1, %eax
sete %sil
orb %cl, %sil
jne 0xf9f1b
movq 0x118(%rsp), %rcx
movq 0x110(%rsp), %r8
movl %eax, %eax
xorl %r10d, %r10d
testb $0x1, %dl
sete %r10b
movq 0x30(%rsp), %rsi
movq 0x10(%rsi), %r9
movl %edx, %edi
shrl %edi
leaq (%rdi,%rdi,4), %rdi
leaq (%r9,%rdi,8), %rdi
movl 0x4(%rdi,%r10,4), %r12d
movl $0x1, %edi
xorl %r10d, %r10d
testb $0x1, %al
sete %r10b
shrl %eax
leaq (%rax,%rax,4), %rax
leaq (%r9,%rax,8), %rax
movl 0x4(%rax,%r10,4), %ebp
movl %edi, 0x2c(%rsp)
leaq 0x40(%rsp), %r15
movl %edi, (%r15)
movq 0x18(%rsi), %rdi
movq %r15, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
movq %r8, %r13
movl $0x1, %r9d
callq 0xe1869
cmpl $0x0, (%r15)
jne 0xf9a79
movq 0x30(%rsp), %rsi
movq 0x18(%rsi), %rdi
movl 0x44(%rsp), %edx
leaq 0x2c(%rsp), %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
movq 0x118(%rsp), %rcx
movq %r13, %r8
movl $0x2, %r9d
callq 0xe1869
cmpl $0x0, 0x28(%rsp)
movq 0x30(%rsp), %r9
je 0xf9ae0
xorl %eax, %eax
leaq 0x1917a2(%rip), %rcx    # 0x28b230
movq 0x10(%r9), %rdx
movq 0x50(%rsp), %rsi
movl (%rsi,%rax,4), %esi
shrl %esi
leaq (%rsi,%rsi,4), %rsi
cmpl $0x0, 0x10(%rdx,%rsi,8)
je 0xf9ad4
leaq (%rdx,%rsi,8), %rdx
xorl %esi, %esi
movq 0x18(%rdx), %rdi
movq (%rdi,%rsi,8), %rdi
shrq $0x21, %rdi
movq 0x28(%rcx), %r8
cmpb $0x5, (%r8,%rdi)
jne 0xf9ac9
movb $0x0, (%r8,%rdi)
incq %rsi
movl 0x10(%rdx), %edi
cmpq %rdi, %rsi
jb 0xf9aad
incq %rax
movl 0x28(%rsp), %edx
cmpq %rdx, %rax
jb 0xf9a8e
movl 0x2c(%rsp), %eax
orl 0x40(%rsp), %eax
je 0xf9f1b
movq 0x100(%rsp), %rdi
movq 0xe0(%rsp), %r14
movl $0x1, %eax
xorl %eax, %r12d
movq 0x38(%rsp), %rax
movl 0x14(%rax), %edx
addl %edx, %edx
movq $0x0, (%r14)
xorl %esi, %esi
callq 0x20e0
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0xf9b4c
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq 0x10(%r14), %rdx
movq %rcx, %rax
incq %rax
movq %rax, (%r14)
movl %r12d, (%rdx,%rcx,4)
je 0xf9c30
leaq -0x1(%rax), %rcx
movq %rcx, (%r14)
movq 0x10(%r14), %rcx
movl -0x4(%rcx,%rax,4), %edx
movq 0x38(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq (%rcx,%rdx,8), %rcx
movq 0x100(%rsp), %rsi
movb $0x1, (%rsi,%rdx)
testl %ecx, %ecx
je 0xf9c24
movq %rcx, %rdx
shrq $0x20, %rdx
movl %ecx, %ecx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %r12
addq $0x8, %r12
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r13
xorl %r15d, %r15d
cmpl $0x0, 0x4(%r12,%r15)
js 0xf9c1b
movl (%r12,%r15), %eax
movq 0x100(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
jne 0xf9c1b
cmpl %ebp, %eax
je 0xf9c1b
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0xf9c06
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%r12,%r15), %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movl %eax, (%rdx,%rcx,4)
addq $0x18, %r15
cmpq %r15, %r13
jne 0xf9bba
movq (%r14), %rax
testq %rax, %rax
jne 0xf9b66
movq 0x100(%rsp), %rax
movb $0x1, (%rax,%rbp)
cmpl $0x0, 0x28(%rsp)
je 0xf9f15
xorl %edx, %edx
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x50(%rsp), %rcx
movq %rdx, 0x70(%rsp)
movl (%rcx,%rdx,4), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
cmpl $0x0, 0x10(%rax,%rcx,8)
je 0xf9f00
leaq (%rax,%rcx,8), %rcx
xorl %edx, %edx
movq %rcx, 0x78(%rsp)
movq 0x18(%rcx), %rax
movq %rdx, 0x80(%rsp)
movq (%rax,%rdx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
andl $-0x2, %ecx
movq %rcx, 0x88(%rsp)
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movq 0x48(%rsp), %rcx
leaq (%rcx,%rax,8), %r15
xorl %eax, %eax
movq %rax, 0x90(%rsp)
movq %rax, %rcx
orq 0x88(%rsp), %rcx
movq 0x100(%rsp), %rax
movq %rcx, 0x98(%rsp)
cmpb $0x0, (%rax,%rcx)
je 0xf9ecf
cmpl $0x0, 0xc(%r15)
je 0xf9ecf
xorl %ebp, %ebp
xorl %r12d, %r12d
movq (%r15), %rdi
movl 0xc(%rdi,%rbp), %edx
leaq (%rdx,%rdx,2), %rcx
movl (%rbx,%rcx,4), %eax
testl %eax, %eax
js 0xf9ebb
movq 0x38(%rsp), %rsi
movq 0x20(%rsi), %rsi
cmpl $0x0, (%rsi,%rdx,8)
js 0xf9ebb
movl 0x4(%rdi,%rbp), %edx
addq %rbp, %rdi
leaq (%rdx,%rdx,2), %rdx
leaq (%rbx,%rdx,4), %rdx
leaq (%rbx,%rcx,4), %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 0x190a13(%rip), %rax    # 0x28a758
movss 0x158(%rax), %xmm0
movl 0xe8(%rsp), %ecx
movl 0xf0(%rsp), %r8d
leaq 0x58(%rsp), %r9
callq 0xa159b
testl %eax, %eax
js 0xf9ebb
movq 0x58(%rsp), %r14
shrq $0x20, %r14
cmpq 0x98(%rsp), %r14
jne 0xf9ebb
movq 0x100(%rsp), %rax
cmpb $0x0, (%rax,%r14)
je 0xf9ebb
movl 0x60(%rsp), %r13d
cmpb $0x0, (%rax,%r13)
je 0xf9ebb
movq 0x108(%rsp), %rsi
movq 0x8(%rsi), %rax
cmpq %rax, (%rsi)
jne 0xf9df2
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rsi)
movq 0x108(%rsp), %rax
movq 0x10(%rax), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x108(%rsp), %rsi
movq %rax, 0x10(%rsi)
movq (%rsi), %rax
movq 0x10(%rsi), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rsi)
leaq (%rax,%rax,2), %rax
movq 0x68(%rsp), %rdx
movq %rdx, 0x10(%rcx,%rax,8)
movups 0x58(%rsp), %xmm0
movups %xmm0, (%rcx,%rax,8)
xorl $0x1, %r13d
xorl $0x1, %r14d
leaq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
movl %r14d, (%rsp)
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
movl 0xe8(%rsp), %ecx
movl 0xf0(%rsp), %r8d
movl %r13d, %r9d
callq 0xb2ced
movq 0x108(%rsp), %rsi
movq 0x8(%rsi), %rax
cmpq %rax, (%rsi)
jne 0xf9e96
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rsi)
movq 0x108(%rsp), %rax
movq 0x10(%rax), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x108(%rsp), %rsi
movq %rax, 0x10(%rsi)
movq (%rsi), %rax
movq 0x10(%rsi), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rsi)
leaq (%rax,%rax,2), %rax
movq 0x68(%rsp), %rdx
movq %rdx, 0x10(%rcx,%rax,8)
movups 0x58(%rsp), %xmm0
movups %xmm0, (%rcx,%rax,8)
incq %r12
movl 0xc(%r15), %eax
addq $0x28, %rbp
cmpq %rax, %r12
jb 0xf9ced
movq 0x90(%rsp), %rcx
leaq 0x1(%rcx), %rax
testq %rcx, %rcx
je 0xf9cb0
movq 0x80(%rsp), %rdx
incq %rdx
movq 0x78(%rsp), %rcx
movl 0x10(%rcx), %eax
cmpq %rax, %rdx
jb 0xf9c7b
movq 0x70(%rsp), %rdx
incq %rdx
movl 0x28(%rsp), %eax
cmpq %rax, %rdx
jb 0xf9c49
movl $0x1, %r14d
movl %r14d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq