// File: /workspace/github_cpp_makefile_star2_O3/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 0x110e67
movq %rdi, 0x10(%rsp)
movq %rdx, %r15
movq %rcx, %r13
movq %r8, %r12
movq %r9, %r14
movq 0x10(%rbp), %rdi
leaq -0x674a(%rip), %rcx     # 0x10a674
movl $0x28, %edx
callq 0x23f0
movq (%rbp), %rdx
movq 0x10(%rbp), %rdi
cmpq $0x2, %rdx
jb 0x110e70
movl $0x1, %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %esi
movl $0x2, %ecx
leal -0x1(%rbx), %r8d
leaq (%r8,%r8,4), %r8
movb 0x12(%rdi,%r8,8), %r10b
leaq (%rsi,%rsi,4), %r9
leaq (%rdi,%r9,8), %rsi
cmpb 0x12(%rsi), %r10b
jne 0x110e23
leaq (%rdi,%r8,8), %r10
movq 0x18(%r10), %r10
cmpq 0x18(%rsi), %r10
jne 0x110e23
movq 0x20(%rdi,%r9,8), %rsi
addq %rsi, 0x20(%rdi,%r8,8)
jmp 0x110e5c
movl %ebx, %edx
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rdx
movq 0x20(%rsi), %r9
movq %r9, 0x20(%rdi,%rdx)
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups %xmm1, 0x10(%rdi,%rdx)
movups %xmm0, (%rdi,%rdx)
movq 0x10(%rbp), %rdi
movb 0x12(%rdi,%rdx), %dl
cmpb 0x12(%rdi,%r8,8), %dl
movq (%rbp), %rdx
cmovnel %ebx, %eax
incl %ebx
movl %ecx, %esi
incl %ecx
cmpq %rsi, %rdx
ja 0x110dee
jmp 0x110e7a
addq $0x18, %rsp
jmp 0x110ef4
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %ebx
movl %ebx, %ecx
movq %rcx, (%rbp)
cmpl %ebx, %eax
cmovbl %eax, %ebx
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 0x110742
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 0x110742
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq