// File: /workspace/github_cpp_makefile_star2_O2/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, 0x1c(%rsp)
movl %esi, %r13d
movq (%r8), %r11
movq (%r9), %rax
movq %rax, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
movq (%rdi), %rcx
movl %esi, %r12d
shrl %r12d
andl $0x1, %r13d
movq %r13, %rax
xorq $0x1, %rax
imulq $0x78, %r12, %rbp
leaq (%rcx,%rbp), %rdx
imulq $0x18, %rax, %rsi
movq 0x10(%rsi,%rdx), %rax
movq 0x20(%rsi,%rdx), %rdx
addq $0xc, %rdx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rax, %rdi
jae 0x1c68b2
cmpq $0x1, %rsi
ja 0x1c68b2
cmpl $0x0, (%rdx)
js 0x1c68a9
movl -0x4(%rdx), %r8d
shrl %r8d
cmpb $0x1, (%r15,%r8)
adcq $0x0, %rsi
incq %rdi
addq $0x20, %rdx
jmp 0x1c6889
xorl %eax, %eax
cmpq $0x1, %rsi
ja 0x1c6b42
movl 0x4(%rcx,%rbp), %eax
movl %eax, 0x4(%rsp)
movb $0x1, (%r15,%r12)
movq (%r14), %rcx
pushq $0x2
popq %rax
cmpq 0x8(%r14), %rcx
movq %r11, 0x8(%rsp)
jne 0x1c6906
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rsp), %r11
movq %rax, 0x10(%r14)
movq (%r14), %rcx
jmp 0x1c690a
movq 0x10(%r14), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %r12, (%rax,%rcx,8)
movq 0x10(%rsp), %rax
addq (%rax), %rbp
imulq $0x18, %r13, %rax
movq 0x10(%rax,%rbp), %r12
movq 0x20(%rax,%rbp), %r13
addq $0x8, %r13
pushq $0x2
popq %rbp
subq $0x1, %r12
jb 0x1c6b0d
cmpl $0x0, 0x4(%r13)
js 0x1c6997
movl (%r13), %ecx
movl %ecx, %eax
shrl %eax
cmpb $0x0, (%r15,%rax)
jne 0x1c6997
movq (%rbx), %rdx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rdx
jne 0x1c698a
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %rbp, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r11
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
jmp 0x1c6932
movq (%rbx), %rsi
cmpq %r11, %rsi
jbe 0x1c6b1d
movq 0x10(%rbx), %rdx
leaq -0x1(%rsi), %rcx
movq %rcx, (%rbx)
movq -0x8(%rdx,%rsi,8), %r12
movq %r12, %r13
shrq %r13
cmpb $0x0, (%r15,%r13)
movq %rcx, %rsi
jne 0x1c69a0
movq 0x10(%rsp), %rdx
movq (%rdx), %rdx
andl $0x1, %r12d
movq %r12, %rsi
xorq $0x1, %rsi
imulq $0x78, %r13, %rbp
leaq (%rdx,%rbp), %rdi
imulq $0x18, %rsi, %r8
movq 0x10(%r8,%rdi), %rsi
movq 0x20(%r8,%rdi), %rdi
addq $0xc, %rdi
xorl %r9d, %r9d
xorl %r8d, %r8d
testq %r9, %r9
jne 0x1c6a2c
cmpq %rsi, %r8
jae 0x1c6a2c
cmpl $0x0, (%rdi)
js 0x1c6a20
movl -0x4(%rdi), %r10d
shrl %r10d
xorl %r9d, %r9d
cmpb $0x0, (%r15,%r10)
sete %r9b
jmp 0x1c6a23
xorl %r9d, %r9d
incq %r8
addq $0x20, %rdi
jmp 0x1c69fc
movq %rcx, %rsi
testq %r9, %r9
jne 0x1c69a0
movl 0x4(%rdx,%rbp), %ecx
addl %eax, %ecx
movl %ecx, 0x4(%rsp)
movb $0x1, (%r15,%r13)
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x1c6a7e
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r11
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %r13, (%rax,%rcx,8)
movq 0x10(%rsp), %rax
addq (%rax), %rbp
imulq $0x18, %r12, %rax
movq 0x10(%rax,%rbp), %r12
movq 0x20(%rax,%rbp), %r13
addq $0x8, %r13
pushq $0x2
popq %rbp
subq $0x1, %r12
jb 0x1c6b0d
cmpl $0x0, 0x4(%r13)
js 0x1c6b07
movl (%r13), %ecx
movl %ecx, %eax
shrl %eax
cmpb $0x0, (%r15,%rax)
jne 0x1c6b07
movq (%rbx), %rdx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rdx
jne 0x1c6afa
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %rbp, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r11
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
jmp 0x1c6aa6
movl 0x4(%rsp), %ecx
movl %ecx, %eax
cmpl 0x1c(%rsp), %ecx
jl 0x1c699d
movq 0x20(%rsp), %rsi
movq %rsi, %rcx
cmpq (%r14), %rcx
jae 0x1c6b3c
movq 0x10(%r14), %rdx
movq (%rdx,%rcx,8), %rdx
movb $0x0, (%r15,%rdx)
incq %rcx
jmp 0x1c6b25
movq %rsi, (%r14)
movq %r11, (%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq