// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %r12
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %r14
movq (%rdi), %r15
leaq (%r15,%rsi), %rcx
cmpq %rcx, 0x8(%rdi)
jae 0x1c9f62
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%r14)
movq 0x10(%r14), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
addq %rbx, %rcx
jmp 0x1c9f66
movq 0x10(%r14), %rax
movq %rcx, (%r14)
leaq (%rax,%r15,8), %rdi
movq %rbx, (%rsp)
leaq (,%rbx,8), %rdx
movl $0xff, %esi
callq 0x20e0
movq 0x10(%r14), %rax
leaq (%rax,%r15,8), %r15
xorl %ecx, %ecx
cmpq (%rsp), %rcx
je 0x1ca0de
movq (%rax,%rcx,8), %rdi
movl %edi, %esi
shrq $0x20, %rdi
cmpq %rdi, %rsi
jbe 0x1ca204
cmpq $-0x1, (%r15,%rcx,8)
je 0x1c9fb6
incq %rcx
jmp 0x1c9f8d
movq %rcx, %rdx
shlq $0x20, %rdx
movq %rdi, %r8
incq %r8
cmpq %rsi, %r8
jae 0x1ca052
movq (%r12,%r8,8), %r9
movq (%r13,%r9,8), %r11
movl %r11d, %r10d
shrq $0x20, %r11
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
cmpq %r10, %r11
jae 0x1ca00d
movq (%rax,%r11,8), %rbx
movq %rbx, %rbp
shrq $0x20, %rbp
cmpq %r9, %rbp
jne 0x1ca169
movl %ebx, %ebx
movq %rbx, %rbp
orq %rdx, %rbp
movq %rbp, (%r15,%rbx,8)
incq %r11
jmp 0x1c9fe3
movq (%r12,%r8,8), %r9
xorq $0x1, %r9
movq (%r13,%r9,8), %r11
movl %r11d, %r10d
shrq $0x20, %r11
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
cmpq %r10, %r11
jae 0x1c9fc0
movq (%rax,%r11,8), %rbp
movq %rbp, %rbx
shrq $0x20, %rbx
cmpq %r9, %rbx
jne 0x1ca188
movl %ebp, %ebx
movq %rbx, %rbp
orq %rdx, %rbp
movq %rbp, (%r15,%rbx,8)
incq %r11
jmp 0x1ca028
movq (%r12,%rdi,8), %rdi
movq (%r13,%rdi,8), %r9
movl %r9d, %r8d
shrq $0x20, %r9
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
cmpq %r8, %r9
jae 0x1ca094
movq (%rax,%r9,8), %r10
movq %r10, %r11
shrq $0x20, %r11
cmpq %rdi, %r11
jne 0x1ca1c6
movl %r10d, %r10d
movq %r10, %r11
orq %rdx, %r11
movq %r11, (%r15,%r10,8)
incq %r9
jmp 0x1ca069
movq (%r12,%rsi,8), %rsi
xorq $0x1, %rsi
movq (%r13,%rsi,8), %r8
movl %r8d, %edi
shrq $0x20, %r8
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
cmpq %rdi, %r8
jae 0x1c9fb1
movq (%rax,%r8,8), %r9
movq %r9, %r10
shrq $0x20, %r10
cmpq %rsi, %r10
jne 0x1ca1e5
movl %r9d, %r9d
movq %r9, %r10
orq %rdx, %r10
movq %r10, (%r15,%r9,8)
incq %r8
jmp 0x1ca0af
movq (%rsp), %rbx
leaq (%r15,%rbx,8), %rsi
movq %r15, %rdi
callq 0x1a3def
movq %rbx, (%r14)
pushq $0x1
popq %rcx
xorl %eax, %eax
xorl %edx, %edx
cmpq %rbx, %rcx
ja 0x1ca14e
je 0x1ca10d
movq (%r15,%rcx,8), %rsi
xorq (%r15,%rdx,8), %rsi
shrq $0x20, %rsi
je 0x1ca149
movq %rax, %rsi
shlq $0x20, %rsi
cmpq %rcx, %rdx
jae 0x1ca143
movq (%r15,%rdx,8), %rdi
cmpq $-0x1, %rdi
je 0x1ca1a7
movl %edi, %edi
orq %rsi, %rdi
movq (%r14), %r8
movq 0x10(%r14), %r9
leaq 0x1(%r8), %r10
movq %r10, (%r14)
movq %rdi, (%r9,%r8,8)
incq %rdx
jmp 0x1ca114
incq %rax
movq %rcx, %rdx
incq %rcx
jmp 0x1ca0f8
addq %rbx, %rbx
cmpq %rbx, (%r14)
jne 0x1ca223
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x306b2(%rip), %rdi     # 0x1fa822
leaq 0x2d362(%rip), %rsi     # 0x1f74d9
leaq 0x30656(%rip), %rcx     # 0x1fa7d4
movl $0x2f3d, %edx           # imm = 0x2F3D
callq 0x2110
leaq 0x30693(%rip), %rdi     # 0x1fa822
leaq 0x2d343(%rip), %rsi     # 0x1f74d9
leaq 0x30637(%rip), %rcx     # 0x1fa7d4
movl $0x2f44, %edx           # imm = 0x2F44
callq 0x2110
leaq 0x30687(%rip), %rdi     # 0x1fa835
leaq 0x2d324(%rip), %rsi     # 0x1f74d9
leaq 0x30618(%rip), %rcx     # 0x1fa7d4
movl $0x2f5b, %edx           # imm = 0x2F5B
callq 0x2110
leaq 0x30655(%rip), %rdi     # 0x1fa822
leaq 0x2d305(%rip), %rsi     # 0x1f74d9
leaq 0x305f9(%rip), %rcx     # 0x1fa7d4
movl $0x2f4b, %edx           # imm = 0x2F4B
callq 0x2110
leaq 0x30636(%rip), %rdi     # 0x1fa822
leaq 0x2d2e6(%rip), %rsi     # 0x1f74d9
leaq 0x305da(%rip), %rcx     # 0x1fa7d4
movl $0x2f52, %edx           # imm = 0x2F52
callq 0x2110
leaq 0x305c3(%rip), %rdi     # 0x1fa7ce
leaq 0x2d2c7(%rip), %rsi     # 0x1f74d9
leaq 0x305bb(%rip), %rcx     # 0x1fa7d4
movl $0x2f31, %edx           # imm = 0x2F31
callq 0x2110
leaq 0x30623(%rip), %rdi     # 0x1fa84d
leaq 0x2d2a8(%rip), %rsi     # 0x1f74d9
leaq 0x3059c(%rip), %rcx     # 0x1fa7d4
movl $0x2f63, %edx           # imm = 0x2F63
callq 0x2110