// File: /workspace/github_cpp_makefile_star2_O3/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, 0x18(%rsp)
xorl %eax, %eax
movq %rax, (%r9)
movq %rax, (%r8)
movq %rax, (%rcx)
movq %rax, (%rdx)
testl %esi, %esi
je 0x110d69
movq %r9, %r13
movq %r8, %r14
movq %rcx, %r12
movq %rdx, %rbp
movl %esi, %ebx
cmpq %rbx, 0x8(%rdx)
movss %xmm0, 0x8(%rsp)
jae 0x1107dd
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(%rbp)
movq 0x10(%rbp), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movss 0x8(%rsp), %xmm0
movq %rax, 0x10(%rbp)
cmpq %rbx, 0x8(%r12)
jae 0x11083f
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 0x8(%rsp), %xmm0
movq %rax, 0x10(%r12)
cmpq %rbx, 0x8(%r14)
jae 0x11089d
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(%r14)
movq 0x10(%r14), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movss 0x8(%rsp), %xmm0
movq %rax, 0x10(%r14)
cmpq %rbx, 0x8(%r13)
movq %rbx, 0x38(%rsp)
jae 0x1108fc
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(%r13)
movq 0x10(%r13), %rdi
callq 0x2370
movss 0x8(%rsp), %xmm0
movq %rax, %r15
movq %rax, 0x10(%r13)
jmp 0x110900
movq 0x10(%r13), %r15
movq 0x10(%rbp), %r8
movq 0x10(%r12), %rax
movq %rax, 0x28(%rsp)
movq 0x10(%r14), %rbp
movss 0x11d7b6(%rip), %xmm5   # 0x22e0d0
divss %xmm0, %xmm5
movq 0xa8(%rsp), %r13
leaq 0x2(%r13,%r13), %rcx
leaq -0x1(%r13), %r10
movq 0x18(%rsp), %rax
leaq -0x10(%rax), %rbx
movq $-0x1, %r13
xorl %edi, %edi
movq %rcx, 0x48(%rsp)
movq %rcx, 0x20(%rsp)
movq $-0x1, 0x10(%rsp)
movq $-0x1, %rdx
movq 0x38(%rsp), %r12
movq %r8, 0x8(%rsp)
movq %r10, 0x40(%rsp)
leaq (%rdi,%rdi,4), %rcx
movq 0x18(%rsp), %rax
movq 0x18(%rax,%rcx,8), %r9
movq 0x20(%rax,%rcx,8), %rcx
movl %r9d, %r11d
movq %r9, %rax
shrq $0x20, %rax
testq %rdi, %rdi
je 0x110ab2
movq %rdx, 0x50(%rsp)
xorps %xmm1, %xmm1
cvtsi2ss %rcx, %xmm1
movq $-0x1, %r14
movq %rbx, 0x60(%rsp)
movq %rbx, %rdx
movq %r13, 0x58(%rsp)
movq %r13, %r8
xorl %r10d, %r10d
movq %rdi, %r13
movq %rcx, 0x30(%rsp)
movq (%rdx), %rsi
movq %rsi, %rbx
shrq $0x20, %rbx
cmpl %ebx, %eax
jbe 0x110a6b
movl %esi, %esi
cmpl %esi, %r11d
jbe 0x110a6b
movslq (%rbp,%r8,4), %rsi
leaq (%rsi,%rsi,4), %rbx
movq 0x18(%rsp), %r9
movq 0x18(%r9,%rbx,8), %rbx
movl %ebx, %r12d
shrq $0x20, %rbx
movq %rax, %r9
subq %rbx, %r9
subq %r11, %r12
addq %r9, %r12
movq %r12, %rbx
negq %rbx
cmovsq %r12, %rbx
xorps %xmm2, %xmm2
cvtsi2ss %rbx, %xmm2
xorps %xmm3, %xmm3
cvtsi2ss %r9, %xmm3
movaps %xmm3, %xmm4
mulss %xmm0, %xmm4
ucomiss %xmm4, %xmm2
ja 0x110a6b
divss %xmm3, %xmm2
mulss %xmm1, %xmm2
mulss %xmm5, %xmm2
cvttss2si %xmm2, %r9
movq 0x30(%rsp), %r12
subq %r9, %r12
movq 0x8(%rsp), %r9
movslq (%r9,%r8,4), %rbx
addq %r12, %rbx
cmpq %rcx, %rbx
jle 0x110a5a
movq %r8, %r14
xorl %r10d, %r10d
jmp 0x110a71
cmpq 0xa0(%rsp), %r10
jge 0x110b9f
incq %r10
movq %rcx, %rbx
movq %r13, %rsi
leaq -0x1(%r8), %r9
incq %r8
addq $-0x28, %rdx
movq %rsi, %r13
movq %rbx, %rcx
cmpq $0x1, %r8
movq %r9, %r8
jg 0x1109bf
movq %rbx, %rcx
movq 0x38(%rsp), %r12
movq 0x8(%rsp), %r8
movq 0x40(%rsp), %r10
movq 0x60(%rsp), %rbx
movq 0x58(%rsp), %r13
movq 0x50(%rsp), %rdx
jmp 0x110aba
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
xorl %esi, %esi
movl %ecx, (%r8,%rdi,4)
movq 0x28(%rsp), %rcx
movl %r14d, (%rcx,%rdi,4)
movl %esi, (%rbp,%rdi,4)
movslq (%r8,%rdi,4), %rcx
cmpq %rcx, %rdx
jge 0x110b1b
movq %rax, %rdx
subq %r11, %rdx
movl $0x0, %esi
cmovaeq %rdx, %rsi
movq %rax, %rdx
notq %rdx
addq 0xa8(%rsp), %rdx
notq %r11
movq 0xb0(%rsp), %r14
addq %r14, %r11
addq %r11, %rax
cmpq %r11, %rdx
cmovleq %r10, %rax
subq %rsi, %rax
incq %rax
movq %rdi, 0x10(%rsp)
movq %rax, 0x20(%rsp)
jmp 0x110b82
movq 0xb0(%rsp), %r14
jne 0x110b7f
movq %rax, %rcx
subq %r11, %rcx
movq %rdx, %rsi
movl $0x0, %edx
cmovaeq %rcx, %rdx
movq %rax, %rcx
notq %rcx
addq 0xa8(%rsp), %rcx
notq %r11
addq %r14, %r11
addq %r11, %rax
cmpq %r11, %rcx
cmovleq %r10, %rax
subq %rdx, %rax
incq %rax
movq 0x20(%rsp), %rdx
cmpq %rdx, %rax
movq 0x10(%rsp), %rcx
cmovlq %rdi, %rcx
movq %rcx, 0x10(%rsp)
cmovlq %rax, %rdx
movq %rdx, 0x20(%rsp)
movq %rsi, %rcx
jmp 0x110b82
movq %rdx, %rcx
movb $0x0, (%r15,%rdi)
incq %rdi
incq %r13
addq $0x28, %rbx
movq %rcx, %rdx
cmpq %r12, %rdi
jne 0x11096a
jmp 0x110baa
movq %rcx, %rbx
movq %r13, %rsi
jmp 0x110a8f
cmpq $0x0, 0x10(%rsp)
movq 0x28(%rsp), %rdi
js 0x110d69
movq 0x18(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x30(%rsp)
movq 0x10(%rsp), %r13
movl $0x1, %esi
xorl %ecx, %ecx
movsd 0x120a53(%rip), %xmm2   # 0x231630
xorl %edx, %edx
xorl %eax, %eax
cmpb $0x1, (%r15,%r13)
movq %rsi, %rbx
sete %al
addq %rax, %rdx
movl %r13d, (%rbp,%rcx,4)
movslq (%rdi,%r13,4), %r13
incq %rcx
incq %rsi
testq %r13, %r13
jns 0x110bdf
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x110c3a
xorl %eax, %eax
movslq (%rbp,%rax,4), %rcx
cmpb $0x1, (%r15,%rcx)
je 0x110c30
movb $0x2, (%r15,%rcx)
incq %rax
cmpq %rax, %rbx
jne 0x110c1f
jmp 0x110c76
movq 0x18(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq %rbp, %rdx
movq 0xa8(%rsp), %r8
movq %r14, %r9
callq 0x118758
movsd 0x1209ce(%rip), %xmm2   # 0x231630
xorl %eax, %eax
movslq (%rbp,%rax,4), %rcx
movb $0x1, (%r15,%rcx)
incq %rax
cmpq %rax, %rbx
jne 0x110c64
movq $-0x1, %rdi
movq 0x30(%rsp), %rcx
xorl %edx, %edx
movq $-0x1, %r13
movq 0x48(%rsp), %rsi
movq 0x8(%rsp), %r11
movq 0x40(%rsp), %rbx
cmpb $0x0, (%r15,%rdx)
je 0x110cb8
movq %rdi, %r9
incq %rdx
addq $0x28, %rcx
movq %r9, %rdi
cmpq %rdx, %r12
jne 0x110c9a
jmp 0x110d4c
movq (%rcx), %r8
movl %r8d, %r10d
shrq $0x20, %r8
movslq (%r11,%rdx,4), %r9
cmpq %r9, %rdi
jge 0x110d06
movq %r8, %rax
subq %r10, %rax
movl $0x0, %esi
cmovaeq %rax, %rsi
movq %r8, %rax
notq %rax
addq 0xa8(%rsp), %rax
notq %r10
addq %r14, %r10
addq %r10, %r8
cmpq %r10, %rax
cmovleq %rbx, %r8
subq %rsi, %r8
incq %r8
movq %r8, %rsi
movq %rdx, %r13
jmp 0x110ca4
jne 0x110ca1
movq %r8, %rax
subq %r10, %rax
movl $0x0, %r9d
cmovaeq %rax, %r9
movq %r8, %rax
notq %rax
addq 0xa8(%rsp), %rax
notq %r10
addq %r14, %r10
addq %r10, %r8
cmpq %r10, %rax
cmovleq %rbx, %r8
subq %r9, %r8
incq %r8
cmpq %rsi, %r8
cmovlq %r8, %rsi
cmovlq %rdx, %r13
jmp 0x110ca1
movl $0x1, %esi
movl $0x0, %ecx
movl $0x0, %edx
testq %r13, %r13
movq 0x28(%rsp), %rdi
jns 0x110bdf
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq