// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movss %xmm0, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x30(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %r9d
movq %r9, %rcx
movq %r9, %r8
movq $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x344830
movq -0x30(%rbp), %rax
movq (%rax), %rax
subq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x20(%rbp), %edx
movq -0x30(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %r9d
movq %r9, %rcx
movq %r9, %r8
movq $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x344830
movq -0x30(%rbp), %rax
movq (%rax), %rax
subq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq -0x60(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq 0x10(%rbp), %rcx
callq 0x345a40
movq 0x10(%rbp), %rax
movq (%rax), %rax
subq -0x80(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq 0x10(%rbp), %rcx
callq 0x345a40
movq 0x10(%rbp), %rax
movq (%rax), %rax
subq -0x80(%rbp), %rax
subq -0x98(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x80(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x80(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq -0x98(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x58(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x98(%rbp), %rax
jae 0x34bd8b
movq -0x88(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq %rax
movq %rax, -0xa8(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0xa8(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x38(%rbp), %rcx
movq 0x80(%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jae 0x34bcae
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0xa8(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq %rax, -0xb0(%rbp)
movabsq $0x80000000, %rax       # imm = 0x80000000
orq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0xb0(%rbp)
jmp 0x34bcce
movq -0xa8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0xb0(%rbp)
movq -0x40(%rbp), %rax
shlq %rax
orq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x34bd54
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x34bd13
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xd8(%rbp)
jmp 0x34bd21
movl $0x2, %eax
movq %rax, -0xd8(%rbp)
jmp 0x34bd21
movq -0xd8(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0xb0(%rbp), %rdx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x34bd7a
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x34bc1b
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0xa0(%rbp), %rax
jae 0x34bf15
movq -0x90(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq %rax
movq %rax, -0xa8(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0xa8(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x38(%rbp), %rcx
movq 0x80(%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jae 0x34be26
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0xa8(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq %rax, -0xb0(%rbp)
movabsq $0x80000000, %rax       # imm = 0x80000000
orq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0xb0(%rbp)
jmp 0x34be46
movq -0xa8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
orq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0x40(%rbp), %rax
shlq %rax
orq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x34bede
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x34be9d
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xe0(%rbp)
jmp 0x34beab
movl $0x2, %eax
movq %rax, -0xe0(%rbp)
jmp 0x34beab
movq -0xe0(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0xb0(%rbp), %rdx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x34bf04
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x34bd93
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x58(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rax
movq (%rax), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x30ed90
movq -0x58(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
movq -0x40(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq (%rcx), %rax
ja 0x34c1cf
movq -0x40(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq (%rcx), %rax
je 0x34bfbc
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq -0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x48(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
je 0x34c1bc
movq -0x40(%rbp), %rax
subq -0x48(%rbp), %rax
cmpq $0x1, %rax
jbe 0x34c17e
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0xc8(%rbp)
movq $0x0, -0xd0(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x34c044
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
andq $0x1, %rax
movq -0xd0(%rbp,%rax,8), %rcx
addq $0x1, %rcx
movq %rcx, -0xd0(%rbp,%rax,8)
cmpq $0x0, -0xd0(%rbp)
jbe 0x34c034
cmpq $0x0, -0xc8(%rbp)
jbe 0x34c034
jmp 0x34c044
jmp 0x34c036
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x34bfec
cmpq $0x0, -0xd0(%rbp)
jbe 0x34c146
cmpq $0x0, -0xc8(%rbp)
jbe 0x34c146
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x34c144
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
andq $0x1, %rax
movq -0xc0(%rbp,%rax,8), %rcx
addq $0x1, %rcx
movq %rcx, -0xc0(%rbp,%rax,8)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
shrq $0x20, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
shrq %rax
movq %rax, -0xb0(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x34c11c
movabsq $0x100000000, %rax      # imm = 0x100000000
orq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x34c068
jmp 0x34c17c
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x34c17a
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
movq $-0x1, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x34c14e
jmp 0x34c17c
jmp 0x34c1b4
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x34c1b2
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
movq $-0x1, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x34c186
jmp 0x34c1b4
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x34c1be
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x34bf6d
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x58(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rax
movq (%rax), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x30ed90
movq -0x58(%rbp), %rcx
addq -0xc0(%rbp), %rcx
addq -0xb8(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x58(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x80(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x58(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0xc0(%rbp), %rsi
movq -0x98(%rbp), %rdx
movl -0x24(%rbp), %eax
movl %eax, %ecx
movss -0x28(%rbp), %xmm0
callq 0x34b720
cmpl $0x0, %eax
je 0x34c26a
movl $0x1, -0x4(%rbp)
jmp 0x34c2bd
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x58(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0xc0(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0xb8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
movl -0x24(%rbp), %eax
movl %eax, %ecx
movss -0x28(%rbp), %xmm0
callq 0x34b720
cmpl $0x0, %eax
je 0x34c2b6
movl $0x1, -0x4(%rbp)
jmp 0x34c2bd
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopl (%rax)