// File: /workspace/github_cpp_makefile_star2_O2/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 %rcx, %r13
movq %rdx, %rbp
movq %rsi, 0x18(%rsp)
movq %rdi, %r12
andq $0x0, (%r8)
xorl %r15d, %r15d
xorl %ebx, %ebx
movq (%r13), %rdx
cmpq %rdx, %r15
jae 0xcbbcd
movq 0x10(%rbp), %rax
movq 0x10(%r13), %rcx
movslq (%rcx,%r15,4), %rcx
imulq $0x28, %rcx, %rcx
movq (%rax,%rcx), %rax
shrq $0x21, %rax
movl 0x10(%r12), %ecx
cmpq %rcx, %rbx
jae 0xcbb4a
movq 0x18(%r12), %rsi
movq (%rsi,%rbx,8), %rsi
shrq $0x21, %rsi
cmpl %esi, %eax
je 0xcbb67
incq %rbx
jmp 0xcbb2f
xorl %ebx, %ebx
cmpq %rbx, %rcx
je 0xcbbb0
movq 0x18(%r12), %rsi
movq (%rsi,%rbx,8), %rsi
shrq $0x21, %rsi
cmpl %esi, %eax
je 0xcbb67
incq %rbx
jmp 0xcbb4c
cmpq %rcx, %rbx
jae 0xcbbb3
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0xcbb9e
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
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
jmp 0xcbb05
movq %rcx, %rbx
movq 0x187426(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x11fada(%rip), %rsi    # 0x1eb69e
xorl %eax, %eax
callq 0x22b0
jmp 0xcbba8
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,4), %rsi
callq 0xcb8d4
movl 0x10(%r12), %edi
pushq $-0x2
popq %r13
pushq $-0x1
popq %r8
xorl %ebp, %ebp
movq %r8, %rcx
movq %r13, %rsi
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
xorl %r10d, %r10d
xorl %r11d, %r11d
cmpq %rdi, %r10
je 0xcbd18
movq %rbp, %r15
movq 0x18(%r12), %rdx
movq (%rdx,%r10,8), %rbp
movq %rbp, %rdx
shrq $0x21, %rdx
movq 0x18(%rsp), %rax
movq 0x20(%rax), %r9
movl (%r9,%rdx,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
leaq (%r15,%r9), %rbx
decq %rbx
imulq $0x18, %rdx, %rdx
movq 0x20(%rsp), %rax
cmpl $0x0, 0xc(%rax,%rdx)
je 0xcbc96
cmpq %rsi, %r15
jle 0xcbc8f
movq 0x68(%rsp), %rax
cmpq %rax, %rsi
cmovgeq %rax, %rsi
movq 0x60(%rsp), %rax
cmpq %rax, %rcx
cmovleq %rax, %rcx
subq %rcx, %rsi
leaq 0x1(%rsi), %rcx
cmpq $-0x1, %rsi
movl $0x0, %eax
cmovlq %rax, %rcx
addq %rcx, 0x10(%rsp)
movq %r15, %rcx
movq %rbx, %rsi
jmp 0xcbc96
cmpq %rbx, %rsi
cmovleq %rbx, %rsi
movl %ebp, %ebp
addq %r15, %rbp
movq (%r14), %r9
cmpq %r9, %r11
movq %r9, %rdx
cmovaq %r11, %rdx
cmpq %r9, %r11
jae 0xcbcbf
movq 0x10(%r14), %rax
movslq (%rax,%r11,4), %rax
cmpq %rax, %r10
jbe 0xcbcc4
incq %r11
jmp 0xcbca8
movq %rdx, %r11
jmp 0xcbd01
jne 0xcbd01
cmpq %r13, %r15
jle 0xcbd0f
movq 0x68(%rsp), %rax
cmpq %rax, %r13
cmovgeq %rax, %r13
movq 0x60(%rsp), %rax
cmpq %rax, %r8
cmovleq %rax, %r8
subq %r8, %r13
leaq 0x1(%r13), %rax
cmpq $-0x1, %r13
movl $0x0, %edx
cmovlq %rdx, %rax
addq %rax, 0x8(%rsp)
movq %r15, %r8
jmp 0xcbd04
movq %r13, %rbx
incq %r10
movq %rbx, %r13
jmp 0xcbc0a
cmpq %rbx, %r13
cmovgq %r13, %rbx
jmp 0xcbd04
movq 0x68(%rsp), %rdi
cmpq %rdi, %r13
cmovgeq %rdi, %r13
movq 0x60(%rsp), %r9
cmpq %r9, %r8
cmovleq %r9, %r8
subq %r8, %r13
leaq 0x1(%r13), %rax
xorl %edx, %edx
cmpq $-0x1, %r13
cmovlq %rdx, %rax
addq 0x8(%rsp), %rax
cmpq %rdi, %rsi
cmovlq %rsi, %rdi
cmpq %r9, %rcx
cmovgq %rcx, %r9
subq %r9, %rdi
leaq 0x1(%rdi), %rcx
cmpq $-0x1, %rdi
cmovlq %rdx, %rcx
cvtsi2sd %rax, %xmm0
movq 0x10(%rsp), %rdx
subq %rax, %rdx
addq %rcx, %rdx
cvtsi2sd %rdx, %xmm1
mulsd 0x11bef8(%rip), %xmm1   # 0x1e7c78
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq