// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movl %ecx, -0x3c(%rbp)
movl %r8d, -0x40(%rbp)
movq %r9, -0x48(%rbp)
callq 0x1b7ca0
movsd %xmm0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x28(%rbp), %rdi
xorl %esi, %esi
callq 0xe7ae0
movq %rax, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x28(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x2140
movq %rax, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0xb0(%rbp)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0xb8(%rbp)
movq $0x0, -0xc8(%rbp)
movq $0x0, -0xd8(%rbp)
leaq -0x140(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
movl -0xb0(%rbp), %eax
movl %eax, %edi
movl $0x1c, %esi
callq 0x2140
movq %rax, -0x140(%rbp)
movq $0x0, -0x148(%rbp)
movl -0xb0(%rbp), %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2140
movq %rax, -0x148(%rbp)
movl $0x0, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
movq -0x58(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x139fff
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0xc0(%rbp), %ecx
movb $0x0, 0x4(%rax,%rcx,8)
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x139fbc
movq -0xa8(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0xfde50
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0xd0(%rbp)
jbe 0x13a48f
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpl -0xb0(%rbp), %eax
jae 0x13a2c4
movq -0x148(%rbp), %rax
movl -0xac(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x13a05e
jmp 0x13a2b0
movq -0xa8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0xac(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0xbc(%rbp)
movq -0xa8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0xa8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0xac(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
cmpl $0x2, -0xbc(%rbp)
jb 0x13a0f3
movq -0xa8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0xac(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x13a0f3
movq -0xa8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0xac(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x13a0f8
jmp 0x13a2b0
movl $0x0, -0xb4(%rbp)
movl $0x0, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
cmpl -0xbc(%rbp), %eax
jae 0x13a15b
movq -0xd8(%rbp), %rax
movl -0xc0(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x13a148
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x13a14a
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x13a10c
cmpl $0x2, -0xb4(%rbp)
jae 0x13a169
jmp 0x13a2b0
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0xac(%rbp), %edx
movq -0xd0(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0x140(%rbp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movl $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
callq 0xfe040
cmpq $0x0, %rax
je 0x13a2ae
movl $0x0, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
cmpq -0x108(%rbp), %rax
jae 0x13a284
movq -0xf8(%rbp), %rax
movl -0xc0(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0xac(%rbp), %eax
je 0x13a233
movq -0xf8(%rbp), %rax
movl -0xc0(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq -0x128(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x13a235
jmp 0x13a270
movq -0x148(%rbp), %rax
movq -0xf8(%rbp), %rcx
movl -0xc0(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movb $0x1, (%rax,%rcx)
movq -0x148(%rbp), %rax
movq -0xf8(%rbp), %rcx
movl -0xc0(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movb $0x1, (%rax,%rcx)
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x13a1ed
movq -0x148(%rbp), %rax
movl -0xac(%rbp), %ecx
movb $0x2, (%rax,%rcx)
movq -0x148(%rbp), %rax
movq -0x128(%rbp), %rcx
movl (%rcx), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movb $0x3, (%rax,%rcx)
jmp 0x13a2b0
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x13a031
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpl -0xb0(%rbp), %eax
jae 0x13a48d
movq -0x148(%rbp), %rax
movl -0xac(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x2, %eax
je 0x13a2fb
jmp 0x13a479
movq -0xa8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0xac(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0xbc(%rbp)
movq -0xa8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0xa8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0xac(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
cmpl $0x2, -0xbc(%rbp)
jb 0x13a390
movq -0xa8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0xac(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x13a390
movq -0xa8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0xac(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x13a395
jmp 0x13a479
movl $0x0, -0xb4(%rbp)
movl $0x0, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
cmpl -0xbc(%rbp), %eax
jae 0x13a3f8
movq -0xd8(%rbp), %rax
movl -0xc0(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x13a3e5
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x13a3e7
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x13a3a9
cmpl $0x1, -0xb4(%rbp)
jbe 0x13a477
movq -0x28(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl -0xac(%rbp), %edx
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %r8
movl -0x3c(%rbp), %r12d
movl -0x40(%rbp), %r15d
leaq 0x28aeaa(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %r14
movq -0x60(%rbp), %rbx
movq -0xd0(%rbp), %rax
leaq -0x88(%rbp), %r9
leaq -0xa0(%rbp), %r11
leaq -0x140(%rbp), %r10
movl %r12d, (%rsp)
movl %r15d, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movq %r11, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x136610
addl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x13a479
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x13a2ce
jmp 0x13a48f
movq $0x0, -0x150(%rbp)
movl $0x0, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
cmpq -0xa0(%rbp), %rax
jae 0x13a505
movq -0x28(%rbp), %rdi
callq 0xd4050
movq %rax, -0x150(%rbp)
movq -0x90(%rbp), %rcx
movl -0xc0(%rbp), %eax
imulq $0x18, %rax, %rax
addq %rax, %rcx
movq -0x150(%rbp), %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x10(%rax)
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x13a4a4
cmpq $0x0, -0xa0(%rbp)
je 0x13a55c
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x2210
movq -0x28(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x0, %ecx
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rdi
callq 0xcdd10
movq -0x28(%rbp), %rdi
callq 0xce020
movq -0x28(%rbp), %rdi
movq -0x48(%rbp), %rax
movl %eax, %esi
callq 0xda630
movq -0x60(%rbp), %rdi
callq 0x2210
movq -0x78(%rbp), %rdi
callq 0x2210
movq -0x90(%rbp), %rdi
callq 0x2210
movq -0x140(%rbp), %rdi
callq 0x2210
movq -0x128(%rbp), %rdi
callq 0x2210
movq -0x110(%rbp), %rdi
callq 0x2210
movq -0xf8(%rbp), %rdi
callq 0x2210
movq -0xe0(%rbp), %rdi
callq 0x2210
cmpq $0x0, -0xc8(%rbp)
je 0x13a5cc
movq -0xa8(%rbp), %rdi
callq 0xcdd10
movq -0x148(%rbp), %rdi
callq 0x2210
movq -0x58(%rbp), %rdi
callq 0xcd6f0
movq 0x2899f8(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x158(%rbp)
callq 0x1b7ca0
movq -0x158(%rbp), %rdi
subsd -0x50(%rbp), %xmm0
movq -0x28(%rbp), %rax
movl 0x4(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl -0xb8(%rbp), %ecx
movl -0x64(%rbp), %r8d
leaq 0x24e5c3(%rip), %rsi    # 0x388be4
leaq 0x24e60e(%rip), %rdx    # 0x388c36
movb $0x1, %al
callq 0x22a0
addq $0x180, %rsp            # imm = 0x180
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop