// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
movq 0x70(%rsp), %rbp
movq (%r8), %rax
movq %rax, 0x28(%rsp)
movq %r9, 0x8(%rsp)
movq (%r9), %rbx
movq %rdx, 0x30(%rsp)
leaq 0x1(%rdx), %r12
movq %rcx, 0x20(%rsp)
cmpq %rcx, %r12
jae 0x20fced
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rcx
movq %rcx, %rax
shrq %rax
imulq $0x78, %rax, %rax
movq 0x18(%rsp), %rdx
addq (%rdx), %rax
andl $0x1, %ecx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %r13
testq %r13, %r13
je 0x20fc48
movq 0x20(%rax,%rcx,8), %r15
addq $0x8, %r15
cmpl $0x0, 0x4(%r15)
js 0x20fc3f
movl (%r15), %eax
xorq $0x1, %rax
cmpb $0x0, (%rbp,%rax)
jne 0x20fc3f
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x20fc2a
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%r15), %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movq %rax, (%rdx,%rcx,8)
addq $0x20, %r15
decq %r13
jne 0x20fbe7
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rax
movl %eax, %ecx
notl %ecx
shrq %rax
imulq $0x78, %rax, %rax
movq 0x18(%rsp), %rdx
addq (%rdx), %rax
andl $0x1, %ecx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %r13
testq %r13, %r13
je 0x20fcdf
movq 0x20(%rax,%rcx,8), %r15
addq $0x8, %r15
cmpl $0x0, 0x4(%r15)
js 0x20fcd6
movl (%r15), %eax
xorq $0x1, %rax
cmpb $0x0, (%rbp,%rax)
jne 0x20fcd6
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x20fcc1
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%r15), %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movq %rax, (%rdx,%rcx,8)
addq $0x20, %r15
decq %r13
jne 0x20fc7e
incq %r12
cmpq 0x20(%rsp), %r12
jne 0x20fbb2
movq 0x10(%rsp), %rax
movq 0x30(%rsp), %rcx
movq (%rax,%rcx,8), %rcx
movq %rcx, %rax
shrq %rax
imulq $0x78, %rax, %rax
movq 0x18(%rsp), %rdx
addq (%rdx), %rax
andl $0x1, %ecx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %r15
testq %r15, %r15
je 0x20fd8c
movq 0x20(%rax,%rcx,8), %r12
addq $0x8, %r12
movl $0x2, %r13d
cmpl $0x0, 0x4(%r12)
js 0x20fd83
movl (%r12), %eax
xorq $0x1, %rax
cmpb $0x0, (%rbp,%rax)
jne 0x20fd83
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x20fd6d
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r13, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%r12), %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movq %rax, (%rdx,%rcx,8)
addq $0x20, %r12
decq %r15
jne 0x20fd2d
movq 0x20(%rsp), %rax
movq 0x10(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movl %eax, %ecx
notl %ecx
shrq %rax
imulq $0x78, %rax, %rax
movq 0x18(%rsp), %rdx
addq (%rdx), %rax
andl $0x1, %ecx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %r15
testq %r15, %r15
je 0x20fe2c
movq 0x20(%rax,%rcx,8), %r12
addq $0x8, %r12
movl $0x2, %r13d
cmpl $0x0, 0x4(%r12)
js 0x20fe23
movl (%r12), %eax
xorq $0x1, %rax
cmpb $0x0, (%rbp,%rax)
jne 0x20fe23
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x20fe0d
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r13, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%r12), %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movq %rax, (%rdx,%rcx,8)
addq $0x20, %r12
decq %r15
jne 0x20fdcd
cmpq $0x0, 0x78(%rsp)
setne %al
sete %dl
movq (%r14), %rcx
cmpq 0x28(%rsp), %rcx
setbe %sil
orb %dl, %sil
jne 0x210063
xorl %esi, %esi
movq %rsi, 0x10(%rsp)
movq 0x10(%r14), %rdx
movq %rcx, %rsi
decq %rcx
movq %rcx, (%r14)
movq -0x8(%rdx,%rsi,8), %r12
cmpb $0x0, (%rbp,%r12)
jne 0x20fee2
movq 0x18(%rsp), %rsi
movq (%rsi), %rsi
movq %r12, %r13
xorq $0x1, %r13
movq %r12, %rdi
shrq %rdi
movl %r13d, %r9d
andl $0x1, %r9d
imulq $0x78, %rdi, %r15
leaq (%rsi,%r15), %r8
leaq (%r9,%r9,2), %r9
movq 0x10(%r8,%r9,8), %rdi
testq %rdi, %rdi
je 0x20fef9
movq 0x20(%r8,%r9,8), %r8
addq $0xc, %r8
movl $0x1, %r9d
cmpl $0x0, (%r8)
js 0x20fec9
movl -0x4(%r8), %r10d
xorq $0x1, %r10
cmpb $0x0, (%rbp,%r10)
setne %r10b
jmp 0x20fecc
movb $0x1, %r10b
cmpq %rdi, %r9
jae 0x20fedd
incq %r9
addq $0x20, %r8
testb %r10b, %r10b
jne 0x20feaf
testb %r10b, %r10b
jne 0x20fef9
cmpq 0x28(%rsp), %rcx
seta %sil
testb %al, %sil
jne 0x20fe58
jmp 0x210067
movl 0x4(%rsi,%r15), %eax
movq %rax, 0x20(%rsp)
movb $0x1, (%rbp,%r12)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rax
cmpq %rax, (%rdi)
jne 0x20ff46
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rdi)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rax
movq 0x10(%rdi), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rdi)
movq %r12, (%rcx,%rax,8)
cmpb $0x0, (%rbp,%r13)
jne 0x20ffb5
movb $0x1, (%rbp,%r13)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rax
cmpq %rax, (%rdi)
jne 0x20ffa3
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rdi)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rax
movq 0x10(%rdi), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rdi)
movq %r13, (%rcx,%rax,8)
movq 0x18(%rsp), %rax
addq (%rax), %r15
andl $0x1, %r12d
leaq (%r12,%r12,2), %rax
movq 0x10(%r15,%rax,8), %r12
testq %r12, %r12
je 0x210039
movq 0x20(%r15,%rax,8), %r15
addq $0x8, %r15
cmpl $0x0, 0x4(%r15)
js 0x210030
movl (%r15), %eax
xorq $0x1, %rax
cmpb $0x0, (%rbp,%rax)
jne 0x210030
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x21001b
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%r15), %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movq %rax, (%rdx,%rcx,8)
addq $0x20, %r15
decq %r12
jne 0x20ffd8
movq 0x10(%rsp), %rsi
addq 0x20(%rsp), %rsi
movq 0x78(%rsp), %rdx
cmpq %rdx, %rsi
setb %al
movq (%r14), %rcx
cmpq 0x28(%rsp), %rcx
jbe 0x21006c
cmpq %rdx, %rsi
jb 0x20fe4f
jmp 0x21006c
xorl %esi, %esi
jmp 0x21006c
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rax
movq %rax, (%r14)
movq 0x8(%rsp), %rcx
cmpq (%rcx), %rbx
jae 0x210090
movq 0x10(%rcx), %rax
movq (%rax,%rbx,8), %rax
movb $0x0, (%rbp,%rax)
incq %rbx
jmp 0x210079
movq %rsi, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq