// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: gen_switch_phasing(asg_t*, ma_ug_t*, unsigned long, ma_hit_t_alloc*, ma_sub_t*, int, int, unsigned char*, unsigned char*, buf_t*, unsigned long, unsigned long, asg_t*, asg64_v*)
int gen_switch_phasing(asg_t *sg, ma_ug_t *ug, uint64_t bi, 
ma_hit_t_alloc* src, ma_sub_t *cov, int32_t max_hang, 
int32_t min_ovlp, uint8_t* trio_flag, uint8_t* vis_r_flag, 
buf_t *b, uint64_t tLen, uint64_t vis_f, asg_t *res, asg64_v *sv)
{
    uint32_t k_i, k_j, k_v, rev, z, zn; asg_arc_t *za; 
    ma_utg_t* nsu = NULL; int sw0, sw1;

    sw0 = sw1 = 1;
    asg_bub_pop1_primary_trio_switch_check(ug->g, ug, bi, tLen, b, FATHER, DROP, 0, NULL, NULL, &sw0);
    if(sw0 == 0) {
        asg_bub_pop1_primary_trio_switch_check(ug->g, ug, bi, tLen, b, MOTHER, DROP, 0, NULL, NULL, &sw1);
    } 

    for (k_i = 0; k_i < b->b.n; k_i++) {
        if((b->b.a[k_i]>>1)==(bi>>1) || (b->b.a[k_i]>>1)==(b->S.a[0]>>1)) continue;
        nsu = &(ug->u.a[b->b.a[k_i]>>1]);
        for (k_j = 0; k_j < nsu->n; k_j++) {
            if(R_INF.trio_flag[nsu->a[k_j]>>33] == DROP) R_INF.trio_flag[nsu->a[k_j]>>33] = AMBIGU;
        }
    }
    if(sw0 == 0 && sw1 == 0) return 0;

    uint64_t i, ei = b->S.a[0]^1, v, bv, ev;
    for (k_i = 0; k_i < b->b.n; k_i++) {
        if((b->b.a[k_i]>>1)==(bi>>1) || (b->b.a[k_i]>>1)==(b->S.a[0]>>1)) continue;
        nsu = &(ug->u.a[b->b.a[k_i]>>1]); rev = b->b.a[k_i]&1;
        for (k_j = 0; k_j < nsu->n; k_j++) {
            v = nsu->a[k_j]>>32; if(rev) v ^= 1;
            vis_r_flag[v] = vis_f;
        }
    }
    bv = (bi&1)?(ug->u.a[bi>>1].start^1):(ug->u.a[bi>>1].end^1); //vis_r_flag[bv] = vis_f;
    ev = (ei&1)?(ug->u.a[ei>>1].start^1):(ug->u.a[ei>>1].end^1); //vis_r_flag[ev] = vis_f;

    ma_hit_t_alloc* x = NULL;
    ma_hit_t *h; ma_sub_t *sq, *st;
    int32_t r; asg_arc_t t0, t1, *p;

    for (k_i = 0; k_i < b->b.n; k_i++) {
        if((b->b.a[k_i]>>1)==(bi>>1) || (b->b.a[k_i]>>1)==(b->S.a[0]>>1)) continue;
        nsu = &(ug->u.a[b->b.a[k_i]>>1]);
        for (k_j = 0; k_j < nsu->n; k_j++) {
            for (k_v = 0; k_v < 2; k_v++) {
                v = ((nsu->a[k_j]>>33)<<1) + k_v;
                if(vis_r_flag[v] != vis_f) continue;;
                x = &(src[v>>1]); 
                za = asg_arc_a(sg, v); 
                zn = asg_arc_n(sg, v); 
                for (i = 0; i < x->length; i++) {
                    h = &(x->buffer[i]);
                    // if(!(h->el)) continue;
                    sq = &(cov[Get_qn(*h)]); st = &(cov[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, &t0);
                
                    ///if it is a contained read, skip
                    if(r < 0) continue;
                    if((t0.ul>>32) != v) continue;
                    if((vis_r_flag[t0.ul>>32] != vis_f) || (vis_r_flag[t0.v] != vis_f)) continue;
                    for (z = 0; (z < zn) && (za[z].v != t0.v); z++);
                    if(z < zn) continue;
                    if(get_edge_from_source(src, cov, NULL, max_hang, min_ovlp, (t0.v^1), ((t0.ul>>32)^1), &t1)) {
                        p = asg_arc_pushp(res); *p = t0;
                        p = asg_arc_pushp(res); *p = t1;
                    }
                }

            }
        }
    }
    kv_push(uint64_t, *sv, (bv<<32)|(ev));
    return 1;
}

int asg_arc_del_trans_aux(asg_t *g, asg_t *aux, uint8_t *mark, int fuzz)
{
    uint32_t v, w, n_vtx = g->n_seq * 2, n_reduced = 0;
    uint32_t L, i, j, nv0, nv1, kv; asg_arc_t *av0, *av1;
    asg_arc_t *aw0, *aw1; uint32_t nw0, nw1; 
    memset(mark, 0, sizeof((*mark))*n_vtx);

    for (v = 0; v < n_vtx; ++v) {
        if (g->seq[v>>1].del) continue;
        nv0 = asg_arc_n(g, v); av0 = asg_arc_a(g, v); 
        nv1 = asg_arc_n(aux, v); av1 = asg_arc_a(aux, v);
        if (nv0 + nv1 == 0) continue; 
        //all outnode of v should be set to "not reduce"
        for (i = kv = 0; i < nv0; ++i) {
            if(av0[i].del) continue;
            mark[av0[i].v] = 1; kv++;
        }
        for (i = 0; i < nv1; ++i) {
            if(av1[i].del) continue;
            mark[av1[i].v] = 1; kv++;
        }
        if(kv == 0) continue; 

        ///av[nv-1] is longest out-dege
        L = 0;
        if(nv0) L = asg_arc_len(av0[nv0-1]);
        if(nv1 && L < asg_arc_len(av1[nv1-1])) L = asg_arc_len(av1[nv1-1]);
        L += fuzz;
        for (i = 0; i < nv0; ++i) {
            //w is an out-node of v
            w = av0[i].v; if (mark[w] != 1) continue; ///w has already been reduced
            nw0 = asg_arc_n(g, w); aw0 = asg_arc_a(g, w);
            nw1 = asg_arc_n(aux, w); aw1 = asg_arc_a(aux, w);

            for (j = 0; j < nw0 && asg_arc_len(aw0[j]) + asg_arc_len(av0[i]) <= L; ++j)
                if (mark[aw0[j].v]) mark[aw0[j].v] = 2;
            for (j = 0; j < nw1 && asg_arc_len(aw1[j]) + asg_arc_len(av0[i]) <= L; ++j)
                if (mark[aw1[j].v]) mark[aw1[j].v] = 2;
        }
        //remove edges
        for (i = 0; i < nv0; ++i) {
            if (mark[av0[i].v] == 2) {
                av0[i].del = 1; ++n_reduced;
            }
            mark[av0[i].v] = 0;
        }
    }

    if (n_reduced) {
        asg_cleanup(g);
        asg_symm(g);
    }

    return n_reduced;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %r9d, 0x30(%rsp)
movq %r8, %r14
movq %rcx, 0x58(%rsp)
movq %rdx, %r12
movq %rdi, 0x68(%rsp)
movq 0x110(%rsp), %rbx
movq 0x108(%rsp), %r8
pushq $0x1
popq %r9
movl %r9d, 0x34(%rsp)
leaq 0x44(%rsp), %r15
movl %r9d, (%r15)
movq 0x18(%rsi), %rdi
movq %r15, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
andl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
movq %rsi, 0x28(%rsp)
movq %rbx, %rcx
callq 0xa58d0
cmpl $0x0, (%r15)
jne 0xa655e
movq 0x28(%rsp), %rsi
movq 0x18(%rsi), %rdi
leaq 0x34(%rsp), %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
andl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
pushq $0x2
popq %r9
movl %r12d, %edx
movq %rbx, %rcx
movq 0x108(%rsp), %r8
callq 0xa58d0
movq %r12, %rax
shrq %rax
movq %rax, 0x38(%rsp)
xorl %eax, %eax
leaq 0x1adcbe(%rip), %rdx    # 0x254230
movl %eax, %eax
movq 0x108(%rsp), %rsi
movq 0x38(%rsi), %rcx
cmpq %rax, %rcx
jbe 0xa65dd
movq 0x48(%rsi), %rcx
movl (%rcx,%rax,4), %ecx
shrl %ecx
cmpq %rcx, 0x38(%rsp)
je 0xa65a1
movq 0x18(%rsi), %rsi
movl (%rsi), %esi
shrl %esi
cmpl %esi, %ecx
jne 0xa65a5
incl %eax
jmp 0xa6572
imulq $0x28, %rcx, %rcx
movq 0x28(%rsp), %rsi
addq 0x10(%rsi), %rcx
xorl %esi, %esi
movl 0x10(%rcx), %edi
cmpq %rdi, %rsi
jae 0xa65a1
movq 0x28(%rdx), %rdi
movq 0x18(%rcx), %r8
movq (%r8,%rsi,8), %r8
shrq $0x21, %r8
cmpb $0x5, (%rdi,%r8)
jne 0xa65d8
movb $0x0, (%rdi,%r8)
incq %rsi
jmp 0xa65b4
movl 0x34(%rsp), %edx
xorl %eax, %eax
orl 0x44(%rsp), %edx
je 0xa69c4
movq 0x108(%rsp), %rax
movq 0x18(%rax), %rax
movl (%rax), %eax
xorq $0x1, %rax
xorl %edx, %edx
movl %edx, %edx
cmpq %rdx, %rcx
jbe 0xa6681
movq 0x108(%rsp), %r8
movq 0x48(%r8), %rsi
movl (%rsi,%rdx,4), %esi
movl %esi, %edi
shrl %edi
cmpq %rdi, 0x38(%rsp)
je 0xa667d
movq 0x18(%r8), %r8
movl (%r8), %r8d
shrl %r8d
cmpl %r8d, %edi
je 0xa667d
imulq $0x28, %rdi, %rcx
movq 0x28(%rsp), %rdi
addq 0x10(%rdi), %rcx
andl $0x1, %esi
xorl %edi, %edi
movl 0x10(%rcx), %r8d
cmpq %r8, %rdi
jae 0xa6671
movq 0x18(%rcx), %r8
movl 0x4(%r8,%rdi,8), %r8d
xorq %rsi, %r8
movq 0x118(%rsp), %r9
movq 0x100(%rsp), %r10
movb %r9b, (%r10,%r8)
incq %rdi
jmp 0xa6643
movq 0x108(%rsp), %rcx
movq 0x38(%rcx), %rcx
incl %edx
jmp 0xa6601
xorl %edx, %edx
testb $0x1, %r12b
sete %dl
movq 0x28(%rsp), %rsi
movq 0x10(%rsi), %rsi
imulq $0x28, 0x38(%rsp), %rdi
addq %rsi, %rdi
movl 0x4(%rdi,%rdx,4), %edx
xorq $0x1, %rdx
movq %rdx, 0x50(%rsp)
xorl %edx, %edx
testb $0x1, %al
sete %dl
shrl %eax
imulq $0x28, %rax, %rax
addq %rsi, %rax
movl 0x4(%rax,%rdx,4), %eax
xorq $0x1, %rax
movq %rax, 0x48(%rsp)
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, %rcx
movq 0x108(%rsp), %rdx
jbe 0xa6966
movq %rax, %rsi
movq 0x48(%rdx), %rax
movq %rsi, 0x70(%rsp)
movl (%rax,%rsi,4), %eax
shrl %eax
cmpq %rax, 0x38(%rsp)
je 0xa695a
movq 0x18(%rdx), %rdx
movl (%rdx), %edx
shrl %edx
cmpl %edx, %eax
je 0xa695a
imulq $0x28, %rax, %rcx
movq 0x28(%rsp), %rax
addq 0x10(%rax), %rcx
movq %rcx, 0x60(%rsp)
xorl %ecx, %ecx
movq 0x60(%rsp), %rax
movl 0x10(%rax), %eax
cmpq %rax, %rcx
jae 0xa694e
xorl %edx, %edx
movq %rcx, 0x78(%rsp)
cmpq $0x2, %rdx
je 0xa6946
movq 0x60(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rdx, 0x80(%rsp)
leaq (%rdx,%rax,2), %rbx
movq 0x100(%rsp), %rcx
movzbl (%rcx,%rbx), %ecx
cmpq 0x118(%rsp), %rcx
jne 0xa6931
imulq $0x18, %rax, %r12
addq 0x58(%rsp), %r12
movq 0x68(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq (%rcx,%rbx,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
movl %ebx, %esi
xorl $0x1, %esi
movl %esi, 0x40(%rsp)
imulq $0x18, %rdx, %rbp
addq $0x18, %rbp
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %r15
addq $0x8, %r15
xorl %r13d, %r13d
movl 0xc(%r12), %eax
cmpq %rax, %r13
jae 0xa6931
movq (%r12), %rdx
imulq $0x28, %r13, %rsi
movl 0xc(%rdx,%rsi), %edi
imulq $0xc, %rdi, %rcx
movl (%r14,%rcx), %eax
testl %eax, %eax
js 0xa6929
movq 0x68(%rsp), %r8
movq 0x20(%r8), %r8
cmpl $0x0, (%r8,%rdi,8)
js 0xa6929
leaq (%rdx,%rsi), %rdi
movl 0x4(%rdx,%rsi), %edx
imulq $0xc, %rdx, %rdx
addq %r14, %rdx
addq %r14, %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 0x1acf38(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl 0x30(%rsp), %ecx
movl 0xf0(%rsp), %r8d
leaq 0x88(%rsp), %r9
callq 0x6a4a7
testl %eax, %eax
js 0xa6929
movl 0x8c(%rsp), %eax
cmpq %rbx, %rax
jne 0xa6929
movq 0x100(%rsp), %rax
movzbl (%rax,%rbx), %eax
cmpq 0x118(%rsp), %rax
jne 0xa6929
movl 0x90(%rsp), %r9d
movq 0x100(%rsp), %rax
movzbl (%rax,%r9), %eax
cmpq 0x118(%rsp), %rax
jne 0xa6929
xorl %eax, %eax
leaq 0x18(%rax), %rcx
cmpq %rcx, %rbp
je 0xa68ac
cmpl %r9d, (%r15,%rax)
movq %rcx, %rax
jne 0xa6898
jmp 0xa6929
xorl $0x1, %r9d
leaq 0xa0(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x40(%rsp), %eax
movl %eax, (%rsp)
movq 0x58(%rsp), %rdi
movq %r14, %rsi
movl 0x30(%rsp), %ecx
movl 0xf0(%rsp), %r8d
callq 0x79204
testl %eax, %eax
je 0xa6929
movq 0x120(%rsp), %rdi
callq 0x6c45d
movups 0x88(%rsp), %xmm0
movups %xmm0, (%rax)
movq 0x98(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq 0x120(%rsp), %rdi
callq 0x6c45d
movups 0xa0(%rsp), %xmm0
movups %xmm0, (%rax)
movq 0xb0(%rsp), %rcx
movq %rcx, 0x10(%rax)
incq %r13
jmp 0xa67b4
movq 0x80(%rsp), %rdx
incq %rdx
movq 0x78(%rsp), %rcx
jmp 0xa6733
incq %rcx
jmp 0xa671b
movq 0x108(%rsp), %rax
movq 0x38(%rax), %rcx
movq 0x70(%rsp), %rax
incl %eax
jmp 0xa66c8
movq 0x128(%rsp), %rbx
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0xa699f
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
pushq $0x2
popq %rsi
cmovneq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
jmp 0xa69a3
movq 0x10(%rbx), %rax
pushq $0x1
popq %rsi
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rdx
shlq $0x20, %rdi
orq %rdx, %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %rdi, (%rax,%rcx,8)
movl %esi, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq