// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: shrink_ul0(all_ul_t*, ul_str_t*, unsigned long, integer_t*, ma_ug_t*)
void shrink_ul0(all_ul_t *uls, ul_str_t *str, uint64_t id, integer_t *buf, ma_ug_t *ug)
{
    uint32_t k, c_k, p_k, cv, pv, bl, i; uc_block_t *xi, *yi; buf->u.n = 0; nid_t *np = NULL;
    asg_arc_t *av; uint32_t nv, s, e, m, d, mm, is_conn; uint64_t *z; ul_vec_t *x;
    if(str->cn < 2) return;
    for (k = 0, bl = 0, c_k = p_k = pv = (uint32_t)-1; k < str->cn; k++) {
        xi = &(uls->a[id].bb.a[str->a[k]>>32]); is_conn = 0;
        c_k = k; cv = ((uint32_t)str->a[k])^1;
        assert(((xi->hid<<1)+xi->rev)==((uint32_t)str->a[k]));
        if(p_k != (uint32_t)-1) {
            if(xi->pidx == (str->a[p_k]>>32)) {
                av = asg_arc_a(ug->g, cv); nv = asg_arc_n(ug->g, cv);
                for (i = 0; i < nv; i++) {
                    if(av[i].del) continue;
                    if(av[i].v == pv) break;
                }
                if(i < nv) {
                    s = (uint32_t)av[i].ul;
                    if(s == xi->pdis) {
                        is_conn = 1;
                    } else {
                        d = (s>=xi->pdis?s-xi->pdis:xi->pdis-s);
                        mm = MAX(s, xi->pdis);
                        if((d <= (mm*0.08)) || (d <= 512)) is_conn = 1;
                    }
                }
            } else if(xi->pidx == (uint32_t)-1) {
                is_conn = 1;
            }
        }
        if(is_conn) {
            is_conn = 0; assert(k);
            yi = &(uls->a[id].bb.a[str->a[k-1]>>32]);
            if((xi->qs >= yi->qs) && (xi->qe >= yi->qe)) is_conn = 1;
        }
        if(is_conn) {
            bl++;
        } else {
            if(bl > 0) {
                kv_pushp(uint64_t, buf->u, &z);
                (*z) = k - 1 - bl; (*z) <<= 32; (*z) += bl + 1;
            }
            bl = 0;
        }
        p_k = c_k; pv = cv;
    }

    if(bl > 0) {
        kv_pushp(uint64_t, buf->u, &z);
        (*z) = k - 1 - bl; (*z) <<= 32; (*z) += bl + 1;
    }
    // fprintf(stderr, "[M::%s::] buf->u.n::%u, str->cn::%u\n", __func__, buf->u.n, str->cn);
    if(buf->u.n <= 0) {
        str->cn = str->n = 0;
    } else if(buf->u.n > 0) {
        uint32_t pidx, aidx, pdis;
        for (i = 0; i + 1 < buf->u.n; i++) {
            s = (buf->u.a[i]>>32); e = s + ((uint32_t)buf->u.a[i]); 
            ///new id
            kv_pushp(nid_t, uls->nid, &np);
            np->n = uls->nid.a[id].n; MALLOC(np->a, np->n+1);
            memcpy(np->a, uls->nid.a[id].a, np->n); np->a[np->n] = '\0';
            ///new ovlps
            kv_pushp(ul_vec_t, *uls, &x); memset(x, 0, sizeof(*x));
            MALLOC(x->bb.a, e - s); x->bb.n = x->bb.m = e - s;

            for (k = s, m = 0; k < e; k++, m++) {
                pidx = uls->a[id].bb.a[str->a[k]>>32].pidx; 
                aidx = uls->a[id].bb.a[str->a[k]>>32].aidx; 
                pdis = uls->a[id].bb.a[str->a[k]>>32].pdis;
                x->bb.a[m] = uls->a[id].bb.a[str->a[k]>>32];
                if(k == s) {
                    x->bb.a[m].pidx = x->bb.a[m].pdis = (uint32_t)-1;
                } else if(pidx != (uint32_t)-1) {
                    x->bb.a[m].pidx = m - 1; x->bb.a[m].pdis = pdis;
                }
                if(k + 1 == e) {
                    x->bb.a[m].aidx = (uint32_t)-1;
                } else if(aidx != (uint32_t)-1) {
                    x->bb.a[m].aidx = m + 1;
                }
            }
            x->bb.n = m;
            assert(x->bb.n > 1);

            shrink_1b(ug, &(x->bb.a[0]), ((x->bb.n>=2)?&(x->bb.a[1]):(NULL)), 1);
            shrink_1b(ug, &(x->bb.a[x->bb.n-1]), ((x->bb.n>=2)?&(x->bb.a[x->bb.n-2]):(NULL)), 0);
            
            d = x->bb.a[0].qs;
            for (k = 0; k < x->bb.n; k++) {
                x->bb.a[k].qs -= d; x->bb.a[k].qe -= d;
            }
            x->rlen = x->bb.a[x->bb.n-1].qe;
            renew_ul_vec_t(x, ug);
        }

        ///the last one; update in-place
        s = (buf->u.a[i]>>32); e = s + ((uint32_t)buf->u.a[i]); x = &(uls->a[id]);
        for (k = s, m = 0; k < e; k++, m++) {
            pidx = x->bb.a[str->a[k]>>32].pidx; 
            aidx = x->bb.a[str->a[k]>>32].aidx; 
            pdis = x->bb.a[str->a[k]>>32].pdis;
            x->bb.a[m] = x->bb.a[str->a[k]>>32];
            if(k == s) {
                x->bb.a[m].pidx = x->bb.a[m].pdis = (uint32_t)-1;
            } else if(pidx != (uint32_t)-1) {
                x->bb.a[m].pidx = m - 1; x->bb.a[m].pdis = pdis;
            }
            if(k + 1 == e) {
                x->bb.a[m].aidx = (uint32_t)-1;
            } else if(aidx != (uint32_t)-1) {
                x->bb.a[m].aidx = m + 1;
            }
        }
        x->bb.n = m;
        assert(x->bb.n > 1);
        shrink_1b(ug, &(x->bb.a[0]), ((x->bb.n>=2)?&(x->bb.a[1]):(NULL)), 1);
        shrink_1b(ug, &(x->bb.a[x->bb.n-1]), ((x->bb.n>=2)?&(x->bb.a[x->bb.n-2]):(NULL)), 0);
        d = x->bb.a[0].qs;
        for (k = 0; k < x->bb.n; k++) {
            x->bb.a[k].qs -= d; x->bb.a[k].qe -= d;
        }
        x->rlen = x->bb.a[x->bb.n-1].qe;
        renew_ul_vec_t(x, ug);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
andq $0x0, 0x90(%rcx)
movq %rsi, 0x10(%rsp)
movl 0x18(%rsi), %eax
testl $0x7ffffffe, %eax       # imm = 0x7FFFFFFE
je 0x1bdaf7
movq %rdi, %rbp
movq %r8, 0x18(%rsp)
movq %rcx, 0x8(%rsp)
pushq $-0x1
popq %rdi
xorl %r9d, %r9d
movq %rdx, 0x28(%rsp)
imulq $0x58, %rdx, %r13
xorl %r14d, %r14d
movl %edi, %r8d
movq %r13, 0x20(%rsp)
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %r14d, %r12d
notl %r12d
cmpq %rcx, %r9
jae 0x1bd621
movq 0x48(%rbp), %rcx
movq 0x30(%rcx,%r13), %rcx
movq 0x10(%rsp), %rdx
movq 0x10(%rdx), %rsi
movq %r9, %r15
movq (%rsi,%r9,8), %r13
movq %r13, %rdx
shrq $0x20, %rdx
imulq $0x24, %rdx, %rdx
movl (%rcx,%rdx), %r9d
movl 0x20(%rcx,%rdx), %r10d
shrl $0x5, %r10d
andl $0x1, %r10d
leal (%r10,%r9,2), %r9d
cmpl %r13d, %r9d
jne 0x1bdb06
xorl $0x1, %r13d
cmpl $-0x1, %r8d
je 0x1bd58f
addq %rcx, %rdx
movl 0x14(%rdx), %r9d
movl %r8d, %r8d
cmpl %r9d, 0x4(%rsi,%r8,8)
jne 0x1bd515
movq 0x18(%rsp), %r8
movq 0x18(%r8), %r9
movq 0x8(%r9), %r8
movq 0x28(%r9), %r9
movl %r13d, %r10d
movq (%r9,%r10,8), %r9
movl %r9d, %r10d
shrq $0x20, %r9
imulq $0x18, %r10, %r10
imulq $0x18, %r9, %r9
leaq (%r8,%r9), %rbx
addq $0xc, %rbx
xorl %r11d, %r11d
cmpq %r11, %r10
je 0x1bd58f
cmpl $0x0, (%rbx,%r11)
js 0x1bd50f
cmpl %edi, -0x4(%rbx,%r11)
je 0x1bd51d
addq $0x18, %r11
jmp 0x1bd4f8
cmpl $-0x1, %r9d
jne 0x1bd58f
jmp 0x1bd530
addq %r9, %r8
movl (%r11,%r8), %edi
movl 0x18(%rdx), %r9d
movl %r9d, %r8d
subl %edi, %r8d
jne 0x1bd561
testq %r15, %r15
je 0x1bdb44
movl -0x4(%rsi,%r15,8), %esi
imulq $0x24, %rsi, %rsi
movl 0x4(%rdx), %edi
cmpl 0x4(%rcx,%rsi), %edi
jb 0x1bd58f
addq %rsi, %rcx
movl 0x8(%rdx), %edx
cmpl 0x8(%rcx), %edx
jb 0x1bd58f
incl %r14d
movl %r14d, %ebx
jmp 0x1bd60a
movl %edi, %r10d
cmoval %r9d, %r10d
subl %r9d, %edi
cmovbl %r8d, %edi
cvtsi2sd %r10, %xmm0
mulsd 0x25fdc(%rip), %xmm0    # 0x1e3558
cmpl $0x201, %edi            # imm = 0x201
jb 0x1bd530
cvtsi2sd %rdi, %xmm1
ucomisd %xmm1, %xmm0
jae 0x1bd530
xorl %ebx, %ebx
testl %r14d, %r14d
je 0x1bd60a
movq 0x8(%rsp), %rdi
movq 0x90(%rdi), %rcx
movq 0xa0(%rdi), %rax
cmpq 0x98(%rdi), %rcx
jne 0x1bd5e6
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x98(%rdi)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %rdi
movq %rax, 0xa0(%rdi)
movq 0x90(%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x90(%rdi)
addl %r15d, %r12d
shlq $0x20, %r12
incl %r14d
orq %r12, %r14
movq %r14, (%rax,%rcx,8)
movq 0x10(%rsp), %rax
movl 0x18(%rax), %eax
movl %r15d, %r8d
leaq 0x1(%r15), %r9
movl %ebx, %r14d
movl %r13d, %edi
movq 0x20(%rsp), %r13
jmp 0x1bd44e
movq 0x8(%rsp), %rdi
movq 0x90(%rdi), %rcx
testl %r14d, %r14d
je 0x1bd69f
movq 0xa0(%rdi), %rax
cmpq 0x98(%rdi), %rcx
jne 0x1bd67c
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x98(%rdi)
shlq $0x3, %rsi
movq %rax, %rdi
movq %r9, %rbx
callq 0x2370
movq %rbx, %r9
movq 0x8(%rsp), %rdi
movq %rax, 0xa0(%rdi)
movq 0x90(%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x90(%rdi)
addl %r12d, %r9d
shlq $0x20, %r9
incl %r14d
orq %r9, %r14
movq %r14, (%rax,%rcx,8)
movq 0x90(%rdi), %rcx
testq %rcx, %rcx
je 0x1bdae7
xorl %eax, %eax
shlq $0x4, 0x28(%rsp)
movl %eax, %edx
leal 0x1(%rax), %esi
movq 0xa0(%rdi), %rax
movq (%rax,%rdx,8), %rdx
movq %rdx, %r12
shrq $0x20, %r12
leaq (%r12,%rdx), %rax
cmpq %rsi, %rcx
jbe 0x1bd975
movq %rdx, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movq (%rbp), %r14
movq 0x10(%rbp), %r13
cmpq 0x8(%rbp), %r14
movq %rsi, 0x30(%rsp)
jne 0x1bd71a
leaq (%r14,%r14), %rsi
testq %r14, %r14
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x4, %rsi
movq %r13, %rdi
callq 0x2370
movq %rax, %r13
movq %rax, 0x10(%rbp)
movq (%rbp), %r14
leaq 0x1(%r14), %rax
movq %rax, (%rbp)
shlq $0x4, %r14
movq 0x28(%rsp), %r15
movl 0x8(%r13,%r15), %ebx
movl %ebx, 0x8(%r13,%r14)
leal 0x1(%rbx), %edi
callq 0x2280
movq %rax, (%r13,%r14)
movq (%r13,%r15), %rsi
movq %rax, %rdi
movq %rbx, %rdx
callq 0x2160
movq (%r13,%r14), %rax
movl 0x8(%r13,%r14), %ecx
movb $0x0, (%rax,%rcx)
movq 0x48(%rbp), %rbx
movq 0x50(%rbp), %rax
cmpq 0x58(%rbp), %rax
jne 0x1bd797
leaq (%rax,%rax), %rcx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rcx
movq %rcx, 0x58(%rbp)
imulq $0x58, %rcx, %rsi
movq %rbx, %rdi
callq 0x2370
movq %rax, %rbx
movq %rax, 0x48(%rbp)
movq 0x50(%rbp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x50(%rbp)
imulq $0x58, %rax, %r14
leaq (%rbx,%r14), %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rbx,%r14)
movups %xmm0, 0x30(%rbx,%r14)
movups %xmm0, 0x20(%rbx,%r14)
andq $0x0, 0x50(%rbx,%r14)
movups %xmm0, 0x10(%rbx,%r14)
movups %xmm0, (%rbx,%r14)
movl 0x38(%rsp), %r15d
imulq $0x24, %r15, %rdi
callq 0x2280
movq %rax, 0x30(%rbx,%r14)
movq %r15, 0x28(%rbx,%r14)
movq %r15, 0x20(%rbx,%r14)
movl 0x40(%rsp), %eax
leaq (,%r12,8), %rcx
movq %r12, %rdx
subq %rax, %rdx
incq %rdx
xorl %edi, %edi
xorl %esi, %esi
leaq (%r12,%rsi), %r8
cmpq %rax, %r8
jae 0x1bd8be
movq 0x48(%rbp), %r8
movq 0x20(%rsp), %r9
movq 0x30(%r8,%r9), %r11
movq 0x10(%rsp), %r8
movq 0x10(%r8), %r8
addq %rcx, %r8
movl 0x4(%r8,%rsi,8), %r8d
imulq $0x24, %r8, %rbx
movl 0x14(%r11,%rbx), %r10d
movl 0x1c(%r11,%rbx), %r8d
movl 0x18(%r11,%rbx), %r9d
movq 0x30(%r13), %r14
movl 0x20(%r11,%rbx), %r15d
movl %r15d, 0x20(%r14,%rdi)
movupd (%r11,%rbx), %xmm0
movupd 0x10(%r11,%rbx), %xmm1
movupd %xmm1, 0x10(%r14,%rdi)
movupd %xmm0, (%r14,%rdi)
testq %rsi, %rsi
je 0x1bd889
cmpl $-0x1, %r10d
je 0x1bd893
leal -0x1(%rsi), %r10d
movq 0x30(%r13), %r11
movl %r10d, 0x14(%r11,%rdi)
movl %r9d, 0x18(%r11,%rdi)
jmp 0x1bd893
movq 0x30(%r13), %r9
orq $-0x1, 0x14(%r9,%rdi)
movq %rdx, %r10
pushq $-0x1
popq %r9
addq %rsi, %r10
je 0x1bd8a9
cmpl $-0x1, %r8d
je 0x1bd8b2
leal 0x1(%rsi), %r9d
movq 0x30(%r13), %r8
movl %r9d, 0x1c(%r8,%rdi)
incq %rsi
addq $0x24, %rdi
jmp 0x1bd803
movq %rsi, 0x20(%r13)
cmpq $0x1, %rsi
jbe 0x1bdb25
movq 0x30(%r13), %rsi
leaq 0x24(%rsi), %rdx
movq 0x18(%rsp), %r14
movq %r14, %rdi
pushq $0x1
popq %rcx
callq 0x1bd095
movq 0x20(%r13), %rax
movq 0x30(%r13), %rcx
imulq $0x24, %rax, %rdx
leaq (%rcx,%rdx), %rsi
addq $-0x24, %rsi
xorl %ebx, %ebx
cmpq $0x2, %rax
leaq -0x48(%rcx,%rdx), %rdx
cmovbq %rbx, %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x1bd095
movq 0x20(%r13), %rcx
movq 0x30(%r13), %rax
movd 0x4(%rax), %xmm0
pshufd $0x50, %xmm0, %xmm0     # xmm0 = xmm0[0,0,1,1]
movl %ebx, %edx
cmpq %rdx, %rcx
jbe 0x1bd943
imulq $0x24, %rdx, %rsi
movq 0x4(%rax,%rsi), %xmm1
psubd %xmm0, %xmm1
movq %xmm1, 0x4(%rax,%rsi)
leal 0x1(%rdx), %ebx
jmp 0x1bd923
imulq $0x24, %rcx, %rcx
movl -0x1c(%rax,%rcx), %eax
movl %eax, 0x18(%r13)
movq %r13, %rdi
movq %r14, %rsi
callq 0x1bd1e4
movq 0x8(%rsp), %rdi
movq 0x90(%rdi), %rcx
movq 0x20(%rsp), %r13
movq 0x30(%rsp), %rax
jmp 0x1bd6b0
addq 0x48(%rbp), %r13
movl %eax, %eax
leaq (,%r12,8), %rcx
movq %r12, %rdx
subq %rax, %rdx
incq %rdx
xorl %esi, %esi
pushq $-0x1
popq %r8
xorl %edi, %edi
leaq (%r12,%rdi), %r9
cmpq %rax, %r9
jae 0x1bda3d
movq 0x30(%r13), %rbx
movq 0x10(%rsp), %r9
movq 0x10(%r9), %r9
addq %rcx, %r9
movl 0x4(%r9,%rdi,8), %r9d
imulq $0x24, %r9, %r14
movl 0x14(%rbx,%r14), %r11d
movl 0x1c(%rbx,%r14), %r9d
movl 0x18(%rbx,%r14), %r10d
movl 0x20(%rbx,%r14), %ebp
movl %ebp, 0x20(%rbx,%rsi)
movupd (%rbx,%r14), %xmm0
movupd 0x10(%rbx,%r14), %xmm1
movupd %xmm1, 0x10(%rbx,%rsi)
movupd %xmm0, (%rbx,%rsi)
testq %rdi, %rdi
je 0x1bda09
cmpl $-0x1, %r11d
je 0x1bda13
leal -0x1(%rdi), %r11d
movq 0x30(%r13), %rbx
movl %r11d, 0x14(%rbx,%rsi)
movl %r10d, 0x18(%rbx,%rsi)
jmp 0x1bda13
movq 0x30(%r13), %r10
orq $-0x1, 0x14(%r10,%rsi)
movq %rdx, %r11
movl %r8d, %r10d
addq %rdi, %r11
je 0x1bda28
cmpl $-0x1, %r9d
je 0x1bda31
leal 0x1(%rdi), %r10d
movq 0x30(%r13), %r9
movl %r10d, 0x1c(%r9,%rsi)
incq %rdi
addq $0x24, %rsi
jmp 0x1bd994
movq %rdi, 0x20(%r13)
cmpq $0x1, %rdi
jbe 0x1bdb63
movq 0x30(%r13), %rsi
leaq 0x24(%rsi), %rdx
pushq $0x1
popq %rcx
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x1bd095
movq 0x20(%r13), %rax
movq 0x30(%r13), %rcx
imulq $0x24, %rax, %rdx
leaq (%rcx,%rdx), %rsi
addq $-0x24, %rsi
xorl %ebx, %ebx
cmpq $0x2, %rax
leaq -0x48(%rcx,%rdx), %rdx
cmovbq %rbx, %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x1bd095
movq 0x20(%r13), %rcx
movq 0x30(%r13), %rax
movd 0x4(%rax), %xmm0
pshufd $0x50, %xmm0, %xmm0     # xmm0 = xmm0[0,0,1,1]
movl %ebx, %edx
cmpq %rdx, %rcx
jbe 0x1bdac2
imulq $0x24, %rdx, %rsi
movq 0x4(%rax,%rsi), %xmm1
psubd %xmm0, %xmm1
movq %xmm1, 0x4(%rax,%rsi)
leal 0x1(%rdx), %ebx
jmp 0x1bdaa2
imulq $0x24, %rcx, %rcx
movl -0x1c(%rax,%rcx), %eax
movl %eax, 0x18(%r13)
movq %r13, %rdi
movq %r14, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1bd1e4
movq 0x10(%rsp), %rax
andq $0x0, (%rax)
andl $0x80000000, 0x18(%rax) # imm = 0x80000000
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3b98a(%rip), %rdi     # 0x1f9497
leaq 0x399c5(%rip), %rsi     # 0x1f74d9
leaq 0x3bf79(%rip), %rcx     # 0x1f9a94
movl $0x1e46, %edx           # imm = 0x1E46
callq 0x2110
leaq 0x3bfb2(%rip), %rdi     # 0x1f9ade
leaq 0x399a6(%rip), %rsi     # 0x1f74d9
leaq 0x3bf5a(%rip), %rcx     # 0x1f9a94
movl $0x1e91, %edx           # imm = 0x1E91
callq 0x2110
leaq 0x3b38a(%rip), %rdi     # 0x1f8ed5
leaq 0x39987(%rip), %rsi     # 0x1f74d9
leaq 0x3bf3b(%rip), %rcx     # 0x1f9a94
movl $0x1e5d, %edx           # imm = 0x1E5D
callq 0x2110
leaq 0x3bf74(%rip), %rdi     # 0x1f9ade
leaq 0x39968(%rip), %rsi     # 0x1f74d9
leaq 0x3bf1c(%rip), %rcx     # 0x1f9a94
movl $0x1eb1, %edx           # imm = 0x1EB1
callq 0x2110