// File: /workspace/github_cpp_makefile_star2_O2/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 0xe48a3
movq %r8, %r14
movq %rcx, %r15
movq %rdx, %r12
movq %rdi, %rbp
movq 0x78(%rsp), %r13
movq 0x70(%rsp), %rbx
leaq 0x38(%rbx), %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x11976
leaq 0x18(%rbp), %rax
movq %rax, 0x28(%rsp)
movl %r15d, %eax
cvtsi2sd %r13, %xmm0
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %r13d, %r13d
imulq $0x28, %rax, %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rbp), %rdx
cmpq %rdx, %r13
jge 0xe48a3
movq %r13, %rax
andq $0x0, (%r12)
movq (%rbp), %rsi
movq %r13, %rdi
shlq $0x4, %rdi
leaq (%rsi,%rdi), %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)
andl $0x0, 0x1c(%rbx)
addq %rsi, %rdi
leaq -0x1(%rdx), %rsi
incq %r13
cmpq %rdx, %r13
jge 0xe481d
movl (%r8), %r10d
movl %r10d, %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
cmpl %r11d, %ecx
jne 0xe4817
shrl $0x1f, %r10d
addq $0x10, %r8
cmpl %r10d, %r9d
je 0xe47f0
leaq -0x1(%r13), %rsi
jmp 0xe4820
movq %rdx, %r13
cmpl %r15d, %ecx
je 0xe47a2
subq %rax, %rsi
incq %rsi
movq 0x10(%r14), %rax
movq 0x8(%rsp), %rdx
movl (%rax,%rdx), %r9d
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %r9d
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %eax
andl %edx, %eax
movq 0x28(%rsp), %rdx
movq %rbx, %rcx
movsd 0x30(%rsp), %xmm0
pushq $0x32
popq %r8
pushq $0x0
pushq %rax
callq 0x13350
addq $0x10, %rsp
movl (%rbx), %ecx
cmpl 0x10(%rbx), %ecx
je 0xe47a2
cvtsi2sd %rax, %xmm0
ucomisd 0x20(%rsp), %xmm0
jbe 0xe47a2
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl 0x14(%rsp), %ecx
xorl %r8d, %r8d
callq 0x12858
jmp 0xe47a2
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq