// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0x68, %rsp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movss %xmm0, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x1796dc
jmp 0x179925
movl $0x0, -0x40(%rbp)
movl $0x1, -0x44(%rbp)
movl $0xffffffff, -0x48(%rbp) # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movl $0x28, %edx
leaq -0x906c(%rip), %rcx     # 0x1706a0
callq 0x23e0
movl $0x1, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x17985d
movl $0x0, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movzbl 0x12(%rax), %eax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x40(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movzbl 0x12(%rcx), %ecx
cmpl %ecx, %eax
jne 0x1797dd
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x40(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpq 0x18(%rcx), %rax
jne 0x17979e
movl $0x1, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x1797db
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x40(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %edx
subl $0x1, %edx
movl %edx, %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
addq 0x20(%rax), %rcx
movq %rcx, 0x20(%rax)
jmp 0x17984f
jmp 0x1797dd
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movl -0x40(%rbp), %eax
imulq $0x28, %rax, %rax
addq %rax, %rsi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x44(%rbp), %eax
imulq $0x28, %rax, %rax
addq %rax, %rdi
movl $0x28, %edx
callq 0x2180
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movzbl 0x12(%rax), %eax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x44(%rbp), %edx
subl $0x1, %edx
movl %edx, %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movzbl 0x12(%rcx), %ecx
cmpl %ecx, %eax
je 0x179846
movl -0x44(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x179718
movl -0x44(%rbp), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movl -0x48(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq (%rcx), %rax
jbe 0x17987f
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x48(%rbp), %esi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %r9
movss -0x3c(%rbp), %xmm0
movq 0x10(%rbp), %rbx
movq 0x18(%rbp), %r11
movq 0x20(%rbp), %r10
movq -0x10(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x178cc0
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x48(%rbp), %eax
imulq $0x28, %rax, %rax
addq %rax, %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x48(%rbp), %ecx
subq %rcx, %rax
movl %eax, %esi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %r9
movss -0x3c(%rbp), %xmm0
movq 0x10(%rbp), %rbx
movq 0x18(%rbp), %r11
movq 0x20(%rbp), %r10
movq -0x10(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x178cc0
addq $0x68, %rsp
popq %rbx
popq %rbp
retq
nopl (%rax)