// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %r9, 0x20(%rsp)
movq %r8, %r14
movq %rdx, %rbp
movq %rsi, %rbx
movq %rdi, %r12
movq $0x0, (%r8)
movq (%rcx), %rdx
testq %rdx, %rdx
je 0x10f6f2
movq %rcx, %r13
movq %rbx, 0x8(%rsp)
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
jae 0x10f652
movq 0x18(%r12), %rsi
movq (%rsi,%rbx,8), %rdi
shrq $0x21, %rdi
cmpl %edi, %eax
je 0x10f679
incq %rbx
cmpq %rbx, %rcx
jne 0x10f63e
testq %rcx, %rcx
je 0x10f677
movq 0x18(%r12), %rsi
xorl %ebx, %ebx
movq (%rsi,%rbx,8), %rdi
shrq $0x21, %rdi
cmpl %edi, %eax
je 0x10f679
incq %rbx
cmpq %rbx, %rcx
jne 0x10f65e
movq %rcx, %rbx
jmp 0x10f6ce
xorl %ebx, %ebx
cmpq %rcx, %rbx
jae 0x10f6ce
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x10f6b2
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movl %ebx, (%rax,%rcx,4)
incq %r15
movq (%r13), %rdx
cmpq %rdx, %r15
jb 0x10f617
jmp 0x10f6e8
movq 0x18590b(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
leaq 0x12a0df(%rip), %rsi    # 0x2397be
xorl %eax, %eax
callq 0x22b0
jmp 0x10f6bc
movq (%r14), %rax
movq 0x8(%rsp), %rbx
jmp 0x10f6f4
xorl %eax, %eax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,4), %rsi
callq 0x10f314
movl 0x10(%r12), %edi
testq %rdi, %rdi
je 0x10f84c
movq 0x18(%r12), %r8
movq 0x20(%rbx), %r10
movq (%r14), %rcx
movq $-0x1, %r15
movq $-0x2, %rax
xorl %r11d, %r11d
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
movq $-0x2, %rsi
movq $-0x1, 0x10(%rsp)
xorl %edx, %edx
movq (%r8,%r12,8), %r9
movq %r9, %rbp
shrq $0x21, %rbp
movl (%r10,%rbp,8), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
addq %rdx, %rbx
decq %rbx
leaq (%rbp,%rbp,2), %rbp
movq 0x20(%rsp), %r13
cmpl $0x0, 0xc(%r13,%rbp,8)
je 0x10f7c5
cmpq %rax, %rdx
jle 0x10f7be
movq 0x68(%rsp), %r13
cmpq %r13, %rax
cmovgeq %r13, %rax
movq 0x60(%rsp), %r13
cmpq %r13, %r15
cmovleq %r13, %r15
subq %r15, %rax
leaq 0x1(%rax), %r15
cmpq $-0x1, %rax
movl $0x0, %eax
cmovlq %rax, %r15
addq %r15, 0x8(%rsp)
movq %rdx, %r15
movq %rbx, %rax
jmp 0x10f7c5
cmpq %rbx, %rax
cmovleq %rbx, %rax
cmpq %rcx, %r11
jae 0x10f829
movq 0x10(%r14), %rbp
movslq (%rbp,%r11,4), %r13
cmpq %r13, %r12
jbe 0x10f7e5
incq %r11
cmpq %r11, %rcx
jne 0x10f7ce
movq %rcx, %r11
jmp 0x10f829
jne 0x10f829
cmpq %rsi, %rdx
jle 0x10f843
movq 0x68(%rsp), %r13
cmpq %r13, %rsi
cmovgeq %r13, %rsi
movq 0x60(%rsp), %r13
movq 0x10(%rsp), %rbp
cmpq %r13, %rbp
cmovleq %r13, %rbp
subq %rbp, %rsi
leaq 0x1(%rsi), %r13
cmpq $-0x1, %rsi
movl $0x0, %esi
cmovlq %rsi, %r13
addq %r13, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
jmp 0x10f82c
movq %rsi, %rbx
movl %r9d, %esi
addq %rsi, %rdx
incq %r12
movq %rbx, %rsi
cmpq %rdi, %r12
jne 0x10f753
jmp 0x10f87c
cmpq %rbx, %rsi
cmovgq %rsi, %rbx
jmp 0x10f82c
movq $0x0, 0x18(%rsp)
movq $-0x2, %rbx
movq $-0x1, 0x10(%rsp)
movq $-0x1, %r15
movq $-0x2, %rax
movq $0x0, 0x8(%rsp)
movq 0x68(%rsp), %rsi
cmpq %rsi, %rbx
cmovgeq %rsi, %rbx
movq 0x60(%rsp), %rdi
movq 0x10(%rsp), %rcx
cmpq %rdi, %rcx
cmovleq %rdi, %rcx
subq %rcx, %rbx
leaq 0x1(%rbx), %rcx
xorl %edx, %edx
cmpq $-0x1, %rbx
cmovlq %rdx, %rcx
addq 0x18(%rsp), %rcx
cmpq %rsi, %rax
cmovlq %rax, %rsi
cmpq %rdi, %r15
cmovgq %r15, %rdi
subq %rdi, %rsi
leaq 0x1(%rsi), %rax
cmpq $-0x1, %rsi
cmovlq %rdx, %rax
cvtsi2sd %rcx, %xmm0
movq 0x8(%rsp), %rdx
subq %rcx, %rdx
addq %rax, %rdx
cvtsi2sd %rdx, %xmm1
mulsd 0x12648f(%rip), %xmm1   # 0x235d78
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq