// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Hash_Table.cpp
// Function: calculate_overlap_region_by_chaining(Candidates_list*, overlap_region_alloc*, kvec_t_u64_warp*, unsigned long, unsigned long, All_reads*, ul_idx_t const*, double, int, overlap_region*, void*)
void calculate_overlap_region_by_chaining(Candidates_list* candidates, overlap_region_alloc* overlap_list, kvec_t_u64_warp* chain_idx,
                                          uint64_t readID, uint64_t readLength, All_reads* R_INF, const ul_idx_t *uref, double band_width_threshold, int add_beg_end, overlap_region* f_cigar, void *km)
{
    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;

    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_DP(candidates->list + sub_region_beg,
				sub_region_end - sub_region_beg + 1, &(candidates->chainDP), f_cigar, band_width_threshold,
				25, /**Get_READ_LENGTH((*R_INF), (*f_cigar).x_id)**/readLength, 
                R_INF?Get_READ_LENGTH((*R_INF), (*f_cigar).y_id):uref->ug->u.a[(*f_cigar).y_id].len, km);

        ///if (tmp_region.x_id != tmp_region.y_id && tmp_region.shared_seed > 1)
        if ((*f_cigar).x_id != (*f_cigar).y_id)
        {
            append_inexact_overlap_region_alloc(overlap_list, f_cigar, readLength, R_INF?Get_READ_LENGTH((*R_INF), (*f_cigar).y_id):uref->ug->u.a[(*f_cigar).y_id].len, add_beg_end, km);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movsd %xmm0, 0x20(%rsp)
movq %r8, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
cmpq $0x0, 0x8(%rdi)
je 0x3fb32
movq %rdx, %r12
movq %rdi, %rbp
movq 0x70(%rsp), %r13
movl $0x0, 0x40(%r13)
movq 0x8(%rdi), %rdx
testq %rdx, %rdx
jle 0x3fb32
movq %r9, %rbx
movq %rcx, %r15
leaq 0x18(%rbp), %rax
movq %rax, 0x18(%rsp)
xorl %r14d, %r14d
movq %r14, %rax
movq $0x0, (%r12)
movq (%rbp), %rcx
movq %r14, %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, (%r13)
movl %r9d, 0xc(%r13)
movl %ecx, 0x10(%r13)
movl $0x0, 0x1c(%r13)
leaq 0x1(%r14), %r10
cmpq %r10, %rdx
cmovgq %rdx, %r10
leaq -0x1(%r10), %rsi
cmpq %r14, %rsi
je 0x3fa70
incq %r14
movl (%r8), %edx
movl %edx, %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
cmpl %r11d, %ecx
jne 0x3fa6a
shrl $0x1f, %edx
addq $0x10, %r8
cmpl %edx, %r9d
je 0x3fa44
leaq -0x1(%r14), %rsi
jmp 0x3fa73
movq %r10, %r14
cmpl %r15d, %ecx
je 0x3fb25
testq %rbx, %rbx
je 0x3fa8b
movq 0x18(%rbx), %rdx
movl (%rdx,%rcx,8), %r10d
jmp 0x3faa9
movq 0x60(%rsp), %rdx
movq (%rdx), %rdx
movq 0x10(%rdx), %rdx
movl %ecx, %ecx
leaq (%rcx,%rcx,4), %rcx
movl (%rdx,%rcx,8), %r10d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r10d
subq %rax, %rsi
incq %rsi
movq 0x18(%rsp), %rdx
movq %r13, %rcx
movsd 0x20(%rsp), %xmm0
movl $0x19, %r8d
movq 0x8(%rsp), %r9
pushq 0x78(%rsp)
pushq %r10
callq 0x3f283
addq $0x10, %rsp
movl 0x10(%r13), %eax
cmpl %eax, (%r13)
je 0x3fb25
testq %rbx, %rbx
je 0x3faf0
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %rcx
jmp 0x3fb09
movq 0x60(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
leaq (%rax,%rax,4), %rax
movl (%rcx,%rax,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movq 0x8(%rsp), %rdx
movl 0x68(%rsp), %r8d
movq 0x78(%rsp), %r9
callq 0x3e645
movq 0x8(%rbp), %rdx
cmpq %rdx, %r14
jl 0x3f9ee
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq