// File: /workspace/github_cpp_makefile_star2_O1/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 $0x148, %rsp            # imm = 0x148
movq %r9, 0x90(%rsp)
movl %r8d, 0x64(%rsp)
movl %ecx, 0x60(%rsp)
movq %rdx, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdi, %rbx
callq 0x1303c7
movsd %xmm0, 0x98(%rsp)
movl $0x0, 0x44(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0xb1a83
movq %rax, %r15
movl 0x14(%rbx), %edi
addl %edi, %edi
pushq $0x1
popq %r14
movq %r14, %rsi
callq 0x2120
movq %rax, %rbp
xorpd %xmm0, %xmm0
movapd %xmm0, 0x120(%rsp)
andq $0x0, 0x130(%rsp)
movapd %xmm0, 0x70(%rsp)
andq $0x0, 0x80(%rsp)
movq %r15, 0x50(%rsp)
movq 0x18(%r15), %r15
movl 0x14(%r15), %r13d
addl %r13d, %r13d
movl 0x4(%rbx), %eax
movl %eax, 0x5c(%rsp)
movapd %xmm0, 0x100(%rsp)
movapd %xmm0, 0xf0(%rsp)
movapd %xmm0, 0xe0(%rsp)
movapd %xmm0, 0xd0(%rsp)
movapd %xmm0, 0xc0(%rsp)
movapd %xmm0, 0xb0(%rsp)
andq $0x0, 0x110(%rsp)
pushq $0x1c
popq %rsi
movq %r13, %rdi
callq 0x2120
movq %rax, 0xb0(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x2120
movq %rax, 0x48(%rsp)
testl $0x7fffffff, 0x14(%r15) # imm = 0x7FFFFFFF
je 0xc525
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 0xc50e
leaq 0xb0(%rsp), %rsi
movq %r15, %rdi
callq 0xbf22d
movq %rax, %r12
testq %rax, %rax
sete %al
testl %r13d, %r13d
sete %cl
orb %al, %cl
movq 0x48(%rsp), %r9
movq %rbp, 0x68(%rsp)
jne 0xc788
movq %r13, 0x88(%rsp)
movl %r13d, %r13d
xorl %ebp, %ebp
movb $0x1, %r14b
cmpb $0x0, (%r9,%rbp)
je 0xc57a
incq %rbp
cmpq %r13, %rbp
jne 0xc566
jmp 0xc688
movq 0x28(%r15), %rax
movq (%rax,%rbp,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xc56d
movq 0x20(%r15), %rcx
movl %ebp, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xc56d
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0xc56d
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 0xc5b8
cmpl $0x2, %ecx
jb 0xc56d
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 %r12, %rcx
leaq 0xb0(%rsp), %r8
pushq $-0x1
popq %r9
callq 0xbf39f
movq 0x48(%rsp), %r9
testq %rax, %rax
je 0xc56d
movq 0xe8(%rsp), %rax
testq %rax, %rax
je 0xc66b
movq 0xc8(%rsp), %rcx
movq 0xf8(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
cmpq %rdi, %rbp
je 0xc661
cmpl (%rcx), %edi
je 0xc661
movq %rdi, %r8
xorq $0x1, %r8
movb %r14b, (%r9,%r8)
movb %r14b, (%r9,%rdi)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rax
ja 0xc646
movb $0x2, (%r9,%rbp)
movq 0xc8(%rsp), %rax
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%r9,%rax)
jmp 0xc56d
movl $0x0, 0x44(%rsp)
cmpl $0x0, 0x88(%rsp)
movq 0x68(%rsp), %rbp
je 0xc788
xorl %r14d, %r14d
movl $0x0, 0x44(%rsp)
cmpb $0x2, (%r9,%r14)
jne 0xc77c
movq 0x28(%r15), %rax
movq (%rax,%r14,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xc77c
movq 0x20(%r15), %rcx
movl %r14d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xc77c
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0xc77c
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 0xc704
cmpl $0x2, %ecx
jb 0xc77c
movq %r12, 0x30(%rsp)
leaq 0xb0(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x70(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x60(%rsp), %eax
movl %eax, (%rsp)
movq %rbx, %rdi
movq 0x50(%rsp), %rsi
movl %r14d, %edx
movq 0xa0(%rsp), %rcx
movq 0xa8(%rsp), %r8
leaq 0x120(%rsp), %r9
callq 0xe2084
movq 0x48(%rsp), %r9
addl %eax, 0x44(%rsp)
incq %r14
cmpq %r13, %r14
jne 0xc6ae
movq 0x70(%rsp), %r14
testq %r14, %r14
je 0xc862
pushq $0x1
popq %r15
movq 0x80(%rsp), %r13
xorl %r12d, %r12d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl 0x4(%rbx), %ecx
andl %ebp, %ecx
movl (%rbx), %eax
cmpl %eax, %ecx
jne 0xc7d3
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
imulq $0x18, %r12, %rdx
movq 0x10(%r13,%rdx), %rsi
movq %rsi, 0x10(%rax,%rcx)
movupd (%r13,%rdx), %xmm0
movupd %xmm0, (%rax,%rcx)
movl %r15d, %r12d
incl %r15d
cmpq %r12, %r14
ja 0xc7aa
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 0x90(%rsp), %rsi
callq 0xa8c8f
movq 0x68(%rsp), %rbp
movq %rbp, %rdi
callq 0x2220
movq 0x130(%rsp), %rdi
callq 0x2220
movq 0x80(%rsp), %rdi
callq 0x2220
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
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl 0x5c(%rsp), %ebp
andl %r14d, %ebp
movq 0x48(%rsp), %rdi
callq 0x2220
movq 0x50(%rsp), %rdi
callq 0xa0004
movq 0x27d6f3(%rip), %rax    # 0x289fe0
movq (%rax), %r15
callq 0x1303c7
subsd 0x98(%rsp), %xmm0
andl 0x4(%rbx), %r14d
subl %ebp, %r14d
leaq 0x21f4bd(%rip), %rsi    # 0x22bdc9
leaq 0x21f508(%rip), %rdx    # 0x22be1b
movq %r15, %rdi
movl %r14d, %ecx
movl 0x44(%rsp), %r8d
movb $0x1, %al
callq 0x22b0
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq