// File: /workspace/github_cpp_makefile_star2_O1/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, 0x2c(%rsp)
movl %r8d, 0x28(%rsp)
movl %ecx, 0x20(%rsp)
movq %rsi, 0x38(%rsp)
movq 0xc0(%rsp), %r15
movq 0x28(%rdi), %r13
movq (%r9), %r12
movq %r12, 0x48(%rsp)
movq (%r15), %rax
movq %rax, 0x40(%rsp)
movq %r9, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %rdi, %rbp
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x202015
movq (%rbx), %r14
movq %r14, %rax
subq %r12, %rax
movq %rax, 0x30(%rsp)
movq %rbx, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %rbp, %rdi
movq 0x38(%rsp), %rsi
movl 0x20(%rsp), %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x202015
movq (%rbx), %r12
movq 0x10(%rbx), %rax
subq %r14, %r12
movq 0x48(%rsp), %rcx
leaq (%rax,%rcx,8), %rsi
leaq (%rax,%r14,8), %rax
movq %rax, 0x20(%rsp)
movq %rbp, %rdi
movq 0x30(%rsp), %rdx
movq %r15, %rcx
callq 0x202e9a
movq (%r15), %r14
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x202e9a
movq (%r15), %rax
movq %rax, 0x20(%rsp)
movq %r15, 0x38(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x68(%rsp)
movq %r14, 0x80(%rsp)
movq %r14, %rcx
movq 0x48(%rsp), %rax
movq %rax, (%rbx)
movq 0x40(%rsp), %rax
subq %rax, %rcx
movq %rcx, 0x30(%rsp)
je 0x20724a
movq 0x40(%rsp), %rax
movq 0x68(%rsp), %rcx
leaq (%rcx,%rax,8), %r12
xorl %r15d, %r15d
movq 0x30(%rsp), %rbp
movq (%r12,%r15,4), %r14
shrq %r14
movq (%r13), %rax
movq 0x80(%r13), %rcx
movl (%rax,%r14,4), %eax
incq %rcx
movq %rax, %rdx
shlq $0x20, %rdx
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
orq %rsi, %rdx
shlq $0x20, %r14
cmpq %rax, %rcx
cmovaq %rdx, %r14
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x207228
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
orq %r15, %r14
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movq %r14, (%rcx,%rax,8)
addq $0x2, %r15
decq %rbp
jne 0x2071c2
movq 0x80(%rsp), %rax
subq %rax, 0x20(%rsp)
je 0x2072f9
movq 0x68(%rsp), %rcx
leaq (%rcx,%rax,8), %r15
xorl %ebp, %ebp
movq 0x20(%rsp), %r12
movq (%r15,%rbp,4), %r14
shrq %r14
movq (%r13), %rax
movq 0x80(%r13), %rcx
movl (%rax,%r14,4), %eax
incq %rcx
movq %rax, %rdx
shlq $0x20, %rdx
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
orq %rsi, %rdx
shlq $0x20, %r14
cmpq %rax, %rcx
cmovaq %rdx, %r14
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x2072d3
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
orq %rbp, %r14
orq $0x1, %r14
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movq %r14, (%rcx,%rax,8)
addq $0x2, %rbp
decq %r12
jne 0x20726d
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
movq 0x48(%rsp), %r12
leaq (%rcx,%r12,8), %rdi
leaq (%rcx,%rax,8), %rsi
callq 0x1e3b6a
leaq 0x1(%r12), %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movq (%rbx), %rax
cmpq %rax, %r14
jbe 0x207330
movl 0x28(%rsp), %ebp
jmp 0x207436
xorl %r13d, %r13d
movq %r12, %rdi
movl 0x28(%rsp), %ebp
cmpq %rax, %r14
je 0x207359
movq 0x10(%rbx), %rax
movq (%rax,%rdi,8), %rcx
xorq (%rax,%r14,8), %rcx
shrq $0x20, %rcx
jne 0x207359
movq %rdi, %rcx
jmp 0x207421
movq %r14, %rax
subq %rdi, %rax
cmpq $0x1, %rax
jbe 0x2073f5
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
cmpq %rdi, %r14
jbe 0x2073b2
movq 0x10(%rbx), %rax
leaq 0x1(%rdi), %rcx
movl -0x8(%rax,%rcx,8), %edx
andl $0x1, %edx
incq 0x50(%rsp,%rdx,8)
cmpq $0x0, 0x50(%rsp)
sete %dl
cmpq $0x0, 0x58(%rsp)
sete %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0x2073b2
leaq 0x1(%rcx), %rdx
cmpq %r14, %rcx
movq %rdx, %rcx
jb 0x20737e
cmpq $0x0, 0x50(%rsp)
je 0x2073f5
cmpq $0x0, 0x58(%rsp)
je 0x2073f5
movq %r14, %rcx
cmpq %rdi, %r14
jbe 0x207421
movq 0x10(%rbx), %rax
movq (%rax,%rdi,8), %rcx
movl %ecx, %edx
andl $0x1, %edx
movl %edx, %esi
incq 0x70(%rsp,%rsi,8)
shrl %ecx
shlq $0x20, %rdx
orq %rcx, %rdx
movq %rdx, (%rax,%rdi,8)
incq %rdi
cmpq %r14, %rdi
jb 0x2073ce
jmp 0x20741e
movq %r14, %rcx
cmpq %rdi, %r14
jbe 0x207421
leaq (%r12,%r13), %rdx
shlq $0x3, %rdx
shlq $0x3, %rdi
subq %rdi, %rdx
addq 0x10(%rbx), %rdi
addq $0x8, %rdx
movl $0xff, %esi
callq 0x20e0
movq %r14, %rcx
incq %r14
movq (%rbx), %rax
incq %r13
movq %rcx, %rdi
cmpq %rax, %r14
jbe 0x20733a
movq 0x10(%rbx), %rcx
leaq (%rcx,%r12,8), %rdi
leaq (,%r12,8), %r15
leaq (%rcx,%rax,8), %rsi
callq 0x1e3b6a
movq %r12, (%rbx)
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
movq %rcx, (%rax)
movq 0x10(%rbx), %rdi
addq %r15, %rdi
movq 0x70(%rsp), %r13
movl %ebp, %r14d
movq %r13, %rsi
movq 0x30(%rsp), %rdx
movq %r14, %rcx
movss 0x2c(%rsp), %xmm0
callq 0x206ec4
testl %eax, %eax
je 0x20749f
movl $0x1, %eax
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 0x2c(%rsp), %xmm0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x206ec4