// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: append_miss_nid(asg_t*, ma_ug_t*, ma_ug_t*, unsigned char*, unsigned long, unsigned long)
uint64_t append_miss_nid(asg_t *sg, ma_ug_t *hap0, ma_ug_t *hap1, uint8_t *ff, uint64_t len_cut, uint64_t occ_cut)
{
    ma_ug_t *ug = NULL; ma_utg_t *u; uint64_t k, z, pscut, n_set, fn, nfn, hap0n, hap1n, n_base = 0; 
    kvec_asg_arc_t_warp fe; memset(&fe, 0, sizeof(fe));
    memset(ff, 0, sizeof((*ff))*sg->n_seq);
    ug = hap0;
    for (k = 0; k < ug->u.n; k++) {
        u = &(ug->u.a[k]); 
        if((u->n == 0) || (u->m == 0) || (ug->g->seq[k].del)) continue;
        for (z = 0; z < u->n; z++) ff[u->a[z]>>33] = 1;
    }
    ug = hap1;
    for (k = 0; k < ug->u.n; k++) {
        u = &(ug->u.a[k]); 
        if((u->n == 0) || (u->m == 0) || (ug->g->seq[k].del)) continue;
        for (z = 0; z < u->n; z++) ff[u->a[z]>>33] = 1;
    }
    ug = NULL; pscut = 0;
    pscut = (asm_opt.hom_global_coverage_set?(asm_opt.hom_global_coverage):(((double)asm_opt.hom_global_coverage)/((double)HOM_PEAK_RATE)));
    pscut *= PHASE_SEF; if(pscut < PHASE_SEP) pscut = PHASE_SEP;
    ug = ma_ug_gen_phase(sg, pscut, PHASE_SEP_RATE);
    for (k = 0; k < ug->u.n; k++) {
        u = &(ug->u.a[k]); 
        if((u->n == 0) || (u->m == 0) || (ug->g->seq[k].del)) continue;
        for (z = n_set = 0; z < u->n; z++) {
            if(ff[u->a[z]>>33]) n_set++;
        }
        if((n_set > 0) && (n_set >= (u->n*0.2))) delete_ug_node(ug, k);
    }
    renew_utg((&ug), sg, &fe);

    for (k = hap0n = hap1n = 0; k < ug->u.n; k++) {
        u = &(ug->u.a[k]); 
        if((u->n == 0) || (u->m == 0) || (ug->g->seq[k].del)) continue;
        if((u->len < len_cut) || (u->n < occ_cut)) continue;
        for (z = n_set = 0; z < u->n; z++) {
            if(ff[u->a[z]>>33]) n_set++;
        }
        if((n_set > 0) && (n_set >= (u->n*0.2))) continue;
        for (z = fn = nfn = 0; z < u->n; z++) {
            if(R_INF.trio_flag[u->a[z]>>33] == FATHER) fn++;
            if(R_INF.trio_flag[u->a[z]>>33] == MOTHER) nfn++;
        }
        if(fn > nfn) {
            push_ma_utg_t(hap0, u); hap0n++; n_base += u->len;
        } else {
            push_ma_utg_t(hap1, u); hap1n++; n_base += u->len;
        }
    }
    ma_ug_destroy(ug); ug = NULL;
    if(hap0n) {
        ug = hap0; 
        free(ug->g->idx); ug->g->idx = 0; ug->g->is_srt = 0;
        asg_cleanup(ug->g); asg_symm(ug->g);
        if(ug->g->seq_vis) {
            REALLOC(ug->g->seq_vis, (ug->g->n_seq*2));
            memset(ug->g->seq_vis, 0, sizeof((*(ug->g->seq_vis)))*(ug->g->n_seq*2));
        }
    }
    if(hap1n) {
        ug = hap1; 
        free(ug->g->idx); ug->g->idx = 0; ug->g->is_srt = 0;
        asg_cleanup(ug->g); asg_symm(ug->g);
        if(ug->g->seq_vis) {
            REALLOC(ug->g->seq_vis, (ug->g->n_seq*2));
            memset(ug->g->seq_vis, 0, sizeof((*(ug->g->seq_vis)))*(ug->g->n_seq*2));
        }
    }
    return n_base;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, 0x20(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, %r13
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %rbp
movq $0x0, (%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movl 0x14(%rdi), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rcx, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r14, (%rsp)
cmpq $0x0, (%r14)
je 0xd95ec
xorl %eax, %eax
movq 0x10(%r14), %rcx
leaq (%rax,%rax,4), %rdx
cmpl $0x0, 0x10(%rcx,%rdx,8)
je 0xd95e4
leaq (%rcx,%rdx,8), %rcx
cmpl $0x0, 0xc(%rcx)
je 0xd95e4
movq 0x18(%r14), %rdx
movq 0x20(%rdx), %rdx
cmpl $0x0, (%rdx,%rax,8)
js 0xd95e4
cmpl $0x0, 0x10(%rcx)
je 0xd95e4
xorl %edx, %edx
movq 0x18(%rcx), %rsi
movq (%rsi,%rdx,8), %rsi
shrq $0x21, %rsi
movb $0x1, (%r13,%rsi)
incq %rdx
movl 0x10(%rcx), %esi
cmpq %rsi, %rdx
jb 0xd95c7
incq %rax
cmpq (%r14), %rax
jb 0xd9598
movq %rbx, (%rsp)
cmpq $0x0, (%rbx)
je 0xd964c
xorl %eax, %eax
movq 0x10(%rbx), %rcx
leaq (%rax,%rax,4), %rdx
cmpl $0x0, 0x10(%rcx,%rdx,8)
je 0xd9644
leaq (%rcx,%rdx,8), %rcx
cmpl $0x0, 0xc(%rcx)
je 0xd9644
movq 0x18(%rbx), %rdx
movq 0x20(%rdx), %rdx
cmpl $0x0, (%rdx,%rax,8)
js 0xd9644
cmpl $0x0, 0x10(%rcx)
je 0xd9644
xorl %edx, %edx
movq 0x18(%rcx), %rsi
movq (%rsi,%rdx,8), %rsi
shrq $0x21, %rsi
movb $0x1, (%r13,%rsi)
incq %rdx
movl 0x10(%rcx), %esi
cmpq %rsi, %rdx
jb 0xd9627
incq %rax
cmpq (%rbx), %rax
jb 0xd95f8
movq $0x0, (%rsp)
leaq 0x1b10fd(%rip), %rax    # 0x28a758
cmpl $0x0, 0x148(%rax)
xorps %xmm0, %xmm0
cvtsi2sdl 0x144(%rax), %xmm0
jne 0xd9677
divsd 0x1504f1(%rip), %xmm0   # 0x229b68
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0x14bbe1(%rip), %xmm0   # 0x225268
cvttsd2si %xmm0, %rdx
sarq $0x3f, %rcx
andq %rcx, %rdx
orq %rax, %rdx
addq %rdx, %rdx
cmpq $0x41, %rdx
movl $0x40, %esi
cmovaeq %rdx, %rsi
movsd 0x14be3a(%rip), %xmm0   # 0x2254e8
movq %rbp, %rdi
callq 0xb09be
movq %rax, (%rsp)
cmpq $0x0, (%rax)
je 0xd9795
movq %rax, %r15
xorl %r12d, %r12d
movq 0x14bb6e(%rip), %xmm3   # 0x225240
movapd 0x14bb76(%rip), %xmm4   # 0x225250
movsd 0x14bd9e(%rip), %xmm5   # 0x225480
movq 0x10(%r15), %rcx
leaq (%r12,%r12,4), %rdx
movl 0x10(%rcx,%rdx,8), %eax
testq %rax, %rax
je 0xd9789
leaq (%rcx,%rdx,8), %rcx
cmpl $0x0, 0xc(%rcx)
je 0xd9789
movq 0x18(%r15), %rdx
movq 0x20(%rdx), %rdx
cmpl $0x0, (%rdx,%r12,8)
js 0xd9789
movq 0x18(%rcx), %rdx
xorl %ecx, %ecx
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
shrq $0x21, %rdi
cmpb $0x1, (%r13,%rdi)
sbbq $-0x1, %rcx
incq %rsi
cmpq %rsi, %rax
jne 0xd971c
testq %rcx, %rcx
je 0xd9789
movq %rcx, %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
addsd %xmm0, %xmm1
mulsd %xmm5, %xmm2
ucomisd %xmm2, %xmm1
jb 0xd9789
movq %r15, %rdi
movq %r12, %rsi
callq 0xd918d
movsd 0x14bd07(%rip), %xmm5   # 0x225480
movapd 0x14bacf(%rip), %xmm4   # 0x225250
movq 0x14bab7(%rip), %xmm3   # 0x225240
incq %r12
cmpq (%r15), %r12
jb 0xd96e2
movq %rsp, %r15
leaq 0x30(%rsp), %rdx
movq %r15, %rdi
movq %rbp, %rsi
callq 0xba760
movq (%r15), %r12
cmpq $0x0, (%r12)
je 0xd9945
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movq 0x20(%rsp), %r8
movq 0x10(%r12), %rcx
leaq (,%rbp,4), %rdx
addq %rbp, %rdx
movl 0x10(%rcx,%rdx,8), %eax
testq %rax, %rax
je 0xd991d
leaq (%rcx,%rdx,8), %r15
cmpl $0x0, 0xc(%r15)
je 0xd991d
movq 0x18(%r12), %rcx
movq 0x20(%rcx), %rcx
cmpl $0x0, (%rcx,%rbp,8)
js 0xd991d
cmpq %r8, %rax
jb 0xd991d
movl (%r15), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq 0x28(%rsp), %rcx
jb 0xd991d
movq 0x18(%r15), %rdx
xorl %ecx, %ecx
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
shrq $0x21, %rdi
cmpb $0x1, (%r13,%rdi)
sbbq $-0x1, %rcx
incq %rsi
cmpq %rsi, %rax
jne 0xd983c
testq %rcx, %rcx
je 0xd9896
movq %rcx, %xmm0
punpckldq 0x14b9d8(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x14b9e0(%rip), %xmm0   # 0x225250
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
addsd %xmm0, %xmm1
mulsd 0x14bbf4(%rip), %xmm2   # 0x225480
ucomisd %xmm2, %xmm1
jae 0xd991d
leaq 0x1b1993(%rip), %rcx    # 0x28b230
movq 0x28(%rcx), %rcx
cmpq $0x1, %rax
adcq $0x0, %rax
movq 0x18(%r15), %rdx
xorl %esi, %esi
xorl %edi, %edi
xorl %r8d, %r8d
movq (%rdx,%r8,8), %r9
shrq $0x21, %r9
movb (%rcx,%r9), %r9b
xorl %r10d, %r10d
cmpb $0x1, %r9b
sete %r10b
addq %r10, %rdi
xorl %r10d, %r10d
cmpb $0x2, %r9b
sete %r10b
addq %r10, %rsi
incq %r8
cmpq %r8, %rax
jne 0xd98b4
cmpq %rsi, %rdi
jbe 0xd98fb
movq %r14, %rdi
movq %r15, %rsi
callq 0xd93d2
incq 0x8(%rsp)
jmp 0xd990b
movq %rbx, %rdi
movq %r15, %rsi
callq 0xd93d2
incq 0x10(%rsp)
movl (%r15), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, 0x18(%rsp)
movq 0x20(%rsp), %r8
incq %rbp
cmpq (%r12), %rbp
jb 0xd97d8
cmpq $0x0, 0x8(%rsp)
sete %r15b
cmpq $0x0, 0x10(%rsp)
sete %bpl
movq 0x18(%rsp), %r13
jmp 0xd994e
movb $0x1, %r15b
xorl %r13d, %r13d
movb $0x1, %bpl
movq %r12, %rdi
callq 0xa0004
movq $0x0, (%rsp)
testb %r15b, %r15b
jne 0xd99d4
movq %r14, (%rsp)
movq 0x18(%r14), %rax
movq 0x28(%rax), %rdi
callq 0x2220
movq 0x18(%r14), %rax
movq $0x0, 0x28(%rax)
andb $0x7f, 0x7(%rax)
movq 0x18(%r14), %rdi
callq 0xa02fa
movq 0x18(%r14), %r15
movq %r15, %rdi
callq 0xa0333
movq %r15, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%r15)
movq 0x18(%r14), %rax
movq 0x30(%rax), %rdi
testq %rdi, %rdi
je 0xd99d4
movl 0x14(%rax), %esi
addl %esi, %esi
callq 0x2370
movq 0x18(%r14), %rcx
movq %rax, 0x30(%rcx)
movl 0x14(%rcx), %edx
addl %edx, %edx
movq %rax, %rdi
xorl %esi, %esi
callq 0x20e0
testb %bpl, %bpl
jne 0xd9a4a
movq %rbx, (%rsp)
movq 0x18(%rbx), %rax
movq 0x28(%rax), %rdi
callq 0x2220
movq 0x18(%rbx), %rax
movq $0x0, 0x28(%rax)
andb $0x7f, 0x7(%rax)
movq 0x18(%rbx), %rdi
callq 0xa02fa
movq 0x18(%rbx), %r14
movq %r14, %rdi
callq 0xa0333
movq %r14, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%r14)
movq 0x18(%rbx), %rax
movq 0x30(%rax), %rdi
testq %rdi, %rdi
je 0xd9a4a
movl 0x14(%rax), %esi
addl %esi, %esi
callq 0x2370
movq 0x18(%rbx), %rcx
movq %rax, 0x30(%rcx)
movl 0x14(%rcx), %edx
addl %edx, %edx
movq %rax, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq