// File: /workspace/github_cpp_makefile_star2_O2/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
andq $0x0, (%r8)
movl %esi, 0x4(%rsp)
andq $0x0, (%rcx)
movq %rdi, 0x8(%rsp)
xorl %eax, %eax
cmpq %rax, 0x8(%rcx)
movl %edx, (%rsp)
je 0xa001e
movl %edx, %r8d
movq 0x10(%r15), %rax
xorl %edx, %edx
jmp 0xa003d
movq $0x2, 0x8(%r15)
movq 0x10(%r15), %rdi
pushq $0x8
popq %rsi
callq 0x2370
movl (%rsp), %r8d
movq %rax, 0x10(%r15)
movq (%r15), %rdx
movq %r14, 0x18(%rsp)
leaq 0x1(%rdx), %rcx
movq %rcx, (%r15)
movl 0x4(%rsp), %edi
movl %edi, (%rax,%rdx,4)
movl %edi, %eax
xorl $0x1, %eax
movq %rax, 0x28(%rsp)
movl %r8d, %eax
movq %rax, 0x30(%rsp)
movq $0x0, 0x10(%rsp)
decq %rcx
cmpq $-0x1, %rcx
je 0xa042a
movq 0x10(%r15), %rax
movq %rcx, (%r15)
movl (%rax,%rcx,4), %r14d
movb (%rbx,%r14), %al
testb %al, %al
je 0xa009b
movq %r14, %rdx
xorq $0x1, %rdx
decq %rcx
cmpb $0x0, (%rbx,%rdx)
jne 0xa006e
cmpl %r8d, %r14d
je 0xa01ba
testb %al, %al
jne 0xa01ba
movl %r14d, %eax
xorl %edi, %eax
cmpl $0x1, %eax
je 0xa01ba
movb $0x1, (%rbx,%r14)
movq 0x18(%rsp), %r12
movq (%r12), %rcx
movq 0x10(%r12), %rax
cmpq 0x8(%r12), %rcx
jne 0xa0104
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r12)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x4(%rsp), %edi
movl (%rsp), %r8d
movq %rax, 0x10(%r12)
movq (%r12), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r12)
movl %r14d, (%rax,%rcx,4)
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq (%rcx,%r14,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
imulq $0x18, %rdx, %r13
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %rbp
addq $0x8, %rbp
xorl %r12d, %r12d
cmpq %r12, %r13
je 0xa01ba
cmpl $0x0, 0x4(%rbp,%r12)
js 0xa01b4
movl (%rbp,%r12), %ecx
movb $0x1, %sil
cmpl %edi, %ecx
je 0xa042c
movl %ecx, %edx
xorl %r8d, %edx
cmpl $0x1, %edx
je 0xa042c
cmpb $0x0, (%rbx,%rcx)
jne 0xa01b4
movq (%r15), %rdx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rdx
jne 0xa01aa
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x4(%rsp), %edi
movl (%rsp), %r8d
movq %rax, 0x10(%r15)
movl (%rbp,%r12), %ecx
movq (%r15), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r15)
movl %ecx, (%rax,%rdx,4)
addq $0x18, %r12
jmp 0xa013a
movq %r14, %rbp
xorq $0x1, %rbp
movb (%rbx,%rbp), %al
cmpq 0x30(%rsp), %rbp
je 0xa02ef
cmpl 0x28(%rsp), %ebp
je 0xa02ef
testb %al, %al
movq 0x18(%rsp), %r12
movq 0x70(%rsp), %r13
jne 0xa02f4
movb $0x1, (%rbx,%rbp)
movq (%r12), %rcx
movq 0x10(%r12), %rax
cmpq 0x8(%r12), %rcx
jne 0xa022f
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r12)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x4(%rsp), %edi
movl (%rsp), %r8d
movq %rax, 0x10(%r12)
movq (%r12), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r12)
movl %ebp, (%rax,%rcx,4)
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq %rbp, 0x20(%rsp)
movq (%rcx,%rbp,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
imulq $0x18, %rdx, %rbp
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %r12
addq $0x8, %r12
xorl %r13d, %r13d
cmpq %r13, %rbp
je 0xa02e7
cmpl $0x0, 0x4(%r12,%r13)
js 0xa02e1
movl (%r12,%r13), %ecx
movb $0x1, %sil
cmpl %edi, %ecx
je 0xa042c
movl %ecx, %edx
xorl %r8d, %edx
cmpl $0x1, %edx
je 0xa042c
cmpb $0x0, (%rbx,%rcx)
jne 0xa02e1
movq (%r15), %rdx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rdx
jne 0xa02d7
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x4(%rsp), %edi
movl (%rsp), %r8d
movq %rax, 0x10(%r15)
movl (%r12,%r13), %ecx
movq (%r15), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r15)
movl %ecx, (%rax,%rdx,4)
addq $0x18, %r13
jmp 0xa0269
movq 0x20(%rsp), %rbp
movb (%rbx,%rbp), %al
movq 0x70(%rsp), %r13
testb %al, %al
je 0xa0416
cmpb $0x0, (%rbx,%r14)
je 0xa0416
movl %r14d, %eax
shrl %eax
cmpb $0x0, (%r13,%rax)
jne 0xa0416
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rdx
movq 0x28(%rcx), %rsi
movq (%rsi,%rbp,8), %rcx
movl %ecx, %edi
shrq $0x20, %rcx
imulq $0x18, %rdi, %rdi
imulq $0x18, %rcx, %rcx
leaq (%rdx,%rcx), %r8
addq $0xc, %r8
xorl %r9d, %r9d
xorl %ecx, %ecx
cmpq %r9, %rdi
je 0xa0384
movslq (%r8,%r9), %r10
testq %r10, %r10
js 0xa037e
movl -0x4(%r8,%r9), %r11d
movq %r11, %r12
xorq $0x1, %r12
cmpb $0x0, (%rbx,%r12)
je 0xa037e
cmpb $0x0, (%rbx,%r11)
je 0xa037e
shrl %r11d
cmpb $0x0, (%r13,%r11)
jne 0xa037e
cmpq %r10, %rcx
cmovbeq %r10, %rcx
addq $0x18, %r9
jmp 0xa0344
movq (%rsi,%r14,8), %rsi
movl %esi, %edi
movq %rsi, %r8
shrq $0x20, %r8
imulq $0x18, %rdi, %rsi
imulq $0x18, %r8, %rdi
addq %rdx, %rdi
addq $0xc, %rdi
xorl %r8d, %r8d
xorl %edx, %edx
cmpq %r8, %rsi
je 0xa03e5
movslq (%rdi,%r8), %r9
testq %r9, %r9
js 0xa03df
movl -0x4(%rdi,%r8), %r10d
movq %r10, %r11
xorq $0x1, %r11
cmpb $0x0, (%rbx,%r11)
je 0xa03df
cmpb $0x0, (%rbx,%r10)
je 0xa03df
shrl %r10d
cmpb $0x0, (%r13,%r10)
jne 0xa03df
cmpq %r9, %rdx
cmovbeq %r9, %rdx
addq $0x18, %r8
jmp 0xa03a5
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 0xa040e
movq 0x10(%rsp), %rcx
subq %rdx, %rcx
addq %rax, %rcx
movq %rcx, 0x10(%rsp)
movl (%rsp), %r8d
movl 0x4(%rsp), %edi
movq 0x10(%rsp), %rax
cmpq 0x78(%rsp), %rax
ja 0xa042a
movq (%r15), %rcx
jmp 0xa006b
xorl %esi, %esi
movq 0x10(%rsp), %rax
cmpq 0x78(%rsp), %rax
pushq $-0x1
popq %rcx
cmovaq %rcx, %rax
testb %sil, %sil
cmovneq %rcx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq