// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq 0x90(%rsp), %rbx
movl %esi, %eax
movq $-0x1, %r13
cmpb $0x0, (%rbx,%rax)
jne 0xdd815
movl %edx, %ebp
movl %esi, %r12d
movq %rdi, 0x30(%rsp)
movl %ecx, 0x8(%rsp)
movq %r9, 0x18(%rsp)
movq 0x88(%rsp), %rcx
movq 0x80(%rsp), %r14
xorl %eax, %eax
movq %rax, (%rcx)
movq %rax, (%r14)
cmpq %rax, 0x8(%r14)
movq %r8, 0x10(%rsp)
je 0xdd0e7
movq 0x10(%r14), %rax
movl %r12d, (%rax)
movl $0x1, %ecx
jmp 0xdd122
movq $0x2, 0x8(%r14)
movq 0x10(%r14), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x10(%rsp), %r8
movq %rax, 0x10(%r14)
movq (%r14), %rdx
movq %rdx, %rcx
incq %rcx
movq %rcx, (%r14)
movl %r12d, (%rax,%rdx,4)
jne 0xdd122
xorl %eax, %eax
xorl %r13d, %r13d
jmp 0xdd7fc
movl %ebp, %eax
xorl $0x1, %eax
movq %rax, 0x38(%rsp)
movl 0x8(%rsp), %esi
movl %esi, %eax
movq %rax, 0x40(%rsp)
xorl %r13d, %r13d
movq 0x18(%rsp), %rdi
movq 0x80(%rsp), %r12
movl %ebp, 0xc(%rsp)
movq %rcx, %rax
movq 0x10(%r12), %rdx
decq %rcx
movq %rcx, (%r12)
movl -0x4(%rdx,%rax,4), %r14d
cmpb $0x0, (%rbx,%r14)
je 0xdd17d
movq %r14, %r15
xorq $0x1, %r15
cmpb $0x0, (%rbx,%r15)
jne 0xdd7ea
jmp 0xdd440
movl %r14d, %eax
xorl %ebp, %eax
cmpl $0x1, %eax
sete %al
cmpl %esi, %r14d
sete %cl
orb %al, %cl
movq %r14, %r15
xorq $0x1, %r15
testb %cl, %cl
jne 0xdd440
movq 0x98(%rsp), %rcx
movzbl (%rcx,%r15), %eax
cmpq %r8, %rax
je 0xdd440
movzbl (%rcx,%r14), %eax
cmpq %rdi, %rax
je 0xdd440
movb $0x1, (%rbx,%r14)
movq 0x88(%rsp), %r12
movq (%r12), %rcx
movq 0x10(%r12), %rax
cmpq 0x8(%r12), %rcx
movq %r13, 0x20(%rsp)
jne 0xdd21d
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r12)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x8(%rsp), %esi
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rdi
movq %rax, 0x10(%r12)
movq (%r12), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r12)
movl %r14d, (%rax,%rcx,4)
movq 0x30(%rsp), %rdx
movq 0x28(%rdx), %rax
movq (%rax,%r14,8), %rax
movq %rax, %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
andq %r9, %rcx
je 0xdd43b
movq 0x8(%rdx), %rdx
shrq $0x20, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r10
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %r12
addq $0x8, %r12
xorl %r13d, %r13d
movq %r10, 0x28(%rsp)
cmpl $0x0, 0x4(%r12,%r13)
js 0xdd42e
movl (%r12,%r13), %ecx
movb $0x1, %al
cmpl %ebp, %ecx
je 0xdd7f7
movl %ecx, %edx
xorl %esi, %edx
cmpl $0x1, %edx
je 0xdd7f7
movq 0x98(%rsp), %r9
movzbl (%r9,%rcx), %eax
cmpq %r8, %rax
jne 0xdd333
movb $0x0, (%r9,%rcx)
movl (%r12,%r13), %ecx
movq %rcx, %rdx
xorq $0x1, %rdx
cmpb $0x0, (%rbx,%rdx)
jne 0xdd333
movq 0x80(%rsp), %r9
movq (%r9), %rcx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rcx
jne 0xdd319
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r9)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x80(%rsp), %r9
movq 0x28(%rsp), %r10
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rdi
movq %rax, 0x10(%r9)
movl (%r12,%r13), %edx
movq (%r9), %rcx
xorl $0x1, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r9)
movl %edx, (%rax,%rcx,4)
movl (%r12,%r13), %ecx
movl 0x8(%rsp), %esi
movq 0x98(%rsp), %r9
movl %ecx, %eax
xorq $0x1, %rax
movzbl (%r9,%rax), %edx
cmpq %rdi, %rdx
jne 0xdd3c2
movq 0x98(%rsp), %rcx
movb $0x0, (%rcx,%rax)
movl (%r12,%r13), %ecx
cmpb $0x0, (%rbx,%rcx)
jne 0xdd3c2
movq 0x80(%rsp), %r9
movq (%r9), %rdx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rdx
jne 0xdd3b0
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r9)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x80(%rsp), %r9
movq 0x28(%rsp), %r10
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rdi
movq %rax, 0x10(%r9)
movl (%r12,%r13), %ecx
movq (%r9), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r9)
movl %ecx, (%rax,%rdx,4)
movl (%r12,%r13), %ecx
movl 0x8(%rsp), %esi
movl %ecx, %eax
cmpb $0x0, (%rbx,%rax)
jne 0xdd42e
movq 0x80(%rsp), %rbp
movq (%rbp), %rdx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rdx
jne 0xdd41b
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x28(%rsp), %r10
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rdi
movq %rax, 0x10(%rbp)
movl (%r12,%r13), %ecx
movq (%rbp), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%rbp)
movl %ecx, (%rax,%rdx,4)
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %ebp
addq $0x18, %r13
cmpq %r13, %r10
jne 0xdd26c
movq 0x20(%rsp), %r13
movq %r13, 0x20(%rsp)
cmpq 0x40(%rsp), %r15
movq 0x98(%rsp), %r12
je 0xdd686
cmpl 0x38(%rsp), %r15d
je 0xdd686
cmpb $0x0, (%rbx,%r15)
jne 0xdd686
movzbl (%r12,%r14), %eax
cmpq %r8, %rax
je 0xdd686
movzbl (%r12,%r15), %eax
cmpq %rdi, %rax
je 0xdd686
movb $0x1, (%rbx,%r15)
movq 0x88(%rsp), %r13
movq (%r13), %rcx
movq 0x10(%r13), %rax
cmpq 0x8(%r13), %rcx
jne 0xdd4db
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r13)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x8(%rsp), %esi
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rdi
movq %rax, 0x10(%r13)
movq (%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r13)
movl %r15d, (%rax,%rcx,4)
movq 0x30(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax,%r15,8), %rax
movq %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
je 0xdd686
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), %r13
addq $0x8, %r13
xorl %ebp, %ebp
movq %r9, 0x28(%rsp)
cmpl $0x0, 0x4(%r13,%rbp)
js 0xdd679
movl (%r13,%rbp), %ecx
movb $0x1, %al
cmpl 0xc(%rsp), %ecx
je 0xdd7f7
movl %ecx, %edx
xorl %esi, %edx
cmpl $0x1, %edx
je 0xdd7f7
movzbl (%r12,%rcx), %eax
cmpq %r8, %rax
jne 0xdd5ea
movb $0x0, (%r12,%rcx)
movl (%r13,%rbp), %ecx
movq %rcx, %rdx
xorq $0x1, %rdx
cmpb $0x0, (%rbx,%rdx)
jne 0xdd5ea
movq 0x80(%rsp), %r10
movq (%r10), %rcx
movq 0x10(%r10), %rax
cmpq 0x8(%r10), %rcx
jne 0xdd5d7
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r10)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x80(%rsp), %r10
movq 0x28(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rdi
movq %rax, 0x10(%r10)
movl (%r13,%rbp), %edx
movq (%r10), %rcx
xorl $0x1, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r10)
movl %edx, (%rax,%rcx,4)
movl (%r13,%rbp), %ecx
movl 0x8(%rsp), %esi
movl %ecx, %eax
xorq $0x1, %rax
movzbl (%r12,%rax), %edx
cmpq %rdi, %rdx
jne 0xdd604
movb $0x0, (%r12,%rax)
movl (%r13,%rbp), %ecx
movl %ecx, %eax
cmpb $0x0, (%rbx,%rax)
jne 0xdd679
movq 0x80(%rsp), %r12
movq (%r12), %rdx
movq 0x10(%r12), %rax
cmpq 0x8(%r12), %rdx
jne 0xdd662
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r12)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x28(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rdi
movq %rax, 0x10(%r12)
movl (%r13,%rbp), %ecx
movq (%r12), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r12)
movl %ecx, (%rax,%rdx,4)
movl 0x8(%rsp), %esi
movq 0x98(%rsp), %r12
addq $0x18, %rbp
cmpq %rbp, %r9
jne 0xdd52d
cmpb $0x0, (%rbx,%r15)
je 0xdd71d
cmpb $0x0, (%rbx,%r14)
movq 0x20(%rsp), %r13
je 0xdd728
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x28(%rax), %rdx
movq (%rdx,%r15,8), %rax
movq %rax, %rsi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rsi
je 0xdd739
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
movl 0xc(%rsp), %ebp
movq 0x80(%rsp), %r12
movslq (%rdi,%r8), %r9
testq %r9, %r9
js 0xdd712
movl -0x4(%rdi,%r8), %r10d
movq %r10, %r11
xorq $0x1, %r11
cmpb $0x0, (%rbx,%r11)
je 0xdd712
cmpb $0x0, (%rbx,%r10)
je 0xdd712
cmpq %r9, %rax
cmovbeq %r9, %rax
addq $0x18, %r8
cmpq %r8, %rsi
jne 0xdd6e8
jmp 0xdd747
movl 0xc(%rsp), %ebp
movq 0x20(%rsp), %r13
jmp 0xdd72c
movl 0xc(%rsp), %ebp
movq 0x80(%rsp), %r12
jmp 0xdd7dc
xorl %eax, %eax
movl 0xc(%rsp), %ebp
movq 0x80(%rsp), %r12
movq (%rdx,%r14,8), %rsi
movq %rsi, %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rdx
je 0xdd7a9
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 0xdd79e
movl -0x4(%rsi,%rdi), %r9d
movq %r9, %r10
xorq $0x1, %r10
cmpb $0x0, (%rbx,%r10)
je 0xdd79e
cmpb $0x0, (%rbx,%r9)
je 0xdd79e
cmpq %r8, %rcx
cmovbeq %r8, %rcx
addq $0x18, %rdi
cmpq %rdi, %rdx
jne 0xdd774
jmp 0xdd7ab
xorl %ecx, %ecx
addq %rax, %rcx
movq 0x30(%rsp), %rax
movq 0x20(%rax), %rax
shrl %r14d
movl (%rax,%r14,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %rcx
jae 0xdd7ce
subq %rcx, %r13
addq %rax, %r13
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r8
movl 0x8(%rsp), %esi
cmpq 0xa0(%rsp), %r13
ja 0xdd7f3
movq (%r12), %rcx
testq %rcx, %rcx
jne 0xdd14b
xorl %eax, %eax
jmp 0xdd7fc
movq 0x20(%rsp), %r13
cmpq 0xa0(%rsp), %r13
movq $-0x1, %rcx
cmovaq %rcx, %r13
testb %al, %al
cmovneq %rcx, %r13
movq %r13, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq