// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/anchor.cpp
// Function: calculate_ug_chaining(Candidates_list*, overlap_region_alloc*, kvec_t_u64_warp*, unsigned long, ma_utg_v*, double, int, overlap_region*, long long, double)
void calculate_ug_chaining(Candidates_list* candidates, overlap_region_alloc* overlap_list, kvec_t_u64_warp* chain_idx,
                                          uint64_t readID, ma_utg_v *ua, double band_width_threshold, int add_beg_end, overlap_region* f_cigar, long long mz_occ, double mz_rate)
{
    long long i = 0;
    uint64_t current_ID;
    uint64_t current_stand;

    if (candidates->length == 0)
    {
        return;
    }

    long long sub_region_beg;
    long long sub_region_end;
	long long chain_len;

    clear_fake_cigar(&((*f_cigar).f_cigar));

    i = 0;
    while (i < candidates->length)
    {
        chain_idx->a.n = 0;
        current_ID = candidates->list[i].readID;
        current_stand = candidates->list[i].strand;

        ///reference read
        (*f_cigar).x_id = readID;
        (*f_cigar).x_pos_strand = current_stand;
        ///query read
        (*f_cigar).y_id = current_ID;
        ///here the strand of query is always 0
        (*f_cigar).y_pos_strand = 0;  

        sub_region_beg = i;
        sub_region_end = i;
        i++;

        while (i < candidates->length 
        && 
        current_ID == candidates->list[i].readID
        &&
        current_stand == candidates->list[i].strand)
        {
            sub_region_end = i;
            i++;
        }

        if ((*f_cigar).x_id == (*f_cigar).y_id)
        {
            continue;
        }

		chain_len = chain_DP(candidates->list + sub_region_beg,
				sub_region_end - sub_region_beg + 1, &(candidates->chainDP), f_cigar, band_width_threshold,
				50, ua->a[(*f_cigar).x_id].len, ua->a[(*f_cigar).y_id].len, NULL);
		
		
		// if ((*f_cigar).x_id != (*f_cigar).y_id)
		if ((*f_cigar).x_id != (*f_cigar).y_id && chain_len > mz_occ*mz_rate)
        {
			append_utg_inexact_overlap_region_alloc(overlap_list, f_cigar, ua, add_beg_end, NULL);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, 0x14(%rsp)
movsd %xmm0, 0x30(%rsp)
movq %rsi, 0x18(%rsp)
cmpq $0x0, 0x8(%rdi)
je 0x1298f6
movq %r8, %r14
movq %rcx, %r15
movq %rdx, %r12
movq %rdi, %rbp
movq 0x70(%rsp), %rbx
leaq 0x38(%rbx), %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x3d054
movsd 0x8(%rsp), %xmm1
movq 0x8(%rbp), %rdx
testq %rdx, %rdx
jle 0x1298f6
leaq 0x18(%rbp), %rax
movq %rax, 0x28(%rsp)
movl %r15d, %eax
xorps %xmm0, %xmm0
cvtsi2sdq 0x78(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %r13d, %r13d
leaq (%rax,%rax,4), %rax
movq %rax, 0x8(%rsp)
movq %r13, %rax
movq $0x0, (%r12)
movq (%rbp), %rcx
movq %r13, %rsi
shlq $0x4, %rsi
leaq (%rcx,%rsi), %rdi
leaq (%rcx,%rsi), %r8
addq $0x10, %r8
movl -0x10(%r8), %r9d
movl %r9d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
shrl $0x1f, %r9d
movl %r15d, (%rbx)
movl %r9d, 0xc(%rbx)
movl %ecx, 0x10(%rbx)
movl $0x0, 0x1c(%rbx)
leaq 0x1(%r13), %r10
cmpq %r10, %rdx
cmovgq %rdx, %r10
leaq -0x1(%r10), %rsi
cmpq %r13, %rsi
je 0x12986f
incq %r13
movl (%r8), %edx
movl %edx, %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
cmpl %r11d, %ecx
jne 0x129869
shrl $0x1f, %edx
addq $0x10, %r8
cmpl %edx, %r9d
je 0x129843
leaq -0x1(%r13), %rsi
jmp 0x129872
movq %r10, %r13
cmpl %r15d, %ecx
je 0x1298e9
subq %rax, %rsi
incq %rsi
movq 0x10(%r14), %rax
movq 0x8(%rsp), %rdx
movl (%rax,%rdx,8), %r9d
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %r9d
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %eax
andl %edx, %eax
movq 0x28(%rsp), %rdx
movq %rbx, %rcx
movsd 0x30(%rsp), %xmm0
movl $0x32, %r8d
pushq $0x0
pushq %rax
callq 0x3e6e8
addq $0x10, %rsp
movl (%rbx), %ecx
cmpl 0x10(%rbx), %ecx
je 0x1298e9
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
ucomisd 0x20(%rsp), %xmm0
jbe 0x1298e9
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl 0x14(%rsp), %ecx
xorl %r8d, %r8d
callq 0x3df28
movq 0x8(%rbp), %rdx
cmpq %rdx, %r13
jl 0x1297f0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq