// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x28(%rbp), %rdi
movl $0x8, %esi
callq 0x2140
movq %rax, -0x98(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x36b262
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
shrq %rax
orq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
shrq $0x2, %rax
orq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
shrq $0x4, %rax
orq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
shrq $0x8, %rax
orq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
shrq $0x10, %rax
orq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, -0x38(%rbp)
jmp 0x36b264
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x36b2c0
movq -0x18(%rbp), %rax
movq -0x68(%rbp), %rcx
cmpq $-0x1, (%rax,%rcx,8)
jne 0x36b287
jmp 0x36b2b2
movq -0x18(%rbp), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
orq -0x68(%rbp), %rdx
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x48(%rbp)
movq %rdx, (%rax,%rcx,8)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x36b26c
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x30ed90
movq $0x1, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x48(%rbp), %rax
ja 0x36b36f
movq -0x68(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x36b321
movq -0x38(%rbp), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq -0x38(%rbp), %rcx
movq -0x70(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
je 0x36b35c
movq -0x70(%rbp), %rdx
shlq $0x20, %rdx
orq -0x68(%rbp), %rdx
movq -0x98(%rbp), %rax
movq -0x38(%rbp), %rcx
movq -0x70(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
shrq $0x20, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x36b35e
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x36b2e8
movq -0x78(%rbp), %rax
cmpq -0x28(%rbp), %rax
jne 0x36b37b
jmp 0x36b39a
leaq 0x2fc01(%rip), %rdi     # 0x39af83
leaq 0x2ae30(%rip), %rsi     # 0x3961b9
movl $0x431e, %edx           # imm = 0x431E
leaq 0x2fbfb(%rip), %rcx     # 0x39af90
callq 0x2130
movq 0x58c3f(%rip), %rax     # 0x3c3fe0
movq (%rax), %rdi
movq -0x78(%rbp), %rcx
leaq 0x2fc3e(%rip), %rsi     # 0x39afed
leaq 0x2fc49(%rip), %rdx     # 0x39afff
movb $0x0, %al
callq 0x22a0
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x78(%rbp), %rax
jae 0x36b7da
movq -0x38(%rbp), %rax
movq -0x98(%rbp), %rcx
movq -0x68(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0x98(%rbp), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq -0x98(%rbp), %rcx
movq -0x68(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
subq %rcx, %rax
movq %rax, -0xb8(%rbp)
xorl %eax, %eax
cmpq $0x0, -0xb8(%rbp)
movb %al, -0xc1(%rbp)
jbe 0x36b451
movq -0xa8(%rbp), %rax
movq (%rax), %rax
movl %eax, %eax
cmpq -0x68(%rbp), %rax
sete %al
movb %al, -0xc1(%rbp)
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0x36b45d
jmp 0x36b45f
jmp 0x36b47e
leaq 0x2fba4(%rip), %rdi     # 0x39b00a
leaq 0x2ad4c(%rip), %rsi     # 0x3961b9
movl $0x4323, %edx           # imm = 0x4323
leaq 0x2fb17(%rip), %rcx     # 0x39af90
callq 0x2130
movq -0x8(%rbp), %rax
imulq $0x18, -0x68(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq -0xa0(%rbp), %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0x36b606
movq -0xa0(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x80(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, %eax
movq %rax, -0x88(%rbp)
movq -0xa0(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x80(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x78(%rbp), %rax
jb 0x36b500
jmp 0x36b5f5
movq -0x88(%rbp), %rax
cmpq -0x90(%rbp), %rax
jbe 0x36b515
jmp 0x36b5f5
movq -0x38(%rbp), %rax
movq -0x98(%rbp), %rcx
movq -0x90(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0x98(%rbp), %rax
movq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq -0x98(%rbp), %rcx
movq -0x90(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
subq %rcx, %rax
movq %rax, -0xc0(%rbp)
xorl %eax, %eax
cmpq $0x0, -0xc0(%rbp)
movb %al, -0xc2(%rbp)
jbe 0x36b59f
movq -0xb0(%rbp), %rax
movq (%rax), %rax
movl %eax, %eax
cmpq -0x90(%rbp), %rax
sete %al
movb %al, -0xc2(%rbp)
movb -0xc2(%rbp), %al
testb $0x1, %al
jne 0x36b5ab
jmp 0x36b5ad
jmp 0x36b5cc
leaq 0x2fa78(%rip), %rdi     # 0x39b02c
leaq 0x2abfe(%rip), %rsi     # 0x3961b9
movl $0x4333, %edx           # imm = 0x4333
leaq 0x2f9c9(%rip), %rcx     # 0x39af90
callq 0x2130
movq -0x8(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq -0xb8(%rbp), %rdx
movq -0xb0(%rbp), %rcx
movq -0xc0(%rbp), %r8
leaq -0x60(%rbp), %r9
callq 0x375fd0
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x36b499
movq -0x10(%rbp), %rax
imulq $0x18, -0x68(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq -0xa0(%rbp), %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0x36b78e
movq -0xa0(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x80(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, %eax
movq %rax, -0x88(%rbp)
movq -0xa0(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x80(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x78(%rbp), %rax
jb 0x36b688
jmp 0x36b77d
movq -0x88(%rbp), %rax
cmpq -0x90(%rbp), %rax
jbe 0x36b69d
jmp 0x36b77d
movq -0x38(%rbp), %rax
movq -0x98(%rbp), %rcx
movq -0x90(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0x98(%rbp), %rax
movq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq -0x98(%rbp), %rcx
movq -0x90(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
subq %rcx, %rax
movq %rax, -0xc0(%rbp)
xorl %eax, %eax
cmpq $0x0, -0xc0(%rbp)
movb %al, -0xc3(%rbp)
jbe 0x36b727
movq -0xb0(%rbp), %rax
movq (%rax), %rax
movl %eax, %eax
cmpq -0x90(%rbp), %rax
sete %al
movb %al, -0xc3(%rbp)
movb -0xc3(%rbp), %al
testb $0x1, %al
jne 0x36b733
jmp 0x36b735
jmp 0x36b754
leaq 0x2f8f0(%rip), %rdi     # 0x39b02c
leaq 0x2aa76(%rip), %rsi     # 0x3961b9
movl $0x4340, %edx           # imm = 0x4340
leaq 0x2f841(%rip), %rcx     # 0x39af90
callq 0x2130
movq -0x10(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq -0xb8(%rbp), %rdx
movq -0xb0(%rbp), %rcx
movq -0xc0(%rbp), %r8
leaq -0x60(%rbp), %r9
callq 0x375fd0
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x36b621
cmpq $0x1, -0xb8(%rbp)
jbe 0x36b7c7
movq -0x10(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq -0xb8(%rbp), %rdx
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x68(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %ecx
callq 0x376520
jmp 0x36b7c9
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x36b3c5
movq -0x98(%rbp), %rdi
callq 0x2210
movq -0x38(%rbp), %rdi
callq 0x2210
movq -0x50(%rbp), %rdi
callq 0x2210
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)