// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: usg_tip_detect(usg_t*, unsigned int, int, unsigned char*, asg64_v*, asg64_v*)
int32_t usg_tip_detect(usg_t *g, uint32_t v0, int max_ext, uint8_t *f, asg64_v *z_a, asg64_v *z_b)
{
    uint64_t a_n = z_a->n, b_n = z_b->n, v = v0, kv, nv, i; 
    int32_t n_ext = 0; usg_arc_t *av;

    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 > 1) return 0;
    n_ext += g->a[v>>1].occ; f[v>>1] = 1; kv_push(uint64_t, *z_b, 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, *z_a, av[i].v);
    }   

    while (z_a->n > a_n && n_ext < max_ext) {
        v = z_a->a[--z_a->n]; if(f[v>>1]) 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; f[v>>1] = 1; kv_push(uint64_t, *z_b, 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, *z_a, av[i].v);
        }        
    }

    for (i = b_n; i < z_b->n; i++) f[z_b->a[i]] = 0;
    z_b->n = b_n; z_a->n = a_n;
    return n_ext;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r14
movq %r8, %rbx
movq %rcx, %r15
movl %edx, 0xc(%rsp)
movl %esi, %r12d
movq (%r8), %rax
movq %rax, 0x10(%rsp)
movq (%r9), %rax
movq %rax, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movq (%rdi), %r9
movl %esi, %ebp
shrl %ebp
andl $0x1, %r12d
movq %r12, %rcx
xorq $0x1, %rcx
imulq $0x78, %rbp, %r13
leaq (%r9,%r13), %rdx
leaq (%rcx,%rcx,2), %rsi
movq 0x10(%rdx,%rsi,8), %rcx
testq %rcx, %rcx
je 0x2144d0
movq 0x20(%rdx,%rsi,8), %rdx
addq $0xc, %rdx
movl $0x1, %edi
xorl %esi, %esi
cmpl $0x0, (%rdx)
js 0x2144b2
movl -0x4(%rdx), %r8d
shrl %r8d
cmpb $0x1, (%r15,%r8)
adcq $0x0, %rsi
cmpq %rcx, %rdi
jae 0x2144c4
incq %rdi
addq $0x20, %rdx
cmpq $0x2, %rsi
jb 0x21449d
xorl %eax, %eax
cmpq $0x1, %rsi
ja 0x21477b
movl 0x4(%r9,%r13), %eax
movl %eax, 0x8(%rsp)
movb $0x1, (%r15,%rbp)
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x214511
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
jmp 0x214515
movq 0x10(%r14), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %rbp, (%rax,%rcx,8)
movq 0x18(%rsp), %rax
addq (%rax), %r13
leaq (%r12,%r12,2), %rax
movq 0x10(%r13,%rax,8), %r12
testq %r12, %r12
je 0x2145a3
movq 0x20(%r13,%rax,8), %r13
addq $0x8, %r13
movl $0x2, %ebp
cmpl $0x0, 0x4(%r13)
js 0x21459a
movl (%r13), %ecx
movl %ecx, %eax
shrl %eax
cmpb $0x0, (%r15,%rax)
jne 0x21459a
movq (%rbx), %rdx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rdx
jne 0x21458d
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %rbp, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movl (%r13), %ecx
movq (%rbx), %rdx
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%rbx)
movq %rcx, (%rax,%rdx,8)
addq $0x20, %r13
decq %r12
jne 0x214544
movl 0x8(%rsp), %eax
cmpl 0xc(%rsp), %eax
setl %cl
movq (%rbx), %rax
cmpq 0x10(%rsp), %rax
seta %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x21474a
movq 0x10(%rbx), %rcx
movq %rax, %rdx
decq %rax
movq %rax, (%rbx)
movq -0x8(%rcx,%rdx,8), %r12
movq %r12, %r13
shrq %r13
cmpb $0x0, (%r15,%r13)
jne 0x21464f
movq 0x18(%rsp), %rdx
movq (%rdx), %rdx
andl $0x1, %r12d
movq %r12, %rsi
xorq $0x1, %rsi
imulq $0x78, %r13, %rbp
leaq (%rdx,%rbp), %rdi
leaq (%rsi,%rsi,2), %r8
movq 0x10(%rdi,%r8,8), %rsi
testq %rsi, %rsi
je 0x21465f
movq 0x20(%rdi,%r8,8), %rdi
xorl %r8d, %r8d
movq %r8, %r9
shlq $0x5, %r9
cmpl $0x0, 0xc(%rdi,%r9)
js 0x214640
addq %rdi, %r9
movl 0x8(%r9), %r9d
shrl %r9d
movb (%r15,%r9), %r9b
testb %r9b, %r9b
je 0x21464a
incq %r8
cmpq %rsi, %r8
jb 0x214614
jmp 0x21464a
incq %r8
cmpq %rsi, %r8
jb 0x214614
jmp 0x21465f
testb %r9b, %r9b
jne 0x21465f
cmpq 0x10(%rsp), %rax
ja 0x2145c8
jmp 0x21474a
movl 0x8(%rsp), %eax
addl 0x4(%rdx,%rbp), %eax
movl %eax, 0x8(%rsp)
movb $0x1, (%r15,%r13)
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x2146a4
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %r13, (%rax,%rcx,8)
movq 0x18(%rsp), %rax
addq (%rax), %rbp
leaq (%r12,%r12,2), %rax
movq 0x10(%rbp,%rax,8), %r12
testq %r12, %r12
je 0x214732
movq 0x20(%rbp,%rax,8), %r13
addq $0x8, %r13
cmpl $0x0, 0x4(%r13)
js 0x214729
movl (%r13), %ecx
movl %ecx, %eax
shrl %eax
cmpb $0x0, (%r15,%rax)
jne 0x214729
movq (%rbx), %rdx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rdx
jne 0x21471c
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movl (%r13), %ecx
movq (%rbx), %rdx
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%rbx)
movq %rcx, (%rax,%rdx,8)
addq $0x20, %r13
decq %r12
jne 0x2146ce
movq (%rbx), %rax
cmpq 0x10(%rsp), %rax
jbe 0x21474a
movl 0x8(%rsp), %ecx
cmpl 0xc(%rsp), %ecx
jl 0x2145c4
movq 0x20(%rsp), %rdx
cmpq (%r14), %rdx
jae 0x21476c
movq %rdx, %rax
movq 0x10(%r14), %rcx
movq (%rcx,%rax,8), %rcx
movb $0x0, (%r15,%rcx)
incq %rax
cmpq (%r14), %rax
jb 0x214757
movq %rdx, (%r14)
movq 0x10(%rsp), %rax
movq %rax, (%rbx)
movl 0x8(%rsp), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq