// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: get_candidate_hap_alignment(kvec_hap_candidates*, kvec_asg_arc_t_offset*, kvec_t_i32_warp*, kvec_t_i32_warp*, kvec_t_i32_warp*, kvec_t_u8_warp*, float, long long, long long, long long)
void get_candidate_hap_alignment(kvec_hap_candidates* u_can, kvec_asg_arc_t_offset* u_buffer,
kvec_t_i32_warp* score_vc, kvec_t_i32_warp* prevIndex_vec, kvec_t_i32_warp* begIndex_vec,
kvec_t_u8_warp* flag_vec, float band_width_threshold, long long max_skip, long long x_readLen, 
long long y_readLen)
{
    u_can->a.n = 0;
    if(u_buffer->a.n == 0) return;
    uint32_t i = 0, /**anchor_i = 0, **/m = 1, break_point = (uint32_t)-1, is_merge;

    qsort(u_buffer->a.a, u_buffer->a.n, sizeof(asg_arc_t_offset), cmp_hap_alignment_chaining);

    ///print_asg_arc_t_offset(u_buffer->a.a, u_buffer->a.n);

    for (i = 1; i < u_buffer->a.n; i++)
    {
        is_merge = 0;
        if(u_buffer->a.a[m-1].x.el == u_buffer->a.a[i].x.el)
        {
            if(u_buffer->a.a[m-1].Off == u_buffer->a.a[i].Off) is_merge = 1;
            ///I think we don't need the following merging
            // if(is_merge == 0 && (Get_xOff(u_buffer->a.a[m-1].Off)==Get_xOff(u_buffer->a.a[i].Off)))
            // {
            //     if((Get_yOff(u_buffer->a.a[i].Off)-(Get_yOff(u_buffer->a.a[m-1].Off))) == (i-anchor_i))///not sure why, does it use for tolerate indels in overlaps? 
            //     {
            //         is_merge = 1;
            //     }
            // }

            if(is_merge)
            {
                u_buffer->a.a[m-1].weight += u_buffer->a.a[i].weight;
                continue;
            }
        } 
        u_buffer->a.a[m] = u_buffer->a.a[i];
        // anchor_i = i;
        if(u_buffer->a.a[m].x.el != u_buffer->a.a[m-1].x.el) break_point = m;
        m++;
    }
    u_buffer->a.n = m;
    if(break_point > u_buffer->a.n) break_point = u_buffer->a.n;

    ///print_asg_arc_t_offset(u_buffer->a.a, u_buffer->a.n);

    hap_chaining(u_buffer->a.a, break_point, score_vc, prevIndex_vec, begIndex_vec, flag_vec,
    band_width_threshold, max_skip, x_readLen, y_readLen, u_can);
    
    hap_chaining(u_buffer->a.a + break_point, u_buffer->a.n - break_point, score_vc, prevIndex_vec, 
    begIndex_vec, flag_vec, band_width_threshold, max_skip, x_readLen, y_readLen, u_can);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movss %xmm0, 0xc(%rsp)
movq %rsi, %rbp
movq $0x0, (%rdi)
movq (%rsi), %rsi
testq %rsi, %rsi
je 0x10bcf4
movq %rdi, 0x10(%rsp)
movq %rdx, %r15
movq %rcx, %r13
movq %r8, %r12
movq %r9, %r14
movq 0x10(%rbp), %rdi
leaq -0x5d79(%rip), %rcx     # 0x105ed5
movl $0x28, %edx
callq 0x23f0
cmpq $0x2, (%rbp)
jb 0x10bcfd
movl $0x1, %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %esi
movl $0x2, %ecx
movq 0x10(%rbp), %rdx
leal -0x1(%rbx), %edi
leaq (%rdi,%rdi,4), %rdi
movb 0x12(%rdx,%rdi,8), %r9b
leaq (%rsi,%rsi,4), %r8
leaq (%rdx,%r8,8), %rsi
cmpb 0x12(%rsi), %r9b
jne 0x10bcaf
leaq (%rdx,%rdi,8), %r9
movq 0x18(%r9), %r9
cmpq 0x18(%rsi), %r9
jne 0x10bcaf
movq 0x20(%rdx,%r8,8), %rsi
addq %rsi, 0x20(%rdx,%rdi,8)
jmp 0x10bce8
movl %ebx, %r8d
shlq $0x3, %r8
leaq (%r8,%r8,4), %r8
movq 0x20(%rsi), %r9
movq %r9, 0x20(%rdx,%r8)
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups %xmm1, 0x10(%rdx,%r8)
movups %xmm0, (%rdx,%r8)
movq 0x10(%rbp), %rdx
movb 0x12(%rdx,%r8), %sil
cmpb 0x12(%rdx,%rdi,8), %sil
cmovnel %ebx, %eax
incl %ebx
movl %ecx, %esi
incl %ecx
cmpq %rsi, (%rbp)
ja 0x10bc77
jmp 0x10bd07
addq $0x18, %rsp
jmp 0x10bd85
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %ebx
movl %ebx, %ecx
movq %rcx, (%rbp)
cmpl %ebx, %eax
cmovbl %eax, %ebx
movq 0x10(%rbp), %rdi
movl %ebx, %esi
movq %r15, %rdx
movq %r13, %rcx
movq %r12, %r8
movq %r14, %r9
movss 0xc(%rsp), %xmm0
pushq 0x10(%rsp)
movq 0x68(%rsp), %rax
pushq %rax
movq 0x68(%rsp), %rax
pushq %rax
movq 0x68(%rsp), %rax
pushq %rax
callq 0x10b5f9
addq $0x20, %rsp
leaq (%rbx,%rbx,4), %rdi
shlq $0x3, %rdi
addq 0x10(%rbp), %rdi
movl (%rbp), %esi
subl %ebx, %esi
movq %r15, %rdx
movq %r13, %rcx
movq %r12, %r8
movq %r14, %r9
movss 0xc(%rsp), %xmm0
pushq 0x10(%rsp)
pushq 0x68(%rsp)
pushq 0x68(%rsp)
pushq 0x68(%rsp)
callq 0x10b5f9
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq