// File: /workspace/github_cpp_makefile_star2_O1/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, 0x14(%rsp)
movl %esi, %r13d
movq (%r8), %r10
movq (%r9), %rax
movq %rax, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movq (%rdi), %r9
movl %esi, %ebp
shrl %ebp
andl $0x1, %r13d
movq %r13, %rcx
xorq $0x1, %rcx
imulq $0x78, %rbp, %r12
leaq (%r9,%r12), %rdx
leaq (%rcx,%rcx,2), %rsi
movq 0x10(%rdx,%rsi,8), %rcx
testq %rcx, %rcx
je 0x209119
movq 0x20(%rdx,%rsi,8), %rdx
addq $0xc, %rdx
movl $0x1, %edi
xorl %esi, %esi
cmpl $0x0, (%rdx)
js 0x2090fb
movl -0x4(%rdx), %r8d
shrl %r8d
cmpb $0x1, (%r15,%r8)
adcq $0x0, %rsi
cmpq %rcx, %rdi
jae 0x20910d
incq %rdi
addq $0x20, %rdx
cmpq $0x2, %rsi
jb 0x2090e6
xorl %eax, %eax
cmpq $0x1, %rsi
ja 0x2093f6
movl 0x4(%r9,%r12), %eax
movl %eax, 0x4(%rsp)
movb $0x1, (%r15,%rbp)
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
movq %r10, 0x8(%rsp)
jne 0x20915f
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rsp), %r10
movq %rax, 0x10(%r14)
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
movq %rbp, (%rcx,%rax,8)
movq 0x18(%rsp), %rax
addq (%rax), %r12
leaq (,%r13,2), %rax
addq %r13, %rax
movq 0x10(%r12,%rax,8), %r13
testq %r13, %r13
je 0x2091fe
movq 0x20(%r12,%rax,8), %r12
addq $0x8, %r12
movl $0x2, %ebp
cmpl $0x0, 0x4(%r12)
js 0x2091f5
movl (%r12), %eax
shrl %eax
cmpb $0x0, (%r15,%rax)
jne 0x2091f5
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x2091df
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %rbp, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rsp), %r10
movq %rax, 0x10(%rbx)
movl (%r12), %eax
movq (%rbx), %rcx
movq 0x10(%rbx), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rax, (%rdx,%rcx,8)
addq $0x20, %r12
decq %r13
jne 0x20919c
movl 0x4(%rsp), %esi
cmpl 0x14(%rsp), %esi
setl %al
movq (%rbx), %rcx
cmpq %r10, %rcx
seta %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x2093cc
movl %esi, 0x4(%rsp)
movq 0x10(%rbx), %rdx
movq %rcx, %rsi
decq %rcx
movq %rcx, (%rbx)
movq -0x8(%rdx,%rsi,8), %rbp
movq %rbp, %r12
shrq %r12
cmpb $0x0, (%r15,%r12)
jne 0x2092b1
movq 0x18(%rsp), %rsi
movq (%rsi), %rsi
andl $0x1, %ebp
movq %rbp, %rdi
xorq $0x1, %rdi
imulq $0x78, %r12, %r13
leaq (%rsi,%r13), %r8
leaq (%rdi,%rdi,2), %r9
movq 0x10(%r8,%r9,8), %rdi
testq %rdi, %rdi
je 0x2092c6
movq 0x20(%r8,%r9,8), %r8
addq $0xc, %r8
movl $0x1, %r9d
cmpl $0x0, (%r8)
js 0x20928f
movl -0x4(%r8), %r10d
shrl %r10d
cmpb $0x0, (%r15,%r10)
setne %r10b
jmp 0x209292
movb $0x1, %r10b
testb %r10b, %r10b
je 0x2092a7
leaq 0x1(%r9), %r11
addq $0x20, %r8
cmpq %rdi, %r9
movq %r11, %r9
jb 0x209277
testb %r10b, %r10b
movq 0x8(%rsp), %r10
jne 0x2092c6
cmpq %r10, %rcx
seta %sil
testb %al, %sil
jne 0x209225
jmp 0x2093c8
movl 0x4(%rsp), %eax
addl 0x4(%rsi,%r13), %eax
movl %eax, 0x4(%rsp)
movb $0x1, (%r15,%r12)
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x20930b
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 0x8(%rsp), %r10
movq %rax, 0x10(%r14)
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
movq %r12, (%rcx,%rax,8)
movq 0x18(%rsp), %rax
addq (%rax), %r13
leaq (,%rbp,2), %rax
addq %rbp, %rax
movq 0x10(%r13,%rax,8), %r12
testq %r12, %r12
movl 0x14(%rsp), %ebp
je 0x2093ad
movq 0x20(%r13,%rax,8), %r13
addq $0x8, %r13
cmpl $0x0, 0x4(%r13)
js 0x2093a4
movl (%r13), %eax
shrl %eax
cmpb $0x0, (%r15,%rax)
jne 0x2093a4
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x20938e
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rsp), %r10
movq %rax, 0x10(%rbx)
movl (%r13), %eax
movq (%rbx), %rcx
movq 0x10(%rbx), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rax, (%rdx,%rcx,8)
addq $0x20, %r13
decq %r12
jne 0x209347
movl 0x4(%rsp), %esi
cmpl %ebp, %esi
setl %al
movq (%rbx), %rcx
cmpq %r10, %rcx
jbe 0x2093cc
cmpl %ebp, %esi
jl 0x20921d
jmp 0x2093cc
movl 0x4(%rsp), %esi
movq 0x20(%rsp), %rdx
cmpq (%r14), %rdx
jae 0x2093ee
movq %rdx, %rax
movq 0x10(%r14), %rcx
movq (%rcx,%rax,8), %rcx
movb $0x0, (%r15,%rcx)
incq %rax
cmpq (%r14), %rax
jb 0x2093d9
movq %rdx, (%r14)
movq %r10, (%rbx)
movl %esi, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq