// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: usg_misjoin_topocut_aux(ul_resolve_t*, ma_ug_t*, unsigned int, unsigned int, unsigned int, asg64_v*, asg64_v*)
uint32_t usg_misjoin_topocut_aux(ul_resolve_t *uidx, ma_ug_t *ug, uint32_t v, uint32_t ref_v, uint32_t beg_v, 
asg64_v *b, asg64_v *ub)
{
    #define cut_rate 0.49999
    uint32_t k, bn = b->n, is_del = 0, kk, ks, ke;
    uint64_t n_ref, n_v, *a_ref, *a_v, n_min, n_ref_ov, n_v_ov, *a_ref_ov, *a_v_ov, l_occ, r_occ, ref_occ, v_occ, ww[2]; 
    ub->n = 0; kv_push(uint64_t, *ub, beg_v);
    get_ul_path_info(uidx, ug, ref_v, NULL, NULL, NULL, NULL, NULL, ub); 
    // fprintf(stderr, "+++[M::%s::] start..., v>>1::%u, v&1::%u, ref_v>>1::%u, ref_v&1::%u, beg_v>>1::%u, beg_v&1::%u\n", __func__, v>>1, v&1, ref_v>>1, ref_v&1, beg_v>>1, beg_v&1);
    n_ref = gen_ug_integer_seq_on_fly(uidx, ub->a, ub->n, b);
    // fprintf(stderr, "+++[M::%s::] done...\n", __func__);

    ub->n = 0; kv_push(uint64_t, *ub, beg_v);
    get_ul_path_info(uidx, ug, v, NULL, NULL, NULL, NULL, NULL, ub);
    // fprintf(stderr, "---[M::%s::] start...\n", __func__);
    n_v = gen_ug_integer_seq_on_fly(uidx, ub->a, ub->n, b);
    // fprintf(stderr, "---[M::%s::] done...\n", __func__);
    
    a_ref = b->a + bn; a_v = b->a + bn + n_ref; n_min = MIN(n_ref, n_v); ub->n = 0;
    for (k = 0; k < n_min && a_ref[k] == a_v[k]; k++); 

    // if((beg_v>>1) == 5958) {
    //     fprintf(stderr, "[M::%s::] beg_v>>1::%u, beg_v&1::%u, v>>1::%u, v&1::%u, n_v::%lu, ref_v>>1::%u, ref_v&1::%u, n_ref::%lu, # prefix::%u\n", 
    //     __func__, beg_v>>1, beg_v&1, v>>1, v&1, n_v, ref_v>>1, ref_v&1, n_ref, k);
    //     for (kk = 0; kk < n_v; kk++) {
    //         fprintf(stderr, "+[M::%s::] a_v[%u]>>1::%lu, a_v[%u]&1::%lu\n", 
    //                                                 __func__, kk, a_v[kk]>>1, kk, a_v[kk]&1);
    //     }

    //     for (kk = 0; kk < n_ref; kk++) {
    //         fprintf(stderr, "+[M::%s::] a_ref[%u]>>1::%lu, a_ref[%u]&1::%lu\n", 
    //                                                 __func__, kk, a_ref[kk]>>1, kk, a_ref[kk]&1);
    //     }
    // }


    if(k > 0) {
        n_ref_ov = get_integer_seq_ovlps(uidx, a_ref, n_ref, k - 1, 1, ub, &ww[0]);
        n_v_ov = get_integer_seq_ovlps(uidx, a_v, n_v, k - 1, 1, ub, &ww[1]);
        if(n_v_ov <= (n_ref_ov*cut_rate)) {
            is_del = 1;
        } else {
            a_ref_ov = ub->a; a_v_ov = ub->a + n_ref_ov; kk = k;
            
            for (k = ref_occ = 0; k < n_ref_ov; k++) {
                ks = a_ref_ov[k]>>32; ke = (uint32_t)a_ref_ov[k];
                l_occ = kk - ks; r_occ = ke - kk;
                ref_occ += MIN(l_occ, r_occ);
            }

            for (k = v_occ = 0; k < n_v_ov; k++) {
                ks = a_v_ov[k]>>32; ke = (uint32_t)a_v_ov[k];
                l_occ = kk - ks; r_occ = ke - kk;
                v_occ += MIN(l_occ, r_occ);
            }

            if(v_occ <= (ref_occ*cut_rate)) is_del = 1;
        }
    }
    b->n = bn;
    return is_del;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %r12
movl %r8d, %ebx
movl %edx, 0x28(%rsp)
movq %rsi, %rbp
movq %rdi, %r13
movq 0x90(%rsp), %r14
movl (%r9), %eax
movq %rax, 0x38(%rsp)
movq $0x0, (%r14)
cmpq $0x0, 0x8(%r14)
jne 0x203d61
movq $0x2, 0x8(%r14)
movq 0x10(%r14), %rdi
movq %rbp, %r15
movl $0x10, %esi
movl %ecx, %ebp
callq 0x2370
movl %ebp, %ecx
movq %r15, %rbp
movq %rax, 0x10(%r14)
movl %ebx, %edi
movq (%r14), %rax
movq 0x10(%r14), %r8
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
movq %rdi, 0x20(%rsp)
movq %rdi, (%r8,%rax,8)
movq %r14, 0x10(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
xorl %r15d, %r15d
movq %r13, %rdi
movq %rbp, %rsi
movl %ecx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x202015
movq (%r14), %rdx
movq 0x10(%r14), %rsi
movq %r13, %rdi
movq %r12, %rcx
callq 0x202e9a
movq %rax, %rbx
movq $0x0, (%r14)
cmpq $0x0, 0x8(%r14)
jne 0x203ddd
movq $0x2, 0x8(%r14)
movq 0x10(%r14), %rdi
movl $0x10, %esi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
movq 0x20(%rsp), %rdx
movq %rdx, (%rcx,%rax,8)
movq %r14, 0x10(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
movq %r13, %rdi
movq %rbp, %rsi
movl 0x28(%rsp), %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x202015
movq (%r14), %rdx
movq 0x10(%r14), %rsi
movq %r13, 0x28(%rsp)
movq %r13, %rdi
movq %r12, %rcx
callq 0x202e9a
movq 0x38(%rsp), %rcx
leaq (,%rcx,8), %rsi
addq 0x10(%r12), %rsi
movq %rax, %r13
leaq (%rsi,%rbx,8), %rbp
cmpq %rax, %rbx
cmovbq %rbx, %rax
movq $0x0, (%r14)
testq %rax, %rax
movq %rbx, %rdx
je 0x203e75
xorl %r15d, %r15d
movq (%rsi,%r15,8), %rcx
cmpq (%rbp,%r15,8), %rcx
jne 0x203e75
incl %r15d
cmpq %r15, %rax
ja 0x203e62
xorl %ecx, %ecx
testl %r15d, %r15d
je 0x203fd1
movq %r12, 0x20(%rsp)
leal -0x1(%r15), %ebx
leaq 0x40(%rsp), %rax
movq %rax, (%rsp)
movl $0x1, %r8d
movq 0x28(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rcx
movq %r14, %r9
callq 0x2034cf
movl %eax, 0x34(%rsp)
leaq 0x48(%rsp), %rax
movq %rax, (%rsp)
movl $0x1, %r8d
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movq %r14, %r9
callq 0x2034cf
movl 0x34(%rsp), %edi
movl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movl %edi, %edx
cvtsi2sd %rdx, %xmm1
mulsd 0x35301(%rip), %xmm1    # 0x2391f0
ucomisd %xmm0, %xmm1
jae 0x203fc7
movq 0x10(%r14), %rsi
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
testl %edi, %edi
movq 0x20(%rsp), %r12
je 0x203f62
xorl %edi, %edi
xorl %r8d, %r8d
movq (%rsi,%rdi,8), %r9
movq %r9, %r10
shrq $0x20, %r10
movl %r15d, %r11d
subl %r10d, %r11d
subl %r15d, %r9d
cmpl %r9d, %r11d
cmovbl %r11d, %r9d
addq %r9, %r8
incq %rdi
cmpq %rdi, %rdx
jne 0x203f13
movq %r8, %xmm2
punpckldq 0x212fa(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x21302(%rip), %xmm2    # 0x225250
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
addsd %xmm2, %xmm0
mulsd 0x3528e(%rip), %xmm0    # 0x2391f0
testl %eax, %eax
je 0x203fb5
leaq (%rsi,%rdx,8), %rax
xorl %edx, %edx
xorl %esi, %esi
movq (%rax,%rdx,8), %rdi
movq %rdi, %r8
shrq $0x20, %r8
movl %r15d, %r9d
subl %r8d, %r9d
subl %r15d, %edi
cmpl %edi, %r9d
cmovbl %r9d, %edi
addq %rdi, %rsi
incq %rdx
cmpq %rdx, %rcx
jne 0x203f6e
movq %rsi, %xmm2
punpckldq 0x2129f(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x212a7(%rip), %xmm2    # 0x225250
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
movl $0x0, %ecx
jb 0x203fd1
movl $0x1, %ecx
jmp 0x203fd1
movl $0x1, %ecx
movq 0x20(%rsp), %r12
movq 0x38(%rsp), %rax
movq %rax, (%r12)
movl %ecx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq