// File: /workspace/github_cpp_makefile_star2_O2/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, 0x28(%rsp)
movq %r8, %r14
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, 0x20(%rsp)
pushq $0x8
popq %rsi
movq %r8, %rdi
callq 0x2120
andq $0x0, 0x50(%rsp)
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
testq %rbx, %rbx
jne 0x1d57e4
xorl %edi, %edi
jmp 0x1d5829
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 %eax, %eax
xorl %r12d, %r12d
cmpq %rax, %rbx
je 0x1d5850
movq (%r15,%rax,8), %rcx
cmpq $-0x1, %rcx
je 0x1d584b
shlq $0x20, %rcx
orq %rax, %rcx
movq %rcx, (%rdi,%r12,8)
incq %r12
incq %rax
jmp 0x1d582e
leaq (%rdi,%r12,8), %rsi
movq %rdi, 0x8(%rsp)
callq 0x1a3def
movq 0x8(%rsp), %r8
pushq $0x1
popq %rax
xorl %edx, %edx
xorl %ecx, %ecx
cmpq %r12, %rax
ja 0x1d58a7
jne 0x1d5877
movq (%r8,%rdx,8), %rsi
jmp 0x1d5888
movq (%r8,%rdx,8), %rsi
movq (%r8,%rax,8), %rdi
xorq %rsi, %rdi
shrq $0x20, %rdi
je 0x1d58a2
shlq $0x20, %rdx
orq %rax, %rdx
shrq $0x20, %rsi
movq 0x10(%rsp), %rdi
movq %rdx, (%rdi,%rsi,8)
incq %rcx
movq %rax, %rdx
incq %rax
jmp 0x1d586a
cmpq %r14, %rcx
jne 0x1d5acf
movq 0x7d729(%rip), %rax     # 0x252fe0
movq (%rax), %rdi
leaq 0x26a03(%rip), %rsi     # 0x1fc2c4
leaq 0x26a0e(%rip), %rdx     # 0x1fc2d6
xorl %ebx, %ebx
movq %r14, %rcx
xorl %eax, %eax
callq 0x22b0
movq 0x8(%rsp), %rsi
movq %r13, 0x30(%rsp)
cmpq %r14, %rbx
je 0x1d5a45
movq 0x10(%rsp), %rax
movq (%rax,%rbx,8), %rax
movl %eax, %edx
shrq $0x20, %rax
subq %rax, %rdx
je 0x1d5ab0
leaq (%rsi,%rax,8), %rax
movq %rax, 0x18(%rsp)
movl (%rax), %eax
cmpq %rbx, %rax
jne 0x1d5ab0
movq %rbx, 0x38(%rsp)
imulq $0x18, %rbx, %rbx
movq 0x20(%rsp), %rax
leaq (%rax,%rbx), %r13
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %rdx, %rbp
movl 0xc(%r13), %eax
cmpq %rax, %r12
jae 0x1d5999
movq (%r13), %rcx
movl 0xc(%rcx,%r15), %eax
cmpq %r14, %rax
jae 0x1d5990
cmpl %eax, 0x4(%rcx,%r15)
ja 0x1d5990
movq 0x10(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movl %ecx, %r8d
shrq $0x20, %rcx
subq %rcx, %r8
je 0x1d5a72
leaq (%rsi,%rcx,8), %rcx
cmpl (%rcx), %eax
jne 0x1d5a72
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rbp, %rdx
leaq 0x40(%rsp), %r9
callq 0x1daf54
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
incq %r12
addq $0x28, %r15
jmp 0x1d592e
movq 0x30(%rsp), %r13
addq %r13, %rbx
xorl %r15d, %r15d
xorl %r12d, %r12d
movl 0xc(%rbx), %eax
cmpq %rax, %r12
jae 0x1d5a0e
movq (%rbx), %rcx
movl 0xc(%rcx,%r15), %eax
cmpq %r14, %rax
jae 0x1d5a05
cmpl %eax, 0x4(%rcx,%r15)
ja 0x1d5a05
movq 0x10(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movl %ecx, %r8d
shrq $0x20, %rcx
subq %rcx, %r8
je 0x1d5a91
leaq (%rsi,%rcx,8), %rcx
cmpl (%rcx), %eax
jne 0x1d5a91
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movq %rbp, %rdx
leaq 0x40(%rsp), %r9
callq 0x1daf54
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
incq %r12
addq $0x28, %r15
jmp 0x1d59a7
cmpq $0x2, %rdx
movq 0x38(%rsp), %rbx
jb 0x1d5a3d
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rax
movl (%rax,%rbx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %r13, %rdi
movq 0x18(%rsp), %rsi
callq 0x1db2a0
movq 0x8(%rsp), %rsi
incq %rbx
jmp 0x1d58de
movq 0x10(%rsp), %rdi
movq %rsi, %rbx
callq 0x2220
movq %rbx, %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 0x2688a(%rip), %rdi     # 0x1fc303
leaq 0x21a59(%rip), %rsi     # 0x1f74d9
leaq 0x267e0(%rip), %rcx     # 0x1fc267
movl $0x4333, %edx           # imm = 0x4333
callq 0x2110
leaq 0x2686b(%rip), %rdi     # 0x1fc303
leaq 0x21a3a(%rip), %rsi     # 0x1f74d9
leaq 0x267c1(%rip), %rcx     # 0x1fc267
movl $0x4340, %edx           # imm = 0x4340
callq 0x2110
leaq 0x2682a(%rip), %rdi     # 0x1fc2e1
leaq 0x21a1b(%rip), %rsi     # 0x1f74d9
leaq 0x267a2(%rip), %rcx     # 0x1fc267
movl $0x4323, %edx           # imm = 0x4323
callq 0x2110
leaq 0x26784(%rip), %rdi     # 0x1fc25a
leaq 0x219fc(%rip), %rsi     # 0x1f74d9
leaq 0x26783(%rip), %rcx     # 0x1fc267
movl $0x431e, %edx           # imm = 0x431E
callq 0x2110