// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %r9, %rbx
movq %r8, %r14
movq %rcx, %r15
xorl %r12d, %r12d
movq %r12, (%rcx)
movq %r12, (%r8)
movq 0x8(%rcx), %rax
cmpq %rax, (%rcx)
movl %edx, 0x10(%rsp)
movl %esi, 0xc(%rsp)
movq %rdi, 0x30(%rsp)
jne 0xdb17e
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x30(%rsp), %rdi
movl 0xc(%rsp), %esi
movl 0x10(%rsp), %edx
movq %rax, 0x10(%r15)
movl 0x80(%rsp), %ebp
movl %esi, %r11d
shrl %r11d
movq (%r15), %rax
movq 0x10(%r15), %rcx
leaq 0x1(%rax), %r8
movq %r8, (%r15)
movl %r11d, (%rcx,%rax,4)
movl %edx, %eax
shrl %eax
movl %eax, 0x2c(%rsp)
movl %r11d, 0x1c(%rsp)
movq (%r15), %rax
decq %rax
cmpq $-0x1, %rax
je 0xdb503
movq 0x10(%r15), %rcx
movq %rax, (%r15)
movl (%rcx,%rax,4), %r13d
decq %rax
cmpb $0x0, (%rbx,%r13)
jne 0xdb1b0
movb $0x1, (%rbx,%r13)
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0xdb214
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movl 0x1c(%rsp), %r11d
movq 0x30(%rsp), %rdi
movl 0xc(%rsp), %esi
movl 0x10(%rsp), %edx
movq %rax, 0x10(%r14)
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %r8
movq %r8, (%r14)
movl %r13d, (%rcx,%rax,4)
movl %r13d, %r8d
addl %r13d, %r8d
cmpl %edx, %r8d
sete %al
movl %r8d, %ecx
xorl %esi, %ecx
cmpl $0x1, %ecx
sete %cl
movl $0x0, 0x14(%rsp)
orb %al, %cl
movl $0x0, 0x8(%rsp)
movq %r12, 0x20(%rsp)
jne 0xdb3ab
movq 0x28(%rdi), %rax
movl %r8d, %ecx
movq (%rax,%rcx,8), %rcx
movq %rcx, %rax
shlq $0x20, %rax
setne %al
je 0xdb399
movl %r8d, 0x18(%rsp)
movq 0x8(%rdi), %r9
movq %rcx, %r8
shrq $0x20, %r8
movl %ecx, %r10d
leaq (%r8,%r8,2), %rcx
leaq (%r9,%rcx,8), %rbp
addq $0x8, %rbp
movq %r10, 0x38(%rsp)
negq %r10
movq %r10, 0x40(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x1, %r12d
movl 0x4(%rbp), %r8d
testl %r8d, %r8d
js 0xdb360
movl (%rbp), %r9d
movl $0x1, %ecx
cmpl %esi, %r9d
je 0xdb386
movl %r9d, %r10d
xorl %edx, %r10d
cmpl $0x1, %r10d
je 0xdb386
shrl %r9d
cmpb $0x0, (%rbx,%r9)
je 0xdb309
movl 0x8(%rsp), %eax
cmpl %r8d, %eax
cmoval %eax, %r8d
cmpl 0x2c(%rsp), %r9d
cmovel %eax, %r8d
cmpl %r11d, %r9d
cmovnel %r8d, %eax
movl %eax, 0x8(%rsp)
jmp 0xdb360
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0xdb349
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x2, %rsi
callq 0x2370
movl 0x1c(%rsp), %r11d
movq 0x30(%rsp), %rdi
movl 0xc(%rsp), %esi
movl 0x10(%rsp), %edx
movq %rax, 0x10(%r15)
movl (%rbp), %eax
shrl %eax
movq (%r15), %rcx
movq 0x10(%r15), %r9
leaq 0x1(%rcx), %r8
movq %r8, (%r15)
movl %eax, (%r9,%rcx,4)
cmpq 0x38(%rsp), %r12
setb %al
addq $0x18, %rbp
movq 0x40(%rsp), %rcx
addq %r12, %rcx
incq %rcx
incq %r12
cmpq $0x1, %rcx
jne 0xdb2af
xorl %ecx, %ecx
movq 0x20(%rsp), %r12
movl 0x80(%rsp), %ebp
movl 0x18(%rsp), %r8d
jmp 0xdb3a3
movl $0x0, 0x8(%rsp)
xorl %ecx, %ecx
testb $0x1, %al
jne 0xdb505
orl $0x1, %r8d
cmpl %edx, %r8d
sete %al
movl %r8d, %ecx
xorl %esi, %ecx
cmpl $0x1, %ecx
sete %cl
orb %al, %cl
jne 0xdb4c8
movq 0x28(%rdi), %rax
movl %r8d, %ecx
movq (%rax,%rcx,8), %rax
testl %eax, %eax
je 0xdb4c8
movq 0x8(%rdi), %rcx
movq %rax, %r8
shrq $0x20, %r8
movl %eax, %eax
leaq (%r8,%r8,2), %r8
leaq (%rcx,%r8,8), %r12
addq $0x8, %r12
shlq $0x3, %rax
leaq (%rax,%rax,2), %r10
xorl %ebp, %ebp
movl $0x0, 0x14(%rsp)
movq %r10, 0x38(%rsp)
movl 0x4(%r12,%rbp), %eax
testl %eax, %eax
js 0xdb4bb
movl (%r12,%rbp), %r9d
movb $0x1, %cl
cmpl %esi, %r9d
je 0xdb54c
movl %r9d, %r8d
xorl %edx, %r8d
cmpl $0x1, %r8d
je 0xdb54c
shrl %r9d
cmpb $0x0, (%rbx,%r9)
je 0xdb45e
movl 0x14(%rsp), %ecx
cmpl %eax, %ecx
cmoval %ecx, %eax
cmpl 0x2c(%rsp), %r9d
cmovel %ecx, %eax
cmpl %r11d, %r9d
cmovnel %eax, %ecx
movl %ecx, 0x14(%rsp)
jmp 0xdb4bb
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0xdb4a3
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x38(%rsp), %r10
movl 0x1c(%rsp), %r11d
movq 0x30(%rsp), %rdi
movl 0xc(%rsp), %esi
movl 0x10(%rsp), %edx
movq %rax, 0x10(%r15)
movl (%r12,%rbp), %eax
shrl %eax
movq (%r15), %rcx
movq 0x10(%r15), %r9
leaq 0x1(%rcx), %r8
movq %r8, (%r15)
movl %eax, (%r9,%rcx,4)
addq $0x18, %rbp
cmpq %rbp, %r10
jne 0xdb40b
movl 0x14(%rsp), %r8d
addl 0x8(%rsp), %r8d
movq 0x20(%rdi), %rax
movl (%rax,%r13,8), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
xorl %ecx, %ecx
subl %r8d, %eax
cmovbl %ecx, %eax
movq 0x20(%rsp), %r12
addl %eax, %r12d
movl 0x80(%rsp), %ebp
cmpl %ebp, %r12d
jbe 0xdb1aa
jmp 0xdb505
xorl %ecx, %ecx
cmpq $0x0, (%r14)
je 0xdb526
movl $0x1, %eax
xorl %edx, %edx
movq 0x10(%r14), %rsi
movl (%rsi,%rdx,4), %edx
movb $0x0, (%rbx,%rdx)
movl %eax, %edx
incl %eax
cmpq %rdx, (%r14)
ja 0xdb512
cmpl %ebp, %r12d
movl %r12d, %eax
movq $-0x1, %rdx
cmovaq %rdx, %rax
testb %cl, %cl
cmovneq %rdx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %r12
movl 0x80(%rsp), %ebp
jmp 0xdb505