// File: /workspace/github_cpp_makefile_star2_O1/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, 0x24(%rsp)
movq %rcx, 0x58(%rsp)
movq %rdx, %rbx
movq %rsi, 0x50(%rsp)
movq %rdi, %r12
callq 0x300fc
movsd %xmm0, 0x60(%rsp)
movq $0x0, 0x28(%rsp)
movl $0x0, %r14d
movl $0x0, %eax
movq %rbx, 0x68(%rsp)
testq %rbx, %rbx
jle 0x4596
movq $0x0, 0x40(%rsp)
xorl %eax, %eax
movq $0x0, 0x38(%rsp)
xorl %r14d, %r14d
movq $0x0, 0x28(%rsp)
xorl %ecx, %ecx
movq %r12, 0x48(%rsp)
movq %r14, 0x10(%rsp)
movq %rax, 0x8(%rsp)
movq %rcx, 0x70(%rsp)
imulq $0x18, %rcx, %rax
leaq (%r12,%rax), %r13
movq %rax, 0x18(%rsp)
cmpl $0x0, 0xc(%r12,%rax)
movq %r13, 0x30(%rsp)
je 0x438a
xorl %r15d, %r15d
movq $0x0, (%rsp)
xorl %r14d, %r14d
xorl %ebx, %ebx
movq (%r13), %rax
movl 0x4(%rax,%r15), %edx
movl 0xc(%rax,%r15), %r13d
imulq $0x18, %r13, %rdi
addq 0x50(%rsp), %rdi
movl %r13d, %esi
callq 0xa04f9
movq %rax, %rbp
cmpl $-0x1, %ebp
jne 0x42ce
movq 0x30(%rsp), %r13
jmp 0x4374
movq 0x38(%rsp), %rax
cmpq %rax, %r14
jne 0x42ff
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, 0x38(%rsp)
jmp 0x4304
movq 0x10(%rsp), %rax
shlq $0x20, %r13
movslq %ebp, %rcx
orq %r13, %rcx
movq %rax, 0x10(%rsp)
movq %rcx, (%rax,%r14,8)
movq 0x40(%rsp), %rax
cmpq %rax, (%rsp)
jne 0x434e
leaq (%rax,%rax), %r12
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r12
leaq (,%r12,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x2370
movq %r12, 0x40(%rsp)
movq 0x48(%rsp), %r12
jmp 0x4358
movq 0x48(%rsp), %r12
movq 0x8(%rsp), %rax
movq 0x30(%rsp), %r13
incq %r14
movq %rax, 0x8(%rsp)
movq (%rsp), %rcx
movq %rbx, (%rax,%rcx,8)
incq %rcx
movq %rcx, (%rsp)
incq %rbx
movl 0xc(%r13), %eax
addq $0x28, %r15
cmpq %rax, %rbx
jb 0x429d
jmp 0x4395
xorl %r14d, %r14d
movq $0x0, (%rsp)
cmpq 0x58(%rsp), %r14
jge 0x43a3
movq 0x18(%rsp), %r15
jmp 0x43f5
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movss 0x24(%rsp), %xmm0
callq 0x3bf57
testl %eax, %eax
movq 0x18(%rsp), %r15
je 0x43f5
movq 0x50(%rsp), %rax
leaq (%rax,%r15), %rsi
movq %r13, %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rdx
movq (%rsp), %rbp
movq %rbp, %rcx
callq 0x9fd92
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x9fe5c
addq %r14, 0x28(%rsp)
movq 0x50(%rsp), %rax
leaq (%rax,%r15), %rbp
cmpl $0x0, 0xc(%rax,%r15)
je 0x4510
xorl %ebx, %ebx
movq $0x0, (%rsp)
xorl %edx, %edx
xorl %r14d, %r14d
movq %rbp, 0x18(%rsp)
movq %rdx, %r13
movq (%rbp), %rax
movl 0x4(%rax,%rbx), %edx
movl 0xc(%rax,%rbx), %ebp
imulq $0x18, %rbp, %rdi
addq %r12, %rdi
movl %ebp, %esi
callq 0xa04f9
movq %rax, %r15
cmpl $-0x1, %r15d
jne 0x4451
movq 0x18(%rsp), %rbp
movq %r13, %rdx
jmp 0x44fb
movq 0x38(%rsp), %rax
cmpq %rax, %r13
jne 0x448a
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, 0x38(%rsp)
movq 0x48(%rsp), %r12
jmp 0x4492
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 0x40(%rsp), %rax
cmpq %rax, (%rsp)
jne 0x44da
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 %r13, %rdx
movq %r15, 0x40(%rsp)
jmp 0x44df
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %rbp
incq %rdx
movq %rax, 0x8(%rsp)
movq (%rsp), %rcx
movq %r14, (%rax,%rcx,8)
incq %rcx
movq %rcx, (%rsp)
incq %r14
movl 0xc(%rbp), %eax
addq $0x28, %rbx
cmpq %rax, %r14
jb 0x441e
jmp 0x451a
xorl %edx, %edx
movq $0x0, (%rsp)
cmpq 0x58(%rsp), %rdx
jge 0x452d
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %r14
jmp 0x4583
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rdx, %r15
movq %rdx, %rsi
movq %r12, %rdx
movss 0x24(%rsp), %xmm0
callq 0x3bf57
testl %eax, %eax
je 0x457e
movq %rbp, %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
movq (%rsp), %rdx
callq 0x9fe5c
movq %r12, %rdi
movq 0x30(%rsp), %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x9fed3
movq %rbx, %rax
addq %r15, 0x28(%rsp)
jmp 0x4583
movq 0x8(%rsp), %rax
movq 0x70(%rsp), %rcx
incq %rcx
cmpq 0x68(%rsp), %rcx
jne 0x4260
movq %r14, %rdi
movq %rax, %rbx
callq 0x2220
movq %rbx, %rdi
callq 0x2220
movq 0x285a30(%rip), %rax    # 0x289fe0
movq (%rax), %rbx
callq 0x300fc
subsd 0x60(%rsp), %xmm0
leaq 0x22088a(%rip), %rsi    # 0x224e4f
leaq 0x2208af(%rip), %rdx    # 0x224e7b
movq %rbx, %rdi
movq 0x28(%rsp), %rcx
movb $0x1, %al
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0