// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x54(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x1292c0
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x129285
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x68(%rbp)
jmp 0x129290
movl $0x2, %eax
movq %rax, -0x68(%rbp)
jmp 0x129290
movq -0x68(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x14(%rbp), %edx
shrl %edx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0x1292e3
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
je 0x12985c
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rdx
movq (%rdx), %rcx
movq %rcx, %rsi
addq $-0x1, %rsi
movq %rsi, (%rdx)
movl -0x4(%rax,%rcx,4), %eax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
je 0x129320
jmp 0x1292e3
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x1293a1
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x129366
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x70(%rbp)
jmp 0x129371
movl $0x2, %eax
movq %rax, -0x70(%rbp)
jmp 0x129371
movq -0x70(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x34(%rbp), %edx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movl %edx, (%rax,%rcx,4)
movl -0x34(%rbp), %eax
shll %eax
movl %eax, -0x38(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x18(%rbp), %eax
je 0x1295e2
movl -0x38(%rbp), %eax
movl -0x14(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
je 0x1295e2
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x38(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x38(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0x1295d5
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x12945c
jmp 0x1295c7
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x14(%rbp), %eax
je 0x12948d
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl -0x18(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
jne 0x129499
movl $0x1, -0x44(%rbp)
jmp 0x1295d5
movq -0x30(%rbp), %rax
movq -0x60(%rbp), %rcx
movl -0x40(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
cmpb $0x0, (%rax,%rcx)
je 0x12952c
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shrl %eax
movl -0x14(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0x129527
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shrl %eax
movl -0x18(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0x129527
movl -0x50(%rbp), %eax
movq -0x60(%rbp), %rcx
movl -0x40(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x129527
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x50(%rbp)
jmp 0x1295c7
jmp 0x12952e
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x129596
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x12955b
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x78(%rbp)
jmp 0x129566
movl $0x2, %eax
movq %rax, -0x78(%rbp)
jmp 0x129566
movq -0x78(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
shrl %edx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0x1295c7
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x129432
cmpl $0x0, -0x44(%rbp)
je 0x1295e0
jmp 0x12985c
jmp 0x1295e2
movl -0x50(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x34(%rbp), %eax
shll %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x18(%rbp), %eax
je 0x12980a
movl -0x38(%rbp), %eax
movl -0x14(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
je 0x12980a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x38(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x38(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0x129800
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x129687
jmp 0x1297f2
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x14(%rbp), %eax
je 0x1296b8
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl -0x18(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
jne 0x1296c4
movl $0x1, -0x44(%rbp)
jmp 0x129800
movq -0x30(%rbp), %rax
movq -0x60(%rbp), %rcx
movl -0x40(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
cmpb $0x0, (%rax,%rcx)
je 0x129757
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shrl %eax
movl -0x14(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0x129752
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shrl %eax
movl -0x18(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0x129752
movl -0x50(%rbp), %eax
movq -0x60(%rbp), %rcx
movl -0x40(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x129752
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x50(%rbp)
jmp 0x1297f2
jmp 0x129759
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x1297c1
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x129786
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x80(%rbp)
jmp 0x129791
movl $0x2, %eax
movq %rax, -0x80(%rbp)
jmp 0x129791
movq -0x80(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
shrl %edx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0x1297f2
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x12965d
cmpl $0x0, -0x44(%rbp)
je 0x129808
jmp 0x12985c
jmp 0x12980a
movl -0x50(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x48(%rbp), %eax
addl -0x4c(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl -0x34(%rbp), %edx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x12984d
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl -0x48(%rbp), %eax
subl -0x4c(%rbp), %eax
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl 0x10(%rbp), %eax
jbe 0x129857
jmp 0x12985c
jmp 0x1292e3
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x129890
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x40(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movb $0x0, (%rax,%rcx)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x129863
cmpl $0x0, -0x44(%rbp)
jne 0x1298a7
movl -0x54(%rbp), %eax
cmpl 0x10(%rbp), %eax
ja 0x1298a7
movl -0x54(%rbp), %eax
movq %rax, -0x8(%rbp)
jmp 0x1298af
movq $-0x1, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)