// File: /workspace/github_cpp_makefile_star2_O2/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 %r9, %rbx
movq %r8, %r14
movq %rcx, %r15
andq $0x0, (%rcx)
movq %rdi, 0x28(%rsp)
andq $0x0, (%r8)
movq (%rcx), %rdi
pushq $0x2
popq %rax
cmpq 0x8(%rcx), %rdi
movl %edx, 0x8(%rsp)
movl %esi, 0x4(%rsp)
jne 0x9ee84
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x2, %rsi
callq 0x2370
movl 0x4(%rsp), %r8d
movl 0x8(%rsp), %edx
movq %rax, 0x10(%r15)
movq (%r15), %rdi
jmp 0x9ee8b
movl %esi, %r8d
movq 0x10(%r15), %rax
movl 0x70(%rsp), %r12d
movl %r8d, %r9d
shrl %r9d
leaq 0x1(%rdi), %rcx
movq %rcx, (%r15)
movl %r9d, (%rax,%rdi,4)
movl %edx, %r10d
shrl %r10d
movl $0x0, (%rsp)
movl %r9d, 0x14(%rsp)
movl %r10d, 0x10(%rsp)
xorl %r13d, %r13d
decq %rcx
cmpq $-0x1, %rcx
je 0x9f1b0
movq 0x10(%r15), %rax
movq %rcx, (%r15)
movl (%rax,%rcx,4), %ebp
decq %rcx
cmpb $0x0, (%rbx,%rbp)
jne 0x9eebe
movb $0x1, (%rbx,%rbp)
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x9ef24
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x10(%rsp), %r10d
movl 0x14(%rsp), %r9d
movl 0x4(%rsp), %r8d
movl 0x8(%rsp), %edx
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movl %ebp, (%rax,%rcx,4)
movl %ebp, %esi
addl %ebp, %esi
cmpl %edx, %esi
sete %al
movl %esi, %ecx
xorl %r8d, %ecx
cmpl $0x1, %ecx
sete %cl
xorl %r11d, %r11d
orb %al, %cl
movl $0x0, 0xc(%rsp)
jne 0x9f062
movq %rbx, 0x18(%rsp)
movq 0x28(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movl %esi, 0x20(%rsp)
movl %esi, %esi
movq (%rcx,%rsi,8), %rcx
movl %ecx, %esi
shrq $0x20, %rcx
imulq $0x18, %rsi, %rsi
movq %rsi, 0x30(%rsp)
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %rbx
addq $0x8, %rbx
xorl %r12d, %r12d
movl $0x0, 0xc(%rsp)
cmpq %r12, 0x30(%rsp)
je 0x9f054
movl 0x4(%rbx,%r12), %eax
testl %eax, %eax
js 0x9f04b
movl (%rbx,%r12), %edi
movb $0x1, %cl
cmpl %r8d, %edi
je 0x9f1b7
movl %edi, %esi
xorl %edx, %esi
cmpl $0x1, %esi
je 0x9f1b7
shrl %edi
movq 0x18(%rsp), %rcx
cmpb $0x0, (%rcx,%rdi)
je 0x9eff3
movl 0xc(%rsp), %ecx
cmpl %eax, %ecx
cmoval %ecx, %eax
cmpl %r10d, %edi
cmovel %ecx, %eax
cmpl %r9d, %edi
cmovnel %eax, %ecx
movl %ecx, 0xc(%rsp)
jmp 0x9f04b
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0x9f041
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
xorl %r11d, %r11d
movl 0x10(%rsp), %r10d
movl 0x14(%rsp), %r9d
movl 0x4(%rsp), %r8d
movl 0x8(%rsp), %edx
movq %rax, 0x10(%r15)
movl (%rbx,%r12), %edi
movq (%r15), %rcx
shrl %edi
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movl %edi, (%rax,%rcx,4)
addq $0x18, %r12
jmp 0x9ef97
movl 0x70(%rsp), %r12d
movl 0x20(%rsp), %esi
movq 0x18(%rsp), %rbx
orl $0x1, %esi
cmpl %edx, %esi
sete %al
movl %esi, %ecx
xorl %r8d, %ecx
cmpl $0x1, %ecx
sete %cl
orb %al, %cl
jne 0x9f17b
movq %r14, 0x20(%rsp)
movq 0x28(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movl %esi, %esi
movq (%rcx,%rsi,8), %rcx
movl %ecx, %esi
shrq $0x20, %rcx
imulq $0x18, %rsi, %rsi
movq %rsi, 0x30(%rsp)
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %r14
addq $0x8, %r14
xorl %r12d, %r12d
xorl %r11d, %r11d
cmpq %r12, 0x30(%rsp)
je 0x9f171
movl 0x4(%r14,%r12), %eax
testl %eax, %eax
js 0x9f168
movl (%r14,%r12), %edi
movb $0x1, %cl
cmpl %r8d, %edi
je 0x9f1c6
movl %edi, %esi
xorl %edx, %esi
cmpl $0x1, %esi
je 0x9f1c6
shrl %edi
cmpb $0x0, (%rbx,%rdi)
je 0x9f109
cmpl %eax, %r11d
cmoval %r11d, %eax
cmpl %r10d, %edi
cmovel %r11d, %eax
cmpl %r9d, %edi
cmovnel %eax, %r11d
jmp 0x9f168
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0x9f15e
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x2, %rsi
movq %rax, %rdi
movl %r11d, 0x18(%rsp)
callq 0x2370
movl 0x18(%rsp), %r11d
movl 0x10(%rsp), %r10d
movl 0x14(%rsp), %r9d
movl 0x4(%rsp), %r8d
movl 0x8(%rsp), %edx
movq %rax, 0x10(%r15)
movl (%r14,%r12), %edi
movq (%r15), %rcx
shrl %edi
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movl %edi, (%rax,%rcx,4)
addq $0x18, %r12
jmp 0x9f0b6
movl 0x70(%rsp), %r12d
movq 0x20(%rsp), %r14
addl 0xc(%rsp), %r11d
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rax
movl (%rax,%rbp,8), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
xorl %ecx, %ecx
subl %r11d, %eax
cmovbl %ecx, %eax
movl (%rsp), %esi
addl %eax, %esi
cmpl %r12d, %esi
ja 0x9f1e4
movl %esi, (%rsp)
movq (%r15), %rcx
jmp 0x9eebb
xorl %ecx, %ecx
movl (%rsp), %esi
jmp 0x9f1e4
movl 0x70(%rsp), %r12d
movl (%rsp), %esi
movq 0x18(%rsp), %rbx
jmp 0x9f1e4
movl 0x70(%rsp), %r12d
movl (%rsp), %esi
movq 0x20(%rsp), %r14
jmp 0x9f1e4
movq 0x10(%r14), %rdx
movl (%rdx,%rax,4), %edx
movb $0x0, (%rbx,%rdx)
leal 0x1(%rax), %r13d
movl %r13d, %eax
cmpq %rax, (%r14)
ja 0x9f1d5
cmpl %r12d, %esi
movl %esi, %eax
pushq $-0x1
popq %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