// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: reduce_hamming_error(asg_t*, ma_hit_t_alloc*, ma_sub_t*, int, int, long long)
void reduce_hamming_error(asg_t *sg, ma_hit_t_alloc* sources, ma_sub_t *coverage_cut, 
int max_hang, int min_ovlp, long long gap_fuzz)
{
    double index_time = yak_realtime();
    ma_ug_t *ug = NULL;
    ug = ma_ug_gen_primary(sg, PRIMARY_LABLE);

    uint8_t* vis_flag = NULL; CALLOC(vis_flag, sg->n_seq*2);
    uint32_t fix_bub = 0;
    kvec_t_u32_warp stack; kv_init(stack.a);
    kv_asg_arc_t e; kv_init(e);
    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 *av = NULL;
    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)
    {
        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 || g->seq[v>>1].c == ALTER_LABLE) 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 || g->seq[v>>1].c == ALTER_LABLE) 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 += bub_complex_hamming(sg, ug, v, sources, coverage_cut, &stack, 
                max_hang, min_ovlp, R_INF.trio_flag, vis_flag, &e, &b, max_dist);
            }
        }
    }

    asg_arc_t* p = NULL;
    for (i = 0; i < e.n; i++)
    {
        p = asg_arc_pushp(sg);
        *p = e.a[i];
    }
    if(e.n != 0)
    {
        free(sg->idx);
        sg->idx = 0;
        sg->is_srt = 0;
        asg_cleanup(sg);
        asg_symm(sg);
        asg_arc_del_trans(sg, gap_fuzz);
    }

    free(vis_flag);
    kv_destroy(stack.a);
    kv_destroy(e);
    
    free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a);
    if (n_pop) asg_cleanup(g);
    free(bs_flag);
    ma_ug_destroy(ug);    

    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 $0x138, %rsp            # imm = 0x138
