// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movss %xmm0, -0x24(%rbp)
callq 0x2510
movsd %xmm0, -0x30(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x1a7b1
movq $0x0, -0x68(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x8(%rbp), %rcx
imulq $0x18, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jge 0x1a508
movq -0x8(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x4c(%rbp)
movq -0x8(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movl -0x50(%rbp), %eax
imulq $0x18, %rax, %rax
addq %rax, %rdi
movl -0x50(%rbp), %esi
movl -0x4c(%rbp), %edx
callq 0xce1b0
movl %eax, -0x8c(%rbp)
cmpl $-0x1, -0x8c(%rbp)
je 0x1a4f5
movl -0x50(%rbp), %eax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movslq -0x8c(%rbp), %rcx
orq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x60(%rbp), %rax
jne 0x1a469
cmpq $0x0, -0x60(%rbp)
je 0x1a43b
movq -0x60(%rbp), %rax
shlq %rax
movq %rax, -0x98(%rbp)
jmp 0x1a449
movl $0x2, %eax
movq %rax, -0x98(%rbp)
jmp 0x1a449
movq -0x98(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, -0x58(%rbp)
movq -0x88(%rbp), %rdx
movq -0x58(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x68(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0x1a489
movq -0x80(%rbp), %rax
cmpq -0x78(%rbp), %rax
jne 0x1a4d8
cmpq $0x0, -0x78(%rbp)
je 0x1a4aa
movq -0x78(%rbp), %rax
shlq %rax
movq %rax, -0xa0(%rbp)
jmp 0x1a4b8
movl $0x2, %eax
movq %rax, -0xa0(%rbp)
jmp 0x1a4b8
movq -0xa0(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rdx
movq -0x70(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x80(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0x1a4f5
jmp 0x1a4f7
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x1a360
movq -0x68(%rbp), %rax
cmpq -0x20(%rbp), %rax
jl 0x1a577
movq -0x58(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x8(%rbp), %rdx
movss -0x24(%rbp), %xmm0
callq 0x1a1a0
cmpl $0x1, %eax
jne 0x1a577
movq -0x8(%rbp), %rdi
imulq $0x18, -0x38(%rbp), %rax
addq %rax, %rdi
movq -0x10(%rbp), %rsi
imulq $0x18, -0x38(%rbp), %rax
addq %rax, %rsi
movq -0x70(%rbp), %rdx
movq -0x80(%rbp), %rcx
callq 0xcd2d0
movq -0x8(%rbp), %rdi
imulq $0x18, -0x38(%rbp), %rax
addq %rax, %rdi
movq -0x70(%rbp), %rsi
movq -0x80(%rbp), %rdx
callq 0xcd450
movq -0x48(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x10(%rbp), %rcx
imulq $0x18, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jge 0x1a737
movq -0x10(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movl -0x50(%rbp), %eax
imulq $0x18, %rax, %rax
addq %rax, %rdi
movl -0x50(%rbp), %esi
movl -0x4c(%rbp), %edx
callq 0xce1b0
movl %eax, -0x8c(%rbp)
cmpl $-0x1, -0x8c(%rbp)
je 0x1a724
movl -0x50(%rbp), %eax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movslq -0x8c(%rbp), %rcx
orq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x60(%rbp), %rax
jne 0x1a698
cmpq $0x0, -0x60(%rbp)
je 0x1a66a
movq -0x60(%rbp), %rax
shlq %rax
movq %rax, -0xa8(%rbp)
jmp 0x1a678
movl $0x2, %eax
movq %rax, -0xa8(%rbp)
jmp 0x1a678
movq -0xa8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, -0x58(%rbp)
movq -0x88(%rbp), %rdx
movq -0x58(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x68(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0x1a6b8
movq -0x80(%rbp), %rax
cmpq -0x78(%rbp), %rax
jne 0x1a707
cmpq $0x0, -0x78(%rbp)
je 0x1a6d9
movq -0x78(%rbp), %rax
shlq %rax
movq %rax, -0xb0(%rbp)
jmp 0x1a6e7
movl $0x2, %eax
movq %rax, -0xb0(%rbp)
jmp 0x1a6e7
movq -0xb0(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rdx
movq -0x70(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x80(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0x1a724
jmp 0x1a726
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x1a58f
movq -0x68(%rbp), %rax
cmpq -0x20(%rbp), %rax
jl 0x1a79e
movq -0x58(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x8(%rbp), %rdx
movss -0x24(%rbp), %xmm0
callq 0x1a1a0
cmpl $0x1, %eax
jne 0x1a79e
movq -0x10(%rbp), %rdi
imulq $0x18, -0x38(%rbp), %rax
addq %rax, %rdi
movq -0x70(%rbp), %rsi
movq -0x80(%rbp), %rdx
callq 0xcd450
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
imulq $0x18, -0x38(%rbp), %rax
addq %rax, %rsi
movq -0x58(%rbp), %rdx
movq -0x68(%rbp), %rcx
callq 0xcd540
movq -0x48(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x1a7a0
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x1a33a
movq -0x58(%rbp), %rdi
callq 0x2210
movq -0x70(%rbp), %rdi
callq 0x2210
movq 0x3a9816(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
callq 0x2510
movq -0xb8(%rbp), %rdi
subsd -0x30(%rbp), %xmm0
movq -0x48(%rbp), %rcx
leaq 0x3673c9(%rip), %rsi    # 0x381bb9
leaq 0x3673ee(%rip), %rdx    # 0x381be5
movb $0x1, %al
callq 0x22a0
addq $0xc0, %rsp
popq %rbp
retq
nopw (%rax,%rax)