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

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


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

        if(o0 + o1 < g->seq[z].len) tot += g->seq[z].len - o0 - o1;
        if(tot > max_path) break;
    }

    for (k = 0; k < b2->n; k++) f[b2->a[k]] = 0;

    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 %r8, %r14
movq %rcx, %r15
movl %esi, %r11d
movq %rdi, 0x18(%rsp)
xorl %eax, %eax
movq %rax, (%rcx)
movq %rax, (%r8)
movq (%rcx), %rsi
cmpq 0x8(%rcx), %rsi
movq %r9, 0x20(%rsp)
movl %edx, 0xc(%rsp)
movl %r11d, 0x8(%rsp)
jne 0xdc577
leaq (%rsi,%rsi), %rax
testq %rsi, %rsi
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x2, %rsi
callq 0x2370
movl 0x8(%rsp), %r11d
movl 0xc(%rsp), %edx
movq 0x20(%rsp), %r9
movq %rax, 0x10(%r15)
movq (%r15), %rsi
jmp 0xdc57b
movq 0x10(%r15), %rax
movl 0x70(%rsp), %edi
movl %r11d, %ebx
shrl %ebx
xorl %ecx, %ecx
movq %rsi, %r10
movl $0x0, %r8d
incq %r10
movq %r10, (%r15)
movl %ebx, 0x10(%rsp)
movl %ebx, (%rax,%rsi,4)
je 0xdc8c2
movl %edx, %eax
shrl %eax
movl %eax, 0x4(%rsp)
xorl %r8d, %r8d
movq %r10, %rax
movq 0x10(%r15), %rcx
decq %r10
movq %r10, (%r15)
movl -0x4(%rcx,%rax,4), %ebx
cmpb $0x0, (%r9,%rbx)
jne 0xdc8ad
movb $0x1, (%r9,%rbx)
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
movl %r8d, (%rsp)
jne 0xdc614
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x8(%rsp), %r11d
movl 0xc(%rsp), %edx
movq 0x20(%rsp), %r9
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movl 0x10(%rsp), %r8d
movl 0x4(%rsp), %r10d
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movl %ebx, (%rax,%rcx,4)
movq %rbx, 0x28(%rsp)
movl %ebx, %r13d
addl %ebx, %r13d
cmpl %edx, %r13d
sete %al
movl %r13d, %ecx
xorl %r11d, %ecx
cmpl $0x1, %ecx
sete %cl
movl $0x0, 0x14(%rsp)
orb %al, %cl
movl $0x0, %r12d
jne 0xdc756
movq 0x18(%rsp), %rax
movq 0x28(%rax), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %rax
testl %eax, %eax
je 0xdc756
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq %rax, %rsi
shrq $0x20, %rsi
movl %eax, %eax
leaq (%rsi,%rsi,2), %rsi
leaq (%rcx,%rsi,8), %rbp
addq $0x8, %rbp
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x30(%rsp)
xorl %ebx, %ebx
xorl %r12d, %r12d
movl 0x4(%rbp,%rbx), %eax
testl %eax, %eax
js 0xdc747
movl (%rbp,%rbx), %edi
movb $0x1, %cl
cmpl %r11d, %edi
je 0xdc8ba
movl %edi, %esi
xorl %edx, %esi
cmpl $0x1, %esi
je 0xdc8ba
shrl %edi
cmpb $0x0, (%r9,%rdi)
je 0xdc6eb
cmpl %eax, %r12d
cmoval %r12d, %eax
cmpl %r10d, %edi
cmovel %r12d, %eax
cmpl %r8d, %edi
cmovnel %eax, %r12d
jmp 0xdc747
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0xdc73d
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x4(%rsp), %r10d
movl 0x10(%rsp), %r8d
movl 0x8(%rsp), %r11d
movl 0xc(%rsp), %edx
movq 0x20(%rsp), %r9
movq %rax, 0x10(%r15)
movl (%rbp,%rbx), %edi
movq (%r15), %rcx
shrl %edi
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movl %edi, (%rax,%rcx,4)
addq $0x18, %rbx
cmpq %rbx, 0x30(%rsp)
jne 0xdc6a3
orl $0x1, %r13d
cmpl %edx, %r13d
sete %al
movl %r13d, %ecx
xorl %r11d, %ecx
cmpl $0x1, %ecx
sete %cl
orb %al, %cl
jne 0xdc874
movq 0x18(%rsp), %rax
movq 0x28(%rax), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %rax
testl %eax, %eax
je 0xdc874
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq %rax, %rsi
shrq $0x20, %rsi
movl %eax, %eax
leaq (%rsi,%rsi,2), %rsi
leaq (%rcx,%rsi,8), %r13
addq $0x8, %r13
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbx
xorl %ebp, %ebp
movl $0x0, 0x14(%rsp)
movl 0x4(%r13,%rbp), %eax
testl %eax, %eax
js 0xdc867
movl (%r13,%rbp), %edi
movb $0x1, %cl
cmpl %r11d, %edi
je 0xdc8ba
movl %edi, %esi
xorl %edx, %esi
cmpl $0x1, %esi
je 0xdc8ba
shrl %edi
cmpb $0x0, (%r9,%rdi)
je 0xdc80a
movl 0x14(%rsp), %ecx
cmpl %eax, %ecx
cmoval %ecx, %eax
cmpl %r10d, %edi
cmovel %ecx, %eax
cmpl %r8d, %edi
cmovnel %eax, %ecx
movl %ecx, 0x14(%rsp)
jmp 0xdc867
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0xdc85d
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x4(%rsp), %r10d
movl 0x10(%rsp), %r8d
movl 0x8(%rsp), %r11d
movl 0xc(%rsp), %edx
movq 0x20(%rsp), %r9
movq %rax, 0x10(%r15)
movl (%r13,%rbp), %edi
movq (%r15), %rcx
shrl %edi
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movl %edi, (%rax,%rcx,4)
addq $0x18, %rbp
cmpq %rbp, %rbx
jne 0xdc7bc
movl 0x14(%rsp), %esi
addl %r12d, %esi
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x28(%rsp), %rcx
movl (%rax,%rcx,8), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
xorl %ecx, %ecx
subl %esi, %eax
cmovbl %ecx, %eax
movl (%rsp), %r8d
addl %eax, %r8d
movl 0x70(%rsp), %edi
cmpl %edi, %r8d
ja 0xdc8c2
movq (%r15), %r10
testq %r10, %r10
jne 0xdc5ad
xorl %ecx, %ecx
jmp 0xdc8c2
movl 0x70(%rsp), %edi
movl (%rsp), %r8d
cmpq $0x0, (%r14)
je 0xdc8e4
movl $0x1, %eax
xorl %edx, %edx
movq 0x10(%r14), %rsi
movl (%rsi,%rdx,4), %edx
movb $0x0, (%r9,%rdx)
movl %eax, %edx
incl %eax
cmpq %rdx, (%r14)
ja 0xdc8cf
cmpl %edi, %r8d
movl %r8d, %eax
movq $-0x1, %rdx
cmovaq %rdx, %rax
testb %cl, %cl
cmovneq %rdx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq