// File: 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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movq %r9, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq 0x10(%rbp), %rax
movq $0x0, (%rax)
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x347222
movq 0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x3471e1
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xc8(%rbp)
jmp 0x3471ef
movl $0x2, %eax
movq %rax, -0xc8(%rbp)
jmp 0x3471ef
movq -0xc8(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x1c(%rbp), %eax
movl %eax, %edx
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x18(%rbp), %edx
movq 0x10(%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 -0x8(%rbp), %rdi
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq 0x10(%rbp), %rax
movq (%rax), %rdx
movq -0x28(%rbp), %rcx
callq 0x345a40
movq %rax, -0x48(%rbp)
movq 0x10(%rbp), %rax
movq $0x0, (%rax)
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x347315
movq 0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x3472d4
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xd0(%rbp)
jmp 0x3472e2
movl $0x2, %eax
movq %rax, -0xd0(%rbp)
jmp 0x3472e2
movq -0xd0(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x1c(%rbp), %eax
movl %eax, %edx
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
movq 0x10(%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 -0x8(%rbp), %rdi
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq 0x10(%rbp), %rax
movq (%rax), %rdx
movq -0x28(%rbp), %rcx
callq 0x345a40
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x30(%rbp), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x30(%rbp), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movq -0x48(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
ja 0x3473da
movq -0x48(%rbp), %rax
movq %rax, -0xd8(%rbp)
jmp 0x3473e5
movq -0x50(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq 0x10(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, %ecx
xorl %eax, %eax
cmpq -0x68(%rbp), %rcx
movb %al, -0xd9(%rbp)
jae 0x347434
movq -0x58(%rbp), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq -0x60(%rbp), %rcx
movl -0x2c(%rbp), %edx
cmpq (%rcx,%rdx,8), %rax
sete %al
movb %al, -0xd9(%rbp)
movb -0xd9(%rbp), %al
testb $0x1, %al
jne 0x347440
jmp 0x34744d
jmp 0x347442
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x347402
cmpl $0x0, -0x2c(%rbp)
jbe 0x347716
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x48(%rbp), %rdx
movl -0x2c(%rbp), %eax
decl %eax
movl %eax, %ecx
movq 0x10(%rbp), %r9
movq %rsp, %rax
leaq -0xc0(%rbp), %r8
movq %r8, (%rax)
movl $0x1, %r8d
movq %r8, -0xe8(%rbp)
callq 0x3465d0
movq -0xe8(%rbp), %r8
movl %eax, %eax
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x50(%rbp), %rdx
movl -0x2c(%rbp), %eax
decl %eax
movl %eax, %ecx
movq 0x10(%rbp), %r9
leaq -0xb8(%rbp), %r10
movq %rsp, %rax
movq %r10, (%rax)
callq 0x3465d0
movl %eax, %eax
movq %rax, -0x78(%rbp)
movaps 0x3ab40(%rip), %xmm3    # 0x382010
movq -0x78(%rbp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movapd 0x3ab3f(%rip), %xmm2    # 0x382020
subpd %xmm2, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
movq -0x70(%rbp), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm2, %xmm0
movaps %xmm0, %xmm2
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm2, %xmm0
movsd 0x4eb88(%rip), %xmm2    # 0x396098
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x347526
movl $0x1, -0x34(%rbp)
jmp 0x347714
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x80(%rbp)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x38(%rbp)
movq $0x0, -0xa0(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpq -0x70(%rbp), %rax
jae 0x347600
movq -0x80(%rbp), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x3c(%rbp)
movq -0x80(%rbp), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x40(%rbp)
movl -0x38(%rbp), %eax
subl -0x3c(%rbp), %eax
movl %eax, %eax
movq %rax, -0x90(%rbp)
movl -0x40(%rbp), %eax
subl -0x38(%rbp), %eax
movl %eax, %eax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x98(%rbp), %rax
ja 0x3475cf
movq -0x90(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0x3475dd
movq -0x98(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
addq -0xa0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x347564
movq $0x0, -0xa8(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpq -0x78(%rbp), %rax
jae 0x3476b4
movq -0x88(%rbp), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x3c(%rbp)
movq -0x88(%rbp), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x40(%rbp)
movl -0x38(%rbp), %eax
subl -0x3c(%rbp), %eax
movl %eax, %eax
movq %rax, -0x90(%rbp)
movl -0x40(%rbp), %eax
subl -0x38(%rbp), %eax
movl %eax, %eax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x98(%rbp), %rax
ja 0x347683
movq -0x90(%rbp), %rax
movq %rax, -0xf8(%rbp)
jmp 0x347691
movq -0x98(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
addq -0xa8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x347612
movq -0xa8(%rbp), %xmm1
movaps 0x3a94d(%rip), %xmm3    # 0x382010
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movapd 0x3a951(%rip), %xmm2    # 0x382020
subpd %xmm2, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
movq -0xa0(%rbp), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm2, %xmm0
movaps %xmm0, %xmm2
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm2, %xmm0
movsd 0x4e997(%rip), %xmm2    # 0x396098
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x347712
movl $0x1, -0x34(%rbp)
jmp 0x347714
jmp 0x347716
movl -0x30(%rbp), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl -0x34(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nop