// File: /workspace/github_cpp_makefile_star2_O2/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:
andq $0x0, (%r9)
andq $0x0, (%r8)
andq $0x0, (%rcx)
andq $0x0, (%rdx)
testl %esi, %esi
je 0xcd275
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, %rbp
movq %r8, %r14
movq %rcx, %r15
movq %rdx, %r13
movq %rdi, %rbx
movl %esi, %r12d
cmpq %r12, 0x8(%rdx)
movss %xmm0, 0x24(%rsp)
jae 0xcce13
leaq -0x1(%r12), %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 0x24(%rsp), %xmm0
movq %rax, 0x10(%r13)
cmpq %r12, 0x8(%r15)
jae 0xcce72
leaq -0x1(%r12), %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 0x24(%rsp), %xmm0
movq %rax, 0x10(%r15)
cmpq %r12, 0x8(%r14)
jae 0xcced1
leaq -0x1(%r12), %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(%r14)
movq 0x10(%r14), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movss 0x24(%rsp), %xmm0
movq %rax, 0x10(%r14)
cmpq %r12, 0x8(%rbp)
movq %r12, 0x28(%rsp)
jae 0xccf31
leaq -0x1(%r12), %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(%rbp)
movq 0x10(%rbp), %rdi
callq 0x2370
movss 0x24(%rsp), %xmm0
movq %rax, %r12
movq %rax, 0x10(%rbp)
jmp 0xccf35
movq 0x10(%rbp), %r12
movq 0x10(%r13), %rax
movq %rax, 0x30(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x60(%rsp)
movq 0x10(%r14), %rbp
andq $0x0, 0x48(%rsp)
movss 0x113177(%rip), %xmm1   # 0x1e00d0
andq $0x0, 0x78(%rsp)
divss %xmm0, %xmm1
movss %xmm1, 0x54(%rsp)
movq 0xc8(%rsp), %rax
leaq 0x2(,%rax,2), %rax
movq %rax, 0x38(%rsp)
leaq -0x10(%rbx), %rax
movq %rax, 0x80(%rsp)
pushq $-0x1
popq %rax
xorl %r14d, %r14d
movq %rax, 0x40(%rsp)
movq %rax, %rcx
movq 0x28(%rsp), %r15
cmpq %r15, %r14
je 0xcd18c
movq %rcx, 0x58(%rsp)
imulq $0x28, %r14, %rax
movq 0x18(%rbx,%rax), %rdx
movq 0x20(%rbx,%rax), %rax
movl %edx, %ecx
shrq $0x20, %rdx
cvtsi2ss %rax, %xmm4
movq %rax, %r13
pushq $-0x1
popq %rsi
movq %rsi, 0x70(%rsp)
movq %r14, 0x68(%rsp)
movq %r14, %r9
xorl %r10d, %r10d
imulq $0x28, %r9, %r11
decq %r9
addq 0x80(%rsp), %r11
leaq 0x1(%r9), %r8
testq %r8, %r8
jle 0xcd0aa
movq (%r11), %r15
movq %r15, %r8
shrq $0x20, %r8
cmpl %r8d, %edx
jbe 0xcd04e
movl %r15d, %r8d
cmpl %r8d, %ecx
jbe 0xcd04e
movslq (%rbp,%r9,4), %r8
imulq $0x28, %r8, %r15
movq 0x18(%rbx,%r15), %r15
movl %r15d, %esi
shrq $0x20, %r15
movq %rdx, %rdi
subq %r15, %rdi
subq %rcx, %rsi
addq %rdi, %rsi
movq %rsi, %r15
negq %r15
cmovsq %rsi, %r15
cvtsi2ss %r15, %xmm1
cvtsi2ss %rdi, %xmm2
movaps %xmm2, %xmm3
mulss %xmm0, %xmm3
ucomiss %xmm3, %xmm1
jbe 0xcd057
decq %r9
addq $-0x28, %r11
jmp 0xccfe9
divss %xmm2, %xmm1
mulss %xmm4, %xmm1
mulss 0x54(%rsp), %xmm1
cvttss2si %xmm1, %rsi
movq %rax, %rdi
subq %rsi, %rdi
movq 0x30(%rsp), %rsi
movslq (%rsi,%r9,4), %r11
addq %rdi, %r11
cmpq %r13, %r11
jg 0xcd098
leaq 0x1(%r10), %rsi
cmpq 0xc0(%rsp), %r10
movq %rsi, %r10
jl 0xccfda
jmp 0xcd0aa
movq %r11, %r13
movq %r9, 0x70(%rsp)
movq %r8, 0x68(%rsp)
jmp 0xccfd7
movq 0x30(%rsp), %rax
movl %r13d, (%rax,%r14,4)
movq 0x60(%rsp), %rsi
movq 0x70(%rsp), %rdi
movl %edi, (%rsi,%r14,4)
movq 0x68(%rsp), %rsi
movl %esi, (%rbp,%r14,4)
movslq (%rax,%r14,4), %r13
cmpq %r13, 0x40(%rsp)
jge 0xcd11a
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rdx, %rdi
movq %rdx, %rsi
movq 0xc8(%rsp), %rdx
movq %rcx, %r8
movq 0xd0(%rsp), %r9
callq 0xd3bc4
movss 0x24(%rsp), %xmm0
movq %rax, 0x38(%rsp)
movq %r13, 0x40(%rsp)
movq %r14, %rcx
movq 0x28(%rsp), %r15
jmp 0xcd17f
jne 0xcd175
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rdx, %rdi
movq %rdx, %rsi
movq 0xc8(%rsp), %rdx
movq %rcx, %r8
movq 0xd0(%rsp), %r9
callq 0xd3bc4
movss 0x24(%rsp), %xmm0
cmpq 0x38(%rsp), %rax
movq 0x28(%rsp), %r15
movq 0x58(%rsp), %rcx
jge 0xcd17f
movq 0x30(%rsp), %rcx
movslq (%rcx,%r14,4), %rcx
movq %rcx, 0x40(%rsp)
movq %r14, %rcx
movq %rax, 0x38(%rsp)
jmp 0xcd17f
movq 0x28(%rsp), %r15
movq 0x58(%rsp), %rcx
movb $0x0, (%r12,%r14)
incq %r14
jmp 0xccf9d
leaq 0x48(%rsp), %r13
movq 0xd8(%rsp), %r14
cmpq $-0x1, %rcx
je 0xcd264
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %r8
leaq 0x78(%rsp), %r9
callq 0xd3eea
movq 0x48(%rsp), %rcx
testq %rcx, %rcx
je 0xcd264
cvtsi2sdq 0x78(%rsp), %xmm0
cvtsi2sd %rcx, %xmm1
mulsd 0x1163ab(%rip), %xmm1   # 0x1e3588
ucomisd %xmm1, %xmm0
jbe 0xcd200
xorl %eax, %eax
cmpq %rcx, %rax
jge 0xcd239
movslq (%rbp,%rax,4), %rdx
cmpb $0x1, (%r12,%rdx)
je 0xcd1fb
movb $0x2, (%r12,%rdx)
incq %rax
jmp 0xcd1e5
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq 0xc8(%rsp), %r8
movq 0xd0(%rsp), %r9
callq 0xd3f13
movq 0x48(%rsp), %rax
xorl %ecx, %ecx
cmpq %rax, %rcx
jge 0xcd239
movslq (%rbp,%rcx,4), %rdx
movb $0x1, (%r12,%rdx)
incq %rcx
jmp 0xcd225
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
movq %r12, %rdx
movq %r15, %rcx
movq 0xc8(%rsp), %r8
movq 0xd0(%rsp), %r9
callq 0xd40a7
movq %rax, %rcx
jmp 0xcd199
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq