// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: hap_chaining(asg_arc_t_offset*, unsigned int, kvec_t_i32_warp*, kvec_t_i32_warp*, kvec_t_i32_warp*, kvec_t_u8_warp*, float, long long, long long, long long, kvec_hap_candidates*)
void hap_chaining(asg_arc_t_offset* x, uint32_t n, 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, kvec_hap_candidates* u_can)
{
    #define DUP_OVLP_RATE 0.75
    score_vc->a.n = prevIndex_vec->a.n = begIndex_vec->a.n = flag_vec->a.n = 0;
    if(n == 0) return;
    kv_resize(int32_t, score_vc->a, n);
    kv_resize(int32_t, prevIndex_vec->a, n);
    kv_resize(int32_t, begIndex_vec->a, n);
    kv_resize(uint8_t, flag_vec->a, n);
    
    int32_t* Scores = score_vc->a.a;
    int32_t* Pres = prevIndex_vec->a.a;
    int32_t* Begs = begIndex_vec->a.a;
    uint8_t* Flag = flag_vec->a.a;
    long long i, j, n_max_skip, x_off, y_off, max_beg, max_j = -1, max_score, score;
    long long distance_x, distance_y, total_distance_x, total_distance_y, distance_gap;
    float gap_rate, band_width_penalty = 1 / band_width_threshold;
    long long max_result, max_i, min_xLen, tmp_xLen, chainLen = 0, dup = 0;
    max_result = max_i = -1; min_xLen = x_readLen * 2 + 2;
    for (i = 0; i < n; i++)
    {
		n_max_skip = 0;

        x_off = (long long)(x[i].Off>>32);
        y_off = (long long)((uint32_t)x[i].Off);
        max_j = -1;
        max_score = x[i].weight;
        max_beg = i; //i itself
        ///may have a pre-cut condition for j
        for (j = i - 1; j >= 0; --j) 
        {
            distance_x = x_off - (long long)(x[j].Off>>32);
            distance_y = y_off - (long long)((uint32_t)x[j].Off);
            ///x has been sorted by x_off
            if(distance_x <= 0 || distance_y <= 0) continue;

            total_distance_x = x_off - (long long)(x[Begs[j]].Off>>32);
            total_distance_y = y_off - (long long)((uint32_t)x[Begs[j]].Off);

            distance_gap = total_distance_x - total_distance_y;
            if(distance_gap < 0) distance_gap = -distance_gap;
            if(distance_gap > band_width_threshold * total_distance_x)
            {
                continue;
            }

            score = x[i].weight;
            gap_rate = (float)((float)(distance_gap)/(float)(total_distance_x));
            score -= (long long)(score * gap_rate * band_width_penalty);
            score += Scores[j];

            ///find a new max score
			if (score > max_score) {
				max_score = score;
				max_j = j;
				max_beg = Begs[j];
				n_max_skip = 0;
			} 
            else 
            {
				if (++n_max_skip > max_skip) break;
			}
        }

        Scores[i] = max_score;
        Pres[i] = max_j;
        Begs[i] = max_beg;

        if(Scores[i] > max_result)
        {
            max_result = Scores[i];
            max_i = i;
            min_xLen = get_hap_overlapLen(x_off, x_off, x_readLen, y_off, y_off, y_readLen, 
            NULL, NULL, NULL, NULL);
        }
        else if(Scores[i] == max_result)
        {
            tmp_xLen = get_hap_overlapLen(x_off, x_off, x_readLen, y_off, y_off, y_readLen, 
            NULL, NULL, NULL, NULL);

            if(tmp_xLen < min_xLen)
            {
                max_result = Scores[i];
                max_i = i;
                min_xLen = tmp_xLen;
            }
        }
        Flag[i] = 0;
    }

    // print_asg_arc_t_offset(x, n);
    // print_chain_data(Scores, Pres, Begs, n);
    while (max_i != -1)
    {
        get_chain_details(Pres, Begs, Flag, max_i, &chainLen, &dup);
        if(chainLen == 0) break;
        if(dup > chainLen*DUP_OVLP_RATE)
        {
            for (i = 0; i < chainLen; i++)
            {
                if(Flag[Begs[i]] == 1) continue;
                Flag[Begs[i]] = 2;
            }
            
        }
        else
        {
            push_hap_can(x, u_can, Begs, chainLen, x_readLen, y_readLen);

            for (i = 0; i < chainLen; i++)
            {
                Flag[Begs[i]] = 1;
            }
        }

        max_i = get_max_index(x, Scores, Flag, n, x_readLen, y_readLen);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, 0x8(%rsp)
xorl %eax, %eax
movq %rax, (%r9)
movq %rax, (%r8)
movq %rax, (%rcx)
movq %rax, (%rdx)
testl %esi, %esi
je 0x10bbf9
movq %r9, %r14
movq %r8, %r12
movq %rcx, %r15
movq %rdx, %r13
movl %esi, %ebx
cmpq %rbx, 0x8(%rdx)
movss %xmm0, (%rsp)
jae 0x10b692
leaq -0x1(%rbx), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r13)
movq 0x10(%r13), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movss (%rsp), %xmm0
movq %rax, 0x10(%r13)
cmpq %rbx, 0x8(%r15)
jae 0x10b6ef
leaq -0x1(%rbx), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r15)
movq 0x10(%r15), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movss (%rsp), %xmm0
movq %rax, 0x10(%r15)
cmpq %rbx, 0x8(%r12)
jae 0x10b750
leaq -0x1(%rbx), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r12)
movq 0x10(%r12), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movss (%rsp), %xmm0
movq %rax, 0x10(%r12)
movq 0xa8(%rsp), %rbp
cmpq %rbx, 0x8(%r14)
jae 0x10b7ac
leaq -0x1(%rbx), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
callq 0x2370
movss (%rsp), %xmm0
movq %rax, 0x10(%r14)
movq 0x10(%r13), %rax
movq %rax, 0x30(%rsp)
movq 0x10(%r15), %r8
movq 0x10(%r12), %rsi
movq 0x10(%r14), %r12
movss 0x1168f6(%rip), %xmm5   # 0x2220c0
divss %xmm0, %xmm5
leaq 0x2(,%rbp,2), %rcx
leaq -0x1(%rbp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x1, %rbx
movq %rbx, 0x40(%rsp)
adcq $0x0, %rbx
movq %rbx, 0x50(%rsp)
movq 0x8(%rsp), %rax
leaq -0x10(%rax), %r9
movq $-0x1, %r10
xorl %r14d, %r14d
movq %rcx, 0x48(%rsp)
movq %rcx, 0x20(%rsp)
movq $-0x1, %rax
movq $-0x1, 0x28(%rsp)
movq %r8, 0x38(%rsp)
movq %rsi, (%rsp)
leaq (%r14,%r14,4), %rcx
movq 0x8(%rsp), %rdx
movq 0x18(%rdx,%rcx,8), %r13
movq 0x20(%rdx,%rcx,8), %rdi
movl %r13d, %r15d
shrq $0x20, %r13
testq %r14, %r14
je 0x10b96b
xorps %xmm1, %xmm1
cvtsi2ss %rdi, %xmm1
movq $-0x1, 0x10(%rsp)
movq %r9, 0x60(%rsp)
movq %r9, %rsi
movq %r10, 0x58(%rsp)
movq %r10, %rcx
xorl %ebp, %ebp
movq %r14, %r8
movq %rdi, %r10
movq (%rsi), %rdx
movq %rdx, %rbx
shrq $0x20, %rbx
cmpl %ebx, %r13d
jbe 0x10b922
movl %edx, %edx
cmpl %edx, %r15d
jbe 0x10b922
movq (%rsp), %rdx
movslq (%rdx,%rcx,4), %rbx
leaq (%rbx,%rbx,4), %rdx
movq 0x8(%rsp), %r9
movq 0x18(%r9,%rdx,8), %rdx
movl %edx, %r9d
shrq $0x20, %rdx
movq %r13, %r11
subq %rdx, %r11
subq %r15, %r9
addq %r11, %r9
movq %r9, %rdx
negq %rdx
cmovsq %r9, %rdx
xorps %xmm2, %xmm2
cvtsi2ss %rdx, %xmm2
xorps %xmm3, %xmm3
cvtsi2ss %r11, %xmm3
movaps %xmm3, %xmm4
mulss %xmm0, %xmm4
ucomiss %xmm4, %xmm2
ja 0x10b922
divss %xmm3, %xmm2
mulss %xmm1, %xmm2
mulss %xmm5, %xmm2
cvttss2si %xmm2, %rdx
movq %rdi, %r9
subq %rdx, %r9
movq 0x30(%rsp), %rdx
movslq (%rdx,%rcx,4), %rdx
addq %r9, %rdx
cmpq %r10, %rdx
jle 0x10b911
movq %rcx, 0x10(%rsp)
xorl %ebp, %ebp
jmp 0x10b928
cmpq 0xa0(%rsp), %rbp
jge 0x10ba31
incq %rbp
movq %r10, %rdx
movq %r8, %rbx
leaq -0x1(%rcx), %r9
incq %rcx
addq $-0x28, %rsi
movq %rbx, %r8
movq %rdx, %r10
cmpq $0x1, %rcx
movq %r9, %rcx
jg 0x10b873
movq %rdx, %rdi
movq 0xa8(%rsp), %rbp
movq 0x38(%rsp), %r8
movq (%rsp), %rsi
movq 0x60(%rsp), %r9
movq 0x58(%rsp), %r10
movq 0x10(%rsp), %rdx
jmp 0x10b973
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %r14, %rbx
movq 0x30(%rsp), %rcx
movl %edi, (%rcx,%r14,4)
movl %edx, (%r8,%r14,4)
movl %ebx, (%rsi,%r14,4)
movslq (%rcx,%r14,4), %rcx
cmpq %rcx, 0x28(%rsp)
jge 0x10b9d3
movq %r13, %rax
subq %r15, %rax
movl $0x0, %edx
cmovaeq %rax, %rdx
movq %r13, %rax
notq %rax
addq %rbp, %rax
notq %r15
addq 0xb0(%rsp), %r15
addq %r15, %r13
cmpq %r15, %rax
cmovleq 0x18(%rsp), %r13
subq %rdx, %r13
incq %r13
movq %rcx, 0x28(%rsp)
movq %r14, %rax
movq %r13, 0x20(%rsp)
jmp 0x10ba15
jne 0x10ba15
movq %r13, %rdx
subq %r15, %rdx
movl $0x0, %esi
cmovaeq %rdx, %rsi
movq %r13, %rdx
notq %rdx
addq %rbp, %rdx
notq %r15
addq 0xb0(%rsp), %r15
addq %r15, %r13
cmpq %r15, %rdx
cmovleq 0x18(%rsp), %r13
subq %rsi, %r13
movq (%rsp), %rsi
incq %r13
cmpq 0x20(%rsp), %r13
jl 0x10b9c4
movb $0x0, (%r12,%r14)
incq %r14
incq %r10
addq $0x28, %r9
cmpq 0x50(%rsp), %r14
jne 0x10b827
jmp 0x10ba3c
movq %r10, %rdx
movq %r8, %rbx
jmp 0x10b946
cmpq $-0x1, %rax
movq 0x40(%rsp), %r14
movq 0xb8(%rsp), %r13
je 0x10bbf9
movq 0x8(%rsp), %rcx
addq $0x18, %rcx
movq %rcx, 0x10(%rsp)
movsd 0x119ac7(%rip), %xmm2   # 0x225530
testq %rax, %rax
js 0x10ba9a
xorl %r15d, %r15d
xorl %ecx, %ecx
xorl %edx, %edx
cmpb $0x1, (%r12,%rax)
sete %dl
addq %rdx, %rcx
movl %eax, (%rsi,%r15,4)
movslq (%r8,%rax,4), %rax
incq %r15
testq %rax, %rax
jns 0x10ba73
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
jmp 0x10baa0
xorps %xmm0, %xmm0
xorl %r15d, %r15d
testq %r15, %r15
je 0x10bbf9
xorps %xmm1, %xmm1
cvtsi2sd %r15, %xmm1
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x10badc
testq %r15, %r15
jle 0x10bb1f
xorl %eax, %eax
movslq (%rsi,%rax,4), %rcx
cmpb $0x1, (%r12,%rcx)
je 0x10bad2
movb $0x2, (%r12,%rcx)
incq %rax
cmpq %rax, %r15
jne 0x10bac2
jmp 0x10bb1f
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq (%rsp), %rdx
movq %r15, %rcx
movq %rbp, %r8
movq 0xb0(%rsp), %r9
callq 0x112c67
movsd 0x119a2d(%rip), %xmm2   # 0x225530
movq (%rsp), %rdx
testq %r15, %r15
jle 0x10bb1f
xorl %eax, %eax
movslq (%rdx,%rax,4), %rcx
movb $0x1, (%r12,%rcx)
incq %rax
cmpq %rax, %r15
jne 0x10bb0e
movq $-0x1, %rsi
movq 0x10(%rsp), %rcx
xorl %edx, %edx
movq $-0x1, %rax
movq 0x48(%rsp), %rdi
movq 0xb0(%rsp), %r15
cmpb $0x0, (%r12,%rdx)
jne 0x10bbd6
movq (%rcx), %r9
movl %r9d, %r10d
shrq $0x20, %r9
movq 0x30(%rsp), %r8
movslq (%r8,%rdx,4), %r8
cmpq %r8, %rsi
jge 0x10bb9f
movq %r9, %rax
subq %r10, %rax
movl $0x0, %esi
cmovaeq %rax, %rsi
movq %r9, %rax
notq %rax
addq %rbp, %rax
notq %r10
addq %r15, %r10
addq %r10, %r9
cmpq %r10, %rax
cmovleq 0x18(%rsp), %r9
subq %rsi, %r9
incq %r9
movq %r9, %rdi
movq %rdx, %rax
movq %r8, %rsi
jmp 0x10bbd6
jne 0x10bbd6
movq %r9, %r11
subq %r10, %r11
movl $0x0, %ebx
cmovaeq %r11, %rbx
movq %r9, %r11
notq %r11
addq %rbp, %r11
notq %r10
addq %r15, %r10
addq %r10, %r9
cmpq %r10, %r11
cmovleq 0x18(%rsp), %r9
subq %rbx, %r9
incq %r9
cmpq %rdi, %r9
jl 0x10bb94
incq %rdx
addq $0x28, %rcx
cmpq %rdx, %r14
jne 0x10bb41
cmpq $-0x1, %rax
movq 0x38(%rsp), %r8
movq (%rsp), %rsi
jne 0x10ba69
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq