// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: get_junction_w(usg_t*, unsigned int, unsigned int, unsigned int, asg64_v*)
uint32_t get_junction_w(usg_t *g, uint32_t v0, uint32_t v1, uint32_t no_inconsist, asg64_v *res)
{
    uint32_t k, i, v[2], nv[2], ou[2], i0, i1, k0, k1, occ, ff, is_mul, nn[2], *c, ww, inconsist; 
    usg_arc_t *av[2]; uint64_t m = 0;

    if(is_junction_circle(g, v0, v1)) return (uint32_t)-1;
    // if((v0>>1) == 257 || (v1>>1) == 256) {
    //     fprintf(stderr, "[M::%s::] v0>>1::%u, v0&1::%u, v0pid::%u, v1>>1::%u, v1&1::%u, v1pid::%u\n", 
    //     __func__, v0>>1, v0&1, g->a[v0>>1].mm, v1>>1, v1&1, g->a[v1>>1].mm);
    // }

    v[0] = v0; v[1] = v1;
    nv[0] = usg_arc_n(g, v[0]); nv[1] = usg_arc_n(g, v[1]); 
    av[0] = usg_arc_a(g, v[0]); av[1] = usg_arc_a(g, v[1]); 

    i = 0;
    for (k = 0, ou[i] = 0; k < nv[i]; k++) {
        if(!av[i][k].ou) continue;
        if(av[i][k].del) continue;
        ou[i]++;
    }
    // if((v0>>1) == 257 || (v1>>1) == 256) {
    //     fprintf(stderr, "[M::%s::] ou[0]::%u\n", __func__, ou[0]);
    // }
    if(!ou[i]) return (uint32_t)-1;
    
    i = 1;
    for (k = 0, ou[i] = 0; k < nv[i]; k++) {
        if(!av[i][k].ou) continue;
        if(av[i][k].del) continue;
        ou[i]++;
    }
    // if((v0>>1) == 257 || (v1>>1) == 256) {
    //     fprintf(stderr, "[M::%s::] ou[1]::%u\n", __func__, ou[1]);
    // }
    if(!ou[i]) return (uint32_t)-1;

    //this is not ture; some UL may not be able to go through nid
    // if(ou[0] != ou[1]) return (uint32_t)-1;
    nn[0] = nn[1] = ww = 0;

    i0 = 0; i1 = 1; is_mul = 0; c = &(nn[0]);
    for (k0 = 0; k0 < nv[i0]; k0++) {
        if(!av[i0][k0].ou) continue;
        if(av[i0][k0].del) continue;
        for (k1 = 0, ff = 0; k1 < nv[i1] && ff <= 1; k1++) {
            if(!av[i1][k1].ou) continue;
            if(av[i1][k1].del) continue;
            occ = usg_arc_mm_consist(g, &(av[i0][k0]), &(av[i1][k1]), &inconsist, NULL);
            if(occ == 0) continue;
            if(no_inconsist && inconsist > 0) {
                ff = 2; break;
            }
            ff++;
        }

        if(ff > 1) {
            is_mul = 1; break;
        } else if(ff == 1) {
            (*c) += 1;
        }
    }
    // if((v0>>1) == 257 || (v1>>1) == 256) {
    //     fprintf(stderr, "[M::%s::] c[0]::%u\n", __func__, *c);
    // }
    if((*c) == 0 || is_mul) return (uint32_t)-1;


    i0 = 1; i1 = 0; is_mul = 0; c = &(nn[1]);
    for (k0 = 0; k0 < nv[i0]; k0++) {
        if(!av[i0][k0].ou) continue;
        if(av[i0][k0].del) continue;
        for (k1 = 0, ff = 0; k1 < nv[i1] && ff <= 1; k1++) {
            if(!av[i1][k1].ou) continue;
            if(av[i1][k1].del) continue;
            occ = usg_arc_mm_consist(g, &(av[i0][k0]), &(av[i1][k1]), &inconsist, NULL);
            if(occ == 0) continue;
            if(no_inconsist && inconsist > 0) {
                ff = 2; break;
            }
            ff++; ww += MIN((av[i0][k0].ou>>1), (av[i1][k1].ou>>1)); 
            m = k0; m <<= 32; m |= k1;
        }

        if(ff > 1) {
            is_mul = 1; break;
        } else if(ff == 1) {
            (*c) += 1;
            if(res) {
                kv_push(uint64_t, *res, (((uint64_t)v[i0])<<32)|(((uint64_t)(m>>32))));
                kv_push(uint64_t, *res, (((uint64_t)v[i1])<<32)|(((uint64_t)((uint32_t)m))));
            }
        }
    }
    // if((v0>>1) == 257 || (v1>>1) == 256) {
    //     fprintf(stderr, "[M::%s::] c[1]::%u\n", __func__, *c);
    // }
    if((*c) == 0 || is_mul) return (uint32_t)-1;
    assert(nn[0] == nn[1]);

    return ww;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r8, %r14
movl %ecx, %ebp
movl %edx, %r12d
movl %esi, %r15d
movq %rdi, %r13
callq 0x216564
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
je 0x216740
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r13), %rax
movl %r15d, %ecx
shrl %ecx
movl %r15d, %edx
andl $0x1, %edx
imulq $0x78, %rcx, %rcx
addq %rax, %rcx
leaq (%rdx,%rdx,2), %rdx
movl 0x10(%rcx,%rdx,8), %r8d
testq %r8, %r8
je 0x21672c
movl %r12d, %esi
shrl %esi
movl %r12d, 0x58(%rsp)
movl %r12d, %edi
andl $0x1, %edi
imulq $0x78, %rsi, %rsi
addq %rsi, %rax
leaq (%rdi,%rdi,2), %rsi
movl 0x10(%rax,%rsi,8), %edi
movq %rdi, 0x28(%rsp)
movq 0x20(%rcx,%rdx,8), %r12
movq 0x20(%rax,%rsi,8), %rdi
movq %r8, 0x10(%rsp)
movq %r8, %rax
shlq $0x5, %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpl $0x0, 0x10(%r12,%rcx)
je 0x2167b6
movl 0xc(%r12,%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x20, %rcx
cmpq %rcx, %rax
jne 0x2167a2
testl %edx, %edx
je 0x21672c
movq 0x28(%rsp), %rax
testq %rax, %rax
je 0x21672c
shlq $0x5, %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpl $0x0, 0x10(%rdi,%rcx)
je 0x2167ef
movl 0xc(%rdi,%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x20, %rcx
cmpq %rcx, %rax
jne 0x2167dd
testl %edx, %edx
je 0x21672c
movq %r13, 0x68(%rsp)
movq %r15, 0x48(%rsp)
movl %ebp, 0x3c(%rsp)
movq %r14, 0x78(%rsp)
xorl %r13d, %r13d
movb $0x1, %dl
xorl %ecx, %ecx
movq 0x10(%rsp), %r14
movq 0x28(%rsp), %rax
movq %rdi, 0x20(%rsp)
movq %r13, %rbx
shlq $0x5, %rbx
cmpl $0x0, 0x10(%r12,%rbx)
je 0x2168e1
addq %r12, %rbx
cmpl $0x0, 0xc(%rbx)
js 0x2168e1
testq %rax, %rax
je 0x2168d6
movb %dl, 0x40(%rsp)
movl %ecx, 0xc(%rsp)
xorl %r14d, %r14d
movl $0x1, %r15d
movq %rdi, %rbp
cmpl $0x0, 0x10(%rbp)
je 0x2168b5
cmpl $0x0, 0xc(%rbp)
js 0x2168b5
movq 0x68(%rsp), %rdi
movq %rbx, %rsi
movq %rbp, %rdx
leaq 0x1c(%rsp), %rcx
xorl %r8d, %r8d
callq 0x216222
testl %eax, %eax
je 0x2168ab
cmpl $0x0, 0x3c(%rsp)
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rdi
je 0x2168a6
cmpl $0x0, 0x1c(%rsp)
jne 0x2168f2
incl %r14d
jmp 0x2168b5
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rdi
cmpq %rax, %r15
jae 0x2168c7
addq $0x20, %rbp
incq %r15
cmpl $0x2, %r14d
jb 0x216866
cmpl $0x1, %r14d
movl 0xc(%rsp), %ecx
jbe 0x2168d9
jmp 0x216b8e
xorl %r14d, %r14d
addl %r14d, %ecx
movq 0x10(%rsp), %r14
incq %r13
cmpq %r14, %r13
setb %dl
jne 0x216829
jmp 0x2168ff
movq 0x10(%rsp), %r14
movl 0xc(%rsp), %ecx
movb 0x40(%rsp), %dl
testl %ecx, %ecx
je 0x216b84
testb $0x1, %dl
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jne 0x21672c
movl %ecx, 0xc(%rsp)
movl 0x58(%rsp), %eax
movq %rax, 0x70(%rsp)
movq 0x48(%rsp), %r15
shlq $0x20, %r15
xorl %r13d, %r13d
movb $0x1, 0xb(%rsp)
movabsq $0x100000000, %rbp      # imm = 0x100000000
xorl %eax, %eax
movq $0x0, 0x50(%rsp)
movl $0x0, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
movq %r15, 0x48(%rsp)
movq %rax, 0x80(%rsp)
shlq $0x5, %rax
cmpl $0x0, 0x10(%rdi,%rax)
je 0x216b19
leaq (%rdi,%rax), %rcx
movq %rcx, 0x58(%rsp)
cmpl $0x0, 0xc(%rcx)
js 0x216b19
testq %r14, %r14
je 0x216b19
addq %rdi, %rax
addq $0x10, %rax
movq %rax, 0x40(%rsp)
xorl %r15d, %r15d
movl $0x1, %ebp
movq %r13, 0x60(%rsp)
movq %r13, %rbx
movq %r12, %r13
cmpl $0x0, 0x10(%r13)
je 0x216a19
cmpl $0x0, 0xc(%r13)
js 0x216a19
movq 0x68(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq %r13, %rdx
leaq 0x1c(%rsp), %rcx
xorl %r8d, %r8d
callq 0x216222
testl %eax, %eax
je 0x216a14
cmpl $0x0, 0x3c(%rsp)
movq 0x20(%rsp), %rdi
je 0x2169f2
cmpl $0x0, 0x1c(%rsp)
jne 0x216b39
incl %r15d
movq 0x40(%rsp), %rax
movl (%rax), %eax
shrl %eax
movl 0x10(%r13), %ecx
shrl %ecx
cmpl %ecx, %eax
cmovbl %eax, %ecx
addl %ecx, 0x38(%rsp)
movq %rbx, 0x50(%rsp)
jmp 0x216a19
movq 0x20(%rsp), %rdi
cmpq %r14, %rbp
jae 0x216a2e
addq $0x20, %r13
incq %rbx
incq %rbp
cmpl $0x2, %r15d
jb 0x2169af
cmpl $0x1, %r15d
movq 0x78(%rsp), %r14
ja 0x216b39
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jne 0x216b05
incl 0x34(%rsp)
testq %r14, %r14
movq 0x48(%rsp), %r15
movq 0x60(%rsp), %r13
movabsq $0x100000000, %rbp      # imm = 0x100000000
je 0x216afe
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x216a9d
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq 0x70(%rsp), %rdx
movq 0x50(%rsp), %rsi
shldq $0x20, %rsi, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movq %rdx, (%rax,%rcx,8)
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x216ae7
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq 0x20(%rsp), %rdi
movl 0x50(%rsp), %edx
orq %r15, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movq %rdx, (%rax,%rcx,8)
movq 0x10(%rsp), %r14
jmp 0x216b19
movq 0x10(%rsp), %r14
movq 0x60(%rsp), %r13
movabsq $0x100000000, %rbp      # imm = 0x100000000
movq 0x80(%rsp), %rax
incq %rax
addq %rbp, %r13
cmpq 0x28(%rsp), %rax
setb 0xb(%rsp)
jne 0x21695d
jmp 0x216b3e
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl 0x34(%rsp), %ecx
testl %ecx, %ecx
movl 0xc(%rsp), %eax
je 0x21672c
testb $0x1, 0xb(%rsp)
jne 0x21672c
movl 0x38(%rsp), %ebx
cmpl %ecx, %eax
je 0x21672c
leaq 0x31a19(%rip), %rdi     # 0x248585
leaq 0x2ea96(%rip), %rsi     # 0x245609
leaq 0x31a1a(%rip), %rcx     # 0x248594
movl $0x2d6e, %edx           # imm = 0x2D6E
callq 0x2110
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x21672c
movq 0x10(%rsp), %r14
jmp 0x2168fb