// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movsd %xmm0, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movsd %xmm1, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x1a1e3d
jmp 0x1a2061
movq 0x10(%rbp), %rdi
addq $0x38, %rdi
callq 0x1e1d0
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jge 0x1a2061
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
shrl $0x1f, %eax
movl %eax, %eax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movq 0x10(%rbp), %rax
movl $0x0, 0x1c(%rax)
movq -0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rcx
movq -0x8(%rbp), %rdx
xorl %eax, %eax
cmpq 0x8(%rdx), %rcx
movb %al, -0x71(%rbp)
jge 0x1a1f5c
movq -0x50(%rbp), %rcx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x71(%rbp)
jne 0x1a1f5c
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rcx
movq (%rcx), %rcx
movq -0x48(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
shrl $0x1f, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
sete %al
movb %al, -0x71(%rbp)
movb -0x71(%rbp), %al
testb $0x1, %al
jne 0x1a1f65
jmp 0x1a1f7e
movq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x1a1ef8
movq 0x10(%rbp), %rax
movl (%rax), %eax
movq 0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jne 0x1a1f92
jmp 0x1a1e52
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x60(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x68(%rbp), %rsi
subq -0x60(%rbp), %rsi
addq $0x1, %rsi
movq -0x8(%rbp), %rdx
addq $0x18, %rdx
movq 0x10(%rbp), %rcx
movsd -0x30(%rbp), %xmm0
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rbp), %r8
movl (%r8), %r8d
imulq $0x28, %r8, %r8
addq %r8, %rax
movl (%rax), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rbp), %r8
movl 0x10(%r8), %r8d
imulq $0x28, %r8, %r8
addq %r8, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0x32, %r8d
xorl %r10d, %r10d
movl %eax, (%rsp)
movq $0x0, 0x8(%rsp)
callq 0x21ca0
movq %rax, -0x70(%rbp)
movq 0x10(%rbp), %rax
movl (%rax), %eax
movq 0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
je 0x1a205c
cvtsi2sdq -0x70(%rbp), %xmm0
cvtsi2sdq 0x18(%rbp), %xmm1
mulsd -0x40(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1a205c
movq -0x10(%rbp), %rdi
movq 0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl -0x34(%rbp), %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0x203b0
jmp 0x1a1e52
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)