// File: chhylp123[P]hifiasm/gfa_ut.cpp
// Function: usg_unique_arcs_cluster(asg64_v*, unsigned long, unsigned long*, unsigned long*)
uint32_t usg_unique_arcs_cluster(asg64_v *b64, uint64_t a_n, uint64_t *idx, uint64_t *integ_seq)
{
    uint64_t bn = b64->n, k, v; 
    kv_resize(uint64_t, *b64, b64->n + a_n); b64->n += a_n; 
    memset(b64->a + bn, -1, sizeof((*(b64->a)))*a_n);
    uint64_t *cidx = b64->a + bn, s, e, i, zs, ze, z;

    ///b64.a[0, a_n]:: all resolvable paths with unique beg && end
    ///b64.a[a_n, bn]:: (raw unitig/non-unqiue node id)|(resolvable path id)
    ///idx:: the idx for b64.a[a_n, bn]
    for (i = 0; i < a_n; i++) {///available interval with beg/end with unique arcs
        s = b64->a[i]>>32; e = (uint32_t)(b64->a[i]); assert(e > s);
        if(cidx[i] != (uint64_t)-1) continue;
        for (k = s + 1; k < e; k++) {///note: here is [s, e]
            v = integ_seq[k];///v is the raw unitig id
            zs = (idx[v]<<1)>>33; ze = (uint32_t)idx[v];
            for (z = zs; z < ze; z++) {
                // if((b64->a[z]>>32)!=v) {
                //     fprintf(stderr, "[M::%s::] v::%lu, b64->a[z]::%lu, zs::%lu, ze::%lu, a_n::%lu\n", 
                //                                     __func__, v, b64->a[z]>>32, zs, ze, a_n);
                // }
                ///(b64->a[z]>>32):: raw unitig id
                ///(uint32_t)b64->a[z]:: available interval id
                assert((b64->a[z]>>32)==v);
                cidx[((uint32_t)b64->a[z])] = (i<<32)|((uint32_t)b64->a[z]);
            }

            v = integ_seq[k]^1;
            zs = (idx[v]<<1)>>33; ze = (uint32_t)idx[v];
            for (z = zs; z < ze; z++) {
                assert((b64->a[z]>>32)==v);
                cidx[((uint32_t)b64->a[z])] = (i<<32)|((uint32_t)b64->a[z]);
            }
        }
        v = integ_seq[s];
        zs = (idx[v]<<1)>>33; ze = (uint32_t)idx[v];
        for (z = zs; z < ze; z++) {
            assert((b64->a[z]>>32)==v);
            cidx[((uint32_t)b64->a[z])] = (i<<32)|((uint32_t)b64->a[z]);
        }

        v = integ_seq[e]^1;
        zs = (idx[v]<<1)>>33; ze = (uint32_t)idx[v];
        for (z = zs; z < ze; z++) {
            assert((b64->a[z]>>32)==v);
            cidx[((uint32_t)b64->a[z])] = (i<<32)|((uint32_t)b64->a[z]);
        }
    }

    radix_sort_srt64(cidx, cidx + a_n); b64->n = a_n;
    for (i = 0, k = 1, v = 0; k <= a_n; k++) {
        if(k == a_n || (cidx[i]>>32) != (cidx[k]>>32)) {
            for (z = i; z < k; z++) {
                assert(cidx[z] != (uint64_t)-1);
                ///cluest integer seqs-> (cluster id)|(available interval id) 
                b64->a[b64->n++] = (v<<32)|((uint32_t)cidx[z]);
            }
            i = k; v++;
        }
    }

    assert(b64->n == (a_n<<1));
    return v;///how many cluster
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq (%rcx), %rcx
addq -0x10(%rbp), %rcx
cmpq %rcx, %rax
jae 0x355fad
movq -0x8(%rbp), %rax
movq (%rax), %rcx
addq -0x10(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rcx
shrq %rcx
movq -0x8(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x2, %rcx
movq -0x8(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x4, %rcx
movq -0x8(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x8, %rcx
movq -0x8(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x10, %rcx
movq -0x8(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x355faf
movq -0x10(%rbp), %rcx
movq -0x8(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rdx
shlq $0x3, %rdx
movl $0xff, %esi
callq 0x2100
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x35640c
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
jbe 0x356049
jmp 0x356068
leaq 0x434af(%rip), %rdi     # 0x3994ff
leaq 0x40162(%rip), %rsi     # 0x3961b9
movl $0x2f31, %edx           # imm = 0x2F31
leaq 0x434a2(%rip), %rcx     # 0x399505
callq 0x2130
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
cmpq $-0x1, (%rax,%rcx,8)
je 0x35607c
jmp 0x3563fb
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x356251
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shlq %rax
shrq $0x21, %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0x356168
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
cmpq -0x38(%rbp), %rax
jne 0x356101
jmp 0x356120
leaq 0x4344b(%rip), %rdi     # 0x399553
leaq 0x400aa(%rip), %rsi     # 0x3961b9
movl $0x2f3d, %edx           # imm = 0x2F3D
leaq 0x433ea(%rip), %rcx     # 0x399505
callq 0x2130
movq -0x58(%rbp), %rdx
shlq $0x20, %rdx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
orq %rax, %rdx
movq -0x40(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x70(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x3560d7
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
xorq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shlq %rax
shrq $0x21, %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0x35623e
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
cmpq -0x38(%rbp), %rax
jne 0x3561d7
jmp 0x3561f6
leaq 0x43375(%rip), %rdi     # 0x399553
leaq 0x3ffd4(%rip), %rsi     # 0x3961b9
movl $0x2f44, %edx           # imm = 0x2F44
leaq 0x43314(%rip), %rcx     # 0x399505
callq 0x2130
movq -0x58(%rbp), %rdx
shlq $0x20, %rdx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
orq %rax, %rdx
movq -0x40(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x70(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x3561ad
jmp 0x356240
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x356088
movq -0x20(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shlq %rax
shrq $0x21, %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0x356323
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
cmpq -0x38(%rbp), %rax
jne 0x3562bc
jmp 0x3562db
leaq 0x43290(%rip), %rdi     # 0x399553
leaq 0x3feef(%rip), %rsi     # 0x3961b9
movl $0x2f4b, %edx           # imm = 0x2F4B
leaq 0x4322f(%rip), %rcx     # 0x399505
callq 0x2130
movq -0x58(%rbp), %rdx
shlq $0x20, %rdx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
orq %rax, %rdx
movq -0x40(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x70(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x356292
movq -0x20(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
xorq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shlq %rax
shrq $0x21, %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0x3563f9
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
cmpq -0x38(%rbp), %rax
jne 0x356392
jmp 0x3563b1
leaq 0x431ba(%rip), %rdi     # 0x399553
leaq 0x3fe19(%rip), %rsi     # 0x3961b9
movl $0x2f52, %edx           # imm = 0x2F52
leaq 0x43159(%rip), %rcx     # 0x399505
callq 0x2130
movq -0x58(%rbp), %rdx
shlq $0x20, %rdx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
orq %rax, %rdx
movq -0x40(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x70(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x356368
jmp 0x3563fb
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x356001
movq -0x40(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x10(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x30ed90
movq -0x10(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq $0x0, -0x58(%rbp)
movq $0x1, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x10(%rbp), %rax
ja 0x356535
movq -0x30(%rbp), %rax
cmpq -0x10(%rbp), %rax
je 0x356488
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
je 0x356522
movq -0x58(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x35650e
movq -0x40(%rbp), %rax
movq -0x70(%rbp), %rcx
cmpq $-0x1, (%rax,%rcx,8)
je 0x3564ab
jmp 0x3564ca
leaq 0x430b4(%rip), %rdi     # 0x399566
leaq 0x3fd00(%rip), %rsi     # 0x3961b9
movl $0x2f5b, %edx           # imm = 0x2F5B
leaq 0x43040(%rip), %rcx     # 0x399505
callq 0x2130
movq -0x38(%rbp), %rdx
shlq $0x20, %rdx
movq -0x40(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
orq %rax, %rdx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x8(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x356490
movq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x356524
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x356447
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
shlq %rcx
cmpq %rcx, %rax
jne 0x35654a
jmp 0x356569
leaq 0x4302d(%rip), %rdi     # 0x39957e
leaq 0x3fc61(%rip), %rsi     # 0x3961b9
movl $0x2f63, %edx           # imm = 0x2F63
leaq 0x42fa1(%rip), %rcx     # 0x399505
callq 0x2130
movq -0x38(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)