// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: ug_ext_strict(asg64_v*, asg64_v*, ul_resolve_t*, usg_t*, unsigned int, unsigned char**, unsigned int**, unsigned long**, asg64_v*, asg64_v*)
uint32_t ug_ext_strict(asg64_v *ob, asg64_v *ub, ul_resolve_t *uidx, usg_t *ng, uint32_t max_ext, 
uint8_t **ff, uint32_t **ng_occ, uint64_t **i_idx, asg64_v *b64, asg64_v *ub64)
{
    // fprintf(stderr, "[M::%s::]\n", __func__);
    uint32_t k, i, n_vtx = ng->n<<1, a_n;
    REALLOC((*ff), n_vtx); REALLOC((*ng_occ), n_vtx); REALLOC((*i_idx), n_vtx);
    u2g_hybrid_aln(uidx, ng, ob, ub); b64->n = ub64->n = 0; 
    uint64_t *int_idx = ub->a, int_idx_n = ub->n, *int_a = ob->a; 
    uint64_t *r_a, r_n, zs, ze, s, e, pi, v, *pz;

    memset((*ng_occ), 0, sizeof((*(*ng_occ)))*n_vtx);  
    for (k = 0; k < int_idx_n; k++) {///scan all integer contigs
        r_a = int_a + (int_idx[k]>>32); r_n = (uint32_t)int_idx[k];
        assert(r_n >= 2);
        for (i = 1; i + 1 < r_n; i++) {
            (*ng_occ)[r_a[i]]++; (*ng_occ)[r_a[i]^1]++;
        }
        (*ng_occ)[r_a[0]]++; (*ng_occ)[r_a[r_n-1]^1]++;
    }

    ma_ug_t *un_g = ma_ug_hybrid_gen(ng); 
    int32_t ui, un; ma_utg_t *u = NULL;
    for (k = 0; k < un_g->u.n; k++) {///all unitigs of raw utg 
        u = &(un_g->u.a[k]); 
        zs = ze = (uint32_t)-1; un = u->n;
        for (ui = 0; ui < un; ui++) {
            if(occ_m((*ng_occ)[(u->a[ui]>>32)^1]) == 1) {
                zs = ui; break;
            }
        }

        for (ui = ((int32_t)un)-1; ui >= 0; ui--) {
            if(occ_m((*ng_occ)[u->a[ui]>>32]) == 1) {
                ze = ui; break;
            }
        }
        if(zs != (uint32_t)-1 && ze != (uint32_t)-1 && zs > ze) continue;
        if(zs != (uint32_t)-1) (*ng_occ)[(u->a[zs]>>32)^1] |= ((uint32_t)0x80000000);
        if(ze != (uint32_t)-1) (*ng_occ)[(u->a[ze]>>32)] |= ((uint32_t)0x80000000);
    }
    ma_ug_destroy(un_g);

        // fprintf(stderr, ">>>>>>[M::%s::] int_idx[0]::%lu, int_idx[1]::%lu\n", __func__, int_idx[0], int_idx[1]);
    // prt_intg_info(int_idx, int_idx_n, int_a, "intg");
    for (i = b64->n = a_n = 0; i < int_idx_n; i++) {///scan all integer contigs
        s = int_idx[i]>>32; e = s + ((uint32_t)int_idx[i]); 
        assert(e > s + 1);//the length is at least 2
        for (k = s, pi = (uint64_t)-1; k < e; k++) {
            v = int_a[k]; 
            if((!((*ng_occ)[v]&((uint32_t)0x80000000)))&&(!((*ng_occ)[v^1]&((uint32_t)0x80000000)))) {
                continue;///it must be a unique node
            }
            if((pi != (uint64_t)-1) && ((*ng_occ)[v^1]&((uint32_t)0x80000000))) {
                pz = (b64->n>0)? &(b64->a[b64->n-1]):(NULL);
                if((!pz) || (((*pz)>>32) != pi)) {//keep the shortest pi<->k 
                    kv_pushp(uint64_t, *b64, &pz); 
                    *pz = pi; (*pz) <<= 32; (*pz) |= k;
                    a_n++;
                }
            }
            if((*ng_occ)[v]&((uint32_t)0x80000000)) pi = k;//keep the shortest pi<->k
        }
    }
    // prt_thread_info(b64.a, a_n+ua_n, NULL, 0, "tt_minus");
    // fprintf(stderr, "**0**[M::%s::] a_n::%lu, ua_n::%lu\n", __func__, a_n, ua_n);
    radix_sort_srt64(b64->a, b64->a + b64->n);///keeps the coordinates within int_a[]

    a_n = ug_ext_0(uidx, ng, ub->a, ub->n, ob->a, max_ext, *ff, *ng_occ, *i_idx, b64, ub64, a_n);
    // if(a_n) usg_cleanup(ng);
    return a_n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %rbx
movl %r8d, 0x4(%rsp)
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r12
movq 0x80(%rsp), %rbp
movq %rcx, 0x8(%rsp)
movl 0x8(%rcx), %r15d
addl %r15d, %r15d
movq (%r9), %rdi
movq %r15, %rsi
callq 0x2370
movq %rbx, 0x28(%rsp)
movq %rax, (%rbx)
movq %rbp, %rbx
movq (%rbp), %rdi
leaq (,%r15,4), %rbp
movq %rbp, %rsi
callq 0x2370
movq %rax, (%rbx)
movq 0x88(%rsp), %rbx
movq (%rbx), %rdi
shlq $0x3, %r15
movq %r15, %rsi
callq 0x2370
movq %rax, (%rbx)
movq %r14, 0x20(%rsp)
movq %r14, %rdi
movq %r12, %rdx
movq %r13, %rcx
callq 0x212940
xorl %ebx, %ebx
movq 0x98(%rsp), %rax
movq %rbx, (%rax)
movq 0x90(%rsp), %rax
movq %rbx, (%rax)
movq (%r13), %rax
movq 0x10(%r13), %rcx
movq %rcx, 0x10(%rsp)
movq %rax, %r13
movq %r12, 0x18(%rsp)
movq 0x10(%r12), %r12
movq 0x80(%rsp), %r14
movq (%r14), %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x20e0
testq %r13, %r13
je 0x2157ef
movq %r14, %r11
leaq 0x8(%r12), %rax
movq 0x10(%rsp), %rcx
movq (%rcx,%rbx,8), %rdx
movl %edx, %ecx
cmpq $0x2, %rcx
jb 0x215ac6
shrq $0x20, %rdx
cmpl $0x2, %ecx
je 0x2157cf
movq (%r11), %rsi
leaq -0x2(%rcx), %rdi
leaq (%rax,%rdx,8), %r8
xorl %r9d, %r9d
movq (%r8,%r9,8), %r10
incl (%rsi,%r10,4)
xorq $0x1, %r10
incl (%rsi,%r10,4)
incq %r9
cmpq %r9, %rdi
jne 0x2157b7
leaq (%r12,%rdx,8), %rdx
movq (%r11), %rsi
movq (%rdx), %rdi
incl (%rsi,%rdi,4)
movq -0x8(%rdx,%rcx,8), %rcx
xorq $0x1, %rcx
incl (%rsi,%rcx,4)
incl %ebx
cmpq %rbx, %r13
ja 0x21578b
movq 0x8(%rsp), %rdi
callq 0x20f140
movq (%rax), %rcx
testq %rcx, %rcx
je 0x2158fc
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x10(%rax), %rsi
xorl %edi, %edi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
leaq (%rdi,%rdi,4), %r9
leaq (%rsi,%r9,8), %r9
movslq 0x10(%r9), %r11
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
testq %r11, %r11
jle 0x215866
movq 0x80(%rsp), %r10
movq (%r10), %rbx
movq 0x18(%r9), %r14
xorl %r10d, %r10d
movl 0x4(%r14,%r10,8), %r15d
shlq $0x2, %r15
xorq $0x4, %r15
movl (%rbx,%r15), %ebp
andl %r8d, %ebp
cmpl $0x1, %ebp
je 0x215866
incq %r10
cmpq %r10, %r11
jne 0x21583f
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl %r11d, %r11d
testl %r11d, %r11d
jle 0x215893
movq 0x80(%rsp), %rbx
movq (%rbx), %rbx
movq 0x18(%r9), %r14
movl -0x4(%r14,%r11,8), %r14d
decq %r11
movl (%rbx,%r14,4), %ebx
andl %r8d, %ebx
cmpl $0x1, %ebx
jne 0x215869
jmp 0x215899
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
cmpq %rdx, %r10
je 0x2158a3
cmpq %r11, %r10
ja 0x2158ed
cmpq %rdx, %r10
je 0x2158cc
movq 0x80(%rsp), %rbx
movq (%rbx), %rbx
movq 0x18(%r9), %r14
movl 0x4(%r14,%r10,8), %r10d
shlq $0x2, %r10
xorq $0x4, %r10
orl $0x80000000, (%rbx,%r10) # imm = 0x80000000
cmpq %rdx, %r11
je 0x2158ed
movq 0x80(%rsp), %r10
movq (%r10), %r10
movq 0x18(%r9), %r9
movl 0x4(%r9,%r11,8), %r9d
orl $0x80000000, (%r10,%r9,4) # imm = 0x80000000
incq %rdi
movl %edi, %r9d
cmpq %r9, %rcx
ja 0x215816
movq %rax, %rdi
callq 0xa0004
movq 0x90(%rsp), %rdi
movq $0x0, (%rdi)
xorl %r14d, %r14d
testq %r13, %r13
je 0x215a62
xorl %r8d, %r8d
movq 0x80(%rsp), %rsi
movq %r13, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movq 0x10(%rsp), %rax
movq %r14, 0x40(%rsp)
movq (%rax,%r14,8), %r14
movl %r14d, %r15d
cmpq $0x1, %r15
jbe 0x215ae5
shrq $0x20, %r14
addq %r14, %r15
leal 0x1(%r14), %r13d
movq $-0x1, %rbp
movq (%r12,%r14,8), %rbx
movq (%rsi), %rax
cmpl $0x0, (%rax,%rbx,4)
js 0x21597f
movq %rbx, %rcx
xorq $0x1, %rcx
cmpl $0x0, (%rax,%rcx,4)
jns 0x215a3a
cmpq $-0x1, %rbp
je 0x215a2f
movq %rbx, %rcx
xorq $0x1, %rcx
cmpl $0x0, (%rax,%rcx,4)
jns 0x215a2f
movq (%rdi), %rax
testq %rax, %rax
je 0x2159b0
movq 0x10(%rdi), %rcx
leaq (%rcx,%rax,8), %rcx
addq $-0x8, %rcx
jmp 0x2159b2
xorl %ecx, %ecx
testq %rcx, %rcx
je 0x2159bf
movl 0x4(%rcx), %ecx
cmpq %rbp, %rcx
je 0x215a2f
movq 0x8(%rdi), %rcx
cmpq %rcx, %rax
jne 0x215a10
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rdi)
movq 0x90(%rsp), %rax
movq 0x10(%rax), %rdi
shlq $0x3, %rsi
movl %r8d, %r12d
callq 0x2370
movl %r12d, %r8d
movq 0x38(%rsp), %r12
movq 0x90(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq %rax, 0x10(%rdi)
movq (%rdi), %rax
movq 0x10(%rdi), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rdi)
movq %rbp, %rdx
shlq $0x20, %rdx
orq %r14, %rdx
movq %rdx, (%rcx,%rax,8)
incl %r8d
movq (%rsi), %rax
cmpl $0x0, (%rax,%rbx,4)
cmovsq %r14, %rbp
movl %r13d, %r14d
incl %r13d
cmpq %r14, %r15
ja 0x215961
movq 0x40(%rsp), %r14
incl %r14d
movq 0x30(%rsp), %r13
cmpq %r14, %r13
ja 0x215934
movl %r8d, %r14d
movq (%rdi), %rax
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1e3b6a
movq 0x18(%rsp), %rax
movq 0x10(%rax), %r8
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %r9d
pushq %r14
pushq 0xa0(%rsp)
pushq %rbx
movq 0xa0(%rsp), %rax
pushq (%rax)
movq 0xa0(%rsp), %rax
pushq (%rax)
movq 0x50(%rsp), %rax
pushq (%rax)
callq 0x214c80
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x271e5(%rip), %rdi     # 0x23ccb2
leaq 0x23851(%rip), %rsi     # 0x239325
leaq 0x279d5(%rip), %rcx     # 0x23d4b0
movl $0x3857, %edx           # imm = 0x3857
callq 0x2110
leaq 0x26e3c(%rip), %rdi     # 0x23c928
leaq 0x23832(%rip), %rsi     # 0x239325
leaq 0x279b6(%rip), %rcx     # 0x23d4b0
movl $0x3878, %edx           # imm = 0x3878
callq 0x2110