movq %r9, 0x88(%rsp)
movl %r8d, 0x5c(%rsp)
movl %ecx, 0x58(%rsp)
movq %rdx, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
movq %rdi, %rbx
callq 0x1365ef
movsd %xmm0, 0x90(%rsp)
movl $0x0, 0x44(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0xb2a6d
movq %rax, %r15
movl 0x14(%rbx), %edi
addl %edi, %edi
pushq $0x1
popq %r14
movq %r14, %rsi
callq 0x2120
movq %rax, 0x60(%rsp)
andq $0x0, 0x120(%rsp)
xorpd %xmm0, %xmm0
andq $0x0, 0x80(%rsp)
movapd %xmm0, 0x110(%rsp)
movapd %xmm0, 0x70(%rsp)
movq %r15, 0x48(%rsp)
movq 0x18(%r15), %r15
movl 0x14(%r15), %ebp
leal (%rbp,%rbp), %r13d
movl 0x4(%rbx), %eax
movl %eax, 0x54(%rsp)
movupd %xmm0, 0x100(%rsp)
movupd %xmm0, 0xf0(%rsp)
movupd %xmm0, 0xe0(%rsp)
movupd %xmm0, 0xd0(%rsp)
movupd %xmm0, 0xc0(%rsp)
movupd %xmm0, 0xb0(%rsp)
pushq $0x1c
popq %rsi
movq %r13, %rdi
callq 0x2120
movq %rax, 0xa8(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x2120
movq %rax, %r12
testl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
je 0xc4a2
movq 0x20(%r15), %rax
xorl %ecx, %ecx
movb $0x0, 0x4(%rax,%rcx,8)
incq %rcx
movl 0x14(%r15), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %rcx
jb 0xc48b
leaq 0xa8(%rsp), %rsi
movq %r15, %rdi
callq 0xc0162
movq %rax, 0x68(%rsp)
testq %rax, %rax
je 0xc7b7
testq %r13, %r13
je 0xc7b7
xorl %ebp, %ebp
movb $0x1, %r14b
cmpb $0x0, (%r12,%rbp)
je 0xc4e2
incq %rbp
cmpq %r13, %rbp
jne 0xc4ce
jmp 0xc5e5
movq 0x28(%r15), %rax
movq (%rax,%rbp,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xc4d5
movq 0x20(%r15), %rcx
movl %ebp, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xc4d5
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0xc4d5
movq 0x8(%r15), %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 0xc520
cmpl $0x2, %ecx
jb 0xc4d5
movq 0x48(%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 0x68(%rsp), %rcx
leaq 0xa8(%rsp), %r8
pushq $-0x1
popq %r9
callq 0xc02d6
testq %rax, %rax
je 0xc4d5
movq 0xc0(%rsp), %rax
movq 0xe0(%rsp), %rcx
testq %rcx, %rcx
je 0xc5d0
movq 0xf0(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
cmpq %rdi, %rbp
je 0xc5c6
cmpl (%rax), %edi
je 0xc5c6
movq %rdi, %r8
xorq $0x1, %r8
movb %r14b, (%r12,%r8)
movb %r14b, (%r12,%rdi)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rcx
ja 0xc5ab
movb $0x2, (%r12,%rbp)
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%r12,%rax)
jmp 0xc4d5
movl $0x0, 0x44(%rsp)
testl %r13d, %r13d
je 0xc7b7
xorl %ebp, %ebp
leaq 0x70(%rsp), %r14
movl $0x0, 0x44(%rsp)
cmpb $0x2, (%r12,%rbp)
jne 0xc6d1
movq 0x28(%r15), %rax
movq (%rax,%rbp,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xc6d1
movq 0x20(%r15), %rcx
movl %ebp, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xc6d1
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0xc6d1
movq 0x8(%r15), %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 0xc65a
cmpl $0x2, %ecx
jb 0xc6d1
movq 0x68(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0xa8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r14, 0x20(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x58(%rsp), %eax
movl %eax, (%rsp)
movq %rbx, %rdi
movq 0x48(%rsp), %rsi
movl %ebp, %edx
movq 0x98(%rsp), %rcx
movq 0xa0(%rsp), %r8
leaq 0x110(%rsp), %r9
callq 0xe33a7
addl %eax, 0x44(%rsp)
incq %rbp
cmpq %r13, %rbp
jne 0xc605
movq 0x70(%rsp), %r14
testq %r14, %r14
je 0xc7b7
pushq $0x1
popq %rbp
movq 0x80(%rsp), %r15
xorl %r13d, %r13d
movl 0x4(%rbx), %edx
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl (%rbx), %ecx
jne 0xc733
leal (%rcx,%rcx), %eax
testl %ecx, %ecx
pushq $0x10
popq %rcx
cmovel %ecx, %eax
movl %eax, (%rbx)
movq 0x8(%rbx), %rdi
imulq $0x18, %rax, %rsi
callq 0x2370
movq %rax, 0x8(%rbx)
movl 0x4(%rbx), %edx
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jmp 0xc737
movq 0x8(%rbx), %rax
leal 0x1(%rdx), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %edx       # imm = 0x80000000
orl %esi, %edx
movl %edx, 0x4(%rbx)
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
imulq $0x18, %r13, %rdx
movq 0x10(%r15,%rdx), %rsi
movq %rsi, 0x10(%rax,%rcx)
movupd (%r15,%rdx), %xmm0
movupd %xmm0, (%rax,%rcx)
movl %ebp, %r13d
incl %ebp
cmpq %r13, %r14
ja 0xc6f9
testq %r14, %r14
je 0xc7b7
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 0x88(%rsp), %rsi
callq 0xa9d09
movq 0x60(%rsp), %rdi
callq 0x2220
movq 0x120(%rsp), %rdi
callq 0x2220
movq 0x80(%rsp), %rdi
callq 0x2220
movq 0xa8(%rsp), %rdi
callq 0x2220
movq 0xc0(%rsp), %rdi
callq 0x2220
movq 0xd8(%rsp), %rdi
callq 0x2220
movq 0xf0(%rsp), %rdi
callq 0x2220
movq 0x108(%rsp), %rdi
callq 0x2220
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl 0x54(%rsp), %r15d
andl %ebp, %r15d
movq %r12, %rdi
callq 0x2220
movq 0x48(%rsp), %rdi
callq 0xa0761
movq 0x28879e(%rip), %rax    # 0x294fe0
movq (%rax), %r14
callq 0x1365ef
subsd 0x90(%rsp), %xmm0
andl 0x4(%rbx), %ebp
subl %r15d, %ebp
leaq 0x22b759(%rip), %rsi    # 0x237fb9
leaq 0x22b7a4(%rip), %rdx    # 0x23800b
movq %r14, %rdi
movl %ebp, %ecx
movl 0x44(%rsp), %r8d
movb $0x1, %al
callq 0x22b0
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq