// File: /workspace/github_cpp_makefile_star2_O2/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 %rcx, %r15
movq %rsi, 0x10(%rsp)
movq 0x8(%rdi), %rcx
testq %rcx, %rcx
je 0x13b2c
movq %r9, %rbx
movq %rdx, %r12
movq %rdi, %rbp
movq 0x70(%rsp), %r13
andl $0x0, 0x40(%r13)
leaq 0x18(%rdi), %rax
movq %rax, 0x18(%rsp)
xorl %eax, %eax
cmpq %rcx, %rax
jge 0x13b2c
andq $0x0, (%r12)
movq (%rbp), %rsi
movq %rax, %rdi
shlq $0x4, %rdi
leaq (%rsi,%rdi), %r8
addq $0x10, %r8
movl -0x10(%r8), %r9d
movl %r9d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
shrl $0x1f, %r9d
movl %r15d, (%r13)
movl %r9d, 0xc(%r13)
movl %edx, 0x10(%r13)
andl $0x0, 0x1c(%r13)
addq %rsi, %rdi
leaq -0x1(%rcx), %rsi
movq %rax, %r14
incq %r14
cmpq %rcx, %r14
jge 0x13a6f
movl (%r8), %r10d
movl %r10d, %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
cmpl %r11d, %edx
jne 0x13a69
shrl $0x1f, %r10d
addq $0x10, %r8
cmpl %r10d, %r9d
je 0x13a42
leaq -0x1(%r14), %rsi
jmp 0x13a72
movq %rcx, %r14
cmpl %r15d, %edx
je 0x13b20
testq %rbx, %rbx
je 0x13a8a
movq 0x18(%rbx), %rcx
movl (%rcx,%rdx,8), %r10d
jmp 0x13aa8
movq 0x60(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
movl %edx, %edx
imulq $0x28, %rdx, %rdx
movl (%rcx,%rdx), %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
pushq $0x19
popq %r8
movq 0x8(%rsp), %r9
pushq 0x78(%rsp)
pushq %r10
callq 0x13350
popq %rcx
popq %rdx
movl 0x10(%r13), %eax
cmpl %eax, (%r13)
je 0x13b20
testq %rbx, %rbx
je 0x13aeb
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %rcx
jmp 0x13b04
movq 0x60(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %rax, %rax
movl (%rcx,%rax), %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 0x1241f
movq 0x8(%rbp), %rcx
movq %r14, %rax
jmp 0x139f5
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq