// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x98(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
callq 0x352840
cmpl $0x0, %eax
je 0x3529f7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3531cc
movl -0x14(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x38(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x34(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x34(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x38(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x34(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x34(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x88(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x30(%rbp), %eax
movl $0x0, -0x48(%rbp,%rax,4)
movl -0x2c(%rbp), %eax
movl -0x30(%rbp), %ecx
cmpl -0x40(%rbp,%rcx,4), %eax
jae 0x352b42
movl -0x30(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x2c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x10(%rax)
jne 0x352b07
jmp 0x352b37
movl -0x30(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x2c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x352b29
jmp 0x352b37
movl -0x30(%rbp), %eax
movl -0x48(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x48(%rbp,%rax,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x352ade
movl -0x30(%rbp), %eax
cmpl $0x0, -0x48(%rbp,%rax,4)
jne 0x352b58
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3531cc
movl $0x1, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x30(%rbp), %eax
movl $0x0, -0x48(%rbp,%rax,4)
movl -0x2c(%rbp), %eax
movl -0x30(%rbp), %ecx
cmpl -0x40(%rbp,%rcx,4), %eax
jae 0x352bd5
movl -0x30(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x2c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x10(%rax)
jne 0x352b9a
jmp 0x352bca
movl -0x30(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x2c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x352bbc
jmp 0x352bca
movl -0x30(%rbp), %eax
movl -0x48(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x48(%rbp,%rax,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x352b71
movl -0x30(%rbp), %eax
cmpl $0x0, -0x48(%rbp,%rax,4)
jne 0x352beb
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3531cc
movl $0x0, -0x7c(%rbp)
movl $0x0, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x1, -0x50(%rbp)
movl $0x0, -0x64(%rbp)
leaq -0x6c(%rbp), %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl -0x4c(%rbp), %ecx
cmpl -0x40(%rbp,%rcx,4), %eax
jae 0x352da0
movl -0x4c(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x54(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x10(%rax)
jne 0x352c54
jmp 0x352d92
movl -0x4c(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x54(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x352c79
jmp 0x352d92
movl $0x0, -0x58(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x58(%rbp), %ecx
movl -0x50(%rbp), %eax
movl %eax, %edx
xorl %eax, %eax
cmpl -0x40(%rbp,%rdx,4), %ecx
movb %al, -0x99(%rbp)
jae 0x352caa
cmpl $0x1, -0x60(%rbp)
setbe %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0x352cb9
jmp 0x352d6e
movl -0x50(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x58(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x10(%rax)
jne 0x352cd9
jmp 0x352d60
movl -0x50(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x58(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x352cfb
jmp 0x352d60
movq -0x10(%rbp), %rdi
movl -0x4c(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rsi
movl -0x54(%rbp), %eax
shlq $0x5, %rax
addq %rax, %rsi
movl -0x50(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rdx
movl -0x58(%rbp), %eax
shlq $0x5, %rax
addq %rax, %rdx
leaq -0x80(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0x3524e0
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jne 0x352d42
jmp 0x352d60
cmpl $0x0, -0x1c(%rbp)
je 0x352d57
cmpl $0x0, -0x80(%rbp)
jbe 0x352d57
movl $0x2, -0x60(%rbp)
jmp 0x352d6e
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x352c87
cmpl $0x1, -0x60(%rbp)
jbe 0x352d7d
movl $0x1, -0x64(%rbp)
jmp 0x352da0
cmpl $0x1, -0x60(%rbp)
jne 0x352d8e
movq -0x78(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x352d90
jmp 0x352d92
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x352c24
movq -0x78(%rbp), %rax
cmpl $0x0, (%rax)
je 0x352daf
cmpl $0x0, -0x64(%rbp)
je 0x352dbb
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3531cc
movl $0x1, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x64(%rbp)
leaq -0x6c(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl -0x4c(%rbp), %ecx
cmpl -0x40(%rbp,%rcx,4), %eax
jae 0x353185
movl -0x4c(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x54(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x10(%rax)
jne 0x352e13
jmp 0x353177
movl -0x4c(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x54(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x352e38
jmp 0x353177
movl $0x0, -0x58(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x58(%rbp), %ecx
movl -0x50(%rbp), %eax
movl %eax, %edx
xorl %eax, %eax
cmpl -0x40(%rbp,%rdx,4), %ecx
movb %al, -0x9a(%rbp)
jae 0x352e69
cmpl $0x1, -0x60(%rbp)
setbe %al
movb %al, -0x9a(%rbp)
movb -0x9a(%rbp), %al
testb $0x1, %al
jne 0x352e78
jmp 0x352fe9
movl -0x50(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x58(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x10(%rax)
jne 0x352e98
jmp 0x352fdb
movl -0x50(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x58(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x352ebd
jmp 0x352fdb
movq -0x10(%rbp), %rdi
movl -0x4c(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rsi
movl -0x54(%rbp), %eax
shlq $0x5, %rax
addq %rax, %rsi
movl -0x50(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rdx
movl -0x58(%rbp), %eax
shlq $0x5, %rax
addq %rax, %rdx
leaq -0x80(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0x3524e0
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jne 0x352f07
jmp 0x352fdb
cmpl $0x0, -0x1c(%rbp)
je 0x352f1f
cmpl $0x0, -0x80(%rbp)
jbe 0x352f1f
movl $0x2, -0x60(%rbp)
jmp 0x352fe9
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
movl -0x4c(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x54(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
shrl %eax
movl -0x50(%rbp), %ecx
movq -0x90(%rbp,%rcx,8), %rcx
movl -0x58(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x10(%rcx), %ecx
shrl %ecx
cmpl %ecx, %eax
ja 0x352f82
movl -0x4c(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x54(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
shrl %eax
movl %eax, -0xa0(%rbp)
jmp 0x352fa2
movl -0x50(%rbp), %eax
movq -0x90(%rbp,%rax,8), %rax
movl -0x58(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
shrl %eax
movl %eax, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
addl -0x7c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x54(%rbp), %eax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0x98(%rbp)
movl -0x58(%rbp), %eax
orq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x352e46
cmpl $0x1, -0x60(%rbp)
jbe 0x352ffb
movl $0x1, -0x64(%rbp)
jmp 0x353185
cmpl $0x1, -0x60(%rbp)
jne 0x353173
movq -0x78(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
cmpq $0x0, -0x28(%rbp)
je 0x353171
jmp 0x35301d
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x35308e
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x35304d
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xa8(%rbp)
jmp 0x35305b
movl $0x2, %eax
movq %rax, -0xa8(%rbp)
jmp 0x35305b
movq -0xa8(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x4c(%rbp), %eax
movl -0x38(%rbp,%rax,4), %eax
movl %eax, %edx
shlq $0x20, %rdx
movq -0x98(%rbp), %rax
shrq $0x20, %rax
orq %rax, %rdx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x3530c8
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x353139
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x3530f8
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xb0(%rbp)
jmp 0x353106
movl $0x2, %eax
movq %rax, -0xb0(%rbp)
jmp 0x353106
movq -0xb0(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x50(%rbp), %eax
movl -0x38(%rbp,%rax,4), %eax
movl %eax, %edx
shlq $0x20, %rdx
movq -0x98(%rbp), %rax
movl %eax, %eax
orq %rax, %rdx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x353171
jmp 0x353173
jmp 0x353175
jmp 0x353177
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x352de3
movq -0x78(%rbp), %rax
cmpl $0x0, (%rax)
je 0x353194
cmpl $0x0, -0x64(%rbp)
je 0x35319d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3531cc
movl -0x6c(%rbp), %eax
cmpl -0x68(%rbp), %eax
jne 0x3531a7
jmp 0x3531c6
leaq 0x460c7(%rip), %rdi     # 0x399275
leaq 0x43004(%rip), %rsi     # 0x3961b9
movl $0x2d6e, %edx           # imm = 0x2D6E
leaq 0x460c3(%rip), %rcx     # 0x399284
callq 0x2130
movl -0x7c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax,%rax)