// File: /workspace/github_cpp_makefile_star2_O1/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 $0xc8, %rsp
movl %r9d, 0x38(%rsp)
movq %r8, %r14
movq %rcx, 0x40(%rsp)
movq %rsi, %r13
movq %rdi, 0x60(%rsp)
movq 0x120(%rsp), %r12
movq 0x118(%rsp), %r15
movl $0x1, %eax
movl %eax, 0x3c(%rsp)
leaq 0x54(%rsp), %rbx
movl %eax, (%rbx)
movq 0x18(%rsi), %rdi
movq %rbx, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
movq %rdx, 0x30(%rsp)
movq %r12, %rcx
movq %r15, %r8
movl $0x1, %r9d
callq 0xe1869
cmpl $0x0, (%rbx)
movq %r15, %rbx
jne 0xe2721
movq 0x18(%r13), %rdi
leaq 0x3c(%rsp), %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
movq %r13, %rsi
movq 0x30(%rsp), %rdx
movq %r12, %rcx
movq %rbx, %r8
movl $0x2, %r9d
callq 0xe1869
cmpq $0x0, 0x38(%rbx)
je 0xe2798
movq 0x30(%rsp), %rax
shrq %rax
xorl %ecx, %ecx
leaq 0x1a8af7(%rip), %rdx    # 0x28b230
movq 0x48(%rbx), %rsi
movl (%rsi,%rcx,4), %esi
shrl %esi
cmpq %rsi, %rax
je 0xe2790
movq 0x18(%rbx), %rdi
movl (%rdi), %edi
shrl %edi
cmpl %edi, %esi
je 0xe2790
movq 0x10(%r13), %rdi
leaq (%rsi,%rsi,4), %rsi
cmpl $0x0, 0x10(%rdi,%rsi,8)
je 0xe2790
leaq (%rdi,%rsi,8), %rsi
xorl %edi, %edi
movq 0x28(%rdx), %r8
movq 0x18(%rsi), %r9
movq (%r9,%rdi,8), %r9
shrq $0x21, %r9
cmpb $0x5, (%r8,%r9)
jne 0xe2784
movb $0x0, (%r8,%r9)
incq %rdi
movl 0x10(%rsi), %r8d
cmpq %r8, %rdi
jb 0xe2768
incl %ecx
cmpq %rcx, 0x38(%rbx)
ja 0xe2739
movl 0x3c(%rsp), %ecx
xorl %eax, %eax
orl 0x54(%rsp), %ecx
je 0xe2c79
movq 0x128(%rsp), %r10
movq 0x110(%rsp), %r11
movq 0x18(%rbx), %rax
movl (%rax), %eax
xorq $0x1, %rax
cmpq $0x0, 0x38(%rbx)
je 0xe2830
movq 0x30(%rsp), %rcx
shrq %rcx
xorl %edx, %edx
movq 0x48(%rbx), %rsi
movl (%rsi,%rdx,4), %esi
movl %esi, %edi
shrl %edi
cmpq %rdi, %rcx
je 0xe2828
movq 0x18(%rbx), %r8
movl (%r8), %r8d
shrl %r8d
cmpl %r8d, %edi
je 0xe2828
movq 0x10(%r13), %r8
leaq (%rdi,%rdi,4), %rdi
cmpl $0x0, 0x10(%r8,%rdi,8)
je 0xe2828
leaq (%r8,%rdi,8), %rdi
andl $0x1, %esi
xorl %r8d, %r8d
movq 0x18(%rdi), %r9
movl 0x4(%r9,%r8,8), %r9d
xorq %rsi, %r9
movb %r10b, (%r11,%r9)
incq %r8
movl 0x10(%rdi), %r9d
cmpq %r9, %r8
jb 0xe280c
incl %edx
cmpq %rdx, 0x38(%rbx)
ja 0xe27d3
xorl %ecx, %ecx
movq 0x30(%rsp), %r15
testb $0x1, %r15b
sete %cl
movq %r13, 0x78(%rsp)
movq 0x10(%r13), %rdx
shrq %r15
movq %r15, 0x30(%rsp)
leaq (%r15,%r15,4), %rsi
leaq (%rdx,%rsi,8), %rsi
movl 0x4(%rsi,%rcx,4), %ecx
movq %rcx, 0x70(%rsp)
xorl %ecx, %ecx
testb $0x1, %al
sete %cl
shrl %eax
leaq (%rax,%rax,4), %rax
leaq (%rdx,%rax,8), %rax
movl 0x4(%rax,%rcx,4), %eax
movq %rax, 0x68(%rsp)
cmpq $0x0, 0x38(%rbx)
je 0xe2c13
xorl %ecx, %ecx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq 0x48(%rbx), %rax
movq %rcx, 0x80(%rsp)
movl (%rax,%rcx,4), %eax
shrl %eax
cmpq %rax, 0x30(%rsp)
je 0xe2bf7
movq 0x118(%rsp), %rcx
movq 0x18(%rcx), %rcx
movl (%rcx), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0xe2bf7
movq 0x78(%rsp), %rcx
movq 0x10(%rcx), %rcx
leaq (%rax,%rax,4), %rax
cmpl $0x0, 0x10(%rcx,%rax,8)
je 0xe2bf7
leaq (%rcx,%rax,8), %rax
movq %rax, 0x58(%rsp)
movq $0x0, 0x48(%rsp)
xorl %r13d, %r13d
movq 0x58(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x48(%rsp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
leaq (,%rax,2), %r12
addq %r13, %r12
movq 0x110(%rsp), %rcx
movzbl (%rcx,%r12), %ecx
cmpq 0x128(%rsp), %rcx
jne 0xe2bc9
leaq (%rax,%rax,2), %rax
movq 0x40(%rsp), %rcx
cmpl $0x0, 0xc(%rcx,%rax,8)
je 0xe2bc9
movq 0x40(%rsp), %rcx
leaq (%rcx,%rax,8), %r15
movq 0x60(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq (%rcx,%r12,8), %rcx
movq %rcx, 0x90(%rsp)
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rax
addq $0x8, %rax
movq %rax, 0x88(%rsp)
xorl %ebx, %ebx
movq (%r15), %rdx
leaq (%rbx,%rbx,4), %rsi
movl 0xc(%rdx,%rsi,8), %edi
leaq (%rdi,%rdi,2), %rcx
movl (%r14,%rcx,4), %eax
testl %eax, %eax
js 0xe2bb9
movq 0x60(%rsp), %r8
movq 0x20(%r8), %r8
cmpl $0x0, (%r8,%rdi,8)
js 0xe2bb9
leaq (%rdx,%rsi,8), %rdi
movl 0x4(%rdi), %edx
leaq (%rdx,%rdx,2), %rdx
leaq (%r14,%rdx,4), %rdx
leaq (%r14,%rcx,4), %rcx
movl 0x4(%rdx), %esi
movl (%rdx), %edx
andl %ebp, %edx
subl %edx, %esi
movl 0x4(%rcx), %edx
subl %eax, %edx
leaq 0x1a7d88(%rip), %rax    # 0x28a758
movss 0x158(%rax), %xmm0
movl 0x38(%rsp), %ecx
movl 0x100(%rsp), %r8d
leaq 0x98(%rsp), %r9
callq 0xa159b
testl %eax, %eax
js 0xe2bb9
movq 0x98(%rsp), %rax
shrq $0x20, %rax
cmpq %r12, %rax
jne 0xe2bb9
movq 0x110(%rsp), %rcx
movzbl (%rcx,%rax), %ecx
cmpq 0x128(%rsp), %rcx
jne 0xe2bb9
movl 0xa0(%rsp), %r9d
movq 0x110(%rsp), %rcx
movzbl (%rcx,%r9), %ecx
cmpq 0x128(%rsp), %rcx
jne 0xe2bb9
movq 0x88(%rsp), %rcx
movq 0x90(%rsp), %rsi
movl %esi, %edx
testl %esi, %esi
je 0xe2a72
cmpl %r9d, (%rcx)
je 0xe2bb9
addq $0x18, %rcx
decl %edx
jne 0xe2a61
xorl $0x1, %r9d
xorl $0x1, %eax
leaq 0xb0(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movl %eax, (%rsp)
movq 0x40(%rsp), %rdi
movq %r14, %rsi
movl 0x38(%rsp), %ecx
movl 0x100(%rsp), %r8d
callq 0xb2ced
testl %eax, %eax
je 0xe2bb9
movq 0x130(%rsp), %rdi
movl 0x4(%rdi), %ecx
andl %ebp, %ecx
movl (%rdi), %eax
cmpl %eax, %ecx
jne 0xe2af1
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x10, %eax
cmovel %eax, %ecx
movl %ecx, (%rdi)
movq 0x130(%rsp), %rax
movq 0x8(%rax), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x130(%rsp), %rdi
movq %rax, 0x8(%rdi)
movq 0x8(%rdi), %rax
movl 0x4(%rdi), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leal 0x1(%rcx), %esi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %esi
andl $0x80000000, %ecx       # imm = 0x80000000
orl %esi, %ecx
movl %ecx, 0x4(%rdi)
leaq (%rdx,%rdx,2), %rcx
movq 0xa8(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx,8)
movups 0x98(%rsp), %xmm0
movups %xmm0, (%rax,%rcx,8)
movl 0x4(%rdi), %ecx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
andl %r8d, %ecx
movl (%rdi), %eax
cmpl %eax, %ecx
jne 0xe2b79
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x10, %eax
cmovel %eax, %ecx
movl %ecx, (%rdi)
movq 0x130(%rsp), %rax
movq 0x8(%rax), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x130(%rsp), %rdi
movq %rax, 0x8(%rdi)
movq 0x8(%rdi), %rax
movl 0x4(%rdi), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leal 0x1(%rcx), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %esi, %ecx
movl %ecx, 0x4(%rdi)
leaq (%rdx,%rdx,2), %rcx
movq 0xc0(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx,8)
movups 0xb0(%rsp), %xmm0
movups %xmm0, (%rax,%rcx,8)
incq %rbx
movl 0xc(%r15), %eax
cmpq %rax, %rbx
jb 0xe2979
leaq 0x1(%r13), %rax
testq %r13, %r13
movq %rax, %r13
je 0xe28ed
movq 0x48(%rsp), %rdx
incq %rdx
movq 0x58(%rsp), %rax
movl 0x10(%rax), %eax
movq %rdx, 0x48(%rsp)
cmpq %rax, %rdx
jb 0xe28ea
movq 0x80(%rsp), %rcx
incl %ecx
movq 0x118(%rsp), %rbx
cmpq %rcx, 0x38(%rbx)
ja 0xe288c
movq 0x70(%rsp), %r14
xorq $0x1, %r14
movq 0x68(%rsp), %r15
xorq $0x1, %r15
movq 0x138(%rsp), %rbx
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0xe2c5b
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
shlq $0x20, %r14
orq %r15, %r14
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movq %r14, (%rcx,%rax,8)
movl $0x1, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq