// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Assembly.cpp
// Function: rescue_edges(ma_hit_t_alloc*, ma_hit_t_alloc*, long long, long long, float)
void rescue_edges(ma_hit_t_alloc* paf, ma_hit_t_alloc* rev_paf, 
long long readNum, long long rescue_threshold, float cluster_threshold)
{
    double startTime = Get_T();
    long long i, j, revises = 0;
    uint32_t qn, tn;

    kvec_t(uint64_t) edge_vector;
    kv_init(edge_vector);
    kvec_t(uint64_t) edge_vector_index;
    kv_init(edge_vector_index);
    uint64_t flag;
    int index;

    for (i = 0; i < readNum; i++)
    {
        edge_vector.n = 0;
        edge_vector_index.n = 0;
        for (j = 0; j < paf[i].length; j++)
        {
            qn = Get_qn(paf[i].buffer[j]);
            tn = Get_tn(paf[i].buffer[j]);
            index = get_specific_overlap(&(rev_paf[tn]), tn, qn);
            if(index != -1)
            {
                flag = tn;
                flag = flag << 32;
                flag = flag | (uint64_t)(index);
                kv_push(uint64_t, edge_vector, flag);
                kv_push(uint64_t, edge_vector_index, j);
            }
        }

        ///the read itself has these overlaps, but all related reads do not have 
        ///we need to remove all overlaps from paf[i], and then add all overlaps to rev_paf[i]
        if((long long)edge_vector.n >= rescue_threshold && 
        check_cluster(edge_vector.a, edge_vector.n, paf, cluster_threshold) == 1)
        {
            add_overlaps(&(paf[i]), &(rev_paf[i]), edge_vector_index.a, edge_vector_index.n);
            remove_overlaps(&(paf[i]), edge_vector_index.a, edge_vector_index.n);
            revises = revises + edge_vector.n;
        }

        edge_vector.n = 0;
        edge_vector_index.n = 0;
        for (j = 0; j < rev_paf[i].length; j++)
        {
            qn = Get_qn(rev_paf[i].buffer[j]);
            tn = Get_tn(rev_paf[i].buffer[j]);
            index = get_specific_overlap(&(paf[tn]), tn, qn);
            if(index != -1)
            {
                flag = tn;
                flag = flag << 32;
                flag = flag | (uint64_t)(index);
                kv_push(uint64_t, edge_vector, flag);
                kv_push(uint64_t, edge_vector_index, j);
            }
        }

        ///the read itself do not have these overlaps, but all related reads have
        ///we need to remove all overlaps from rev_paf[i], and then add all overlaps to paf[i]
        if((long long)edge_vector.n >= rescue_threshold && 
        check_cluster(edge_vector.a, edge_vector.n, paf, cluster_threshold) == 1)
        {
            remove_overlaps(&(rev_paf[i]), edge_vector_index.a, edge_vector_index.n);
            add_overlaps_from_different_sources(paf, &(paf[i]), edge_vector.a, edge_vector.n);
            revises = revises + edge_vector.n;
        }
    }


    kv_destroy(edge_vector);
    kv_destroy(edge_vector_index);

    fprintf(stderr, "[M::%s] took %0.2fs, revise edges #: %lld\n\n", __func__, Get_T()-startTime, revises);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movss %xmm0, 0x2c(%rsp)
movq %rcx, 0x50(%rsp)
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
callq 0x251c
movsd %xmm0, 0x60(%rsp)
xorl %eax, %eax
testq %rbx, %rbx
cmovleq %rax, %rbx
movq %rbx, 0x68(%rsp)
movq $0x0, 0x48(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x40(%rsp)
movq $0x0, 0x8(%rsp)
movq $0x0, 0x38(%rsp)
movq %r15, %r12
movq %r15, 0x58(%rsp)
movq %r14, 0x30(%rsp)
cmpq 0x68(%rsp), %rax
je 0xf6e1
movq %rbx, 0x10(%rsp)
movq %rax, 0x70(%rsp)
imulq $0x18, %rax, %rax
movq %rax, 0x18(%rsp)
leaq (%r14,%rax), %rdx
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r13d, %r13d
movq $0x0, (%rsp)
movq %rdx, 0x20(%rsp)
movl 0xc(%rdx), %eax
cmpq %rax, %r14
jae 0xf514
movq (%rdx), %rax
movl 0x4(%rax,%rbx), %edx
movl 0xc(%rax,%rbx), %ebp
imulq $0x18, %rbp, %rdi
addq %r12, %rdi
movl %ebp, %esi
callq 0x696ab
movq %rax, %r15
cmpl $-0x1, %r15d
jne 0xf461
movq 0x20(%rsp), %rdx
jmp 0xf508
movq 0x40(%rsp), %rax
cmpq %rax, %r13
jne 0xf49a
leaq (%rax,%rax), %r12
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r12
leaq (,%r12,8), %rsi
movq 0x10(%rsp), %rdi
callq 0x2370
movq %r13, %rdx
movq %r12, 0x40(%rsp)
movq 0x58(%rsp), %r12
jmp 0xf4a2
movq %r13, %rdx
movq 0x10(%rsp), %rax
shlq $0x20, %rbp
movslq %r15d, %rcx
orq %rbp, %rcx
movq %rax, 0x10(%rsp)
movq %rcx, (%rax,%rdx,8)
movq 0x38(%rsp), %rax
cmpq %rax, (%rsp)
jne 0xf4e7
leaq (%rax,%rax), %r15
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r15
leaq (,%r15,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x2370
movq %r15, 0x38(%rsp)
jmp 0xf4ec
movq 0x8(%rsp), %rax
movq 0x20(%rsp), %rdx
incq %r13
movq %rax, 0x8(%rsp)
movq (%rsp), %rcx
movq %r14, (%rax,%rcx,8)
incq %rcx
movq %rcx, (%rsp)
incq %r14
addq $0x28, %rbx
jmp 0xf429
cmpq 0x50(%rsp), %r13
jl 0xf571
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movq 0x30(%rsp), %rdx
movss 0x2c(%rsp), %xmm0
callq 0xf294
testl %eax, %eax
je 0xf571
movq 0x18(%rsp), %r15
leaq (%r12,%r15), %rsi
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rdx
movq (%rsp), %rbp
movq %rbp, %rcx
callq 0x6900b
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x690d3
addq %r13, 0x48(%rsp)
jmp 0xf576
movq 0x18(%rsp), %r15
addq %r12, %r15
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r13d, %r13d
movq $0x0, (%rsp)
movq %r15, 0x18(%rsp)
movl 0xc(%r15), %eax
cmpq %rax, %r14
jae 0xf676
movq (%r15), %rax
movl 0x4(%rax,%rbx), %edx
movl 0xc(%rax,%rbx), %ebp
imulq $0x18, %rbp, %rdi
addq 0x30(%rsp), %rdi
movl %ebp, %esi
callq 0x696ab
movq %rax, %r15
cmpl $-0x1, %r15d
jne 0xf5c9
movq 0x18(%rsp), %r15
jmp 0xf66a
movq 0x40(%rsp), %rax
cmpq %rax, %r13
jne 0xf5ff
leaq (%rax,%rax), %r12
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r12
leaq (,%r12,8), %rsi
movq 0x10(%rsp), %rdi
callq 0x2370
movq %r12, 0x40(%rsp)
movq 0x58(%rsp), %r12
jmp 0xf604
movq 0x10(%rsp), %rax
shlq $0x20, %rbp
movslq %r15d, %rcx
orq %rbp, %rcx
movq %rax, 0x10(%rsp)
movq %rcx, (%rax,%r13,8)
movq 0x38(%rsp), %rax
cmpq %rax, (%rsp)
jne 0xf649
leaq (%rax,%rax), %r15
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r15
leaq (,%r15,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x2370
movq %r15, 0x38(%rsp)
jmp 0xf64e
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %r15
incq %r13
movq %rax, 0x8(%rsp)
movq (%rsp), %rcx
movq %r14, (%rax,%rcx,8)
incq %rcx
movq %rcx, (%rsp)
incq %r14
addq $0x28, %rbx
jmp 0xf58e
cmpq 0x50(%rsp), %r13
jge 0xf689
movq 0x30(%rsp), %r14
movq 0x10(%rsp), %rbx
jmp 0xf6d4
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movq 0x30(%rsp), %r14
movq %r14, %rdx
movss 0x2c(%rsp), %xmm0
callq 0xf294
testl %eax, %eax
je 0xf6d4
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq (%rsp), %rdx
callq 0x690d3
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x69143
addq %r13, 0x48(%rsp)
movq 0x70(%rsp), %rax
incq %rax
jmp 0xf3f2
movq %rbx, %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0x2220
movq 0x2438e6(%rip), %rax    # 0x252fe0
movq (%rax), %rbx
callq 0x251c
subsd 0x60(%rsp), %xmm0
leaq 0x1d3770(%rip), %rsi    # 0x1e2e7f
leaq 0x1d3795(%rip), %rdx    # 0x1e2eab
movq %rbx, %rdi
movq 0x48(%rsp), %rcx
movb $0x1, %al
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0