// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movq %r9, %rbx
movss %xmm0, 0x34(%rsp)
movl %r8d, 0x5c(%rsp)
movl %ecx, 0x28(%rsp)
movq %rsi, 0x48(%rsp)
movq 0xc0(%rsp), %r12
movq 0x28(%rdi), %r15
movq (%r9), %rax
movq %rax, 0x38(%rsp)
movq (%r12), %rax
movq %rax, 0x40(%rsp)
movq %r9, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
xorl %ebp, %ebp
movq %rdi, %r14
movq %rdi, 0x20(%rsp)
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1c0c77
movq (%rbx), %r13
movq %r13, %rax
subq 0x38(%rsp), %rax
movq %rax, 0x50(%rsp)
movq %rbx, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movq 0x48(%rsp), %rsi
movl 0x28(%rsp), %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1c0c77
movq (%rbx), %r14
movq 0x10(%rbx), %rax
subq %r13, %r14
movq 0x38(%rsp), %rcx
leaq (%rax,%rcx,8), %rsi
leaq (%rax,%r13,8), %rax
movq %rax, 0x28(%rsp)
movq 0x20(%rsp), %rdi
movq 0x50(%rsp), %rdx
movq %r12, %rcx
callq 0x1c1740
movq (%r12), %r13
movq %r13, %rax
subq 0x40(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x1c1740
movq (%r12), %rax
movq %rax, 0x20(%rsp)
movq %r12, 0x28(%rsp)
movq 0x10(%r12), %rax
movq %rax, 0x80(%rsp)
movq 0x40(%rsp), %rcx
leaq (%rax,%rcx,8), %r14
movq 0x38(%rsp), %rax
movq %rax, (%rbx)
movq %r13, 0x48(%rsp)
cmpq %r13, 0x40(%rsp)
je 0x1c4f29
movq (%r14,%rbp,4), %r12
shrq %r12
movq (%r15), %rax
movq 0x80(%r15), %rcx
movl (%rax,%r12,4), %eax
incq %rcx
movq %rax, %rdx
shlq $0x20, %rdx
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
orq %rsi, %rdx
shlq $0x20, %r12
cmpq %rax, %rcx
cmovaq %rdx, %r12
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x1c4f0f
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
orq %rbp, %r12
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r12, (%rax,%rcx,8)
decq %r13
addq $0x2, %rbp
jmp 0x1c4e9b
movq 0x20(%rsp), %r13
movq 0x48(%rsp), %rax
subq %rax, %r13
movq 0x80(%rsp), %rcx
leaq (%rcx,%rax,8), %rbp
xorl %r14d, %r14d
movq %r13, 0x20(%rsp)
subq $0x1, %r13
jb 0x1c4fd9
movq (%rbp,%r14,4), %r12
shrq %r12
movq (%r15), %rax
movq 0x80(%r15), %rcx
movl (%rax,%r12,4), %eax
incq %rcx
movq %rax, %rdx
shlq $0x20, %rdx
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
orq %rsi, %rdx
shlq $0x20, %r12
cmpq %rax, %rcx
cmovaq %rdx, %r12
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x1c4fbe
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
orq %r14, %r12
orq $0x1, %r12
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r12, (%rax,%rcx,8)
addq $0x2, %r14
jmp 0x1c4f4a
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
movq 0x38(%rsp), %r14
leaq (%rcx,%r14,8), %rdi
leaq (%rcx,%rax,8), %rsi
callq 0x1a3def
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
pushq $0x1
popq %rax
movq %r14, %rcx
movq (%rbx), %rsi
movq %rcx, %rdi
subq %rsi, %rdi
leaq (,%rcx,8), %r8
movq %rax, %r10
leaq (%rcx,%r10), %rdx
cmpq %rsi, %rdx
ja 0x1c5106
movq %r10, %r9
movq %rdi, %r10
addq %r9, %r10
je 0x1c5048
movq 0x10(%rbx), %r10
movq (%r10,%rcx,8), %r11
addq %r8, %r10
xorq (%r10,%r9,8), %r11
shrq $0x20, %r11
leaq 0x1(%r9), %r10
testq %r11, %r11
je 0x1c5014
cmpq $0x1, %r9
jbe 0x1c50d8
movaps %xmm0, 0x60(%rsp)
xorl %edi, %edi
xorl %r8d, %r8d
movq %rcx, %rsi
cmpq %rdx, %rsi
jae 0x1c5097
movq 0x10(%rbx), %rdi
movl (%rdi,%rsi,8), %edi
andl $0x1, %edi
incq 0x60(%rsp,%rdi,8)
movq 0x60(%rsp), %r8
movq 0x68(%rsp), %rdi
testq %r8, %r8
setne %r9b
testq %rdi, %rdi
setne %r10b
andb %r9b, %r10b
incq %rsi
cmpb $0x1, %r10b
jne 0x1c505f
testq %r8, %r8
setne %sil
testq %rdi, %rdi
setne %dil
testb %dil, %sil
je 0x1c50eb
cmpq %rdx, %rcx
jae 0x1c50fe
movq 0x10(%rbx), %rsi
movq (%rsi,%rcx,8), %rdi
movl %edi, %r8d
andl $0x1, %r8d
movl %r8d, %r9d
incq 0x70(%rsp,%r9,8)
shrl %edi
shlq $0x20, %r8
orq %rdi, %r8
movq %r8, (%rsi,%rcx,8)
incq %rcx
jmp 0x1c50aa
cmpq %rdx, %rcx
jae 0x1c50fe
movq 0x10(%rbx), %rsi
orq $-0x1, (%rsi,%rcx,8)
incq %rcx
jmp 0x1c50d8
cmpq %rdx, %rcx
jae 0x1c50fe
movq 0x10(%rbx), %rsi
orq $-0x1, (%rsi,%rcx,8)
incq %rcx
jmp 0x1c50eb
movq %rdx, %rcx
jmp 0x1c5000
movq 0x10(%rbx), %rax
leaq (%rax,%r14,8), %rdi
leaq (,%r14,8), %r15
leaq (%rax,%rsi,8), %rsi
callq 0x1a3def
movq %r14, (%rbx)
movq 0x28(%rsp), %rax
movq 0x40(%rsp), %rcx
movq %rcx, (%rax)
movq 0x10(%rbx), %rdi
addq %r15, %rdi
movq 0x70(%rsp), %r13
movl 0x5c(%rsp), %r14d
movq %r13, %rsi
movq 0x50(%rsp), %rdx
movq %r14, %rcx
movss 0x34(%rsp), %xmm0
callq 0x1c4bc9
testl %eax, %eax
je 0x1c516f
pushq $0x1
popq %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq 0x10(%rbx), %r15
leaq (%r15,%r13,8), %rdi
movq 0x78(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %r14, %rcx
movss 0x34(%rsp), %xmm0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1c4bc9