// File: /workspace/github_cpp_makefile_star2_O3/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 $0xd8, %rsp
movq %r8, 0x90(%rsp)
movq %rsi, %rax
movq %rdi, 0x60(%rsp)
movl %ecx, %esi
xorl %r15d, %r15d
cmpq %rsi, 0x80(%rdx)
jbe 0xfd6f9
movq %rax, 0x58(%rsp)
movq %r9, 0x48(%rsp)
xorl %r15d, %r15d
leaq 0xa0(%rsp), %r8
movq %r15, (%r8)
movq %r15, (%rsp)
leaq 0x74(%rsp), %r14
leaq 0xd4(%rsp), %r13
leaq 0x44(%rsp), %r9
movq %rdx, %rdi
movq %r14, %rdx
movq %r13, %rcx
callq 0x13df01
movl (%r14), %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rdx
sete %cl
movl (%r13), %eax
cmpl $-0x1, %eax
sete %sil
orb %cl, %sil
jne 0xfd6f9
movq 0x148(%rsp), %r13
movq 0x140(%rsp), %r14
movl %eax, %eax
xorl %ecx, %ecx
testb $0x1, %dl
sete %cl
movq 0x58(%rsp), %rsi
movq 0x10(%rsi), %r8
movl %edx, %edi
shrl %edi
leaq (%rdi,%rdi,4), %rdi
leaq (%r8,%rdi,8), %rdi
movl 0x4(%rdi,%rcx,4), %ebp
movl $0x1, %r9d
movl %eax, %ecx
shrl %ecx
movl %eax, %edi
shrl %edi
testb $0x1, %al
leaq (%rcx,%rcx,4), %rax
leaq 0x4(%r8,%rax,8), %rax
leaq (%rdi,%rdi,4), %rcx
leaq 0x8(%r8,%rcx,8), %rcx
cmovneq %rax, %rcx
movl (%rcx), %r12d
movl %r9d, 0x54(%rsp)
leaq 0x70(%rsp), %rbx
movl %r9d, (%rbx)
movq 0x18(%rsi), %rdi
movq %rbx, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
movq %r13, %rcx
movq %r14, %r8
callq 0xe2bf7
cmpl $0x0, (%rbx)
jne 0xfd088
movq 0x58(%rsp), %rsi
movq 0x18(%rsi), %rdi
movl 0x74(%rsp), %edx
leaq 0x54(%rsp), %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
movq %r13, %rcx
movq %r14, %r8
movl $0x2, %r9d
callq 0xe2bf7
movl 0x44(%rsp), %esi
testl %esi, %esi
movq 0x58(%rsp), %r10
je 0xfd0ff
xorl %eax, %eax
leaq 0x199192(%rip), %rcx    # 0x296230
movq 0x10(%r10), %rdi
movq 0xa0(%rsp), %rdx
movl (%rdx,%rax,4), %edx
shrl %edx
leaq (%rdx,%rdx,4), %r8
movl 0x10(%rdi,%r8,8), %edx
testl %edx, %edx
je 0xfd0f5
leaq (%rdi,%r8,8), %rsi
movq 0x28(%rcx), %rdi
xorl %r8d, %r8d
movq 0x18(%rsi), %r9
movq (%r9,%r8,8), %r9
shrq $0x21, %r9
cmpb $0x5, (%rdi,%r9)
jne 0xfd0e6
movb $0x0, (%rdi,%r9)
movq 0x28(%rcx), %rdi
movl 0x10(%rsi), %edx
incq %r8
movl %edx, %r9d
cmpq %r9, %r8
jb 0xfd0c7
movl 0x44(%rsp), %esi
incq %rax
movl %esi, %edx
cmpq %rdx, %rax
jb 0xfd09e
movl 0x54(%rsp), %eax
orl 0x70(%rsp), %eax
je 0xfd6f9
movq 0x130(%rsp), %rdi
movq 0x110(%rsp), %r14
movl $0x1, %eax
xorl %eax, %ebp
movq 0x60(%rsp), %rax
movl 0x14(%rax), %edx
addl %edx, %edx
movq $0x0, (%r14)
xorl %esi, %esi
callq 0x20e0
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0xfd16f
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
jmp 0xfd173
movq 0x10(%r14), %rax
movq %rcx, %rdx
incq %rdx
movq %rdx, (%r14)
movl %ebp, (%rax,%rcx,4)
je 0xfd251
movl $0x2, %ebx
leaq -0x1(%rdx), %rax
movq %rax, (%r14)
movq 0x10(%r14), %rax
movl -0x4(%rax,%rdx,4), %edx
movq 0x60(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq (%rcx,%rdx,8), %rcx
movq 0x130(%rsp), %rsi
movb $0x1, (%rsi,%rdx)
testl %ecx, %ecx
je 0xfd245
movq %rcx, %rdx
shrq $0x20, %rdx
movl %ecx, %ecx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %r13
addq $0x8, %r13
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rbp
xorl %r15d, %r15d
cmpl $0x0, 0x4(%r13,%r15)
js 0xfd23c
movl (%r13,%r15), %ecx
movq 0x130(%rsp), %rax
cmpb $0x0, (%rax,%rcx)
jne 0xfd23c
cmpl %r12d, %ecx
je 0xfd23c
movq (%r14), %rdx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rdx
jne 0xfd232
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %rbx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%r13,%r15), %ecx
movq (%r14), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r14)
movl %ecx, (%rax,%rdx,4)
addq $0x18, %r15
cmpq %r15, %rbp
jne 0xfd1de
movq (%r14), %rdx
testq %rdx, %rdx
jne 0xfd18a
movq 0x130(%rsp), %rax
movb $0x1, (%rax,%r12)
movl 0x44(%rsp), %eax
testl %eax, %eax
movq 0x48(%rsp), %r14
je 0xfd6f3
xorl %esi, %esi
movq 0x58(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq 0xa0(%rsp), %rdx
movl (%rdx,%rsi,4), %edx
shrl %edx
leaq (%rdx,%rdx,4), %rdx
cmpl $0x0, 0x10(%rcx,%rdx,8)
je 0xfd6e5
movq %rsi, 0xa8(%rsp)
leaq (%rcx,%rdx,8), %rcx
xorl %edx, %edx
movq %rcx, 0xb0(%rsp)
movq 0x18(%rcx), %rax
movq %rdx, 0xb8(%rsp)
movq (%rax,%rdx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
andl $-0x2, %ecx
movq %rcx, 0xc0(%rsp)
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movq 0x90(%rsp), %rcx
leaq (%rcx,%rax,8), %rbx
movb $0x1, %al
xorl %ecx, %ecx
movq %rbx, 0x38(%rsp)
movl %eax, 0x6c(%rsp)
orq 0xc0(%rsp), %rcx
movq 0x130(%rsp), %rax
movq %rcx, 0x98(%rsp)
cmpb $0x0, (%rax,%rcx)
je 0xfd6a5
movl 0xc(%rbx), %r8d
testl %r8d, %r8d
je 0xfd6a5
movq 0x98(%rsp), %rax
xorl $0x1, %eax
movl %eax, 0x68(%rsp)
movl $0x21, %r15d
xorl %r12d, %r12d
movq (%rbx), %rcx
movl -0x15(%rcx,%r15), %esi
leaq (%rsi,%rsi,2), %rdx
movl (%r14,%rdx,4), %eax
testl %eax, %eax
js 0xfd465
movq 0x60(%rsp), %rdi
movq 0x20(%rdi), %rdi
cmpl $0x0, (%rdi,%rsi,8)
js 0xfd465
movl %r8d, 0x34(%rsp)
leaq (%r14,%rdx,4), %rdx
movl 0x4(%rdx), %r9d
subl %eax, %r9d
movq -0x9(%rcx,%r15), %rax
movq %rax, 0xc8(%rsp)
testl %eax, %eax
js 0xfd39b
movl -0x11(%rcx,%r15), %eax
movl -0xd(%rcx,%r15), %r11d
subl %r11d, %r9d
movl %r9d, %r10d
movl %eax, 0x50(%rsp)
movl %eax, %r9d
jmp 0xfd3ad
movl -0x11(%rcx,%r15), %r10d
movl -0xd(%rcx,%r15), %r11d
subl %r11d, %r9d
movl %r10d, 0x50(%rsp)
movq -0x21(%rcx,%r15), %rdi
movq %rdi, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%r14,%rax,4), %rdx
movl 0x4(%rdx), %eax
movl (%rdx), %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edx
subl %edx, %eax
cmpl %edi, %r9d
movl %edi, %edx
cmovll %r9d, %edx
movl -0x19(%rcx,%r15), %ebp
movl %eax, %r8d
subl %ebp, %r8d
cmpl %r10d, %r8d
movl %r10d, %ebx
cmovll %r8d, %ebx
movl 0x118(%rsp), %r13d
cmpl %r13d, %edx
jg 0xfd45b
cmpl %r13d, %ebx
jg 0xfd45b
movl %ebp, %r13d
subl %edi, %r13d
cvtsi2ss %r13, %xmm1
addl %edx, %r13d
addl %ebx, %r13d
xorps %xmm2, %xmm2
cvtsi2ss %r13, %xmm2
leaq 0x198337(%rip), %r14    # 0x295758
movss 0x158(%r14), %xmm0
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0xfd456
subl 0x50(%rsp), %r11d
xorps %xmm1, %xmm1
cvtsi2ss %r11, %xmm1
addl %edx, %ebx
addl %r11d, %ebx
xorps %xmm2, %xmm2
cvtsi2ss %rbx, %xmm2
mulss %xmm2, %xmm0
ucomiss %xmm1, %xmm0
jbe 0xfd47d
movq 0x48(%rsp), %r14
movq 0x38(%rsp), %rbx
movl 0x34(%rsp), %r8d
incq %r12
movl %r8d, %eax
addq $0x28, %r15
cmpq %rax, %r12
jb 0xfd336
jmp 0xfd6a5
cmpl %r10d, %r8d
setl %dl
setg %r11b
cmpl %r9d, %edi
setg %r14b
setl %r8b
orb %r11b, %r14b
cmpb $0x1, %r14b
jne 0xfd456
orb %dl, %r8b
je 0xfd456
movl 0x120(%rsp), %edx
cmpl %edx, %r13d
jl 0xfd456
cmpl %edx, %ebx
movq 0x48(%rsp), %r14
jl 0xfd45b
movq 0xc8(%rsp), %rbx
testl %ebx, %ebx
setns %dl
addl %ebp, %r10d
movl %eax, %r8d
subl %r10d, %r8d
xorl %r11d, %r11d
movl %edi, %r10d
subl %r9d, %r10d
setle %r11b
cmovlel %r8d, %r10d
setg %r8b
xorb %dl, %r8b
shrq $0x1f, %rdi
andl $-0x2, %edi
orq %r11, %rdi
addl %esi, %esi
movzbl %r8b, %r13d
orq %rsi, %r13
movq %rdi, %rdx
shlq $0x20, %rdx
addq %r10, %rdx
movq %rdx, 0x78(%rsp)
movl %r13d, 0x80(%rsp)
subl %r10d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, 0x84(%rsp)
movzwl 0x88(%rsp), %eax
shrl $0x10, %ebx
andl $0x4000, %ebx           # imm = 0x4000
andl $0x3fff, %eax           # imm = 0x3FFF
orl %ebx, %eax
movb -0x1(%rcx,%r15), %dl
movb %dl, 0x8a(%rsp)
movzbl (%rcx,%r15), %ecx
shll $0xf, %ecx
orl %eax, %ecx
movw %cx, 0x88(%rsp)
testl %r10d, %r10d
movq 0x38(%rsp), %rbx
js 0xfd460
movq 0x98(%rsp), %rcx
cmpq %rcx, %rdi
movl 0x34(%rsp), %r8d
jne 0xfd465
movq 0x130(%rsp), %rax
cmpb $0x0, (%rax,%rcx)
je 0xfd465
cmpb $0x0, (%rax,%r13)
je 0xfd465
movq 0x138(%rsp), %rsi
movq (%rsi), %rcx
movq 0x10(%rsi), %rax
cmpq 0x8(%rsi), %rcx
jne 0xfd5db
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rsi)
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rsi
movq %rax, %rdi
callq 0x2370
movq 0x138(%rsp), %rsi
movq %rax, 0x10(%rsi)
movq (%rsi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rsi)
leaq (%rcx,%rcx,2), %rcx
movq 0x88(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx,8)
movups 0x78(%rsp), %xmm0
movups %xmm0, (%rax,%rcx,8)
xorl $0x1, %r13d
leaq 0x78(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x68(%rsp), %eax
movl %eax, (%rsp)
movq 0x90(%rsp), %rdi
movq %r14, %rsi
movl 0x118(%rsp), %ecx
movl 0x120(%rsp), %r8d
movl %r13d, %r9d
callq 0xb3df3
movq 0x138(%rsp), %rsi
movq (%rsi), %rcx
movq 0x10(%rsi), %rax
cmpq 0x8(%rsi), %rcx
jne 0xfd67b
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rsi)
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rsi
movq %rax, %rdi
callq 0x2370
movq 0x138(%rsp), %rsi
movq %rax, 0x10(%rsi)
movq (%rsi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rsi)
leaq (%rcx,%rcx,2), %rcx
movq 0x88(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx,8)
movups 0x78(%rsp), %xmm0
movups %xmm0, (%rax,%rcx,8)
movl 0xc(%rbx), %r8d
jmp 0xfd465
movl $0x1, %ecx
testb $0x1, 0x6c(%rsp)
movl $0x0, %eax
jne 0xfd2eb
movq 0xb8(%rsp), %rdx
incq %rdx
movq 0xb0(%rsp), %rcx
movl 0x10(%rcx), %eax
cmpq %rax, %rdx
jb 0xfd2ac
movl 0x44(%rsp), %eax
movq 0xa8(%rsp), %rsi
incq %rsi
movl %eax, %ecx
cmpq %rcx, %rsi
jb 0xfd271
movl $0x1, %r15d
movl %r15d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq