// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq %rcx, %rbx
movq %rdx, %r12
movq %rsi, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
movl $0x8, %esi
movq %r8, %rdi
callq 0x2120
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
testq %rbx, %rbx
jne 0x2247f4
movq $0x0, 0x8(%rsp)
xorl %edi, %edi
xorl %esi, %esi
callq 0x1ee586
jmp 0x224871
leaq -0x1(%rbx), %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, %rdi
xorl %r13d, %r13d
xorl %eax, %eax
movq (%r12,%rax,8), %rcx
cmpq $-0x1, %rcx
je 0x224856
shlq $0x20, %rcx
orq %rax, %rcx
movq %rcx, (%rdi,%r13,8)
incq %r13
incq %rax
cmpq %rax, %rbx
jne 0x22483e
leaq (%rdi,%r13,8), %rsi
movq %rdi, 0x8(%rsp)
callq 0x1ee586
testq %r13, %r13
jne 0x224875
xorl %eax, %eax
jmp 0x2248d2
leaq 0x1(%r13), %rax
cmpq $0x3, %rax
movl $0x2, %ecx
cmovaeq %rax, %rcx
movl $0x1, %edx
xorl %eax, %eax
xorl %esi, %esi
movq 0x8(%rsp), %r9
cmpq %rdx, %r13
jne 0x22489f
movq (%r9,%rsi,8), %rdi
jmp 0x2248b0
movq (%r9,%rsi,8), %rdi
movq (%r9,%rdx,8), %r8
xorq %rdi, %r8
shrq $0x20, %r8
je 0x2248ca
shlq $0x20, %rsi
orq %rdx, %rsi
shrq $0x20, %rdi
movq 0x10(%rsp), %r8
movq %rsi, (%r8,%rdi,8)
incq %rax
movq %rdx, %rsi
incq %rdx
cmpq %rdx, %rcx
jne 0x224894
cmpq %r14, %rax
jne 0x224b15
movq 0x706fe(%rip), %rax     # 0x294fe0
movq (%rax), %rdi
leaq 0x25a19(%rip), %rsi     # 0x24a305
leaq 0x25a24(%rip), %rdx     # 0x24a317
xorl %ebx, %ebx
movq %r14, %rcx
xorl %eax, %eax
callq 0x22b0
testq %r14, %r14
je 0x224a8e
xorl %ebx, %ebx
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rax
movq (%rax,%rbx,8), %rax
movl %eax, %r13d
shrq $0x20, %rax
subq %rax, %r13
je 0x224af6
leaq (%rsi,%rax,8), %r12
movl (%r12), %eax
cmpq %rbx, %rax
jne 0x224af6
movq %rbx, 0x38(%rsp)
leaq (%rbx,%rbx,2), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x28(%rsp)
movl 0xc(%rax,%rcx,8), %eax
testl %eax, %eax
je 0x2249c9
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rdx
leaq (%rcx,%rdx,8), %r15
xorl %ebx, %ebx
xorl %ebp, %ebp
movq (%r15), %rcx
movl 0xc(%rcx,%rbx), %edx
cmpq %r14, %rdx
jae 0x2249bb
cmpl %edx, 0x4(%rcx,%rbx)
ja 0x2249bb
movq 0x10(%rsp), %rax
movq (%rax,%rdx,8), %rax
movl %eax, %r8d
shrq $0x20, %rax
subq %rax, %r8
je 0x224ab8
leaq (%rsi,%rax,8), %rcx
cmpl (%rcx), %edx
jne 0x224ab8
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
leaq 0x40(%rsp), %r9
callq 0x22a3ec
movq 0x8(%rsp), %rsi
movl 0xc(%r15), %eax
incq %rbp
movl %eax, %ecx
addq $0x28, %rbx
cmpq %rcx, %rbp
jb 0x224966
movq 0x18(%rsp), %rax
movq 0x28(%rsp), %rdx
movl 0xc(%rax,%rdx,8), %eax
testl %eax, %eax
je 0x224a4b
movq 0x18(%rsp), %rcx
leaq (%rcx,%rdx,8), %r15
xorl %ebx, %ebx
xorl %ebp, %ebp
movq (%r15), %rcx
movl 0xc(%rcx,%rbx), %edx
cmpq %r14, %rdx
jae 0x224a3d
cmpl %edx, 0x4(%rcx,%rbx)
ja 0x224a3d
movq 0x10(%rsp), %rax
movq (%rax,%rdx,8), %rax
movl %eax, %r8d
shrq $0x20, %rax
subq %rax, %r8
je 0x224ad7
leaq (%rsi,%rax,8), %rcx
cmpl (%rcx), %edx
jne 0x224ad7
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
leaq 0x40(%rsp), %r9
callq 0x22a3ec
movq 0x8(%rsp), %rsi
movl 0xc(%r15), %eax
incq %rbp
movl %eax, %ecx
addq $0x28, %rbx
cmpq %rcx, %rbp
jb 0x2249e8
cmpq $0x2, %r13
movq 0x38(%rsp), %rbx
jb 0x224a7d
movq 0x30(%rsp), %rax
movq 0x20(%rax), %rax
movl (%rax,%rbx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x22a8b9
movq 0x8(%rsp), %rsi
incq %rbx
cmpq %r14, %rbx
jne 0x22490f
movq 0x50(%rsp), %rbx
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0x2220
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220
leaq 0x25885(%rip), %rdi     # 0x24a344
leaq 0x20b43(%rip), %rsi     # 0x245609
leaq 0x257db(%rip), %rcx     # 0x24a2a8
movl $0x4333, %edx           # imm = 0x4333
callq 0x2110
leaq 0x25866(%rip), %rdi     # 0x24a344
leaq 0x20b24(%rip), %rsi     # 0x245609
leaq 0x257bc(%rip), %rcx     # 0x24a2a8
movl $0x4340, %edx           # imm = 0x4340
callq 0x2110
leaq 0x25825(%rip), %rdi     # 0x24a322
leaq 0x20b05(%rip), %rsi     # 0x245609
leaq 0x2579d(%rip), %rcx     # 0x24a2a8
movl $0x4323, %edx           # imm = 0x4323
callq 0x2110
leaq 0x2577f(%rip), %rdi     # 0x24a29b
leaq 0x20ae6(%rip), %rsi     # 0x245609
leaq 0x2577e(%rip), %rcx     # 0x24a2a8
movl $0x431e, %edx           # imm = 0x431E
callq 0x2110