// File: /workspace/github_cpp_makefile_star2_O2/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, 0x70(%rsp)
movl %r8d, 0x5c(%rsp)
movl %ecx, 0x58(%rsp)
movq %rdx, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movq %rdi, %r12
callq 0xefea7
movsd %xmm0, 0x78(%rsp)
xorl %ebx, %ebx
movq %r12, %rdi
xorl %esi, %esi
callq 0x781bd
movq %rax, %r15
movl 0x14(%r12), %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, 0xa0(%rsp)
movapd %xmm0, 0x110(%rsp)
movapd %xmm0, 0x90(%rsp)
movq %r15, 0x48(%rsp)
movq 0x18(%r15), %r15
movl 0x14(%r15), %ebp
leal (%rbp,%rbp), %r13d
movq %r12, 0x68(%rsp)
movl 0x4(%r12), %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
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
cmpq %rbp, %rbx
jae 0xa80ba
movq 0x20(%r15), %rax
movb $0x0, 0x4(%rax,%rbx,8)
incq %rbx
movl 0x14(%r15), %ebp
jmp 0xa809d
leaq 0xa8(%rsp), %rsi
movq %r15, %rdi
callq 0x84975
xorl %ebx, %ebx
movl $0x0, %ebp
movl $0x0, %r14d
movl $0x0, %r8d
movq %rax, 0x40(%rsp)
testq %rax, %rax
je 0xa831a
xorl %r14d, %r14d
movb $0x1, %bpl
cmpq %r13, %r14
je 0xa8210
cmpb $0x0, (%r12,%r14)
je 0xa8106
incq %r14
jmp 0xa80f1
movq 0x28(%r15), %rax
movq (%rax,%r14,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xa8101
movq 0x20(%r15), %rcx
movl %r14d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xa8101
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0xa8101
movq 0x8(%r15), %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 0xa8160
movl (%rcx,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x18, %rdx
jmp 0xa814b
cmpl $0x2, %esi
jb 0xa8101
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 %r14d, %edx
movq 0x40(%rsp), %rcx
leaq 0xa8(%rsp), %r8
pushq $-0x1
popq %r9
callq 0x84a86
testq %rax, %rax
je 0xa8101
movq 0xc0(%rsp), %rax
movq 0xe0(%rsp), %rcx
movq 0xf0(%rsp), %rdx
xorl %esi, %esi
movl %esi, %esi
cmpq %rsi, %rcx
jbe 0xa81fb
movl (%rdx,%rsi,4), %edi
cmpq %rdi, %r14
je 0xa81f7
cmpl (%rax), %edi
je 0xa81f7
movq %rdi, %r8
xorq $0x1, %r8
movb %bpl, (%r12,%r8)
movb %bpl, (%r12,%rdi)
incl %esi
jmp 0xa81d5
movb $0x2, (%r12,%r14)
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%r12,%rax)
jmp 0xa8101
xorl %ebp, %ebp
xorl %r8d, %r8d
cmpq %r13, %rbp
je 0xa830a
cmpb $0x2, (%r12,%rbp)
jne 0xa8302
movq 0x28(%r15), %rax
movq (%rax,%rbp,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xa8302
movq 0x20(%r15), %rcx
movl %ebp, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xa8302
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0xa8302
movq 0x8(%r15), %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 0xa828e
movl (%rcx,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x18, %rdx
jmp 0xa8279
cmpl $0x2, %esi
jb 0xa8302
movq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0xa8(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 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 0x68(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl %ebp, %edx
movq 0x80(%rsp), %rcx
movl %r8d, %r14d
movq 0x88(%rsp), %r8
leaq 0x110(%rsp), %r9
callq 0xa5f21
movl %r14d, %r8d
addl %eax, %r8d
incq %rbp
jmp 0xa8215
movq 0x90(%rsp), %r14
movq 0xa0(%rsp), %rbp
movl %r8d, 0x40(%rsp)
movq 0x68(%rsp), %r13
movl %ebx, %ebx
cmpq %rbx, %r14
jbe 0xa834e
movq %r13, %rdi
callq 0x6c45d
imulq $0x18, %rbx, %rcx
movupd (%rbp,%rcx), %xmm0
movupd %xmm0, (%rax)
movq 0x10(%rbp,%rcx), %rcx
movq %rcx, 0x10(%rax)
incl %ebx
jmp 0xa8324
testq %r14, %r14
je 0xa8383
movq 0x28(%r13), %rdi
callq 0x2220
andq $0x0, 0x28(%r13)
andb $0x7f, 0x7(%r13)
movq %r13, %rdi
callq 0x694eb
movq %r13, %rdi
callq 0x69638
movq %r13, %rdi
movq 0x70(%rsp), %rsi
callq 0x701c4
movq 0x60(%rsp), %rdi
callq 0x2220
movq 0x120(%rsp), %rdi
callq 0x2220
movq %rbp, %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), %ebx
andl %ebp, %ebx
movq %r12, %rdi
callq 0x2220
movq 0x48(%rsp), %rdi
callq 0x6921d
movq 0x1aabd9(%rip), %rax    # 0x252fe0
movq (%rax), %r15
callq 0xefea7
subsd 0x78(%rsp), %xmm0
andl 0x4(%r13), %ebp
subl %ebx, %ebp
leaq 0x141a97(%rip), %rsi    # 0x1e9eb9
leaq 0x141ae2(%rip), %rdx    # 0x1e9f0b
movq %r15, %rdi
movl %ebp, %ecx
movl 0x40(%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