// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: get_ext_tip(usg_t*, unsigned long*, unsigned long, unsigned long, asg64_v*, asg64_v*, unsigned char*, unsigned long)
uint64_t get_ext_tip(usg_t *g, uint64_t *seq, uint64_t s, uint64_t e, asg64_v *buf, asg64_v *set, uint8_t *f, uint64_t max_ext)
{
    uint64_t bn = buf->n, sn = set->n, v, k, nv, z, n_ext = 0, i, kv; usg_arc_t *av;
    for (k = s + 1; k < e; k++) {///note: here is [s, e]
        v = seq[k]; av = usg_arc_a(g, v); nv = usg_arc_n(g, v);
        for (z = 0; z < nv; z++) {
            if(av[z].del || f[av[z].v^1]) continue;
            kv_push(uint64_t, *buf, av[z].v);
        }

        v = seq[k]^1; av = usg_arc_a(g, v); nv = usg_arc_n(g, v);
        for (z = 0; z < nv; z++) {
            if(av[z].del || f[av[z].v^1]) continue;
            kv_push(uint64_t, *buf, av[z].v);
        }
    }

    v = seq[s]; av = usg_arc_a(g, v); nv = usg_arc_n(g, v);
    for (z = 0; z < nv; z++) {
        if(av[z].del || f[av[z].v^1]) continue;
        kv_push(uint64_t, *buf, av[z].v);
    }

    v = seq[e]^1; av = usg_arc_a(g, v); nv = usg_arc_n(g, v);
    for (z = 0; z < nv; z++) {
        if(av[z].del || f[av[z].v^1]) continue;
        kv_push(uint64_t, *buf, av[z].v);
    }


    while (buf->n > bn && n_ext < max_ext) {
        v = buf->a[--buf->n]; if(f[v]) continue;
        av = usg_arc_a(g, v^1); nv = usg_arc_n(g, v^1);
        for (i = kv = 0; i < nv && kv < 1; i++) {
            if (av[i].del || f[av[i].v^1]) continue;
            kv++; 
        }
        if(kv > 0) continue;
        n_ext += g->a[v>>1].occ; 
        if(!(f[v])) {
            f[v] = 1; kv_push(uint64_t, *set, v);
        }
        if(!(f[v^1])) {
            f[v^1] = 1; kv_push(uint64_t, *set, (v^1));
        }

        av = usg_arc_a(g, v); nv = usg_arc_n(g, v);
        for (i = 0; i < nv; i++) {
            if (av[i].del || f[av[i].v^1]) continue;
            kv_push(uint64_t, *buf, av[i].v);
        }    
    }
    buf->n = bn;
    for (k = sn; k < set->n; k++) f[set->a[k]] = 0;
    return n_ext;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %r14
movq %rsi, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
movq 0x70(%rsp), %rbp
movq (%r8), %rax
movq %rax, 0x28(%rsp)
movq %r9, 0x20(%rsp)
movq (%r9), %rax
movq %rax, 0x30(%rsp)
movq %rdx, 0x18(%rsp)
leaq 0x1(%rdx), %r12
movq %rcx, %r15
cmpq %rcx, %r12
jae 0x21b12a
movq 0x8(%rsp), %rax
movq (%rax,%r12,8), %rax
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq %rax, %rdx
shrq %rdx
andl $0x1, %eax
imulq $0x78, %rdx, %rsi
addq %rcx, %rsi
leaq (%rax,%rax,2), %rdi
movq 0x10(%rsi,%rdi,8), %r13
testq %r13, %r13
je 0x21b09a
movq 0x20(%rsi,%rdi,8), %rbx
addq $0x8, %rbx
cmpl $0x0, 0x4(%rbx)
js 0x21b077
movl (%rbx), %ecx
movq %rcx, %rax
xorq $0x1, %rax
cmpb $0x0, (%rbp,%rax)
jne 0x21b077
movq (%r14), %rdx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rdx
jne 0x21b06a
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%rbx), %ecx
movq (%r14), %rdx
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r14)
movq %rcx, (%rax,%rdx,8)
addq $0x20, %rbx
decq %r13
jne 0x21b01e
movq 0x8(%rsp), %rax
movq (%rax,%r12,8), %rax
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq %rax, %rdx
shrq %rdx
andl $0x1, %eax
xorq $0x1, %rax
imulq $0x78, %rdx, %rdx
addq %rcx, %rdx
leaq (%rax,%rax,2), %rax
movq 0x10(%rdx,%rax,8), %r13
testq %r13, %r13
je 0x21b11e
movq 0x20(%rdx,%rax,8), %rbx
addq $0x8, %rbx
cmpl $0x0, 0x4(%rbx)
js 0x21b115
movl (%rbx), %ecx
movq %rcx, %rax
xorq $0x1, %rax
cmpb $0x0, (%rbp,%rax)
jne 0x21b115
movq (%r14), %rdx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rdx
jne 0x21b108
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%rbx), %ecx
movq (%r14), %rdx
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r14)
movq %rcx, (%rax,%rdx,8)
addq $0x20, %rbx
decq %r13
jne 0x21b0bc
incq %r12
cmpq %r15, %r12
jne 0x21afe2
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rdx, %rcx
shrq %rcx
andl $0x1, %edx
imulq $0x78, %rcx, %rcx
addq %rax, %rcx
leaq (%rdx,%rdx,2), %rdx
movq 0x10(%rcx,%rdx,8), %rbx
testq %rbx, %rbx
je 0x21b1d8
movq 0x20(%rcx,%rdx,8), %r12
addq $0x8, %r12
movl $0x2, %r13d
cmpl $0x0, 0x4(%r12)
js 0x21b1c7
movl (%r12), %ecx
movq %rcx, %rax
xorq $0x1, %rax
cmpb $0x0, (%rbp,%rax)
jne 0x21b1c7
movq (%r14), %rdx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rdx
jne 0x21b1ba
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %r13, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%r12), %ecx
movq (%r14), %rdx
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r14)
movq %rcx, (%rax,%rdx,8)
addq $0x20, %r12
decq %rbx
jne 0x21b16d
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%r15,8), %rcx
movl %ecx, %edx
notl %edx
shrq %rcx
andl $0x1, %edx
imulq $0x78, %rcx, %rcx
addq %rax, %rcx
leaq (%rdx,%rdx,2), %rax
movq 0x10(%rcx,%rax,8), %rbx
testq %rbx, %rbx
movq 0x28(%rsp), %r15
je 0x21b277
movq 0x20(%rcx,%rax,8), %r12
addq $0x8, %r12
movl $0x2, %r13d
cmpl $0x0, 0x4(%r12)
js 0x21b26e
movl (%r12), %ecx
movq %rcx, %rax
xorq $0x1, %rax
cmpb $0x0, (%rbp,%rax)
jne 0x21b26e
movq (%r14), %rdx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rdx
jne 0x21b261
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %r13, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%r12), %ecx
movq (%r14), %rdx
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r14)
movq %rcx, (%rax,%rdx,8)
addq $0x20, %r12
decq %rbx
jne 0x21b214
cmpq $0x0, 0x78(%rsp)
sete %cl
movq (%r14), %r10
cmpq %r15, %r10
setbe %dl
xorl %eax, %eax
orb %cl, %dl
jne 0x21b48a
movq %rax, 0x8(%rsp)
movq 0x10(%r14), %rcx
movq %r10, %rdx
decq %r10
movq %r10, (%r14)
movq -0x8(%rcx,%rdx,8), %r12
cmpb $0x0, (%rbp,%r12)
jne 0x21b329
movq 0x10(%rsp), %rdx
movq (%rdx), %rdx
movq %r12, %r13
xorq $0x1, %r13
movq %r12, %rsi
shrq %rsi
movl %r13d, %r8d
andl $0x1, %r8d
imulq $0x78, %rsi, %rbx
leaq (%rdx,%rbx), %rdi
leaq (%r8,%r8,2), %r8
movq 0x10(%rdi,%r8,8), %rsi
testq %rsi, %rsi
je 0x21b337
movq 0x20(%rdi,%r8,8), %rdi
xorl %r8d, %r8d
movq %r8, %r9
shlq $0x5, %r9
cmpl $0x0, 0xc(%rdi,%r9)
js 0x21b31a
addq %rdi, %r9
movl 0x8(%r9), %r9d
xorq $0x1, %r9
movb (%rbp,%r9), %r9b
incq %r8
cmpq %rsi, %r8
jae 0x21b324
testb %r9b, %r9b
jne 0x21b2ec
jmp 0x21b324
incq %r8
cmpq %rsi, %r8
jb 0x21b2ec
jmp 0x21b337
testb %r9b, %r9b
jne 0x21b337
cmpq %r15, %r10
ja 0x21b29c
jmp 0x21b485
movl 0x4(%rdx,%rbx), %eax
movq %rax, 0x18(%rsp)
movb $0x1, (%rbp,%r12)
movq 0x20(%rsp), %r15
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0x21b37f
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movq %r12, (%rax,%rcx,8)
cmpb $0x0, (%rbp,%r13)
jne 0x21b3dc
movb $0x1, (%rbp,%r13)
movq 0x20(%rsp), %r15
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0x21b3d1
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movq %r13, (%rax,%rcx,8)
movq 0x10(%rsp), %rax
addq (%rax), %rbx
andl $0x1, %r12d
leaq (%r12,%r12,2), %rax
movq 0x10(%rbx,%rax,8), %r12
testq %r12, %r12
movq 0x28(%rsp), %r15
je 0x21b466
movq 0x20(%rbx,%rax,8), %rbx
addq $0x8, %rbx
cmpl $0x0, 0x4(%rbx)
js 0x21b45d
movl (%rbx), %ecx
movq %rcx, %rax
xorq $0x1, %rax
cmpb $0x0, (%rbp,%rax)
jne 0x21b45d
movq (%r14), %rdx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rdx
jne 0x21b450
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%rbx), %ecx
movq (%r14), %rdx
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r14)
movq %rcx, (%rax,%rdx,8)
addq $0x20, %rbx
decq %r12
jne 0x21b404
movq 0x8(%rsp), %rax
addq 0x18(%rsp), %rax
movq (%r14), %r10
cmpq %r15, %r10
jbe 0x21b48a
cmpq 0x78(%rsp), %rax
jb 0x21b293
jmp 0x21b48a
movq 0x8(%rsp), %rax
movq %r15, (%r14)
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %rdx
cmpq (%rcx), %rdx
jae 0x21b4ae
movq 0x10(%rcx), %rsi
movq (%rsi,%rdx,8), %rsi
movb $0x0, (%rbp,%rsi)
incq %rdx
jmp 0x21b497
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq