// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: cal_self_close_tang_flank0(asg_t*, unsigned int, unsigned int, unsigned int, unsigned long, unsigned long, asg32_v*, asg32_v*, unsigned char*, unsigned char*, unsigned long)
uint64_t cal_self_close_tang_flank0(asg_t *g, uint32_t v0, uint32_t s0, uint32_t e0, uint64_t sf, uint64_t ef, asg32_v *b1, asg32_v *b2, uint8_t *f, uint8_t *fs, uint64_t max_path)
{
    uint64_t se = 0, k, v, o, o0, o1, nv, tot = 0; asg_arc_t *av;
    
    if(f[v0]) return (uint64_t)-1;

    // if((v0>>1) == 47687) fprintf(stderr, "+[M::%s] v0->utg%.6ul(%c), s0::%u, e0::%u, sf::%lu, ef::%lu\n", __func__, (v0>>1)+1, "+-"[v0&1], s0, e0, sf, ef);
    
    b1->n = b2->n = 0; kv_push(uint32_t, *b1, v0);
    while(b1->n) {
        v = b1->a[--b1->n];
        if(f[v] && f[v^1]) continue;
        // if((v0>>1) == 47687) fprintf(stderr, "[M::%s] utg%.6lul(%c), tot::%lu\n", __func__, (v>>1)+1, "+-"[v&1], tot);

        v = v; 
        // if((v0>>1) == 47687) fprintf(stderr, "[M::%s] v::%lu, f[v]::%u, fs[v]::%u, fs[v^1]::%u\n", __func__, v, f[v], fs[v], fs[v^1]);
        if((!f[v]) && (v!=(s0^1)) && (v!=e0)) {
            if((fs[v^1] != sf) && (fs[v] != ef)) {
                f[v] = 1; kv_push(uint32_t, *b2, v);
                av = asg_arc_a(g, v); nv = asg_arc_n(g, v); 
                for (k = 0; k < nv; k++) {
                    if(av[k].del) continue;
                    // if((v0>>1) == 47687) fprintf(stderr, "[M::%s] k::%lu, f[av[k].v]::%u, fs[av[k].v]::%u, fs[av[k].v^1]::%u\n", __func__, k, f[av[k].v], fs[av[k].v], fs[av[k].v^1]);
                    if((av[k].v == s0) || (av[k].v == (e0^1))) {
                        se = 1; break;
                    }
                    if(fs[av[k].v] == sf) {
                        fs[av[k].v] = 0;///not a beg node anymore
                        if(!f[av[k].v^1]) kv_push(uint32_t, *b1, (av[k].v^1));///push reverse node
                    }
                    if(fs[(av[k].v^1)] == ef) {
                        fs[(av[k].v^1)] = 0;///not a end node anymore; do not need to push as it will be pushed later
                        if(!f[av[k].v]) kv_push(uint32_t, *b1, (av[k].v));///push reverse node
                    }

                    /**if((fs[av[k].v^1] != sf) && (fs[av[k].v] != ef))**/ {
                        if((!f[av[k].v])) kv_push(uint32_t, *b1, av[k].v);
                    }
                }
                if(se) break;
            }
        }

        v = v^1; 
        // if((v0>>1) == 47687) fprintf(stderr, "[M::%s] v::%lu, f[v]::%u, fs[v]::%u, fs[v^1]::%u\n", __func__, v, f[v], fs[v], fs[v^1]);
        if((!f[v]) && (v != (s0^1)) && (v != e0)) {
            if((fs[v^1] != sf) && (fs[v] != ef)) {
                f[v] = 1; kv_push(uint32_t, *b2, v);
                av = asg_arc_a(g, v); nv = asg_arc_n(g, v); 
                for (k = 0; k < nv; k++) {
                    if(av[k].del) continue;
                    // if((v0>>1) == 47687) fprintf(stderr, "[M::%s] k::%lu, f[av[k].v]::%u, fs[av[k].v]::%u, fs[av[k].v^1]::%u\n", __func__, k, f[av[k].v], fs[av[k].v], fs[av[k].v^1]);
                    if((av[k].v == s0) || (av[k].v == (e0^1))) {
                        se = 1; break;
                    }
                    if(fs[av[k].v] == sf) {
                        fs[av[k].v] = 0;///not a beg node anymore
                        if(!f[av[k].v^1]) kv_push(uint32_t, *b1, (av[k].v^1));///push reverse node
                    }
                    if(fs[(av[k].v^1)] == ef) fs[(av[k].v^1)] = 0;///not a end node anymore; do not need to push as it will be pushed later

                    /**if((fs[av[k].v^1] != sf) && (fs[av[k].v] != ef))**/ {
                        if((!f[av[k].v])) kv_push(uint32_t, *b1, av[k].v);
                    }
                }
                if(se) break;
            }
        }

        if(f[v] && f[v^1]) {
            av = asg_arc_a(g, v); nv = asg_arc_n(g, v); 
            for (k = o = 0; k < nv; k++) {
                if(av[k].del) continue;
                if((f[av[k].v^1]) && (f[av[k].v]) && (o < av[k].ol)) o = av[k].ol;
            }
            o0 = o;

            av = asg_arc_a(g, (v^1)); nv = asg_arc_n(g, (v^1)); 
            for (k = o = 0; k < nv; k++) {
                if(av[k].del) continue;
                if((f[av[k].v^1]) && (f[av[k].v]) && (o < av[k].ol)) o = av[k].ol;
            }
            o1 = o;

            if(o0 + o1 < g->seq[v>>1].len) tot += g->seq[v>>1].len - o0 - o1;
        }

        if(tot > max_path) break;
    }

    // if((v0>>1) == 47687) fprintf(stderr, "-[M::%s] v0->utg%.6ul(%c), tot::%lu, se::%lu\n", __func__, (v0>>1)+1, "+-"[v0&1], tot, se);

    if((!se) && (tot <= max_path)) return tot;
    return (uint64_t)-1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, %r12d
movq 0xa0(%rsp), %rbx
movl %esi, %esi
movq $-0x1, %rax
cmpb $0x0, (%rbx,%rsi)
jne 0xdc438
movq %r9, %r13
movq %rdi, %rbp
movl %edx, 0x1c(%rsp)
movl %ecx, 0xc(%rsp)
movq 0x98(%rsp), %rcx
movq 0x90(%rsp), %r14
xorl %eax, %eax
movq %rax, (%rcx)
movq %rax, (%r14)
cmpq %rax, 0x8(%r14)
movq %r8, 0x10(%rsp)
jne 0xdbd9d
movq $0x2, 0x8(%r14)
movq 0x10(%r14), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x10(%rsp), %r8
movq %rax, 0x10(%r14)
movq 0xb0(%rsp), %r15
movq 0xa8(%rsp), %rsi
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
movl %r12d, (%rcx,%rax,4)
movl 0x1c(%rsp), %eax
xorl $0x1, %eax
movq %rax, 0x48(%rsp)
movl 0xc(%rsp), %edi
movl %edi, %eax
movq %rax, 0x50(%rsp)
movq $0x0, 0x20(%rsp)
movq %rbp, 0x30(%rsp)
movq (%r14), %rax
decq %rax
cmpq $-0x1, %rax
je 0xdc41d
movq 0x10(%r14), %rcx
movq %rax, (%r14)
movl (%rcx,%rax,4), %r12d
movb (%rbx,%r12), %cl
testb %cl, %cl
je 0xdbe17
movq %r12, %rdx
xorq $0x1, %rdx
decq %rax
cmpb $0x0, (%rbx,%rdx)
jne 0xdbdea
cmpl %edi, %r12d
je 0xdc08f
testb %cl, %cl
jne 0xdc08f
movl %r12d, %eax
xorl 0x1c(%rsp), %eax
cmpl $0x1, %eax
je 0xdc08f
movq %r12, %rax
xorq $0x1, %rax
movzbl (%rsi,%rax), %eax
cmpq %r8, %rax
je 0xdc08f
movzbl (%rsi,%r12), %eax
cmpq %r13, %rax
je 0xdc08f
movb $0x1, (%rbx,%r12)
movq 0x98(%rsp), %r15
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0xdbea6
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0xa8(%rsp), %rsi
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %r8
movq %rax, 0x10(%r15)
movq (%r15), %rax
movq 0x10(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
movl %r12d, (%rcx,%rax,4)
movq 0x30(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax,%r12,8), %rax
movq %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
je 0xdc08f
movq 0x30(%rsp), %rdx
movq 0x8(%rdx), %rdx
shrq $0x20, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r9
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rbp
addq $0x8, %rbp
xorl %r15d, %r15d
movq %r9, 0x28(%rsp)
cmpl $0x0, 0x4(%rbp,%r15)
js 0xdc082
movl (%rbp,%r15), %eax
movb $0x1, %cl
cmpl 0x1c(%rsp), %eax
je 0xdc447
movl %eax, %edx
xorl %edi, %edx
cmpl $0x1, %edx
je 0xdc447
movzbl (%rsi,%rax), %ecx
cmpq %r8, %rcx
jne 0xdbfa2
movb $0x0, (%rsi,%rax)
movl (%rbp,%r15), %eax
xorq $0x1, %rax
cmpb $0x0, (%rbx,%rax)
jne 0xdbfa2
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0xdbf81
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x28(%rsp), %r9
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %r8
movq %rax, 0x10(%r14)
movl (%rbp,%r15), %eax
xorl $0x1, %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movl %eax, (%rdx,%rcx,4)
movq 0xa8(%rsp), %rsi
movl (%rbp,%r15), %eax
xorq $0x1, %rax
movzbl (%rsi,%rax), %ecx
cmpq %r13, %rcx
jne 0xdc01d
movb $0x0, (%rsi,%rax)
movl (%rbp,%r15), %eax
cmpb $0x0, (%rbx,%rax)
jne 0xdc01d
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0xdbfff
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x28(%rsp), %r9
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %r8
movq %rax, 0x10(%r14)
movl (%rbp,%r15), %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movl %eax, (%rdx,%rcx,4)
movq 0xa8(%rsp), %rsi
movl (%rbp,%r15), %eax
cmpb $0x0, (%rbx,%rax)
jne 0xdc082
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0xdc064
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x28(%rsp), %r9
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %r8
movq %rax, 0x10(%r14)
movl (%rbp,%r15), %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movl %eax, (%rdx,%rcx,4)
movq 0xa8(%rsp), %rsi
addq $0x18, %r15
cmpq %r15, %r9
jne 0xdbeff
movq %r12, %r15
xorq $0x1, %r15
cmpq 0x50(%rsp), %r15
je 0xdc2d8
cmpl 0x48(%rsp), %r15d
je 0xdc2d8
cmpb $0x0, (%rbx,%r15)
jne 0xdc2d8
movzbl (%rsi,%r12), %eax
cmpq %r8, %rax
je 0xdc2d8
movzbl (%rsi,%r15), %eax
cmpq %r13, %rax
je 0xdc2d8
movb $0x1, (%rbx,%r15)
movq 0x98(%rsp), %rbp
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0xdc120
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbp)
movq 0x10(%rbp), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0xa8(%rsp), %rsi
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %r8
movq %rax, 0x10(%rbp)
movq %r13, 0x28(%rsp)
movq (%rbp), %rax
movq 0x10(%rbp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbp)
movl %r15d, (%rcx,%rax,4)
movq 0x30(%rsp), %rdx
movq 0x28(%rdx), %rax
movq (%rax,%r15,8), %rcx
movq %rcx, %r9
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %r9
setne %al
je 0xdc2c9
movq 0x8(%rdx), %rdx
shrq $0x20, %rcx
movq %r9, %r10
negq %r10
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rbp
addq $0x8, %rbp
movl $0x1, %r13d
movq %r9, 0x40(%rsp)
movq %r10, 0x38(%rsp)
cmpl $0x0, 0x4(%rbp)
js 0xdc2ab
movl (%rbp), %edx
movl $0x1, %ecx
cmpl 0x1c(%rsp), %edx
je 0xdc2cb
movl %edx, %esi
xorl %edi, %esi
cmpl $0x1, %esi
movq 0xa8(%rsp), %rsi
je 0xdc2cb
movzbl (%rsi,%rdx), %eax
cmpq %r8, %rax
jne 0xdc22f
movb $0x0, (%rsi,%rdx)
movl (%rbp), %eax
xorq $0x1, %rax
cmpb $0x0, (%rbx,%rax)
jne 0xdc22f
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0xdc210
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x38(%rsp), %r10
movq 0x40(%rsp), %r9
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %r8
movq %rax, 0x10(%r14)
movl (%rbp), %eax
xorl $0x1, %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movl %eax, (%rdx,%rcx,4)
movq 0xa8(%rsp), %rsi
movl (%rbp), %eax
xorq $0x1, %rax
movzbl (%rsi,%rax), %ecx
cmpq 0x28(%rsp), %rcx
jne 0xdc245
movb $0x0, (%rsi,%rax)
movl (%rbp), %eax
cmpb $0x0, (%rbx,%rax)
jne 0xdc2ab
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0xdc28f
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x38(%rsp), %r10
movq 0x40(%rsp), %r9
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %r8
movq %rax, 0x10(%r14)
movl (%rbp), %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movl %eax, (%rdx,%rcx,4)
movq 0xa8(%rsp), %rsi
cmpq %r9, %r13
setb %al
leaq (%r10,%r13), %rcx
incq %rcx
incq %r13
addq $0x18, %rbp
cmpq $0x1, %rcx
jne 0xdc184
xorl %ecx, %ecx
testb $0x1, %al
movq 0x28(%rsp), %r13
jne 0xdc447
cmpb $0x0, (%rbx,%r15)
je 0xdc35e
cmpb $0x0, (%rbx,%r12)
movq 0x30(%rsp), %rbp
je 0xdc35e
movq 0x8(%rbp), %rcx
movq 0x28(%rbp), %rdx
movq (%rdx,%r15,8), %rax
movq %rax, %rsi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rsi
je 0xdc36b
shrq $0x20, %rax
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,8), %rdi
addq $0xc, %rdi
xorl %r8d, %r8d
xorl %eax, %eax
movq 0xb0(%rsp), %r15
movslq (%rdi,%r8), %r9
testq %r9, %r9
js 0xdc353
movl -0x4(%rdi,%r8), %r10d
movq %r10, %r11
xorq $0x1, %r11
cmpb $0x0, (%rbx,%r11)
je 0xdc353
cmpb $0x0, (%rbx,%r10)
je 0xdc353
cmpq %r9, %rax
cmovbeq %r9, %rax
addq $0x18, %r8
cmpq %r8, %rsi
jne 0xdc329
jmp 0xdc375
movq 0xb0(%rsp), %r15
jmp 0xdc412
xorl %eax, %eax
movq 0xb0(%rsp), %r15
movq (%rdx,%r12,8), %rsi
movq %rsi, %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rdx
je 0xdc3d7
shrq $0x20, %rsi
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rsi,%rsi,2), %rsi
leaq (%rcx,%rsi,8), %rsi
addq $0xc, %rsi
xorl %edi, %edi
xorl %ecx, %ecx
movslq (%rsi,%rdi), %r8
testq %r8, %r8
js 0xdc3cc
movl -0x4(%rsi,%rdi), %r9d
movq %r9, %r10
xorq $0x1, %r10
cmpb $0x0, (%rbx,%r10)
je 0xdc3cc
cmpb $0x0, (%rbx,%r9)
je 0xdc3cc
cmpq %r8, %rcx
cmovbeq %r8, %rcx
addq $0x18, %rdi
cmpq %rdi, %rdx
jne 0xdc3a2
jmp 0xdc3d9
xorl %ecx, %ecx
addq %rax, %rcx
movq 0x20(%rbp), %rax
shrl %r12d
movl (%rax,%r12,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %rcx
jae 0xdc401
movq 0x20(%rsp), %rdx
subq %rcx, %rdx
addq %rax, %rdx
movq %rdx, 0x20(%rsp)
movq 0x10(%rsp), %r8
movl 0xc(%rsp), %edi
movq 0xa8(%rsp), %rsi
cmpq %r15, 0x20(%rsp)
jbe 0xdbde4
xorl %ecx, %ecx
movq 0x20(%rsp), %rax
cmpq %r15, %rax
movq $-0x1, %rdx
cmovaq %rdx, %rax
testb %cl, %cl
cmovneq %rdx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb0(%rsp), %r15
jmp 0xdc41f