// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: ul_homo_path_check(ul_resolve_t*, ma_ug_t*, unsigned int, unsigned int, unsigned int, float, asg64_v*, asg64_v*)
uint32_t ul_homo_path_check(ul_resolve_t *uidx, ma_ug_t *ug, uint32_t v, uint32_t w, uint32_t raw_ug_occ, float match_rate, asg64_v *b, asg64_v *rb)
{
    // if(((v>>1) == 8158 && (w>>1) == 22318) || ((v>>1) == 16783 && (w>>1) == 28464)) {
    //     fprintf(stderr, "\n++++++[M::%s::] v>>1::%u, v&1::%u, w>>1::%u, w&1::%u\n", 
    //     __func__, v>>1, v&1, w>>1, w&1);
    // }
    
    bubble_type *bub = uidx->bub; 
    uint64_t k, l, z, bn = b->n, vn = 0, wn = 0, *va, *wa, rbn = rb->n, *rva, *rwa, rvn, rwn, rid, x;
    get_ul_path_info(uidx, ug, v, NULL, NULL, NULL, NULL, NULL, b); vn = b->n - bn;
    get_ul_path_info(uidx, ug, w, NULL, NULL, NULL, NULL, NULL, b); wn = b->n - bn - vn;
    va = b->a + bn; wa = b->a + bn + vn;
    // if(((v>>1) == 8158 && (w>>1) == 22318) || ((v>>1) == 16783 && (w>>1) == 28464)) {
    //     fprintf(stderr, "[M::%s::] vn::%lu, wn::%lu\n", __func__, vn, wn);
    // }

    gen_ug_integer_seq_on_fly(uidx, va, vn, rb); rvn = rb->n - rbn;
    gen_ug_integer_seq_on_fly(uidx, wa, wn, rb); rwn = rb->n - rbn - rvn;
    rva = rb->a + rbn; rwa = rb->a + rbn + rvn;
    // if(((v>>1) == 8158 && (w>>1) == 22318) || ((v>>1) == 16783 && (w>>1) == 28464)) {
    //     fprintf(stderr, "[M::%s::] rvn::%lu, rwn::%lu, raw_ug_occ::%u\n", __func__, rvn, rwn, raw_ug_occ);
    // }

    b->n = bn;
    for (k = 0; k < rvn; k++) {
        rid = rva[k]>>1;
        if(IF_BUB(rid, *bub)) {
            x = bub->index[rid]; 
            x |= ((uint64_t)(0x80000000)); x <<= 32; ///bubble id
        } else {
            x = rid; ///node id
            x <<= 32;
        }
        x |= (k<<1); kv_push(uint64_t, *b, x);
    }

    for (k = 0; k < rwn; k++) {
        rid = rwa[k]>>1;
        if(IF_BUB(rid, *bub)) {
            x = bub->index[rid]; 
            x |= ((uint64_t)(0x80000000)); x <<= 32; ///bubble id
        } else {
            x = rid; ///node id
            x <<= 32;
        }
        x |= 1; x |= (k<<1); kv_push(uint64_t, *b, x);
    }
    radix_sort_srt64(b->a + bn, b->a + b->n);

    uint64_t o[2], c[2];
    for (l = bn, k = bn + 1, o[0] = o[1] = 0; k <= b->n; k++) {
        if (k == b->n || (b->a[k]>>32) != (b->a[l]>>32)) {
            if((k - l > 1)) {
                for (z = l, c[0] = c[1] = 0; z < k; z++) {
                    c[b->a[z]&1]++;
                    if(c[0] > 0 && c[1] > 0) break;
                }
                if(c[0] > 0 && c[1] > 0) {
                    for (z = l; z < k; z++) {
                        o[b->a[z]&1]++;

                        x = b->a[z]; 
                        x <<= 32; x >>= 32; x >>= 1; 
                        if(b->a[z]&1) x|= ((uint64_t)(0x100000000));
                        b->a[z] = x;
                    }
                } else {
                    for (z = l; z < k; z++) b->a[z] = (uint64_t)-1;
                }
            } else {
                for (z = l; z < k; z++) b->a[z] = (uint64_t)-1;
            }
            l = k;
        }
    }
    // fprintf(stderr, "\n[M::%s::] rvn::%lu, rwn::%lu, bn::%lu, b->n::%lu, o[0]::%lu, o[1]::%lu\n", __func__, 
    // rvn, rwn, bn, (uint64_t)b->n, o[0], o[1]);
    radix_sort_srt64(b->a + bn, b->a + b->n); b->n = bn + o[0] + o[1]; 
    b->n = bn; rb->n = rbn;

    if(idx_check_rate(b->a + bn, o[0], rvn, raw_ug_occ, match_rate)) return 1;
    if(idx_check_rate(b->a + bn + o[0], o[1], rwn, raw_ug_occ, match_rate)) return 1;

    // if(o[0] >= (rvn*match_rate)) return 1;
    // if(o[1] >= (rwn*match_rate)) return 1;

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, %rbx
movss %xmm0, 0x24(%rsp)
movl %r8d, 0x20(%rsp)
movl %ecx, 0x18(%rsp)
movq %rsi, 0x30(%rsp)
movq 0xb0(%rsp), %r15
movq 0x28(%rdi), %r13
movq (%r9), %r12
movq %r12, 0x40(%rsp)
movq (%r15), %rax
movq %rax, 0x38(%rsp)
movq %r9, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %rdi, %rbp
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x20d67b
movq (%rbx), %r14
movq %r14, %rax
subq %r12, %rax
movq %rax, 0x28(%rsp)
movq %rbx, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %rbp, %rdi
movq 0x30(%rsp), %rsi
movl 0x18(%rsp), %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x20d67b
movq (%rbx), %r12
movq 0x10(%rbx), %rax
subq %r14, %r12
movq 0x40(%rsp), %rcx
leaq (%rax,%rcx,8), %rsi
leaq (%rax,%r14,8), %rax
movq %rax, 0x18(%rsp)
movq %rbp, %rdi
movq 0x28(%rsp), %rdx
movq %r15, %rcx
callq 0x20e46d
movq (%r15), %r14
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x20e46d
movq (%r15), %rax
movq %rax, 0x18(%rsp)
movq %r15, 0x30(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x48(%rsp)
movq %r14, 0x70(%rsp)
movq %r14, %rcx
movq 0x40(%rsp), %rax
movq %rax, (%rbx)
movq 0x38(%rsp), %rax
subq %rax, %rcx
movq %rcx, 0x28(%rsp)
je 0x212731
movq 0x38(%rsp), %rax
movq 0x48(%rsp), %rcx
leaq (%rcx,%rax,8), %r12
movq 0x10(%rbx), %rax
xorl %r15d, %r15d
movq 0x28(%rsp), %rbp
movq (%r12,%r15,4), %r14
shrq %r14
movq (%r13), %rcx
movq 0x80(%r13), %rdx
movl (%rcx,%r14,4), %ecx
incq %rdx
movq %rcx, %rsi
shlq $0x20, %rsi
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
orq %rdi, %rsi
shlq $0x20, %r14
cmpq %rcx, %rdx
cmovaq %rsi, %r14
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0x21271a
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
orq %r15, %r14
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r14, (%rax,%rcx,8)
addq $0x2, %r15
decq %rbp
jne 0x2126b2
movq 0x18(%rsp), %r12
movq 0x70(%rsp), %rax
subq %rax, %r12
movq %r12, 0x18(%rsp)
jne 0x21274e
movq 0x10(%rbx), %rax
jmp 0x2127e4
movq 0x48(%rsp), %rcx
leaq (%rcx,%rax,8), %r15
movq 0x10(%rbx), %rax
xorl %ebp, %ebp
movq (%r15,%rbp,4), %r14
shrq %r14
movq (%r13), %rcx
movq 0x80(%r13), %rdx
movl (%rcx,%r14,4), %ecx
incq %rdx
movq %rcx, %rsi
shlq $0x20, %rsi
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
orq %rdi, %rsi
shlq $0x20, %r14
cmpq %rcx, %rdx
cmovaq %rsi, %r14
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0x2127c5
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
orq %rbp, %r14
orq $0x1, %r14
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r14, (%rax,%rcx,8)
addq $0x2, %rbp
decq %r12
jne 0x21275d
movq 0x40(%rsp), %r12
leaq (%rax,%r12,8), %rdi
movq (%rbx), %rcx
leaq (%rax,%rcx,8), %rsi
callq 0x1ee586
leaq 0x1(%r12), %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq (%rbx), %rax
xorl %r13d, %r13d
cmpq %rax, %r14
jbe 0x21281a
movl 0x20(%rsp), %ebp
jmp 0x21292e
movq %r12, %rdi
movl 0x20(%rsp), %ebp
cmpq %rax, %r14
je 0x212840
movq 0x10(%rbx), %rax
movq (%rax,%rdi,8), %rcx
xorq (%rax,%r14,8), %rcx
shrq $0x20, %rcx
jne 0x212840
movq %rdi, %rcx
jmp 0x212914
movq %r14, %rax
subq %rdi, %rax
cmpq $0x1, %rax
jbe 0x2128e8
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq %r14, %rcx
cmpq %rdi, %r14
jbe 0x212914
movq 0x10(%rbx), %rax
leaq 0x1(%rdi), %rcx
movl -0x8(%rax,%rcx,8), %edx
andl $0x1, %edx
incq 0x50(%rsp,%rdx,8)
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
testq %rsi, %rsi
sete %r8b
testq %rdx, %rdx
sete %r9b
orb %r8b, %r9b
cmpb $0x1, %r9b
jne 0x2128a5
leaq 0x1(%rcx), %r8
cmpq %r14, %rcx
movq %r8, %rcx
jb 0x21286c
testq %rsi, %rsi
setne %al
testq %rdx, %rdx
setne %cl
testb %cl, %al
je 0x2128e8
movq %r14, %rcx
cmpq %rdi, %r14
jbe 0x212914
movq 0x10(%rbx), %rax
movq (%rax,%rdi,8), %rcx
movl %ecx, %edx
andl $0x1, %edx
movl %edx, %esi
incq 0x60(%rsp,%rsi,8)
shrl %ecx
shlq $0x20, %rdx
orq %rcx, %rdx
movq %rdx, (%rax,%rdi,8)
incq %rdi
cmpq %r14, %rdi
jb 0x2128c1
jmp 0x212911
movq %r14, %rcx
cmpq %rdi, %r14
jbe 0x212914
leaq (%r12,%r13), %rax
leaq 0x8(,%rax,8), %rdx
shlq $0x3, %rdi
subq %rdi, %rdx
addq 0x10(%rbx), %rdi
movl $0xff, %esi
callq 0x20e0
movq %r14, %rcx
incq %r14
movq (%rbx), %rax
incq %r13
movq %rcx, %rdi
cmpq %rax, %r14
jbe 0x212821
movq 0x60(%rsp), %r13
movq 0x10(%rbx), %rcx
leaq (%rcx,%r12,8), %rdi
leaq (,%r12,8), %r15
leaq (%rcx,%rax,8), %rsi
callq 0x1ee586
movq %r12, (%rbx)
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
movq %rcx, (%rax)
movq 0x10(%rbx), %rdi
addq %r15, %rdi
movl %ebp, %r14d
movq %r13, %rsi
movq 0x28(%rsp), %rdx
movq %r14, %rcx
movss 0x24(%rsp), %xmm0
callq 0x2123b6
testl %eax, %eax
je 0x21298f
movl $0x1, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq 0x10(%rbx), %r15
leaq (%r15,%r13,8), %rdi
movq 0x68(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r14, %rcx
movss 0x24(%rsp), %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2123b6