// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: get_chain_score(ma_utg_t*, asg_t*, kvec_asg_arc_t_offset*, kvec_t_i32_warp*, kvec_t_i32_warp*, ma_hit_t_alloc*, long long, long long)
long long get_chain_score(ma_utg_t *xReads, asg_t *read_g, kvec_asg_arc_t_offset* u_buffer, kvec_t_i32_warp* tailIndex, kvec_t_i32_warp* idx, 
ma_hit_t_alloc* reverse_sources, long long xBegPos, long long xEndPos)
{
    long long offset, r_beg, r_end, inp_beg, inp_end, hap_beg, hap_end, inp_match, hap_match, ovlp;
    uint64_t i, k, rId;
    idx->a.n = 0;
    
    for (i = k = 0; i < tailIndex->a.n; i++)
    {
        rId = u_buffer->a.a[tailIndex->a.a[i]].x.ul>>33;
        
        
        for (; k < xReads->n; k++)
        {
            if(rId == (xReads->a[k]>>33)) break;
        }

        if(k >= xReads->n)
        {
            for (k = 0; k < xReads->n; k++)
            {
                if(rId == (xReads->a[k]>>33)) break;
            }
        }

        if(k < xReads->n) kv_push(int32_t, idx->a, k);
        else
        {
            fprintf(stderr, "\nERROR-get_chain_score: tailIndex->a.n: %lu, xReads->n: %lu\n", (uint64_t)tailIndex->a.n, (uint64_t)xReads->n);
        } 
    }
    

    radix_sort_i32(idx->a.a, idx->a.a + idx->a.n);
    inp_beg = -1; inp_end = -2;
    hap_beg = -1; hap_end = -2;
    for (i = k = 0, offset = 0, inp_match = hap_match = 0; i < xReads->n; i++)
    {
        rId = xReads->a[i]>>33;
        r_beg = offset; r_end = offset + (long long)(read_g->seq[rId].len) - 1;
        offset += (uint32_t)xReads->a[i];

        if(reverse_sources[rId].length > 0)
        {
            if(r_beg <= hap_end)
            {
                hap_end = MAX(hap_end, r_end);
            } 
            else
            {
                ///match += (hap_end - hap_beg + 1);
                ovlp = (long long)(MIN(hap_end, xEndPos)) - (long long)(MAX(hap_beg, xBegPos)) + 1;
                hap_match += (ovlp >= 0? ovlp : 0);
                hap_beg = r_beg; hap_end = r_end;
            }
        }

        for (; k < idx->a.n; k++)
        {
            if(i <= (uint64_t)idx->a.a[k]) break;
        }

        if(k >= idx->a.n) continue;
        
        if(i == (uint64_t)idx->a.a[k])
        {
            if(r_beg <= inp_end)
            {
                inp_end = MAX(inp_end, r_end);
            } 
            else
            {
                ovlp = (long long)(MIN(inp_end, xEndPos)) - (long long)(MAX(inp_beg, xBegPos)) + 1;
                inp_match += (ovlp >= 0? ovlp : 0);
                inp_beg = r_beg; inp_end = r_end;
            }
        }
    }
    
    ovlp = (long long)(MIN(inp_end, xEndPos)) - (long long)(MAX(inp_beg, xBegPos)) + 1;
    inp_match += (ovlp >= 0? ovlp : 0);
    
    ovlp = (long long)(MIN(hap_end, xEndPos)) - (long long)(MAX(hap_beg, xBegPos)) + 1;
    hap_match += (ovlp >= 0? ovlp : 0);

    // if(inp_match > (xEndPos - xBegPos + 1)) fprintf(stderr, "ERROR1\n");
    // if(hap_match > (xEndPos - xBegPos + 1)) fprintf(stderr, "ERRO2\n");
    // if(inp_match > hap_match) fprintf(stderr, "ERROR3\n");
    // fprintf(stderr, "tailIndex->a.n: %u, xReads->n: %u, total_match: %lld, hap_match: %lld, inp_match: %lld\n", 
    //                     tailIndex->a.n, xReads->n, (xEndPos - xBegPos + 1), hap_match, inp_match);

    return ((double)(inp_match)*CHAIN_MATCH) - ((double)(hap_match-inp_match)*CHAIN_UNMATCH);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x30(%rsp)
movq %r8, %r14
movq %rdx, %rbp
movq %rsi, 0x8(%rsp)
movq %rdi, %r12
movq $0x0, (%r8)
movq (%rcx), %rdx
testq %rdx, %rdx
je 0x10a599
movq %rcx, %r13
xorl %ebx, %ebx
xorl %r15d, %r15d
movq 0x10(%rbp), %rax
movq 0x10(%r13), %rcx
movslq (%rcx,%r15,4), %rcx
leaq (%rcx,%rcx,4), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl 0x10(%r12), %ecx
cmpq %rcx, %rbx
setb %dil
jae 0x10a502
movq 0x18(%r12), %rsi
movq (%rsi,%rbx,8), %r8
shrq $0x21, %r8
cmpl %r8d, %eax
je 0x10a4fd
leaq -0x1(%rcx), %rdi
cmpq %rbx, %rdi
je 0x10a502
movq 0x8(%rsi,%rbx,8), %r8
shrq $0x21, %r8
incq %rbx
cmpl %r8d, %eax
jne 0x10a4e0
cmpq %rcx, %rbx
setb %dil
testb %dil, %dil
jne 0x10a529
testq %rcx, %rcx
je 0x10a527
movq 0x18(%r12), %rsi
xorl %ebx, %ebx
movq (%rsi,%rbx,8), %rdi
shrq $0x21, %rdi
cmpl %edi, %eax
je 0x10a529
incq %rbx
cmpq %rbx, %rcx
jne 0x10a50e
movq %rcx, %rbx
jmp 0x10a529
xorl %ebx, %ebx
cmpq %rcx, %rbx
jae 0x10a57f
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x10a55c
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
movl %ebx, (%rcx,%rax,4)
incq %r15
movq (%r13), %rdx
cmpq %rdx, %r15
jb 0x10a4a4
jmp 0x10a599
movq 0x17fa5a(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
leaq 0x12301e(%rip), %rsi    # 0x22d5ae
xorl %eax, %eax
callq 0x22b0
jmp 0x10a56d
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,4), %rsi
callq 0x10a1a4
movl 0x10(%r12), %edi
testq %rdi, %rdi
je 0x10a753
movq 0x18(%r12), %r13
movq 0x8(%rsp), %rax
movq 0x20(%rax), %r10
movq (%r14), %rax
movq %rax, 0x8(%rsp)
leaq -0x1(%rax), %r15
movq $-0x1, 0x18(%rsp)
movq $-0x2, %rbp
xorl %r11d, %r11d
xorl %ecx, %ecx
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
movq $-0x2, %rsi
movq $-0x1, 0x10(%rsp)
xorl %edx, %edx
movq (%r13,%rcx,8), %rax
movq %rax, %r8
shrq $0x21, %r8
movl (%r10,%r8,8), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
addq %rdx, %rbx
decq %rbx
leaq (%r8,%r8,2), %r8
movq 0x30(%rsp), %r9
cmpl $0x0, 0xc(%r9,%r8,8)
je 0x10a684
cmpq %rbp, %rdx
jle 0x10a67d
movq 0x78(%rsp), %r8
cmpq %r8, %rbp
cmovgeq %r8, %rbp
movq 0x70(%rsp), %r8
movq 0x18(%rsp), %r9
cmpq %r8, %r9
cmovleq %r8, %r9
subq %r9, %rbp
leaq 0x1(%rbp), %r8
cmpq $-0x1, %rbp
movl $0x0, %r9d
cmovlq %r9, %r8
addq %r8, 0x28(%rsp)
movq %rdx, 0x18(%rsp)
movq %rbx, %rbp
jmp 0x10a684
cmpq %rbx, %rbp
cmovleq %rbx, %rbp
cmpq 0x8(%rsp), %r11
setb %r8b
jae 0x10a71c
movq %rdi, %r12
movq %r13, %rdi
movq 0x10(%r14), %r13
movslq (%r13,%r11,4), %r9
cmpq %r9, %rcx
jbe 0x10a6c2
cmpq %r11, %r15
je 0x10a726
movslq 0x4(%r13,%r11,4), %r8
incq %r11
cmpq %r8, %rcx
ja 0x10a6a7
cmpq 0x8(%rsp), %r11
setb %r8b
testb %r8b, %r8b
movq %rdi, %r13
je 0x10a721
movq 0x10(%r14), %r8
movslq (%r8,%r11,4), %r8
cmpq %r8, %rcx
movq %r12, %rdi
jne 0x10a71c
cmpq %rsi, %rdx
jle 0x10a74a
movq 0x78(%rsp), %r8
cmpq %r8, %rsi
cmovgeq %r8, %rsi
movq 0x70(%rsp), %r8
movq 0x10(%rsp), %r9
cmpq %r8, %r9
cmovleq %r8, %r9
subq %r9, %rsi
leaq 0x1(%rsi), %r8
cmpq $-0x1, %rsi
movl $0x0, %esi
cmovlq %rsi, %r8
addq %r8, 0x20(%rsp)
movq %rdx, 0x10(%rsp)
jmp 0x10a734
movq %rsi, %rbx
jmp 0x10a734
movq %rsi, %rbx
jmp 0x10a731
movq 0x8(%rsp), %r11
movq %rsi, %rbx
movq %rdi, %r13
movq %r12, %rdi
movl %eax, %eax
addq %rax, %rdx
incq %rcx
movq %rbx, %rsi
cmpq %rdi, %rcx
jne 0x10a60a
jmp 0x10a785
cmpq %rbx, %rsi
cmovgq %rsi, %rbx
jmp 0x10a734
movq $0x0, 0x20(%rsp)
movq $-0x2, %rbx
movq $-0x1, 0x10(%rsp)
movq $-0x1, 0x18(%rsp)
movq $-0x2, %rbp
movq $0x0, 0x28(%rsp)
movq 0x78(%rsp), %rsi
cmpq %rsi, %rbx
cmovgeq %rsi, %rbx
movq 0x70(%rsp), %rdx
movq 0x10(%rsp), %rax
cmpq %rdx, %rax
cmovleq %rdx, %rax
subq %rax, %rbx
leaq 0x1(%rbx), %rax
xorl %ecx, %ecx
cmpq $-0x1, %rbx
cmovlq %rcx, %rax
addq 0x20(%rsp), %rax
cmpq %rsi, %rbp
cmovlq %rbp, %rsi
movq 0x18(%rsp), %rdi
cmpq %rdx, %rdi
cmovgq %rdi, %rdx
subq %rdx, %rsi
leaq 0x1(%rsi), %rdx
cmpq $-0x1, %rsi
cmovlq %rcx, %rdx
cvtsi2sd %rax, %xmm0
movq 0x28(%rsp), %rcx
subq %rax, %rcx
addq %rdx, %rcx
cvtsi2sd %rcx, %xmm1
mulsd 0x11f389(%rip), %xmm1   # 0x229b80
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq