// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: u2g_hybrid_extend(usg_t*, unsigned long*, asg64_v*, asg64_v*)
void u2g_hybrid_extend(usg_t *ng, uint64_t* i_max_dist, asg64_v *in, asg64_v *ib)
{
    uint32_t v, w, n_vtx = ng->n<<1, n_arc, nv, i, mm;
    uint64_t n_pop = 0, max_dist; usg_arc_t *av = NULL; 
    asg64_v tx = {0,0,0}, tb = {0,0,0}, *ob = NULL, *ub = NULL; 
    ob = (in?(in):(&tx)); ub = (ib?(ib):(&tb)); ob->n = ub->n = 0;
    buf_t b; memset(&b, 0, sizeof(buf_t)); CALLOC(b.a, n_vtx);
    if(i_max_dist) max_dist = (*i_max_dist);
    else max_dist = usg_max_bub(ng, &b, ob);
    uint8_t* bs_flag = NULL; CALLOC(bs_flag, n_vtx);

    if(max_dist > 0) {
        for(v = 0; v < n_vtx; ++v) {
            if(bs_flag[v] != 0) continue;
            nv = usg_arc_n(ng, v); av = usg_arc_a(ng, v);
            if(nv < 2 || ng->a[v>>1].del) continue;
            for (i = n_arc = 0; i < nv; ++i) {
                if (!av[i].del) ++n_arc;
            }
            if (n_arc < 2) continue;
            if(usg_bub_pop1(ng, v, max_dist, &b)) {
                //beg is v, end is b.S.a[0]; note b.b include end, does not include beg
                for (i = 0; i < b.b.n; i++) {
                    if(b.b.a[i]==v || b.b.a[i]==b.S.a[0]) continue;
                    bs_flag[b.b.a[i]] = bs_flag[b.b.a[i]^1] = 1;
                }
                bs_flag[v] = 2; bs_flag[b.S.a[0]^1] = 3;
            }
        }

        for(v = ob->n = 0; v < n_vtx; ++v) {
            if(bs_flag[v] <= 1) continue;
            if(get_usg_unitig(ng, v^1, &w, NULL, NULL, NULL, NULL) != LOOP && bs_flag[w] > 1) {
                mm = get_junction_w(ng, v, w, 0, NULL);
                if(mm == (uint32_t)-1) continue;
                mm = ((uint32_t)-1) - mm;
                kv_push(uint64_t, *ob, ((((uint64_t)mm)<<32)|((uint64_t)v)));
                bs_flag[v] = bs_flag[w] = 0;
            }
        }

        radix_sort_srt64(ob->a, ob->a + ob->n); 
        n_pop += u2g_n_hybrid_thread(ng, 0, ob, ub);
    }

    free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a);
    if(n_pop) usg_cleanup(ng);
    if(!in) free(tx.a); if(!ib) free(tb.a);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rcx, %r14
movq %rdx, %r12
movq %rdi, %r15
movl 0x8(%rdi), %ebp
addl %ebp, %ebp
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rcx
andq $0x0, 0x10(%rcx)
leaq 0xb0(%rsp), %rax
andq $0x0, 0x10(%rax)
testq %rdx, %rdx
movq %rdx, %r13
cmoveq %rcx, %r13
movaps %xmm0, (%rax)
testq %r14, %r14
cmovneq %r14, %rax
movaps %xmm0, (%rcx)
movq %rax, 0x28(%rsp)
andq $0x0, (%rax)
andq $0x0, (%r13)
movq %rsi, %rbx
movups %xmm0, 0xa0(%rsp)
movups %xmm0, 0x90(%rsp)
movups %xmm0, 0x80(%rsp)
movups %xmm0, 0x70(%rsp)
movups %xmm0, 0x60(%rsp)
movups %xmm0, 0x50(%rsp)
pushq $0x1c
popq %rsi
movq %rbp, %rdi
callq 0x2120
movq %rax, 0x48(%rsp)
testq %rbx, %rbx
movq %r13, 0x8(%rsp)
je 0x1c926c
movq (%rbx), %r13
jmp 0x1c927f
leaq 0x48(%rsp), %rsi
movq %r15, %rdi
movq %r13, %rdx
callq 0x1c7c9a
movq %rax, %r13
pushq $0x1
popq %rsi
movq %rbp, %rdi
callq 0x2120
testq %r13, %r13
je 0x1c94aa
movq %rax, %rbx
movq %r12, 0x18(%rsp)
movq %r14, 0x20(%rsp)
xorl %r14d, %r14d
movb $0x1, %r12b
movq 0x8(%rsp), %r9
leaq 0x30(%rsp), %r10
cmpq %rbp, %r14
je 0x1c938b
cmpb $0x0, (%rbx,%r14)
je 0x1c92c5
incq %r14
jmp 0x1c92b0
movl %r14d, %eax
shrl %eax
imulq $0x78, %rax, %rax
addq (%r15), %rax
movl %r14d, %ecx
andl $0x1, %ecx
imulq $0x18, %rcx, %rcx
movq 0x10(%rcx,%rax), %rdx
testl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
je 0x1c92c0
cmpb $0x0, 0x70(%rax)
jne 0x1c92c0
movq 0x20(%rcx,%rax), %rax
movl %edx, %ecx
shlq $0x5, %rcx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdx, %rcx
je 0x1c9313
movl 0xc(%rax,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x20, %rdx
jmp 0x1c92fd
cmpl $0x2, %esi
jb 0x1c92c0
movq %r15, %rdi
movl %r14d, %esi
movq %r13, %rdx
leaq 0x48(%rsp), %rcx
callq 0x1c7de9
leaq 0x30(%rsp), %r10
movq 0x8(%rsp), %r9
testq %rax, %rax
je 0x1c92c0
movq 0x60(%rsp), %rax
movq 0x80(%rsp), %rcx
movq 0x90(%rsp), %rdx
xorl %esi, %esi
movl %esi, %esi
cmpq %rsi, %rcx
jbe 0x1c9377
movl (%rdx,%rsi,4), %edi
cmpq %rdi, %r14
je 0x1c9373
cmpl (%rax), %edi
je 0x1c9373
movq %rdi, %r8
xorq $0x1, %r8
movb %r12b, (%rbx,%r8)
movb %r12b, (%rbx,%rdi)
incl %esi
jmp 0x1c9351
movb $0x2, (%rbx,%r14)
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%rbx,%rax)
jmp 0x1c92c0
andq $0x0, (%r9)
movq 0x18(%rsp), %rax
testq %rax, %rax
cmovneq %rax, %r10
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0x1c9474
cmpb $0x2, (%rbx,%r12)
jb 0x1c946c
movl %r12d, %esi
xorl $0x1, %esi
andq $0x0, (%rsp)
movq %r15, %rdi
leaq 0x14(%rsp), %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %r10, %r14
callq 0x1dee72
movq %r14, %r10
movq 0x8(%rsp), %r9
cmpl $0x7, %eax
je 0x1c946c
movl 0x14(%rsp), %edx
cmpb $0x2, (%rbx,%rdx)
jb 0x1c946c
movq %r15, %rdi
movl %r12d, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1c852c
movq %r14, %r10
movq 0x8(%rsp), %r9
cmpl $-0x1, %eax
je 0x1c946c
movl %eax, %r13d
movq (%r9), %rcx
movq 0x10(%r10), %rax
cmpq 0x8(%r10), %rcx
jne 0x1c944a
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r10)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %r14, %r10
movq 0x8(%rsp), %r9
movq %rax, 0x10(%r14)
movq (%r9), %rcx
notl %r13d
shlq $0x20, %r13
addq %r12, %r13
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
movq %r13, (%rax,%rcx,8)
movl 0x14(%rsp), %eax
xorl %ecx, %ecx
movb %cl, (%rbx,%rax)
movb %cl, (%rbx,%r12)
incq %r12
jmp 0x1c939e
movq 0x10(%r10), %rdi
movq (%r9), %rax
leaq (%rdi,%rax,8), %rsi
movq %r9, %rbx
callq 0x1a3def
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %rdx
movq 0x28(%rsp), %rcx
callq 0x1c8fbc
testl %eax, %eax
sete %bl
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %r12
jmp 0x1c94ac
movb $0x1, %bl
movq 0x48(%rsp), %rdi
callq 0x2220
movq 0x60(%rsp), %rdi
callq 0x2220
movq 0x78(%rsp), %rdi
callq 0x2220
movq 0x90(%rsp), %rdi
callq 0x2220
movq 0xa8(%rsp), %rdi
callq 0x2220
testb %bl, %bl
jne 0x1c94f0
movq %r15, %rdi
callq 0x1c5ce9
testq %r12, %r12
jne 0x1c94ff
movq 0x40(%rsp), %rdi
callq 0x2220
testq %r14, %r14
jne 0x1c9511
movq 0xc0(%rsp), %rdi
callq 0x2220
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq