// File: /workspace/github_cpp_makefile_star2_O1/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), %rax
cmpq %rax, 0x8(%rdi)
jae 0x20d290
decq %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 0x1(%rcx), %rax
movq %rax, 0x8(%r14)
movq 0x10(%r14), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r14)
addq %rbx, (%r14)
leaq (,%r15,8), %rdi
addq 0x10(%r14), %rdi
leaq (,%rbx,8), %rdx
movl $0xff, %esi
callq 0x20e0
movq 0x10(%r14), %rax
leaq (%rax,%r15,8), %r15
testq %rbx, %rbx
je 0x20d43c
xorl %ecx, %ecx
movq %rbx, (%rsp)
movq (%rax,%rcx,8), %rsi
movl %esi, %edx
shrq $0x20, %rsi
cmpq %rsi, %rdx
jbe 0x20d56d
cmpq $-0x1, (%r15,%rcx,8)
jne 0x20d430
leaq 0x1(%rsi), %rdi
cmpq %rdx, %rdi
jae 0x20d392
movq %rcx, %r8
shlq $0x20, %r8
movq (%r12,%rdi,8), %r9
movq (%r13,%r9,8), %r11
movl %r11d, %r10d
shrq $0x20, %r11
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
cmpq %r10, %r11
jae 0x20d33e
movq (%rax,%r11,8), %rbx
movq %rbx, %rbp
shrq $0x20, %rbp
cmpq %r9, %rbp
jne 0x20d4d2
movl %ebx, %ebx
movq %rbx, %rbp
orq %r8, %rbp
movq %rbp, (%r15,%rbx,8)
incq %r11
cmpq %r11, %r10
jne 0x20d316
movq (%r12,%rdi,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 0x20d386
movq (%rax,%r11,8), %rbp
movq %rbp, %rbx
shrq $0x20, %rbx
cmpq %r9, %rbx
jne 0x20d4f1
movl %ebp, %ebx
movq %rbx, %rbp
orq %r8, %rbp
movq %rbp, (%r15,%rbx,8)
incq %r11
cmpq %r11, %r10
jne 0x20d35e
incq %rdi
cmpq %rdx, %rdi
jne 0x20d2fa
movq (%r12,%rsi,8), %rsi
movq (%r13,%rsi,8), %r8
movl %r8d, %edi
shrq $0x20, %r8
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
cmpq %rdi, %r8
movq (%rsp), %rbx
jae 0x20d3e2
movq %rcx, %r9
shlq $0x20, %r9
movq (%rax,%r8,8), %r10
movq %r10, %r11
shrq $0x20, %r11
cmpq %rsi, %r11
jne 0x20d52f
movl %r10d, %r10d
movq %r10, %r11
orq %r9, %r11
movq %r11, (%r15,%r10,8)
incq %r8
cmpq %r8, %rdi
jne 0x20d3b9
movq (%r12,%rdx,8), %rdx
xorq $0x1, %rdx
movq (%r13,%rdx,8), %rdi
movl %edi, %esi
shrq $0x20, %rdi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
cmpq %rsi, %rdi
jae 0x20d430
movq %rcx, %r8
shlq $0x20, %r8
movq (%rax,%rdi,8), %r9
movq %r9, %r10
shrq $0x20, %r10
cmpq %rdx, %r10
jne 0x20d54e
movl %r9d, %r9d
movq %r9, %r10
orq %r8, %r10
movq %r10, (%r15,%r9,8)
incq %rdi
cmpq %rdi, %rsi
jne 0x20d407
incq %rcx
cmpq %rbx, %rcx
jne 0x20d2c8
leaq (%r15,%rbx,8), %rsi
movq %r15, %rdi
callq 0x1e3b6a
movq %rbx, (%r14)
testq %rbx, %rbx
je 0x20d4b5
movl $0x1, %ecx
xorl %edx, %edx
xorl %eax, %eax
cmpq %rbx, %rcx
je 0x20d46c
movq (%r15,%rcx,8), %rsi
xorq (%r15,%rdx,8), %rsi
shrq $0x20, %rsi
je 0x20d4ab
cmpq %rcx, %rdx
jae 0x20d4a5
movq %rax, %rsi
shlq $0x20, %rsi
movq (%r15,%rdx,8), %rdi
cmpq $-0x1, %rdi
je 0x20d510
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
cmpq %rdx, %rcx
jne 0x20d478
incq %rax
movq %rcx, %rdx
incq %rcx
cmpq %rbx, %rcx
jbe 0x20d459
jmp 0x20d4b7
xorl %eax, %eax
addq %rbx, %rbx
cmpq %rbx, (%r14)
jne 0x20d58c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2f0a6(%rip), %rdi     # 0x23c57f
leaq 0x2be45(%rip), %rsi     # 0x239325
leaq 0x2f04a(%rip), %rcx     # 0x23c531
movl $0x2f3d, %edx           # imm = 0x2F3D
callq 0x2110
leaq 0x2f087(%rip), %rdi     # 0x23c57f
leaq 0x2be26(%rip), %rsi     # 0x239325
leaq 0x2f02b(%rip), %rcx     # 0x23c531
movl $0x2f44, %edx           # imm = 0x2F44
callq 0x2110
leaq 0x2f07b(%rip), %rdi     # 0x23c592
leaq 0x2be07(%rip), %rsi     # 0x239325
leaq 0x2f00c(%rip), %rcx     # 0x23c531
movl $0x2f5b, %edx           # imm = 0x2F5B
callq 0x2110
leaq 0x2f049(%rip), %rdi     # 0x23c57f
leaq 0x2bde8(%rip), %rsi     # 0x239325
leaq 0x2efed(%rip), %rcx     # 0x23c531
movl $0x2f4b, %edx           # imm = 0x2F4B
callq 0x2110
leaq 0x2f02a(%rip), %rdi     # 0x23c57f
leaq 0x2bdc9(%rip), %rsi     # 0x239325
leaq 0x2efce(%rip), %rcx     # 0x23c531
movl $0x2f52, %edx           # imm = 0x2F52
callq 0x2110
leaq 0x2efb7(%rip), %rdi     # 0x23c52b
leaq 0x2bdaa(%rip), %rsi     # 0x239325
leaq 0x2efaf(%rip), %rcx     # 0x23c531
movl $0x2f31, %edx           # imm = 0x2F31
callq 0x2110
leaq 0x2f017(%rip), %rdi     # 0x23c5aa
leaq 0x2bd8b(%rip), %rsi     # 0x239325
leaq 0x2ef90(%rip), %rcx     # 0x23c531
movl $0x2f63, %edx           # imm = 0x2F63
callq 0x2110