// File: /workspace/github_cpp_makefile_star2_O2/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 $0x118, %rsp            # imm = 0x118
movl %r9d, 0x44(%rsp)
movl %r8d, 0x48(%rsp)
movq %rcx, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movq %rsi, %rbx
movq %rdi, %r13
callq 0xefea7
movsd %xmm0, 0xa8(%rsp)
movq %r13, %r14
testq %r13, %r13
jne 0x8f17b
movq %rbx, %rdi
xorl %esi, %esi
callq 0x781bd
movq %rax, %r14
movl 0x14(%rbx), %edi
addl %edi, %edi
pushq $0x1
popq %r15
movq %r15, %rsi
callq 0x2120
movq %rax, 0x50(%rsp)
movq %r14, 0x58(%rsp)
movq 0x18(%r14), %r12
movl 0x14(%r12), %ebp
addl %ebp, %ebp
movl 0x4(%rbx), %eax
movl %eax, 0x74(%rsp)
callq 0x68f5d
movq %rax, %r14
andq $0x0, 0xa0(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
movupd %xmm0, 0x108(%rsp)
movupd %xmm0, 0xf8(%rsp)
movupd %xmm0, 0xe8(%rsp)
movupd %xmm0, 0xd8(%rsp)
movupd %xmm0, 0xc8(%rsp)
movupd %xmm0, 0xb8(%rsp)
pushq $0x1c
popq %rsi
movq %rbp, %rdi
callq 0x2120
movq %rax, 0xb0(%rsp)
movq %rbp, 0x68(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2120
movq %rax, %rbp
xorl %eax, %eax
movl 0x14(%r12), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rax
jae 0x8f243
movq 0x20(%r12), %rcx
movb $0x0, 0x4(%rcx,%rax,8)
incq %rax
jmp 0x8f224
leaq 0xb0(%rsp), %rsi
movq %r12, %rdi
callq 0x84975
xorl %r9d, %r9d
movq %rax, 0x60(%rsp)
testq %rax, %rax
je 0x8f56f
movq %r13, 0x78(%rsp)
movq 0x160(%rsp), %rcx
testq %rcx, %rcx
jne 0x8f3ac
xorl %r15d, %r15d
movq 0x68(%rsp), %r10
cmpl %r10d, %r15d
jae 0x8f466
movl %r15d, %r13d
cmpb $0x0, (%rbp,%r13)
je 0x8f29b
incl %r15d
jmp 0x8f282
movq 0x28(%r12), %rax
movq (%rax,%r13,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x8f296
movq 0x20(%r12), %rcx
movl %r15d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x8f296
movq 0x8(%r12), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %eax
imulq $0x18, %rax, %rax
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
addq $0xc, %rcx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdx, %rax
je 0x8f2f1
movl (%rcx,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x18, %rdx
jmp 0x8f2dc
cmpl $0x2, %esi
jb 0x8f296
movq 0x58(%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 %r15d, %edx
movq 0x60(%rsp), %rcx
leaq 0xb0(%rsp), %r8
pushq $-0x1
popq %r9
callq 0x84a86
movb $0x1, %r9b
movq 0x68(%rsp), %r10
testq %rax, %rax
je 0x8f296
movq 0xc8(%rsp), %rax
movq 0xe8(%rsp), %rcx
movq 0xf8(%rsp), %rdx
xorl %esi, %esi
movl %esi, %esi
cmpq %rsi, %rcx
jbe 0x8f396
movl (%rdx,%rsi,4), %edi
cmpl %r15d, %edi
je 0x8f392
cmpl (%rax), %edi
je 0x8f392
movq %rdi, %r8
xorq $0x1, %r8
movb %r9b, (%rbp,%r8)
movb %r9b, (%rbp,%rdi)
incl %esi
jmp 0x8f36e
movb $0x2, (%rbp,%r13)
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%rbp,%rax)
jmp 0x8f296
xorl %eax, %eax
leaq 0x90(%rsp), %r13
xorl %r9d, %r9d
movl %eax, %r15d
cmpq %r15, 0x80(%rcx)
jbe 0x8f56a
andq $0x0, (%rsp)
movq 0x160(%rsp), %rdi
movq %r15, %rsi
leaq 0x4c(%rsp), %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %r9d, %r12d
xorl %r9d, %r9d
callq 0xf6ab5
movl 0x4c(%rsp), %edx
movq %r13, 0x38(%rsp)
movq %r14, 0x30(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0xb0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x44(%rsp), %eax
movl %eax, (%rsp)
movq $0x1, 0x28(%rsp)
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
movq 0x80(%rsp), %rcx
movq 0x88(%rsp), %r8
movl 0x48(%rsp), %r9d
callq 0xa64a9
movq 0x160(%rsp), %rcx
movl %r12d, %r9d
addl %eax, %r9d
leal 0x1(%r15), %eax
jmp 0x8f3b9
xorl %r15d, %r15d
xorl %r9d, %r9d
movl %r15d, 0x4c(%rsp)
cmpl %r10d, %r15d
jae 0x8f56a
movl %r15d, %edx
cmpb $0x2, (%rbp,%rdx)
jne 0x8f562
movq 0x28(%r12), %rax
movq (%rax,%rdx,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x8f562
movq 0x20(%r12), %rcx
movl %r15d, %esi
shrl %esi
cmpl $0x0, (%rcx,%rsi,8)
js 0x8f562
movq 0x8(%r12), %rcx
movq %rax, %rsi
shrq $0x20, %rsi
movl %eax, %eax
imulq $0x18, %rax, %rax
imulq $0x18, %rsi, %rsi
addq %rsi, %rcx
addq $0xc, %rcx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rax
je 0x8f4ea
movl (%rcx,%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %edi
addq $0x18, %rsi
jmp 0x8f4d1
cmpl $0x2, %edi
jb 0x8f562
leaq 0x90(%rsp), %rax
movq %rax, 0x38(%rsp)
movq %r14, 0x30(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0xb0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x44(%rsp), %eax
movl %eax, (%rsp)
movq $0x1, 0x28(%rsp)
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
movq 0x80(%rsp), %rcx
movq 0x88(%rsp), %r8
movl %r9d, %r13d
movl 0x48(%rsp), %r9d
callq 0xa64a9
movq 0x68(%rsp), %r10
movl %r13d, %r9d
addl %eax, %r9d
incl %r15d
jmp 0x8f46c
movq 0x78(%rsp), %r13
movl %r9d, 0x44(%rsp)
movq %rbp, %rdi
callq 0x2220
testq %r13, %r13
jne 0x8f58b
movq 0x58(%rsp), %rdi
callq 0x6921d
movq 0x90(%rsp), %r13
testq %r13, %r13
jne 0x8f6e0
movq 0xa0(%rsp), %r12
movq %r12, %rdi
callq 0x2220
movq 0x50(%rsp), %rdi
callq 0x2220
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
movl 0x4(%r14), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r13
jae 0x8f604
movq 0x8(%r14), %rax
cmpl $0x0, 0xc(%rax,%r15)
js 0x8f5fb
movq %rbx, %rdi
callq 0x6c45d
movq 0x8(%r14), %rcx
movupd (%rcx,%r15), %xmm0
movupd %xmm0, (%rax)
movq 0x10(%rcx,%r15), %rcx
movq %rcx, 0x10(%rax)
incq %r12
incq %r13
addq $0x18, %r15
jmp 0x8f5bf
testq %r12, %r12
je 0x8f63b
movq 0x28(%rbx), %rdi
callq 0x2220
andq $0x0, 0x28(%rbx)
andb $0x7f, 0x7(%rbx)
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
movq %rbx, %rdi
movq 0x150(%rsp), %rsi
callq 0x701c4
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl 0x74(%rsp), %r15d
andl %ebp, %r15d
movq 0xb0(%rsp), %rdi
callq 0x2220
movq 0xc8(%rsp), %rdi
callq 0x2220
movq 0xe0(%rsp), %rdi
callq 0x2220
movq 0xf8(%rsp), %rdi
callq 0x2220
movq 0x110(%rsp), %rdi
callq 0x2220
movq %r14, %rdi
callq 0x68f68
movq 0x1c3948(%rip), %rax    # 0x252fe0
movq (%rax), %r14
callq 0xefea7
subsd 0xa8(%rsp), %xmm0
andl 0x4(%rbx), %ebp
subl %r15d, %ebp
leaq 0x15a803(%rip), %rsi    # 0x1e9eb9
leaq 0x15a835(%rip), %rdx    # 0x1e9ef2
movq %r14, %rdi
movl %ebp, %ecx
movl 0x44(%rsp), %r8d
movb $0x1, %al
callq 0x22b0
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x14(%rbx), %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl %r15d, 0x10(%r14)
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(%rbx), %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x2160
movq %r14, %rdi
callq 0x694eb
movq %r14, %rdi
movq %rbx, %rsi
movq 0x50(%rsp), %rdx
movq 0x150(%rsp), %rcx
callq 0xa69d6
leaq 0xb0(%rsp), %r15
movq (%r15), %rdi
movl 0x14(%r14), %eax
addl %eax, %eax
cltq
imulq $0x1c, %rax, %rsi
callq 0x2370
movq %rax, (%r15)
movl 0x14(%r14), %ecx
addl %ecx, %ecx
movslq %ecx, %rcx
imulq $0x1c, %rcx, %rdx
xorl %ebp, %ebp
movq %rax, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0xa0(%rsp), %r12
movl %ebp, %ebp
cmpq %rbp, %r13
jbe 0x8f5a4
movq (%r12,%rbp,8), %r8
movq %r8, %rcx
shrq $0x20, %rcx
andq $0x0, (%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq 0x60(%rsp), %r9
callq 0xa7c6c
incl %ebp
jmp 0x8f77e