// File: /workspace/github_cpp_makefile_star2_O1/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, 0x44(%rsp)
movl %r8d, 0x54(%rsp)
movq %rcx, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
callq 0x1303c7
movsd %xmm0, 0xb8(%rsp)
movq %r14, %r12
movq %r14, 0xb0(%rsp)
testq %r14, %r14
jne 0x92db
movq %rbx, %rdi
xorl %esi, %esi
callq 0xb1a83
movq %rax, %r12
movl 0x14(%rbx), %edi
addl %edi, %edi
pushq $0x1
popq %r15
movq %r15, %rsi
callq 0x2120
movq %rax, 0x58(%rsp)
movq 0x18(%r12), %rax
movq %rax, %rbp
movl 0x14(%rax), %r14d
addl %r14d, %r14d
movq %rbx, 0x48(%rsp)
movl 0x4(%rbx), %r13d
pushq $0x48
popq %rsi
movq %r15, %rdi
callq 0x2120
movq %rax, 0x68(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
andq $0x0, 0xa0(%rsp)
movapd %xmm0, 0x110(%rsp)
movapd %xmm0, 0x100(%rsp)
movapd %xmm0, 0xf0(%rsp)
movapd %xmm0, 0xe0(%rsp)
movapd %xmm0, 0xd0(%rsp)
movapd %xmm0, 0xc0(%rsp)
andq $0x0, 0x120(%rsp)
pushq $0x1c
popq %rsi
movq %r14, %rdi
callq 0x2120
movq %rax, 0xc0(%rsp)
movq %r14, 0x60(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2120
movq %rax, %r15
movq %r12, %rbx
movq 0x18(%r12), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
je 0x93c1
movq 0x20(%rax), %rcx
xorl %edx, %edx
movb $0x0, 0x4(%rcx,%rdx,8)
incq %rdx
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpq %rsi, %rdx
jb 0x93ab
leaq 0xc0(%rsp), %rsi
movq %rbp, %rdi
callq 0xbf22d
xorl %r12d, %r12d
testq %rax, %rax
movq %r15, 0x78(%rsp)
movl %r13d, 0x74(%rsp)
movq %rax, %r14
jne 0x96b7
movl %r12d, 0x44(%rsp)
movq 0x68(%rsp), %r13
movq 0x78(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0xb0(%rsp)
jne 0x9411
movq %rbx, %rdi
callq 0xa0004
movq 0x90(%rsp), %r15
testq %r15, %r15
je 0x9500
movq %r14, %rbx
movq 0x48(%rsp), %r12
movl 0x14(%r12), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl %r14d, 0x10(%r13)
movl $0x80000000, %eax       # imm = 0x80000000
andl 0x14(%r13), %eax
orl %r14d, %eax
movl %eax, 0x14(%r13)
shlq $0x3, %r14
movq %r14, %rdi
callq 0x2280
movq %rax, 0x20(%r13)
movq 0x20(%r12), %rsi
movq %rax, %rdi
movq %r14, %rdx
callq 0x2160
movq %r13, %rdi
callq 0xa02fa
movq %r13, %rdi
movq %r12, %rsi
movq 0x58(%rsp), %rdx
movq 0x160(%rsp), %rcx
callq 0xe2c8b
leaq 0xc0(%rsp), %r14
movq (%r14), %rdi
movl 0x14(%r13), %eax
addl %eax, %eax
cltq
imulq $0x1c, %rax, %rsi
callq 0x2370
movq %rax, (%r14)
movl 0x14(%r13), %ecx
addl %ecx, %ecx
movslq %ecx, %rcx
imulq $0x1c, %rcx, %rdx
xorl %r14d, %r14d
movq %rax, %rdi
xorl %esi, %esi
callq 0x20e0
pushq $0x1
popq %rbp
movq 0xa0(%rsp), %r12
movq (%r12,%r14,8), %r8
movq %r8, %rcx
shrq $0x20, %rcx
andq $0x0, (%rsp)
leaq 0xc0(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq %r13, %rdx
movq %rbx, %r9
callq 0xe4391
movl %ebp, %r14d
incl %ebp
cmpq %r14, %r15
ja 0x94ce
movq 0xa0(%rsp), %rdi
callq 0x2220
movq 0x58(%rsp), %rdi
callq 0x2220
testl $0x7fffffff, 0x4(%r13)  # imm = 0x7FFFFFFF
movq %r13, %r14
movq 0x48(%rsp), %rbx
je 0x9610
xorl %r15d, %r15d
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x8(%r14), %rax
cmpl $0x0, 0xc(%rax,%r15)
js 0x95b4
movl 0x4(%rbx), %ecx
andl %r12d, %ecx
movl (%rbx), %eax
cmpl %eax, %ecx
jne 0x9571
leal (%rax,%rax), %ecx
testl %eax, %eax
pushq $0x10
popq %rax
cmovel %eax, %ecx
movl %ecx, (%rbx)
movq 0x8(%rbx), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movq %rax, 0x8(%rbx)
movq 0x8(%rbx), %rax
movl 0x4(%rbx), %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(%rbx)
imulq $0x18, %rdx, %rcx
movq 0x8(%r14), %rdx
movupd (%rdx,%r15), %xmm0
movupd %xmm0, (%rax,%rcx)
movq 0x10(%rdx,%r15), %rdx
movq %rdx, 0x10(%rax,%rcx)
incq %r13
incq %rbp
movl 0x4(%r14), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq $0x18, %r15
cmpq %rax, %rbp
jb 0x953b
testq %r13, %r13
je 0x9610
movq 0x28(%rbx), %rdi
callq 0x2220
andq $0x0, 0x28(%rbx)
andb $0x7f, 0x7(%rbx)
movq %rbx, %rdi
callq 0xa02fa
movq %rbx, %rdi
callq 0xa0333
movq %rbx, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%rbx)
movq %rbx, %rdi
movq 0x160(%rsp), %rsi
callq 0xa8c8f
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl 0x74(%rsp), %ebp
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 %r14, %rdi
callq 0x9fcdb
movq 0x280973(%rip), %rax    # 0x289fe0
movq (%rax), %r14
callq 0x1303c7
subsd 0xb8(%rsp), %xmm0
andl 0x4(%rbx), %r15d
subl %ebp, %r15d
leaq 0x22273d(%rip), %rsi    # 0x22bdc9
leaq 0x22276f(%rip), %rdx    # 0x22be02
movq %r14, %rdi
movl %r15d, %ecx
movl 0x44(%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
movq 0x170(%rsp), %r13
testq %r13, %r13
jne 0x97da
andl $0x0, 0x40(%rsp)
movq 0x60(%rsp), %r10
testl %r10d, %r10d
jne 0x988c
andl $0x0, 0x40(%rsp)
testl %r10d, %r10d
je 0x93ea
xorl %r12d, %r12d
leaq 0x90(%rsp), %r13
xorl %eax, %eax
movl %eax, %edx
movq 0x78(%rsp), %rax
cmpb $0x2, (%rax,%rdx)
jne 0x97c2
movq 0x28(%rbp), %rax
movq (%rax,%rdx,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x97c2
movq 0x20(%rbp), %rcx
movl %edx, %esi
shrl %esi
cmpl $0x0, (%rcx,%rsi,8)
js 0x97c2
movq 0x8(%rbp), %rcx
movq %rax, %rsi
shrq $0x20, %rsi
movl %eax, %eax
imulq $0x18, %rsi, %rsi
addq %rcx, %rsi
addq $0xc, %rsi
xorl %ecx, %ecx
movl (%rsi), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %ecx
addq $0x18, %rsi
decq %rax
jne 0x9746
cmpl $0x2, %ecx
jb 0x97c2
movq %r13, 0x38(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x30(%rsp)
movq %r14, 0x20(%rsp)
leaq 0xc0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x44(%rsp), %eax
movl %eax, (%rsp)
movq $0x1, 0x28(%rsp)
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
movq 0x80(%rsp), %rcx
movq 0x88(%rsp), %r8
movl 0x54(%rsp), %r9d
callq 0xe265f
movq 0x60(%rsp), %r10
addl %eax, %r12d
movl 0x40(%rsp), %eax
incl %eax
movl %eax, 0x40(%rsp)
cmpl %r10d, %eax
jb 0x96f6
jmp 0x93ea
cmpq $0x0, 0x80(%r13)
je 0x93ea
pushq $0x1
popq %rbp
xorl %esi, %esi
leaq 0x90(%rsp), %r15
xorl %r12d, %r12d
andq $0x0, (%rsp)
movq %r13, %rdi
leaq 0x40(%rsp), %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x13812e
movl 0x40(%rsp), %edx
movq %r15, 0x38(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x30(%rsp)
movq %r14, 0x20(%rsp)
leaq 0xc0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x44(%rsp), %eax
movl %eax, (%rsp)
movq $0x1, 0x28(%rsp)
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
movq 0x80(%rsp), %rcx
movq 0x88(%rsp), %r8
movl 0x54(%rsp), %r9d
callq 0xe265f
addl %eax, %r12d
movl %ebp, %esi
incl %ebp
cmpq %rsi, 0x80(%r13)
ja 0x97f8
jmp 0x93ea
xorl %eax, %eax
movb $0x1, %r13b
movl %eax, %edx
cmpb $0x0, (%r15,%rdx)
je 0x98ae
movl 0x40(%rsp), %eax
incl %eax
movl %eax, 0x40(%rsp)
cmpl %r10d, %eax
jb 0x9891
jmp 0x96db
movq 0x28(%rbp), %rax
movq (%rax,%rdx,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x989a
movq 0x20(%rbp), %rcx
movl %edx, %esi
shrl %esi
cmpl $0x0, (%rcx,%rsi,8)
js 0x989a
movq 0x8(%rbp), %rcx
movq %rax, %rsi
shrq $0x20, %rsi
movl %eax, %eax
imulq $0x18, %rsi, %rsi
addq %rcx, %rsi
addq $0xc, %rsi
xorl %ecx, %ecx
movl (%rsi), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %ecx
addq $0x18, %rsi
decq %rax
jne 0x98e5
cmpl $0x2, %ecx
jb 0x989a
movq 0x18(%rbx), %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
movq %r14, %rcx
leaq 0xc0(%rsp), %r8
pushq $-0x1
popq %r9
callq 0xbf39f
movq 0x60(%rsp), %r10
testq %rax, %rax
je 0x989a
movq 0xf8(%rsp), %rax
testq %rax, %rax
je 0x9998
movq 0xd8(%rsp), %rcx
movq 0x108(%rsp), %rdx
movl 0x40(%rsp), %esi
xorl %edi, %edi
movl (%rdx,%rdi,4), %r8d
cmpl %esi, %r8d
je 0x998d
cmpl (%rcx), %r8d
je 0x998d
movq %r8, %r9
xorq $0x1, %r9
movb %r13b, (%r15,%r9)
movb %r13b, (%r15,%r8)
incq %rdi
movl %edi, %r8d
cmpq %r8, %rax
ja 0x9970
movl 0x40(%rsp), %eax
movb $0x2, (%r15,%rax)
movq 0xd8(%rsp), %rax
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%r15,%rax)
jmp 0x989a