// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movl %r8d, %ebp
movl %ecx, %r13d
movl %edx, 0x20(%rsp)
movq %rsi, %r14
movq %rdi, %r12
movq 0x80(%rsp), %r15
movq %r9, 0x18(%rsp)
movl (%r9), %eax
movq %rax, 0x28(%rsp)
movq $0x0, (%r15)
cmpq $0x0, 0x8(%r15)
je 0x20f286
movq 0x10(%r15), %rax
xorl %ecx, %ecx
jmp 0x20f2a3
movq $0x2, 0x8(%r15)
movq 0x10(%r15), %rdi
movl $0x10, %esi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
movl %ebp, %ebx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movq %rbx, (%rax,%rcx,8)
movq %r15, 0x10(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
movq %r12, %rdi
movq %r14, %rsi
movl %r13d, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x20d67b
movq (%r15), %rdx
movq 0x10(%r15), %rsi
movq %r12, %rdi
movq 0x18(%rsp), %rbp
movq %rbp, %rcx
callq 0x20e46d
movq %rax, %r13
movq $0x0, (%r15)
cmpq $0x0, 0x8(%r15)
movq 0x10(%r15), %rax
je 0x20f304
xorl %ecx, %ecx
jmp 0x20f320
movq $0x2, 0x8(%r15)
movl $0x10, %esi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movq %rbx, (%rax,%rcx,8)
movq %r15, 0x10(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
movq %r12, %rdi
movq %r14, %rsi
movl 0x20(%rsp), %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x20d67b
movq (%r15), %rdx
movq 0x10(%r15), %rsi
movq %r12, %rdi
movq %rbp, %rcx
callq 0x20e46d
movq %rax, %r14
movq 0x28(%rsp), %rdx
leaq (,%rdx,8), %rsi
addq 0x10(%rbp), %rsi
cmpq %rax, %r13
movq %rax, %rdi
cmovbq %r13, %rdi
movq $0x0, (%r15)
testq %rdi, %rdi
movl $0x0, %eax
je 0x20f511
leaq (%rsi,%r13,8), %rbp
xorl %ebx, %ebx
movq (%rsi,%rbx,8), %rcx
cmpq (%rbp,%rbx,8), %rcx
jne 0x20f3ad
incl %ebx
cmpq %rbx, %rdi
ja 0x20f39b
xorl %eax, %eax
testl %ebx, %ebx
je 0x20f4fb
leal -0x1(%rbx), %ecx
movq %rcx, 0x20(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
movl $0x1, %r8d
movq %r12, %rdi
movq %r13, %rdx
movq %r15, %r9
callq 0x20eadf
movl %eax, %r13d
leaq 0x38(%rsp), %rax
movq %rax, (%rsp)
movl $0x1, %r8d
movq %r12, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq 0x20(%rsp), %rcx
movq %r15, %r9
callq 0x20eadf
movl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movl %r13d, %edx
cvtsi2sd %rdx, %xmm1
mulsd 0x360a2(%rip), %xmm1    # 0x2454c0
ucomisd %xmm0, %xmm1
jae 0x20f502
movq 0x10(%r15), %rsi
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
testl %r13d, %r13d
movq 0x18(%rsp), %rbp
je 0x20f492
xorl %edi, %edi
xorl %r8d, %r8d
movq (%rsi,%rdi,8), %r9
movq %r9, %r10
shrq $0x20, %r10
movl %ebx, %r11d
subl %r10d, %r11d
subl %ebx, %r9d
cmpl %r9d, %r11d
cmovbl %r11d, %r9d
addq %r9, %r8
incq %rdi
cmpq %rdi, %rdx
jne 0x20f443
movq %r8, %xmm2
punpckldq 0x21e0a(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x21e12(%rip), %xmm2    # 0x231290
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
addsd %xmm2, %xmm0
mulsd 0x3602e(%rip), %xmm0    # 0x2454c0
testl %eax, %eax
je 0x20f4e4
leaq (%rsi,%rdx,8), %rax
xorl %edx, %edx
xorl %esi, %esi
movq (%rax,%rdx,8), %rdi
movq %rdi, %r8
shrq $0x20, %r8
movl %ebx, %r9d
subl %r8d, %r9d
subl %ebx, %edi
cmpl %edi, %r9d
cmovbl %r9d, %edi
addq %rdi, %rsi
incq %rdx
cmpq %rdx, %rcx
jne 0x20f49e
movq %rsi, %xmm2
punpckldq 0x21db0(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x21db8(%rip), %xmm2    # 0x231290
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
movq 0x28(%rsp), %rdx
movl $0x0, %eax
jb 0x20f511
movl $0x1, %eax
jmp 0x20f511
movq 0x18(%rsp), %rbp
jmp 0x20f511
movl $0x1, %eax
movq 0x18(%rsp), %rbp
movq 0x28(%rsp), %rdx
movq %rdx, (%rbp)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq