// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: update_paf(ma_hit_t_alloc*, ma_hit_t_alloc*, unsigned long*, unsigned long, unsigned long, asg_t*)
void update_paf(ma_hit_t_alloc *src, ma_hit_t_alloc *r_src, uint64_t *rmap, uint64_t rid_n, uint64_t pre_gn, asg_t *ng)
{
    asg64_v clus, bb; uint64_t k, l, dn = 0, j, qn, tn; 
    uint64_t *idx; ma_hit_t_alloc *z; 
    uint64_t *a0, *a1, a0n, a1n;
    CALLOC(idx, pre_gn); kv_init(bb);
    kv_init(clus); kv_resize(uint64_t, clus, rid_n);
    for (k = 0; k < rid_n; k++) {
        if(rmap[k] == ((uint64_t)-1)) continue;///scaffold
        clus.a[clus.n++] = (rmap[k]<<32)|k;
    }
    radix_sort_srt64(clus.a, clus.a+clus.n);
    for (k = 1, l = 0; k <= clus.n; k++) {
       if(k == clus.n || (clus.a[k]>>32) != (clus.a[l]>>32)) {
            idx[(clus.a[l]>>32)] = (l<<32)|k;
            l = k; dn++;
       }
    }
    assert(dn == pre_gn);
    fprintf(stderr, "+[M::%s] dn::%lu\n", __func__, dn);

    for (k = 0; k < dn; k++) {
        a0 = clus.a + (idx[k]>>32); a0n = ((uint32_t)idx[k]) - (idx[k]>>32);
        assert(a0n > 0 && ((uint32_t)a0[0]) == k);
        // if(k == 4265018) {
        //     fprintf(stderr, "\n[M::%s]\tgroup::%lu\n", __func__, k);
        //     for (j = 0; j < a0n; j++) {
        //         fprintf(stderr, "[M::%s]\tnid::%uu\n", __func__, (uint32_t)a0[j]);
        //     }
        // }
        z = &(src[k]);
        for (j = 0; j < z->length; j++) {
            qn = Get_qn(z->buffer[j]);
            tn = Get_tn(z->buffer[j]);
            if(tn >= dn) continue;
            if(qn > tn) continue;
            a1 = clus.a + (idx[tn]>>32); 
            a1n = ((uint32_t)idx[tn]) - (idx[tn]>>32);
            // fprintf(stderr, "+[M::%s] qn::%lu, tn::%lu, a0n::%lu, a1n::%lu\n", __func__, qn, tn, a0n, a1n);
            assert(a1n > 0 && ((uint32_t)a1[0]) == tn);
            renew_paf0(src, a0, a0n, a1, a1n, &bb);
        }

        z = &(r_src[k]);
        for (j = 0; j < z->length; j++) {
            qn = Get_qn(z->buffer[j]);
            tn = Get_tn(z->buffer[j]);
            if(tn >= dn) continue;
            if(qn > tn) continue;
            a1 = clus.a + (idx[tn]>>32); 
            a1n = ((uint32_t)idx[tn]) - (idx[tn]>>32);
            // fprintf(stderr, "-[M::%s] qn::%lu, tn::%lu, a0n::%lu, a1n::%lu\n", __func__, qn, tn, a0n, a1n);
            assert(a1n > 0 && ((uint32_t)a1[0]) == tn);
            renew_paf0(r_src, a0, a0n, a1, a1n, &bb);
        }



        // for (l = k; l < dn; l++) {
        //     a1 = clus.a + (idx[l]>>32); a1n = ((uint32_t)idx[l]) - (idx[l]>>32);
        //     fprintf(stderr, "[M::%s] k::%lu, l::%lu, a0n::%lu, a1n::%lu\n", __func__, k, l, a0n, a1n);
        //     assert(a1n > 0 && ((uint32_t)a1[0]) == l);
        //     renew_paf0(src, a0, a0n, a1, a1n);
        //     renew_paf0(r_src, a0, a0n, a1, a1n);
        // }
        if(a0n > 1) renew_paf1(r_src, a0, a0n, ng->seq[k].len);
    }
    free(idx); free(clus.a); free(bb.a);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, 0x30(%rsp)
movq %r8, %r12
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
movl $0x8, %esi
movq %r8, %rdi
callq 0x2120
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
testq %r14, %r14
jne 0x2192e1
xorl %r13d, %r13d
jmp 0x219326
leaq -0x1(%r14), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x8(,%rcx,8), %rdi
callq 0x2280
movq %rax, %r13
xorl %ebx, %ebx
testq %r14, %r14
je 0x219350
xorl %eax, %eax
movq (%r15,%rax,8), %rcx
cmpq $-0x1, %rcx
je 0x219348
shlq $0x20, %rcx
orq %rax, %rcx
movq %rcx, (%r13,%rbx,8)
incq %rbx
incq %rax
cmpq %rax, %r14
jne 0x21932f
leaq (,%rbx,8), %rsi
addq %r13, %rsi
movq %r13, %rdi
callq 0x1e3b6a
testq %rbx, %rbx
jne 0x21936c
xorl %ebp, %ebp
jmp 0x2193be
leaq 0x1(%rbx), %rcx
cmpq $0x3, %rcx
movl $0x2, %eax
cmovaeq %rcx, %rax
movl $0x1, %ecx
xorl %ebp, %ebp
xorl %edx, %edx
movq 0x8(%rsp), %rdi
cmpq %rcx, %rbx
je 0x2193a0
movq (%r13,%rdx,8), %rsi
xorq (%r13,%rcx,8), %rsi
shrq $0x20, %rsi
je 0x2193b6
movl 0x4(%r13,%rdx,8), %esi
shlq $0x20, %rdx
orq %rcx, %rdx
movq %rdx, (%rdi,%rsi,8)
incq %rbp
movq %rcx, %rdx
incq %rcx
cmpq %rcx, %rax
jne 0x21938b
cmpq %r12, %rbp
jne 0x219604
movq 0x70c12(%rip), %rax     # 0x289fe0
movq (%rax), %rdi
leaq 0x24c49(%rip), %rsi     # 0x23e021
leaq 0x24c54(%rip), %rdx     # 0x23e033
movq %rbp, %rcx
xorl %eax, %eax
callq 0x22b0
testq %rbp, %rbp
je 0x21957d
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
movq (%rax,%rbx,8), %rax
movl %eax, %r15d
shrq $0x20, %rax
subq %rax, %r15
je 0x2195e5
leaq (,%rax,8), %rax
addq %r13, %rax
movq %rax, 0x20(%rsp)
movl (%rax), %eax
cmpq %rbx, %rax
jne 0x2195e5
movq %rbx, 0x38(%rsp)
leaq (%rbx,%rbx,2), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x28(%rsp)
cmpl $0x0, 0xc(%rax,%rcx,8)
je 0x2194bc
movq 0x18(%rsp), %rax
movq 0x28(%rsp), %rcx
leaq (%rax,%rcx,8), %rbx
xorl %r12d, %r12d
xorl %r14d, %r14d
movq (%rbx), %rcx
movl 0xc(%rcx,%r12), %eax
cmpq %rax, %rbp
jbe 0x2194ad
cmpl %eax, 0x4(%rcx,%r12)
ja 0x2194ad
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movl %ecx, %r8d
shrq $0x20, %rcx
subq %rcx, %r8
je 0x2195a7
leaq (,%rcx,8), %rcx
addq %r13, %rcx
cmpl (%rcx), %eax
jne 0x2195a7
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
leaq 0x40(%rsp), %r9
callq 0x21ec0b
incq %r14
movl 0xc(%rbx), %eax
addq $0x28, %r12
cmpq %rax, %r14
jb 0x219456
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %rcx
cmpl $0x0, 0xc(%rax,%rcx,8)
je 0x219542
movq 0x10(%rsp), %rax
leaq (%rax,%rcx,8), %rbx
xorl %r12d, %r12d
xorl %r14d, %r14d
movq (%rbx), %rcx
movl 0xc(%rcx,%r12), %eax
cmpq %rax, %rbp
jbe 0x219533
cmpl %eax, 0x4(%rcx,%r12)
ja 0x219533
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movl %ecx, %r8d
shrq $0x20, %rcx
subq %rcx, %r8
je 0x2195c6
leaq (,%rcx,8), %rcx
addq %r13, %rcx
cmpl (%rcx), %eax
jne 0x2195c6
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
leaq 0x40(%rsp), %r9
callq 0x21ec0b
incq %r14
movl 0xc(%rbx), %eax
addq $0x28, %r12
cmpq %rax, %r14
jb 0x2194dc
cmpq $0x2, %r15
movq 0x38(%rsp), %rbx
jb 0x219571
movq 0x30(%rsp), %rax
movq 0x20(%rax), %rax
movl (%rax,%rbx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
callq 0x21f0a7
incq %rbx
cmpq %rbp, %rbx
jne 0x2193f4
movq 0x8(%rsp), %rdi
callq 0x2220
movq %r13, %rdi
callq 0x2220
movq 0x50(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220
leaq 0x24ab2(%rip), %rdi     # 0x23e060
leaq 0x1fd70(%rip), %rsi     # 0x239325
leaq 0x24a08(%rip), %rcx     # 0x23dfc4
movl $0x4333, %edx           # imm = 0x4333
callq 0x2110
leaq 0x24a93(%rip), %rdi     # 0x23e060
leaq 0x1fd51(%rip), %rsi     # 0x239325
leaq 0x249e9(%rip), %rcx     # 0x23dfc4
movl $0x4340, %edx           # imm = 0x4340
callq 0x2110
leaq 0x24a52(%rip), %rdi     # 0x23e03e
leaq 0x1fd32(%rip), %rsi     # 0x239325
leaq 0x249ca(%rip), %rcx     # 0x23dfc4
movl $0x4323, %edx           # imm = 0x4323
callq 0x2110
leaq 0x249ac(%rip), %rdi     # 0x23dfb7
leaq 0x1fd13(%rip), %rsi     # 0x239325
leaq 0x249ab(%rip), %rcx     # 0x23dfc4
movl $0x431e, %edx           # imm = 0x431E
callq 0x2110