// File: /workspace/github_cpp_makefile_star2_O2/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)
andq $0x0, (%rdi)
movq %rsi, %rbp
movq (%rsi), %rsi
testq %rsi, %rsi
je 0xcd3bc
movq %rdi, 0x10(%rsp)
movq %rdx, %r15
movq %rcx, %r13
movq %r8, %r12
movq %r9, %r14
movq 0x10(%rbp), %rdi
leaq -0x532b(%rip), %rcx     # 0xc7f8e
pushq $0x28
popq %rdx
callq 0x23f0
pushq $-0x1
popq %rax
pushq $0x1
popq %rcx
movl %ecx, %ebx
movl %ecx, %ecx
movl %ebx, %esi
cmpq %rcx, (%rbp)
jbe 0xcd342
movq 0x10(%rbp), %rdx
leal -0x1(%rbx), %edi
imulq $0x28, %rdi, %rdi
movb 0x12(%rdx,%rdi), %r10b
imulq $0x28, %rcx, %r9
leaq (%rdx,%r9), %r8
cmpb 0x12(%rdx,%r9), %r10b
jne 0xcd30c
leaq (%rdx,%rdi), %r10
movq 0x18(%r10), %r10
cmpq 0x18(%r8), %r10
jne 0xcd30c
movq 0x20(%rdx,%r9), %rsi
addq %rsi, 0x20(%rdx,%rdi)
jmp 0xcd33e
imulq $0x28, %rsi, %rsi
movq 0x20(%r8), %r9
movq %r9, 0x20(%rdx,%rsi)
movups (%r8), %xmm0
movups 0x10(%r8), %xmm1
movups %xmm1, 0x10(%rdx,%rsi)
movups %xmm0, (%rdx,%rsi)
movq 0x10(%rbp), %rdx
movb 0x12(%rdx,%rsi), %sil
cmpb 0x12(%rdx,%rdi), %sil
cmovnel %ebx, %eax
incl %ebx
incl %ecx
jmp 0xcd2c9
movq %rsi, (%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 0xccd73
addq $0x20, %rsp
imulq $0x28, %rbx, %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 0xccd73
addq $0x38, %rsp
jmp 0xcd3c0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq