// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %r8, %r14
movl %ecx, %ebp
movl %edx, %r13d
movl %esi, %r15d
movq %rdi, %r12
callq 0x20b1e1
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
je 0x20b3c0
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r12), %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 0x20b3af
movl %r13d, %esi
shrl %esi
movl %r13d, 0x28(%rsp)
movl %r13d, %edi
andl $0x1, %edi
imulq $0x78, %rsi, %rsi
addq %rsi, %rax
leaq (%rdi,%rdi,2), %rsi
movq 0x10(%rax,%rsi,8), %rdi
movq 0x20(%rax,%rsi,8), %r13
movq %rdi, 0x30(%rsp)
movl %edi, %eax
movq %rax, 0x8(%rsp)
movq 0x20(%rcx,%rdx,8), %rdi
movq %r8, %rax
shlq $0x5, %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpl $0x0, 0x10(%rdi,%rcx)
je 0x20b437
movl 0xc(%rdi,%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x20, %rcx
cmpq %rcx, %rax
jne 0x20b425
testl %edx, %edx
je 0x20b3af
cmpl $0x0, 0x30(%rsp)
je 0x20b3af
movq 0x8(%rsp), %rax
shlq $0x5, %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpl $0x0, 0x10(%r13,%rcx)
je 0x20b474
movl 0xc(%r13,%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x20, %rcx
cmpq %rcx, %rax
jne 0x20b460
testl %edx, %edx
je 0x20b3af
movq %r12, 0x60(%rsp)
movq %r15, 0x20(%rsp)
movq %r14, 0x70(%rsp)
xorl %ebx, %ebx
movb $0x1, %r12b
xorl %edx, %edx
movq %r8, 0x58(%rsp)
movq %r13, 0x18(%rsp)
movq 0x8(%rsp), %rcx
movq %rdi, 0x38(%rsp)
movl %ebp, 0x44(%rsp)
movq %rbx, %r14
shlq $0x5, %r14
cmpl $0x0, 0x10(%rdi,%r14)
je 0x20b578
addq %rdi, %r14
cmpl $0x0, 0xc(%r14)
js 0x20b578
movl %edx, 0x4(%rsp)
cmpl $0x0, 0x30(%rsp)
je 0x20b551
xorl %r13d, %r13d
movl $0x1, %ebp
movq 0x18(%rsp), %r15
cmpl $0x0, 0x10(%r15)
je 0x20b53d
cmpl $0x0, 0xc(%r15)
js 0x20b53d
movq 0x60(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
leaq 0x14(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20aeb5
testl %eax, %eax
je 0x20b533
cmpl $0x0, 0x44(%rsp)
movq 0x8(%rsp), %rcx
movq 0x38(%rsp), %rdi
je 0x20b52e
cmpl $0x0, 0x14(%rsp)
jne 0x20b556
incl %r13d
jmp 0x20b53d
movq 0x8(%rsp), %rcx
movq 0x38(%rsp), %rdi
cmpq %rcx, %rbp
jae 0x20b55c
addq $0x20, %r15
incq %rbp
cmpl $0x2, %r13d
jb 0x20b4ec
jmp 0x20b55c
xorl %r13d, %r13d
jmp 0x20b55c
movl $0x2, %r13d
cmpl $0x1, %r13d
ja 0x20b58a
sete %al
movzbl %al, %eax
movl 0x4(%rsp), %edx
addl %eax, %edx
movq 0x58(%rsp), %r8
movq 0x18(%rsp), %r13
incq %rbx
cmpq %r8, %rbx
setb %r12b
jne 0x20b4b3
jmp 0x20b593
movq 0x18(%rsp), %r13
movl 0x4(%rsp), %edx
testl %edx, %edx
je 0x20b7d2
testb $0x1, %r12b
jne 0x20b7d2
movl %edx, 0x4(%rsp)
cmpl $0x0, 0x30(%rsp)
setne %r9b
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
je 0x20b7dc
movl 0x28(%rsp), %eax
movq %rax, 0x68(%rsp)
movq 0x20(%rsp), %r15
shlq $0x20, %r15
xorl %edx, %edx
movabsq $0x100000000, %rsi      # imm = 0x100000000
xorl %r8d, %r8d
movq $0x0, 0x28(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x0, 0x10(%rsp)
movq %r8, %rax
shlq $0x5, %rax
cmpl $0x0, 0x10(%r13,%rax)
je 0x20b7bd
leaq (%rax,%r13), %r10
cmpl $0x0, 0xc(%r10)
js 0x20b7bd
movq %r10, %rbp
movb %r9b, 0x3(%rsp)
movq %r8, 0x48(%rsp)
movq %r13, %rcx
movq %r15, %r13
addq %rcx, %rax
addq $0x10, %rax
movq %rax, 0x30(%rsp)
xorl %r12d, %r12d
movl $0x1, %r15d
movq %rdx, 0x50(%rsp)
movq %rdx, %rbx
movq %rdi, %r14
cmpl $0x0, 0x10(%r14)
je 0x20b6b0
cmpl $0x0, 0xc(%r14)
js 0x20b6b0
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
leaq 0x14(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20aeb5
testl %eax, %eax
je 0x20b6ab
cmpl $0x0, 0x44(%rsp)
movq 0x38(%rsp), %rdi
je 0x20b689
cmpl $0x0, 0x14(%rsp)
jne 0x20b6c9
incl %r12d
movq 0x30(%rsp), %rax
movl (%rax), %eax
shrl %eax
movl 0x10(%r14), %ecx
shrl %ecx
cmpl %ecx, %eax
cmovbl %eax, %ecx
addl %ecx, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
jmp 0x20b6b0
movq 0x38(%rsp), %rdi
cmpq 0x58(%rsp), %r15
jae 0x20b6cf
addq $0x20, %r14
incq %rbx
incq %r15
cmpl $0x2, %r12d
jb 0x20b64c
jmp 0x20b6cf
movl $0x2, %r12d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $0x1, %r12d
ja 0x20b7ee
jne 0x20b79c
incl 0x10(%rsp)
movq 0x70(%rsp), %r14
testq %r14, %r14
movq %r13, %r15
je 0x20b79f
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
movq 0x18(%rsp), %r13
jne 0x20b72c
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq 0x68(%rsp), %rax
movq 0x28(%rsp), %rcx
shldq $0x20, %rcx, %rax
movq (%r14), %rcx
movq 0x10(%r14), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %rax, (%rdi,%rcx,8)
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x20b777
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movl 0x28(%rsp), %eax
orq %r15, %rax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movq %rax, (%rdx,%rcx,8)
movq 0x8(%rsp), %rcx
movq 0x38(%rsp), %rdi
jmp 0x20b7a9
movq %r13, %r15
movq 0x18(%rsp), %r13
movq 0x8(%rsp), %rcx
movq 0x50(%rsp), %rdx
movabsq $0x100000000, %rsi      # imm = 0x100000000
movq 0x48(%rsp), %r8
incq %r8
addq %rsi, %rdx
cmpq %rcx, %r8
setb %r9b
jne 0x20b5f7
jmp 0x20b7f3
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x20b3af
movl $0x0, 0x10(%rsp)
movl $0x0, 0x20(%rsp)
jmp 0x20b7f3
movb 0x3(%rsp), %r9b
movl 0x10(%rsp), %eax
testl %eax, %eax
je 0x20b3af
testb $0x1, %r9b
jne 0x20b3af
movl 0x20(%rsp), %ebx
cmpl %eax, 0x4(%rsp)
je 0x20b3af
leaq 0x30a83(%rip), %rdi     # 0x23c2a1
leaq 0x2db00(%rip), %rsi     # 0x239325
leaq 0x30a84(%rip), %rcx     # 0x23c2b0
movl $0x2d6e, %edx           # imm = 0x2D6E
callq 0x2110