// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: reduce_hamming_error_adv(ma_ug_t*, asg_t*, ma_hit_t_alloc*, ma_sub_t*, int, int, long long, R_to_U*, bubble_type*)
void reduce_hamming_error_adv(ma_ug_t *iug, asg_t *sg, ma_hit_t_alloc* sources, ma_sub_t *coverage_cut, 
int max_hang, int min_ovlp, long long gap_fuzz, R_to_U *ru, bubble_type* bub)
{
    double index_time = yak_realtime();
    ma_ug_t *ug = NULL; ug = (iug)?(iug):(ma_ug_gen_primary(sg, PRIMARY_LABLE));
    uint8_t* vis_flag = NULL; CALLOC(vis_flag, sg->n_seq*2);
    uint32_t fix_bub = 0; asg_t *g = ug->g;
    uint32_t v, n_vtx = g->n_seq * 2, n_arc, n_arc_0 = sg->n_arc, nv, i;
    uint64_t n_pop = 0, max_dist; asg_arc_t *p;
    asg_arc_t *av; asg_t *ig = asg_init(); asg64_v sv; kv_init(sv);
    buf_t b; memset(&b, 0, sizeof(buf_t));
    b.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t));
    uint8_t* bs_flag = NULL; CALLOC(bs_flag, n_vtx);
    for (i = 0; i < ug->g->n_seq; i++) ug->g->seq[i].c = 0;
    max_dist = get_bub_pop_max_dist_advance(g, &b);
    
    if(max_dist > 0) {
        if(bub) {
            for (i = 0; i < bub->f_bub; i++) {
                get_bubbles(bub, i, &v, NULL, NULL, NULL, NULL);
                fix_bub += gen_switch_phasing(sg, ug, v, sources, coverage_cut, max_hang, min_ovlp, 
                    R_INF.trio_flag, vis_flag, &b, max_dist, 1, ig, &sv);
            }
        } else {
            for (v = 0; v < n_vtx; ++v) {
                if(bs_flag[v] != 0) continue;
                nv = asg_arc_n(g, v); av = asg_arc_a(g, v);
                ///some node could be deleted
                if (nv < 2 || g->seq[v>>1].del) continue;
                ///some edges could be deleted
                for (i = n_arc = 0; i < nv; ++i) // asg_bub_pop1() may delete some edges/arcs
                    if (!av[i].del) ++n_arc;
                if (n_arc < 2) continue;
                if(asg_bub_pop1_primary_trio(ug->g, NULL, v, max_dist, &b, (uint32_t)-1, (uint32_t)-1, 0, NULL, NULL, NULL, 0, 0, NULL)) {
                    //beg is v, end is b.S.a[0]
                    //note b.b include end, does not include beg
                    for (i = 0; i < b.b.n; i++) {
                        if(b.b.a[i]==v || b.b.a[i]==b.S.a[0]) continue;
                        bs_flag[b.b.a[i]] = bs_flag[b.b.a[i]^1] = 1;
                    }
                    bs_flag[v] = 2; bs_flag[b.S.a[0]^1] = 3;
                }
            }

            //traverse all node with two directions 
            for (v = 0; v < n_vtx; ++v) {
                if(bs_flag[v] !=2) continue;
                nv = asg_arc_n(g, v);
                av = asg_arc_a(g, v);
                ///some node could be deleted
                if (nv < 2 || g->seq[v>>1].del) continue;
                ///some edges could be deleted
                for (i = n_arc = 0; i < nv; ++i) // asg_bub_pop1() may delete some edges/arcs
                    if (!av[i].del) ++n_arc;
                if (n_arc > 1) {
                    fix_bub += gen_switch_phasing(sg, ug, v, sources, coverage_cut, max_hang, min_ovlp, 
                    R_INF.trio_flag, vis_flag, &b, max_dist, 1, ig, &sv);
                }
            }
        }
    }
    free(bs_flag); if(!iug) ma_ug_destroy(ug);    

    if(sv.n > 0) {
        ig->n_seq = ig->m_seq = sg->n_seq; 
        MALLOC(ig->seq, ig->n_seq);
        memcpy(ig->seq, sg->seq, (sizeof((*(ig->seq)))*ig->n_seq));
        asg_cleanup(ig); asg_arc_del_trans_aux(ig, sg, vis_flag, gap_fuzz);
        REALLOC(b.a, (ig->n_seq<<1)); memset(b.a, 0, sizeof((*(b.a)))*(ig->n_seq<<1));

        for (i = 0; i < sv.n; i++) rd_hamming_symm_simple0(&b, sg, ig, sv.a[i]>>32, (uint32_t)(sv.a[i]), max_dist, NULL);
        /**
        rd_hamming_t aux_t; memset((&aux_t), 0, sizeof(aux_t));
        aux_t.n_thread = 1; // aux_t.n_thread = asm_opt.thread_num; 
        CALLOC(aux_t.a, aux_t.n_thread);
        for (i = 0; i < aux_t.n_thread; i++) aux_t.a[i].a = b.a;
        aux_t.g = ig; aux_t.ref = sg; aux_t.rr = &sv; aux_t.max_dist = max_dist;
        // print_debug_gfa(ug, sg, coverage_cut, "debug_hamming", sources, ru);
        // kt_for(aux_t.n_thread, rd_hamming_symm, &aux_t, aux_t.rr->n);
        for (i = 0; i < aux_t.rr->n; i++) {
            rd_hamming_symm_simple(&aux_t, aux_t.rr->a[i]>>32, (uint32_t)(aux_t.rr->a[i]));
        }        
        for (i = 0; i < aux_t.n_thread; i++) {
            free(aux_t.a[i].S.a); free(aux_t.a[i].T.a); 
            free(aux_t.a[i].b.a); free(aux_t.a[i].e.a);
        }
        free(aux_t.a);
        **/
    }
    free(sv.a); free(vis_flag);

    for (i = n_pop = 0; i < ig->n_arc; i++) {
        if(ig->arc[i].del) continue;
        p = asg_arc_pushp(sg); *p = (ig->arc[i]); n_pop++;
    }
    if(n_pop) {
        free(sg->idx);
        sg->idx = 0;
        sg->is_srt = 0;
        asg_cleanup(sg);
        asg_symm(sg);
        asg_arc_del_trans(sg, gap_fuzz);
    }
    free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a); asg_destroy(ig);
    fprintf(stderr, "[M::%s::%.3f] # inserted edges: %u, # fixed bubbles: %u\n", 
                        __func__, yak_realtime() - index_time, sg->n_arc - n_arc_0, fix_bub);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movl %r9d, 0x40(%rsp)
