// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
movl %ecx, %r14d
movl %edx, %r13d
movl %esi, %r15d
movq %rdi, %r12
callq 0x1c847f
pushq $-0x1
popq %rbp
testl %eax, %eax
je 0x1c8566
movl %ebp, %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
imulq $0x18, %rdx, %rdx
movl 0x10(%rdx,%rcx), %r8d
movl %r13d, %esi
shrl %esi
movl %r13d, 0x30(%rsp)
movl %r13d, %edi
andl $0x1, %edi
imulq $0x78, %rsi, %rsi
addq %rax, %rsi
imulq $0x18, %rdi, %rax
movl 0x10(%rax,%rsi), %edi
movq %rdi, 0x8(%rsp)
movq 0x20(%rdx,%rcx), %r13
movq 0x20(%rax,%rsi), %rdi
movq %r8, 0x48(%rsp)
movq %r8, %rax
shlq $0x5, %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rcx, %rax
je 0x1c85e2
cmpl $0x0, 0x10(%r13,%rcx)
je 0x1c85dc
movl 0xc(%r13,%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x20, %rcx
jmp 0x1c85c3
testl %edx, %edx
je 0x1c8555
movq 0x8(%rsp), %rax
shlq $0x5, %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rcx, %rax
je 0x1c8614
cmpl $0x0, 0x10(%rdi,%rcx)
je 0x1c860e
movl 0xc(%rdi,%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x20, %rcx
jmp 0x1c85f7
testl %edx, %edx
je 0x1c8555
movq %r12, 0x58(%rsp)
movq %r15, 0x50(%rsp)
movl %r14d, 0x2c(%rsp)
movq %rbx, 0x68(%rsp)
xorl %r14d, %r14d
movl $0x0, 0x10(%rsp)
movq 0x8(%rsp), %rax
movq %rdi, 0x18(%rsp)
cmpq 0x48(%rsp), %r14
je 0x1c86fa
movq %r14, %rbx
shlq $0x5, %rbx
cmpl $0x0, 0x10(%r13,%rbx)
je 0x1c86f2
addq %r13, %rbx
cmpl $0x0, 0xc(%rbx)
js 0x1c86f2
xorl %ebp, %ebp
movq %rdi, %r15
xorl %r12d, %r12d
cmpq %rax, %rbp
jae 0x1c86dd
cmpl $0x1, %r12d
ja 0x1c86dd
cmpl $0x0, 0x10(%r15)
je 0x1c86d4
cmpl $0x0, 0xc(%r15)
js 0x1c86d4
movq 0x58(%rsp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
leaq 0x14(%rsp), %rcx
xorl %r8d, %r8d
callq 0x1c8245
testl %eax, %eax
je 0x1c86ca
cmpl $0x0, 0x2c(%rsp)
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %rdi
je 0x1c86c5
cmpl $0x0, 0x14(%rsp)
jne 0x1c86fa
incl %r12d
jmp 0x1c86d4
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %rdi
incq %rbp
addq $0x20, %r15
jmp 0x1c8678
cmpl $0x1, %r12d
ja 0x1c86fa
sete %al
movzbl %al, %eax
addl %eax, 0x10(%rsp)
movq 0x8(%rsp), %rax
incq %r14
jmp 0x1c8645
cmpl $0x0, 0x10(%rsp)
je 0x1c894f
cmpq 0x48(%rsp), %r14
pushq $-0x1
popq %rbp
jb 0x1c8555
movl 0x30(%rsp), %ecx
movq %rcx, 0x60(%rsp)
shlq $0x20, 0x50(%rsp)
movq $0x0, 0x30(%rsp)
movabsq $0x100000000, %r14      # imm = 0x100000000
xorl %ebx, %ebx
movl $0x0, 0x24(%rsp)
movl $0x0, 0x28(%rsp)
movq $0x0, 0x40(%rsp)
cmpq %rax, %rbx
je 0x1c890d
movq %rbx, %rax
shlq $0x5, %rax
cmpl $0x0, 0x10(%rdi,%rax)
je 0x1c88e5
leaq (%rdi,%rax), %rcx
cmpl $0x0, 0xc(%rcx)
js 0x1c88e5
movq %rcx, %r12
movq %rbx, 0x38(%rsp)
addq %rdi, %rax
addq $0x10, %rax
movq %rax, 0x70(%rsp)
xorl %r15d, %r15d
movq %r13, %rbx
xorl %r14d, %r14d
cmpq 0x48(%rsp), %r15
jae 0x1c881c
cmpl $0x1, %r14d
ja 0x1c881c
cmpl $0x0, 0x10(%rbx)
je 0x1c8810
cmpl $0x0, 0xc(%rbx)
js 0x1c8810
movq 0x58(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
leaq 0x14(%rsp), %rcx
xorl %r8d, %r8d
callq 0x1c8245
testl %eax, %eax
je 0x1c880b
cmpl $0x0, 0x2c(%rsp)
movq 0x18(%rsp), %rdi
je 0x1c87e2
cmpl $0x0, 0x14(%rsp)
jne 0x1c8903
incl %r14d
movq 0x70(%rsp), %rax
movl (%rax), %eax
shrl %eax
movl 0x10(%rbx), %ecx
shrl %ecx
cmpl %ecx, %eax
cmovbl %eax, %ecx
addl %ecx, 0x28(%rsp)
movq 0x30(%rsp), %rax
addq %r15, %rax
movq %rax, 0x40(%rsp)
jmp 0x1c8810
movq 0x18(%rsp), %rdi
incq %r15
addq $0x20, %rbx
jmp 0x1c8796
cmpl $0x1, %r14d
movq 0x8(%rsp), %rax
ja 0x1c8908
movq 0x38(%rsp), %rbx
jne 0x1c88f7
incl 0x24(%rsp)
movq 0x68(%rsp), %r15
testq %r15, %r15
movabsq $0x100000000, %r14      # imm = 0x100000000
je 0x1c88ea
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0x1c8884
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
movq 0x60(%rsp), %rdx
movq 0x40(%rsp), %rsi
shldq $0x20, %rsi, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movq %rdx, (%rax,%rcx,8)
movq (%r15), %rcx
cmpq 0x8(%r15), %rcx
jne 0x1c88cc
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
movq 0x18(%rsp), %rdi
movl 0x40(%rsp), %edx
orq 0x50(%rsp), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movq %rdx, (%rax,%rcx,8)
movq 0x8(%rsp), %rax
incq %rbx
addq %r14, 0x30(%rsp)
jmp 0x1c8750
movabsq $0x100000000, %r14      # imm = 0x100000000
jmp 0x1c88ea
movq 0x8(%rsp), %rax
movq 0x38(%rsp), %rbx
movl 0x24(%rsp), %ecx
testl %ecx, %ecx
je 0x1c8555
cmpq %rax, %rbx
jb 0x1c8555
movl 0x28(%rsp), %ebp
cmpl %ecx, 0x10(%rsp)
je 0x1c8555
leaq 0x31c0d(%rip), %rdi     # 0x1fa544
leaq 0x2eb9b(%rip), %rsi     # 0x1f74d9
leaq 0x31c0e(%rip), %rcx     # 0x1fa553
movl $0x2d6e, %edx           # imm = 0x2D6E
callq 0x2110
pushq $-0x1
popq %rbp
jmp 0x1c8555