// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: cal_self_close_tang_flank1(asg_t*, unsigned int, unsigned int, asg32_v*, asg32_v*, unsigned char*, unsigned char*, unsigned long)
uint64_t cal_self_close_tang_flank1(asg_t *g, uint32_t s0, uint32_t e0, asg32_v *b1, asg32_v *b2, uint8_t *f, uint8_t *fsk, uint64_t max_path)
{
    uint64_t se = 0, k, v, o, o0, o1, nv, tot = 0; asg_arc_t *av;
        
    b1->n = b2->n = 0; kv_push(uint32_t, *b1, s0);
    while(b1->n) {
        v = b1->a[--b1->n];
        if(f[v] && f[v^1]) continue;

        v = v; 
        if((!f[v]) && (v!=(s0^1)) && (v!=e0)) {
            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((av[k].v == s0) || (av[k].v == (e0^1))) {
                    se = 1; break;
                }
                if((!f[av[k].v])) kv_push(uint32_t, *b1, av[k].v);
            }
            if(se) break;
        }

        v = v^1; 
        if((!f[v]) && (v!=(s0^1)) && (v!=e0)) {
            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((av[k].v == s0) || (av[k].v == (e0^1))) {
                    se = 1; break;
                }
                if((!f[av[k].v])) kv_push(uint32_t, *b1, av[k].v);
            }
            if(se) break;
        }

        if(f[v] && f[v^1] && (!fsk[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]) && (!fsk[av[k].v>>1]) && (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]) && (!fsk[av[k].v>>1]) && (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((!se) && (tot <= max_path)) return tot;
    return (uint64_t)-1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %rbx
movq %r8, %r14
movq %rcx, %r15
movq %rdi, 0x8(%rsp)
xorl %eax, %eax
movq %rax, (%r8)
movq %rax, (%rcx)
cmpq %rax, 0x8(%rcx)
movl %edx, 0x4(%rsp)
movl %esi, (%rsp)
je 0xdd868
movl %edx, %r8d
movq 0x10(%r15), %rax
movl %esi, (%rax)
movl $0x1, %ecx
jmp 0xdd8ab
movq $0x2, 0x8(%r15)
movq 0x10(%r15), %rdi
movl $0x8, %esi
callq 0x2370
movl (%rsp), %esi
movl 0x4(%rsp), %r8d
movq %rax, 0x10(%r15)
movq (%r15), %rdx
movq %rdx, %rcx
incq %rcx
movq %rcx, (%r15)
movl %esi, (%rax,%rdx,4)
jne 0xdd8ab
xorl %edi, %edi
movq $0x0, 0x10(%rsp)
jmp 0xddd08
movq %r14, 0x18(%rsp)
movl %esi, %eax
xorl $0x1, %eax
movq %rax, 0x28(%rsp)
movl %r8d, %eax
movq %rax, 0x30(%rsp)
movq $0x0, 0x10(%rsp)
movq %rcx, %rax
movq 0x10(%r15), %rdx
decq %rcx
movq %rcx, (%r15)
movl -0x4(%rdx,%rax,4), %r9d
cmpb $0x0, (%rbx,%r9)
je 0xdd8fa
movq %r9, %rax
xorq $0x1, %rax
cmpb $0x0, (%rbx,%rax)
jne 0xddcfd
jmp 0xdda36
movl %r9d, %eax
xorl %esi, %eax
cmpl $0x1, %eax
sete %al
cmpl %r8d, %r9d
sete %cl
orb %al, %cl
jne 0xdda36
movb $0x1, (%rbx,%r9)
movq 0x18(%rsp), %r14
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0xdd95f
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r9, %r12
callq 0x2370
movq %r12, %r9
movl (%rsp), %esi
movl 0x4(%rsp), %r8d
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movl %r9d, (%rax,%rcx,4)
movq 0x8(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax,%r9,8), %rax
movq %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
je 0xdda36
movq 0x8(%rsp), %rdx
movq 0x8(%rdx), %rdx
shrq $0x20, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r13
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rbp
addq $0x8, %rbp
xorl %r12d, %r12d
cmpl $0x0, 0x4(%rbp,%r12)
js 0xdda29
movl (%rbp,%r12), %ecx
movb $0x1, %dil
cmpl %esi, %ecx
je 0xddd08
movl %ecx, %edx
xorl %r8d, %edx
cmpl $0x1, %edx
je 0xddd08
cmpb $0x0, (%rbx,%rcx)
jne 0xdda29
movq (%r15), %rdx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rdx
jne 0xdda1f
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r9, %r14
callq 0x2370
movq %r14, %r9
movl (%rsp), %esi
movl 0x4(%rsp), %r8d
movq %rax, 0x10(%r15)
movl (%rbp,%r12), %ecx
movq (%r15), %rdx
leaq 0x1(%rdx), %rdi
movq %rdi, (%r15)
movl %ecx, (%rax,%rdx,4)
addq $0x18, %r12
cmpq %r12, %r13
jne 0xdd9ac
movq %r9, %r13
xorq $0x1, %r13
cmpq 0x30(%rsp), %r13
je 0xddb99
cmpl 0x28(%rsp), %r13d
je 0xddb99
cmpb $0x0, (%rbx,%r13)
jne 0xddb99
movb $0x1, (%rbx,%r13)
movq 0x18(%rsp), %r14
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0xddaaa
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r9, %r12
callq 0x2370
movq %r12, %r9
movl (%rsp), %esi
movl 0x4(%rsp), %r8d
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movl %r13d, (%rax,%rcx,4)
movq 0x8(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax,%r13,8), %rax
movq %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
je 0xddb99
movq 0x8(%rsp), %rdx
movq 0x8(%rdx), %rdx
shrq $0x20, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rbp
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %r12
addq $0x8, %r12
xorl %r14d, %r14d
movq %r13, 0x20(%rsp)
cmpl $0x0, 0x4(%r12,%r14)
js 0xddb8c
movl (%r12,%r14), %ecx
movb $0x1, %dil
cmpl %esi, %ecx
je 0xddd08
movl %ecx, %edx
xorl %r8d, %edx
cmpl $0x1, %edx
je 0xddd08
cmpb $0x0, (%rbx,%rcx)
jne 0xddb8c
movq (%r15), %rdx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rdx
jne 0xddb82
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r15, %r13
movq %rbx, %r15
movq %r9, %rbx
callq 0x2370
movq %rbx, %r9
movq %r15, %rbx
movq %r13, %r15
movq 0x20(%rsp), %r13
movl (%rsp), %esi
movl 0x4(%rsp), %r8d
movq %rax, 0x10(%r15)
movl (%r12,%r14), %ecx
movq (%r15), %rdx
leaq 0x1(%rdx), %rdi
movq %rdi, (%r15)
movl %ecx, (%rax,%rdx,4)
addq $0x18, %r14
cmpq %r14, %rbp
jne 0xddafc
cmpb $0x0, (%rbx,%r13)
je 0xddcee
cmpb $0x0, (%rbx,%r9)
je 0xddcee
movl %r9d, %eax
shrl %eax
movq 0x70(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
jne 0xddcee
movq %r9, %r12
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rdx
movq 0x28(%rcx), %rsi
movq (%rsi,%r13,8), %rcx
movq %rcx, %rdi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
andq %r8, %rdi
je 0xddc46
shrq $0x20, %rcx
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %rdi
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %r8
addq $0xc, %r8
xorl %r9d, %r9d
xorl %ecx, %ecx
movslq (%r8,%r9), %r10
testq %r10, %r10
js 0xddc3b
movl -0x4(%r8,%r9), %r11d
movq %r11, %r14
xorq $0x1, %r14
cmpb $0x0, (%rbx,%r14)
je 0xddc3b
cmpb $0x0, (%rbx,%r11)
je 0xddc3b
shrl %r11d
movq 0x70(%rsp), %r14
cmpb $0x0, (%r14,%r11)
jne 0xddc3b
cmpq %r10, %rcx
cmovbeq %r10, %rcx
addq $0x18, %r9
cmpq %r9, %rdi
jne 0xddc02
jmp 0xddc48
xorl %ecx, %ecx
movq (%rsi,%r12,8), %rdi
movq %rdi, %rsi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
andq %r8, %rsi
je 0xddcbb
shrq $0x20, %rdi
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
leaq (%rdi,%rdi,2), %rdi
leaq (%rdx,%rdi,8), %rdi
addq $0xc, %rdi
xorl %r8d, %r8d
xorl %edx, %edx
movslq (%rdi,%r8), %r9
testq %r9, %r9
js 0xddcb0
movl -0x4(%rdi,%r8), %r10d
movq %r10, %r11
xorq $0x1, %r11
cmpb $0x0, (%rbx,%r11)
je 0xddcb0
cmpb $0x0, (%rbx,%r10)
je 0xddcb0
shrl %r10d
movq 0x70(%rsp), %r11
cmpb $0x0, (%r11,%r10)
jne 0xddcb0
cmpq %r9, %rdx
cmovbeq %r9, %rdx
addq $0x18, %r8
cmpq %r8, %rsi
jne 0xddc77
jmp 0xddcbd
xorl %edx, %edx
addq %rcx, %rdx
movq 0x8(%rsp), %rcx
movq 0x20(%rcx), %rcx
movl (%rcx,%rax,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %rdx
jae 0xddce6
movq 0x10(%rsp), %rcx
subq %rdx, %rcx
addq %rax, %rcx
movq %rcx, 0x10(%rsp)
movl 0x4(%rsp), %r8d
movl (%rsp), %esi
movq 0x10(%rsp), %rax
cmpq 0x78(%rsp), %rax
ja 0xddd06
movq (%r15), %rcx
testq %rcx, %rcx
jne 0xdd8cb
xorl %edi, %edi
movq 0x10(%rsp), %rax
cmpq 0x78(%rsp), %rax
movq $-0x1, %rcx
cmovaq %rcx, %rax
testb %dil, %dil
cmovneq %rcx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq