// File: chhylp123[P]hifiasm/gfa_ut.cpp
// Function: is_best_path(ul_resolve_t*, usg_t*, unsigned long*, unsigned long*, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long*, unsigned long*, asg64_v*, asg64_v*, double)
uint64_t is_best_path(ul_resolve_t *uidx, usg_t *ng, uint64_t *int_idx, uint64_t *int_a, 
uint64_t s, uint64_t e, uint64_t it, uint64_t v, uint64_t *ridx_a, uint64_t *ridx, 
asg64_v *b0, asg64_v *b1, double cutoff)
{
    uint64_t *arc_a, arc_n, k, is, ie, z, zn, w0, w1, min_w0, min_w1, alt_n = 0, is_contain = 1;
    gen_sub_integer_path(int_a, s, e, it, v, b0);

    arc_a = ridx_a + (ridx[v>>1]>>32); 
    arc_n = (uint32_t)ridx[v>>1];


    // uint64_t is_debug = 0;
    // if((((v>>1) == 2736) && (v&1))/** && (b0->n > 1 && (b0->a[b0->n-1]>>1) == 34944)**/) {
    //     // is_debug = 1;
    //     fprintf(stderr, "[M::%s::] utg%.6dl(%c)\n", __func__, 
    //                 (int32_t)(v>>1)+1, "+-"[v&1]);
    //     prt_sub_integer_path(b0, it, (uint64_t)-1, (uint64_t)-1, (uint64_t)-1, (uint64_t)-1);
    // }


    for (k = 0, min_w0 = min_w1 = (uint64_t)-1, is_contain = 1; k < arc_n; k++) {
        if(it == ((uint32_t)arc_a[k])) continue;
        is = int_idx[arc_a[k]>>32]>>32; alt_n++;
        ie = is + ((uint32_t)(int_idx[arc_a[k]>>32])); 
        gen_sub_integer_path(int_a, is, ie, ((uint32_t)arc_a[k]), v, b1);
        zn = MIN(b0->n, b1->n);
        for (z = 0; z < zn && b0->a[z] == b1->a[z]; z++); ///z: first raw unitig that is different between two paths
        assert(z > 0); w0 = w1 = (uint64_t)-1;
        if(z < b0->n) get_integer_seq_ovlps(uidx, b0->a, b0->n, z - 1, 0, NULL, &w0);
        else return 0;///b0 is contained
        if(z < b1->n) get_integer_seq_ovlps(uidx, b1->a, b1->n, z - 1, 0, NULL, &w1);
        else continue;///b1 is contained
        // if(((v>>1) == 2736) && (v&1)) {
        //     prt_sub_integer_path(b0, it, w0, w1, zn, z);
        //     prt_sub_integer_path(b1, it, w0, w1, zn, z);
        // }
        if(w0 == (uint64_t)-1) w0 = 0;
        if(w1 == (uint64_t)-1) w1 = 0;
        if((w0 <= w1) || (w1 > (w0*cutoff))) return 0;
        // if(b0->n == zn) return 0;///b0 is shorter
        // if(b1->n == zn) continue;///b1 is shorter
        if((min_w0 == (uint64_t)-1) || (z == zn) || (min_w0 > w0) || (min_w0 == w0 && min_w1 < w1)) {
            min_w0 = w0; min_w1 = w1;
        }
        is_contain = 0;
    }

    // if(((v>>1) == 2736) && (v&1)) {
    //     fprintf(stderr, "[M::%s::] min_w0::%lu, min_w1::%lu\n\n", __func__, min_w0, min_w1);
    // }
    // fprintf(stderr, "[M::%s::] utg%.6dl(%c)->utg%.6dl(%c)\n", __func__, 
    //                 (int32_t)(v>>1)+1, "+-"[v&1], (int32_t)(int_a[k]>>1)+1, "+-"[int_a[k]&1]);
    if(alt_n == 0) return 1;
    if(is_contain) return 1;
    
    if(min_w0 == (uint64_t)-1) min_w0 = 0;
    if(min_w1 == (uint64_t)-1) min_w1 = 0;
    if((min_w0 > min_w1) && (min_w1 <= (min_w0*cutoff))) return 1;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movsd %xmm0, -0x40(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x1, -0xa8(%rbp)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %r8
movq 0x30(%rbp), %r9
callq 0x3620a0
movq 0x20(%rbp), %rax
movq 0x28(%rbp), %rcx
movq 0x18(%rbp), %rdx
shrq %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq 0x28(%rbp), %rax
movq 0x18(%rbp), %rcx
shrq %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $-0x1, -0x98(%rbp)
movq $-0x1, -0x90(%rbp)
movq $0x1, -0xa8(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x362851
movq 0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
cmpq %rcx, %rax
jne 0x36256d
jmp 0x362840
movq -0x20(%rbp), %rax
movq -0x48(%rbp), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x60(%rbp)
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
movq -0x48(%rbp), %rdx
movq -0x58(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
shrq $0x20, %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movl %eax, %ecx
movq 0x18(%rbp), %r8
movq 0x38(%rbp), %r9
callq 0x3620a0
movq 0x30(%rbp), %rax
movq (%rax), %rax
movq 0x38(%rbp), %rcx
cmpq (%rcx), %rax
ja 0x36260d
movq 0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
jmp 0x36261b
movq 0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rcx
xorl %eax, %eax
cmpq -0x78(%rbp), %rcx
movb %al, -0xb1(%rbp)
jae 0x362669
movq 0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x38(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x70(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
sete %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x362675
jmp 0x362685
jmp 0x362677
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x36262e
cmpq $0x0, -0x70(%rbp)
jbe 0x36268e
jmp 0x3626ad
leaq 0x367f8(%rip), %rdi     # 0x398e8d
leaq 0x33b1d(%rip), %rsi     # 0x3961b9
movl $0x370d, %edx           # imm = 0x370D
leaq 0x37a9b(%rip), %rcx     # 0x39a143
callq 0x2130
movq $-0x1, -0x88(%rbp)
movq $-0x1, -0x80(%rbp)
movq -0x70(%rbp), %rax
movq 0x30(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x3626ff
movq -0x10(%rbp), %rdi
movq 0x30(%rbp), %rax
movq 0x10(%rax), %rsi
movq 0x30(%rbp), %rax
movq (%rax), %rdx
movq -0x70(%rbp), %rcx
subq $0x1, %rcx
xorl %eax, %eax
movl %eax, %r9d
leaq -0x80(%rbp), %rax
movq %r9, %r8
movq %rax, (%rsp)
callq 0x3465d0
jmp 0x36270c
movq $0x0, -0x8(%rbp)
jmp 0x36291b
movq -0x70(%rbp), %rax
movq 0x38(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x36274e
movq -0x10(%rbp), %rdi
movq 0x38(%rbp), %rax
movq 0x10(%rax), %rsi
movq 0x38(%rbp), %rax
movq (%rax), %rdx
movq -0x70(%rbp), %rcx
subq $0x1, %rcx
xorl %eax, %eax
movl %eax, %r9d
leaq -0x88(%rbp), %rax
movq %r9, %r8
movq %rax, (%rsp)
callq 0x3465d0
jmp 0x362753
jmp 0x362840
cmpq $-0x1, -0x80(%rbp)
jne 0x362762
movq $0x0, -0x80(%rbp)
cmpq $-0x1, -0x88(%rbp)
jne 0x362777
movq $0x0, -0x88(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x88(%rbp), %rax
jbe 0x3627d1
movq -0x88(%rbp), %xmm0
movaps 0x1f87d(%rip), %xmm3    # 0x382010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x1f881(%rip), %xmm2    # 0x382020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0x80(%rbp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd -0x40(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3627de
movq $0x0, -0x8(%rbp)
jmp 0x36291b
cmpq $-0x1, -0x90(%rbp)
je 0x36281c
movq -0x70(%rbp), %rax
cmpq -0x78(%rbp), %rax
je 0x36281c
movq -0x90(%rbp), %rax
cmpq -0x80(%rbp), %rax
ja 0x36281c
movq -0x90(%rbp), %rax
cmpq -0x80(%rbp), %rax
jne 0x362835
movq -0x98(%rbp), %rax
cmpq -0x88(%rbp), %rax
jae 0x362835
movq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x98(%rbp)
movq $0x0, -0xa8(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x362543
cmpq $0x0, -0xa0(%rbp)
jne 0x362868
movq $0x1, -0x8(%rbp)
jmp 0x36291b
cmpq $0x0, -0xa8(%rbp)
je 0x36287f
movq $0x1, -0x8(%rbp)
jmp 0x36291b
cmpq $-0x1, -0x90(%rbp)
jne 0x362894
movq $0x0, -0x90(%rbp)
cmpq $-0x1, -0x98(%rbp)
jne 0x3628a9
movq $0x0, -0x98(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x98(%rbp), %rax
jbe 0x362913
movq -0x98(%rbp), %xmm1
movaps 0x1f748(%rip), %xmm3    # 0x382010
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movapd 0x1f74c(%rip), %xmm2    # 0x382020
subpd %xmm2, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
movq -0x90(%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
mulsd -0x40(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x362913
movq $0x1, -0x8(%rbp)
jmp 0x36291b
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax,%rax)