// File: /workspace/github_cpp_makefile_star2_O2/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
movl %r8d, %ebp
movl %ecx, %r13d
movl %edx, 0x24(%rsp)
movq 0x90(%rsp), %r14
movq %r9, 0x30(%rsp)
movl (%r9), %eax
movq %rax, 0x38(%rsp)
andq $0x0, (%r14)
movq %rsi, %r12
movq %rdi, 0x28(%rsp)
cmpq $0x0, 0x8(%r14)
je 0x1c22e9
movq 0x10(%r14), %rax
xorl %ecx, %ecx
jmp 0x1c2304
movq $0x2, 0x8(%r14)
movq 0x10(%r14), %rdi
pushq $0x10
popq %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movl %ebp, %ebx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %rbx, (%rax,%rcx,8)
movq %r14, 0x10(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
xorl %r15d, %r15d
movq 0x28(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movl %r13d, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1c0c77
movq (%r14), %rdx
movq 0x10(%r14), %rsi
movq %rbp, %rdi
movq 0x30(%rsp), %rcx
callq 0x1c1740
andq $0x0, (%r14)
movq %rax, %r13
cmpq $0x0, 0x8(%r14)
movq 0x10(%r14), %rax
je 0x1c2367
xorl %ecx, %ecx
jmp 0x1c2381
movq $0x2, 0x8(%r14)
pushq $0x10
popq %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %rbx, (%rax,%rcx,8)
movq %r14, 0x10(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movl 0x24(%rsp), %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1c0c77
movq (%r14), %rdx
movq 0x10(%r14), %rsi
movq %rbx, %rdi
movq 0x30(%rsp), %rbx
movq %rbx, %rcx
callq 0x1c1740
movq %rax, %r12
movq 0x38(%rsp), %rax
leaq (,%rax,8), %rsi
addq 0x10(%rbx), %rsi
cmpq %r12, %r13
movq %r12, %rax
cmovbq %r13, %rax
leaq (%rsi,%r13,8), %rbp
andq $0x0, (%r14)
movl %r15d, %r15d
cmpq %r15, %rax
jbe 0x1c240b
movq (%rsi,%r15,8), %rcx
cmpq (%rbp,%r15,8), %rcx
jne 0x1c240b
incl %r15d
jmp 0x1c23f3
testq %r15, %r15
je 0x1c2543
leal -0x1(%r15), %ebx
leaq 0x40(%rsp), %rax
movq %rax, (%rsp)
pushq $0x1
popq %r8
movq %r13, %rdx
movq 0x28(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rcx
movq %r14, %r9
callq 0x1c1c92
movl %eax, 0x24(%rsp)
leaq 0x48(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %rbx, %rcx
pushq $0x1
popq %r8
movq %r14, %r9
callq 0x1c1c92
movl %eax, %ecx
cvtsi2sd %rcx, %xmm0
movl 0x24(%rsp), %esi
cvtsi2sd %rsi, %xmm1
mulsd 0x34f10(%rip), %xmm1    # 0x1f7388
pushq $0x1
popq %rax
ucomisd %xmm0, %xmm1
jae 0x1c2545
movq 0x10(%r14), %rdi
movl %r15d, %r8d
negl %r8d
xorl %r9d, %r9d
xorl %edx, %edx
cmpq %r9, %rsi
je 0x1c24bc
movq (%rdi,%r9,8), %r10
movq %r10, %r11
shrq $0x20, %r11
movl %r15d, %ebx
subl %r11d, %ebx
addl %r8d, %r10d
cmpl %r10d, %ebx
cmovbl %ebx, %r10d
addq %r10, %rdx
incq %r9
jmp 0x1c2494
leaq (%rdi,%rsi,8), %rsi
movl %r15d, %edi
negl %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq %r8, %rcx
je 0x1c24f3
movq (%rsi,%r8,8), %r10
movq %r10, %r11
shrq $0x20, %r11
movl %r15d, %ebx
subl %r11d, %ebx
addl %edi, %r10d
cmpl %r10d, %ebx
cmovbl %ebx, %r10d
addq %r10, %r9
incq %r8
jmp 0x1c24cb
movq %r9, %xmm0
movq 0x20d70(%rip), %xmm1    # 0x1e3270
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x20d74(%rip), %xmm2    # 0x1e3280
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movq %rdx, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x34e4b(%rip), %xmm1    # 0x1f7388
ucomisd %xmm3, %xmm1
jae 0x1c2545
xorl %eax, %eax
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rdx
movq %rdx, (%rcx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq