// File: /workspace/github_cpp_makefile_star2_O2/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
movq 0xa0(%rsp), %r12
movl %esi, %eax
cmpb $0x0, (%r12,%rax)
je 0x9f945
pushq $-0x1
popq %r13
jmp 0x9ffc7
movq %r9, %r15
movl %esi, %r13d
movq %rdi, 0x30(%rsp)
movl %edx, 0x1c(%rsp)
movl %ecx, 0xc(%rsp)
movq %r8, 0x10(%rsp)
movq 0x98(%rsp), %rax
andq $0x0, (%rax)
movq 0x90(%rsp), %rbx
andq $0x0, (%rbx)
xorl %eax, %eax
cmpq %rax, 0x8(%rbx)
je 0x9f985
movq 0x10(%rbx), %rax
xorl %edx, %edx
jmp 0x9f9a0
movq $0x2, 0x8(%rbx)
movq 0x10(%rbx), %rdi
pushq $0x8
popq %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rdx
movq 0xa8(%rsp), %r11
leaq 0x1(%rdx), %rcx
movq %rcx, (%rbx)
movl %r13d, (%rax,%rdx,4)
movl 0x1c(%rsp), %eax
xorl $0x1, %eax
movq %rax, 0x48(%rsp)
movl 0xc(%rsp), %esi
movl %esi, %eax
movq %rax, 0x50(%rsp)
xorl %r13d, %r13d
movq 0x10(%rsp), %rdi
decq %rcx
cmpq $-0x1, %rcx
je 0x9ffb0
movq 0x10(%rbx), %rax
movq %rcx, (%rbx)
movl (%rax,%rcx,4), %r14d
movb (%r12,%r14), %al
testb %al, %al
je 0x9fa03
movq %r14, %rdx
xorq $0x1, %rdx
decq %rcx
cmpb $0x0, (%r12,%rdx)
jne 0x9f9d5
movq %r13, 0x20(%rsp)
movq %r14, %r8
xorq $0x1, %r8
cmpl %esi, %r14d
movq %r8, 0x28(%rsp)
je 0x9fa41
testb %al, %al
jne 0x9fa41
movl %r14d, %eax
xorl 0x1c(%rsp), %eax
cmpl $0x1, %eax
je 0x9fa41
movzbl (%r11,%r8), %eax
cmpq %rdi, %rax
je 0x9fa41
movzbl (%r11,%r14), %eax
cmpq %r15, %rax
jne 0x9fc4f
movb (%r12,%r8), %al
cmpq 0x50(%rsp), %r8
je 0x9fe92
cmpl 0x48(%rsp), %r8d
je 0x9fe92
testb %al, %al
movq 0x20(%rsp), %r13
jne 0x9fe97
movzbl (%r11,%r14), %eax
cmpq %rdi, %rax
je 0x9ff9e
movzbl (%r11,%r8), %eax
cmpq %r15, %rax
je 0x9ff9e
movb $0x1, (%r12,%r8)
movq 0x98(%rsp), %r13
movq (%r13), %rcx
movq 0x10(%r13), %rax
cmpq 0x8(%r13), %rcx
movq %r15, 0x40(%rsp)
jne 0x9fae0
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r13)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x28(%rsp), %r8
movq 0xa8(%rsp), %r11
movl 0xc(%rsp), %esi
movq 0x10(%rsp), %rdi
movq %rax, 0x10(%r13)
movq (%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r13)
movl %r8d, (%rax,%rcx,4)
movq 0x30(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq (%rcx,%r8,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
imulq $0x18, %rdx, %r13
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %rbp
addq $0x8, %rbp
xorl %r15d, %r15d
cmpq %r15, %r13
je 0x9fe89
cmpl $0x0, 0x4(%rbp,%r15)
js 0x9fc46
movl (%rbp,%r15), %ecx
movb $0x1, %al
cmpl 0x1c(%rsp), %ecx
je 0x9ffd9
movl %ecx, %edx
xorl %esi, %edx
cmpl $0x1, %edx
je 0x9ffd9
movzbl (%r11,%rcx), %eax
cmpq %rdi, %rax
jne 0x9fbca
movb $0x0, (%r11,%rcx)
movl (%rbp,%r15), %ecx
movq %rcx, %rdx
xorq $0x1, %rdx
cmpb $0x0, (%r12,%rdx)
jne 0x9fbca
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x9fbb7
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x28(%rsp), %r8
movq 0xa8(%rsp), %r11
movq 0x10(%rsp), %rdi
movq %rax, 0x10(%rbx)
movl (%rbp,%r15), %edx
movq (%rbx), %rcx
xorl $0x1, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movl %edx, (%rax,%rcx,4)
movl (%rbp,%r15), %ecx
movl 0xc(%rsp), %esi
movl %ecx, %eax
xorq $0x1, %rax
movzbl (%r11,%rax), %edx
cmpq 0x40(%rsp), %rdx
jne 0x9fbe6
movb $0x0, (%r11,%rax)
movl (%rbp,%r15), %ecx
movl %ecx, %eax
cmpb $0x0, (%r12,%rax)
jne 0x9fc46
movq (%rbx), %rdx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rdx
jne 0x9fc38
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x28(%rsp), %r8
movq 0xa8(%rsp), %r11
movq 0x10(%rsp), %rdi
movq %rax, 0x10(%rbx)
movl (%rbp,%r15), %ecx
movq (%rbx), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%rbx)
movl %ecx, (%rax,%rdx,4)
movl 0xc(%rsp), %esi
addq $0x18, %r15
jmp 0x9fb16
movb $0x1, (%r12,%r14)
movq 0x98(%rsp), %r13
movq (%r13), %rcx
movq 0x10(%r13), %rax
cmpq 0x8(%r13), %rcx
jne 0x9fca6
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r13)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x28(%rsp), %r8
movq 0xa8(%rsp), %r11
movl 0xc(%rsp), %esi
movq 0x10(%rsp), %rdi
movq %rax, 0x10(%r13)
movq (%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r13)
movl %r14d, (%rax,%rcx,4)
movq 0x30(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq (%rcx,%r14,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
imulq $0x18, %rdx, %r9
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %r13
addq $0x8, %r13
xorl %ebp, %ebp
movq %r9, 0x38(%rsp)
cmpq %rbp, %r9
je 0x9fa41
cmpl $0x0, 0x4(%r13,%rbp)
js 0x9fe80
movl (%r13,%rbp), %ecx
movb $0x1, %al
cmpl 0x1c(%rsp), %ecx
je 0x9ffd9
movl %ecx, %edx
xorl %esi, %edx
cmpl $0x1, %edx
je 0x9ffd9
movzbl (%r11,%rcx), %eax
cmpq %rdi, %rax
jne 0x9fd99
movb $0x0, (%r11,%rcx)
movl (%r13,%rbp), %ecx
movq %rcx, %rdx
xorq $0x1, %rdx
cmpb $0x0, (%r12,%rdx)
jne 0x9fd99
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x9fd86
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x38(%rsp), %r9
movq 0x28(%rsp), %r8
movq 0xa8(%rsp), %r11
movq 0x10(%rsp), %rdi
movq %rax, 0x10(%rbx)
movl (%r13,%rbp), %edx
movq (%rbx), %rcx
xorl $0x1, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movl %edx, (%rax,%rcx,4)
movl (%r13,%rbp), %ecx
movl 0xc(%rsp), %esi
movl %ecx, %eax
xorq $0x1, %rax
movzbl (%r11,%rax), %edx
cmpq %r15, %rdx
jne 0x9fe1b
movb $0x0, (%r11,%rax)
movl (%r13,%rbp), %ecx
cmpb $0x0, (%r12,%rcx)
jne 0x9fe1b
movq (%rbx), %rdx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rdx
jne 0x9fe08
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x38(%rsp), %r9
movq 0x28(%rsp), %r8
movq 0xa8(%rsp), %r11
movq 0x10(%rsp), %rdi
movq %rax, 0x10(%rbx)
movl (%r13,%rbp), %ecx
movq (%rbx), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%rbx)
movl %ecx, (%rax,%rdx,4)
movl (%r13,%rbp), %ecx
movl 0xc(%rsp), %esi
movl %ecx, %eax
cmpb $0x0, (%r12,%rax)
jne 0x9fe80
movq (%rbx), %rdx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rdx
jne 0x9fe72
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x38(%rsp), %r9
movq 0x28(%rsp), %r8
movq 0xa8(%rsp), %r11
movq 0x10(%rsp), %rdi
movq %rax, 0x10(%rbx)
movl (%r13,%rbp), %ecx
movq (%rbx), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%rbx)
movl %ecx, (%rax,%rdx,4)
movl 0xc(%rsp), %esi
addq $0x18, %rbp
jmp 0x9fce0
movb (%r12,%r8), %al
movq 0x40(%rsp), %r15
movq 0x20(%rsp), %r13
testb %al, %al
je 0x9ff9e
cmpb $0x0, (%r12,%r14)
je 0x9ff9e
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x28(%rax), %rdx
movq (%rdx,%r8,8), %rax
movl %eax, %esi
shrq $0x20, %rax
imulq $0x18, %rsi, %rsi
imulq $0x18, %rax, %rax
leaq (%rcx,%rax), %rdi
addq $0xc, %rdi
xorl %r8d, %r8d
xorl %eax, %eax
cmpq %r8, %rsi
je 0x9ff1d
movslq (%rdi,%r8), %r9
testq %r9, %r9
js 0x9ff17
movl -0x4(%rdi,%r8), %r10d
movq %r10, %r11
xorq $0x1, %r11
cmpb $0x0, (%r12,%r11)
je 0x9ff0f
cmpb $0x0, (%r12,%r10)
movq 0xa8(%rsp), %r11
je 0x9ff17
cmpq %r9, %rax
cmovbeq %r9, %rax
jmp 0x9ff17
movq 0xa8(%rsp), %r11
addq $0x18, %r8
jmp 0x9fed6
movq (%rdx,%r14,8), %rdx
movl %edx, %esi
movq %rdx, %rdi
shrq $0x20, %rdi
imulq $0x18, %rsi, %rdx
imulq $0x18, %rdi, %rsi
addq %rcx, %rsi
addq $0xc, %rsi
xorl %edi, %edi
xorl %ecx, %ecx
cmpq %rdi, %rdx
je 0x9ff72
movslq (%rsi,%rdi), %r8
testq %r8, %r8
js 0x9ff6c
movl -0x4(%rsi,%rdi), %r9d
movq %r9, %r10
xorq $0x1, %r10
cmpb $0x0, (%r12,%r10)
je 0x9ff6c
cmpb $0x0, (%r12,%r9)
je 0x9ff6c
cmpq %r8, %rcx
cmovbeq %r8, %rcx
addq $0x18, %rdi
jmp 0x9ff3d
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 0x9ff95
subq %rcx, %r13
addq %rax, %r13
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
cmpq 0xb0(%rsp), %r13
ja 0x9ffb0
movq (%rbx), %rcx
jmp 0x9f9d2
xorl %eax, %eax
cmpq 0xb0(%rsp), %r13
pushq $-0x1
popq %rcx
cmovaq %rcx, %r13
testb %al, %al
cmovneq %rcx, %r13
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %r13
jmp 0x9ffb2