movl %r8d, 0x5c(%rsp)
movq %rcx, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
callq 0x1365ef
movsd %xmm0, 0xb8(%rsp)
movq %r14, %r13
movq %r14, 0xb0(%rsp)
testq %r14, %r14
jne 0x9287
movq %rbx, %rdi
xorl %esi, %esi
callq 0xb2a6d
movq %rax, %r13
movl 0x14(%rbx), %edi
addl %edi, %edi
pushq $0x1
popq %r15
movq %r15, %rsi
callq 0x2120
movq %rax, 0x78(%rsp)
movq %r13, 0x50(%rsp)
movq 0x18(%r13), %r13
movl 0x14(%r13), %ebp
addl %ebp, %ebp
movq %rbx, 0x48(%rsp)
movl 0x4(%rbx), %ebx
pushq $0x48
popq %rsi
movq %r15, %rdi
callq 0x2120
movq %rax, 0x68(%rsp)
andq $0x0, 0xa0(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
movupd %xmm0, 0x118(%rsp)
movupd %xmm0, 0x108(%rsp)
movupd %xmm0, 0xf8(%rsp)
movupd %xmm0, 0xe8(%rsp)
movupd %xmm0, 0xd8(%rsp)
movupd %xmm0, 0xc8(%rsp)
pushq $0x1c
popq %rsi
movq %rbp, %rdi
callq 0x2120
movq %rax, 0xc0(%rsp)
movq %rbp, %r14
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2120
movq %rax, 0x60(%rsp)
testl $0x7fffffff, 0x14(%r13) # imm = 0x7FFFFFFF
je 0x935d
movq 0x20(%r13), %rax
xorl %ecx, %ecx
movb $0x0, 0x4(%rax,%rcx,8)
incq %rcx
movl 0x14(%r13), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %rcx
jb 0x9346
leaq 0xc0(%rsp), %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0xc0162
xorl %r8d, %r8d
testq %rax, %rax
movl %ebx, 0x58(%rsp)
movq %rax, 0x70(%rsp)
jne 0x93d5
movq 0x78(%rsp), %rbx
movl %r8d, 0x40(%rsp)
movq 0x60(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0xb0(%rsp)
jne 0x93aa
movq 0x50(%rsp), %rdi
callq 0xa0761
movq 0x160(%rsp), %r12
movq 0x90(%rsp), %r13
testq %r13, %r13
movl 0x58(%rsp), %ebp
jne 0x93fe
movq 0xa0(%rsp), %r15
movq 0x68(%rsp), %r12
jmp 0x94eb
movq 0x170(%rsp), %r15
testq %r15, %r15
movq 0x78(%rsp), %rbx
jne 0x96a2
movq %r14, %r10
testl %r10d, %r10d
jne 0x9757
andl $0x0, 0x44(%rsp)
jmp 0x9386
movq 0x48(%rsp), %rbp
movl 0x14(%rbp), %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq 0x68(%rsp), %r14
movl %r15d, 0x10(%r14)
movq %r13, 0x50(%rsp)
movl $0x80000000, %eax       # imm = 0x80000000
andl 0x14(%r14), %eax
orl %r15d, %eax
movl %eax, 0x14(%r14)
shlq $0x3, %r15
movq %r15, %rdi
callq 0x2280
movq %rax, 0x20(%r14)
movq 0x20(%rbp), %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x2160
movq %r14, %rdi
callq 0xa0a7d
movq %r14, %rdi
movq %rbp, %r13
movq %rbp, %rsi
movq %rbx, %rdx
movl %r12d, %ecx
callq 0xe43f9
leaq 0xc0(%rsp), %rbx
movq (%rbx), %rdi
movl 0x14(%r14), %eax
addl %eax, %eax
cltq
imulq $0x1c, %rax, %r15
movq %r15, %rsi
callq 0x2370
movq %rax, (%rbx)
xorl %ebp, %ebp
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x20e0
movq %r14, %rbx
pushq $0x1
popq %r12
movq 0xa0(%rsp), %r15
movq 0x70(%rsp), %r14
movq (%r15,%rbp,8), %r8
movq %r8, %rcx
shrq $0x20, %rcx
andq $0x0, (%rsp)
leaq 0xc0(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %r14, %r9
callq 0xe5b08
movl %r12d, %ebp
incl %r12d
cmpq %rbp, 0x50(%rsp)
ja 0x94ac
movl 0x58(%rsp), %ebp
movq %rbx, %r12
movq 0x78(%rsp), %rbx
movq %r15, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
movl 0x4(%r12), %r14d
andq $0x7fffffff, %r14       # imm = 0x7FFFFFFF
je 0x95fa
movq %r12, %rbx
movq 0x8(%r12), %r15
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x48(%rsp), %rdi
cmpl $0x0, 0xc(%r15)
js 0x959f
movl 0x4(%rdi), %edx
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl (%rdi), %ecx
jne 0x956b
leal (%rcx,%rcx), %eax
testl %ecx, %ecx
pushq $0x10
popq %rcx
cmovel %ecx, %eax
movl %eax, (%rdi)
movq 0x48(%rsp), %rcx
movq 0x8(%rcx), %rdi
imulq $0x18, %rax, %rsi
callq 0x2370
movq 0x48(%rsp), %rdi
movq %rax, 0x8(%rdi)
movl 0x4(%rdi), %edx
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jmp 0x956f
movq 0x8(%rdi), %rax
leal 0x1(%rdx), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %edx       # imm = 0x80000000
orl %esi, %edx
movl %edx, 0x4(%rdi)
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
movq 0x10(%r15), %rdx
movq %rdx, 0x10(%rax,%rcx)
movupd (%r15), %xmm0
movupd %xmm0, (%rax,%rcx)
incq %r13
incq %r12
addq $0x18, %r15
cmpq %r14, %r12
jb 0x9520
testq %r13, %r13
movq %rbx, %r12
je 0x95fa
movq 0x48(%rsp), %rbx
movq 0x28(%rbx), %rdi
callq 0x2220
andq $0x0, 0x28(%rbx)
andb $0x7f, 0x7(%rbx)
movq %rbx, %rdi
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbx)
movq %rbx, %rdi
movq 0x160(%rsp), %rsi
callq 0xa9d09
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
andl %r15d, %ebp
movq 0xc0(%rsp), %rdi
callq 0x2220
movq 0xd8(%rsp), %rdi
callq 0x2220
movq 0xf0(%rsp), %rdi
callq 0x2220
movq 0x108(%rsp), %rdi
callq 0x2220
movq 0x120(%rsp), %rdi
callq 0x2220
movq %r12, %rdi
callq 0xa0494
movq 0x28b98d(%rip), %rax    # 0x294fe0
movq (%rax), %r14
callq 0x1365ef
subsd 0xb8(%rsp), %xmm0
movq 0x48(%rsp), %rax
andl 0x4(%rax), %r15d
subl %ebp, %r15d
leaq 0x22e942(%rip), %rsi    # 0x237fb9
leaq 0x22e974(%rip), %rdx    # 0x237ff2
movq %r14, %rdi
movl %r15d, %ecx
movl 0x40(%rsp), %r8d
movb $0x1, %al
callq 0x22b0
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x80(%r15)
je 0x9386
pushq $0x1
popq %r14
xorl %esi, %esi
leaq 0x90(%rsp), %r13
xorl %r8d, %r8d
andq $0x0, (%rsp)
movq %r15, %rdi
leaq 0x44(%rsp), %rdx
xorl %ecx, %ecx
movl %r8d, %ebp
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x13df01
movl 0x44(%rsp), %edx
movq %r13, 0x38(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r12, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
movl 0x40(%rsp), %eax
movl %eax, (%rsp)
movq $0x1, 0x28(%rsp)
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x80(%rsp), %rcx
movq 0x88(%rsp), %r8
movl 0x5c(%rsp), %r9d
callq 0xe3b7a
movl %ebp, %r8d
addl %eax, %r8d
movl %r14d, %esi
incl %r14d
cmpq %rsi, 0x80(%r15)
ja 0x96c1
jmp 0x9386
xorl %ebp, %ebp
movb $0x1, %r15b
movq 0x60(%rsp), %r9
movl %ebp, %ebp
cmpb $0x0, (%r9,%rbp)
je 0x9776
incl %ebp
cmpl %r10d, %ebp
jb 0x9761
jmp 0x9879
movq 0x28(%r13), %rax
movq (%rax,%rbp,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x976a
movq 0x20(%r13), %rcx
movl %ebp, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x976a
movq 0x8(%r13), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %eax
imulq $0x18, %rdx, %rdx
addq %rcx, %rdx
addq $0xc, %rdx
xorl %ecx, %ecx
movl (%rdx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %ecx
addq $0x18, %rdx
decq %rax
jne 0x97ad
cmpl $0x2, %ecx
jb 0x976a
movq 0x50(%rsp), %rax
movq 0x18(%rax), %rdi
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
andq $0x0, 0x38(%rsp)
andl $0x0, 0x30(%rsp)
andl $0x0, 0x28(%rsp)
andq $0x0, 0x20(%rsp)
andl $0x0, 0x8(%rsp)
orl $-0x1, (%rsp)
xorl %esi, %esi
movl %ebp, %edx
movq 0x70(%rsp), %rcx
leaq 0xc0(%rsp), %r8
pushq $-0x1
popq %r9
callq 0xc02d6
movq %r14, %r10
movq 0x60(%rsp), %r9
testq %rax, %rax
je 0x976a
movq 0xd8(%rsp), %rax
movq 0xf8(%rsp), %rcx
testq %rcx, %rcx
je 0x9864
movq 0x108(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
cmpl %ebp, %edi
je 0x985a
cmpl (%rax), %edi
je 0x985a
movq %rdi, %r8
xorq $0x1, %r8
movb %r15b, (%r9,%r8)
movb %r15b, (%r9,%rdi)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rcx
ja 0x9840
movb $0x2, (%r9,%rbp)
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%r9,%rax)
jmp 0x976a
andl $0x0, 0x44(%rsp)
xorl %r8d, %r8d
testl %r10d, %r10d
je 0x9386
xorl %r8d, %r8d
xorl %r15d, %r15d
movl %r15d, %r15d
movq 0x60(%rsp), %rax
cmpb $0x2, (%rax,%r15)
jne 0x9970
movq 0x28(%r13), %rax
movq (%rax,%r15,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
movq 0x48(%rsp), %rdi
je 0x9970
movq 0x20(%r13), %rcx
movl %r15d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x9970
movq 0x8(%r13), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %eax
imulq $0x18, %rdx, %rdx
addq %rcx, %rdx
addq $0xc, %rdx
xorl %ecx, %ecx
movl (%rdx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %ecx
addq $0x18, %rdx
decq %rax
jne 0x98e8
cmpl $0x2, %ecx
jb 0x9970
leaq 0x90(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0xc0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
movl 0x40(%rsp), %eax
movl %eax, (%rsp)
movq $0x1, 0x28(%rsp)
movq 0x50(%rsp), %rsi
movq %r15, %rdx
movq 0x80(%rsp), %rcx
movl %r8d, %ebp
movq 0x88(%rsp), %r8
movl 0x5c(%rsp), %r9d
callq 0xe3b7a
movq %r14, %r10
movl %ebp, %r8d
addl %eax, %r8d
incl %r15d
movl %r15d, 0x44(%rsp)
cmpl %r10d, %r15d
jb 0x9890
jmp 0x9386