// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: gen_scaf_res_via(sec_t*, unsigned long, ma_ug_t*, scaf_res_t*, ul_vec_t*)
void gen_scaf_res_via(sec_t *scp, uint64_t scp_len, ma_ug_t *mg, scaf_res_t *ms, ul_vec_t *res)
{
    uint64_t k, off, uid, urev, m, tt, s, e, crn, zi; ma_utg_t *z = NULL; ul_vec_t *ua; uc_block_t *p, *des, *src;
    for (k = off = res->bb.n = tt = 0; k < scp->n; k++) {
        uid = ((uint32_t)scp->a[k])>>1; urev = ((uint32_t)scp->a[k])&1;
        z = &(mg->u.a[uid]); ua = &(ms->a[uid]);
        for (m = 0; m < ua->bb.n; m++) {
            kv_pushp(uc_block_t, res->bb, &p); memset(p, 0, sizeof((*p))); 

            (*p) = ua->bb.a[m]; p->aidx = off; //offset of the whole contig within scaf
            p->qs = ua->bb.a[m].qs; p->qe = ua->bb.a[m].qe;
            if(urev) {
                p->qs = ((z->len>ua->bb.a[m].qe)?(z->len-ua->bb.a[m].qe):(0)); 
                p->qe = ((z->len>ua->bb.a[m].qs)?(z->len-ua->bb.a[m].qs):(0)); 
            } 
            p->qs += off; p->qe += off;

            p->hid = uid; p->hid <<= 1; p->hid |= urev;
            if(p->qs >= scp_len) p->qs = scp_len;
            if(p->qe >= scp_len) p->qe = scp_len;
            if(p->qe <= p->qs) res->bb.n--;
            else tt += p->te - p->ts;//how many elements
        }
        off += z->len; off += (scp->a[k]>>32);
    }

    sort_uc_block_qe(res->bb.a, res->bb.n);
    kv_resize(uc_block_t, res->bb, res->bb.n + tt); 
	memset(res->bb.a + res->bb.n, 0, tt * sizeof(*(res->bb.a))); //res->bb.a[0, res->bb.n) -> index; res->bb.a[res->bb.n, res->bb.n + tt) -> details

    
	for (k = 0, tt = res->bb.n; k < res->bb.n; k++) {
		s = tt; e = tt + res->bb.a[k].te - res->bb.a[k].ts; tt = e; off = res->bb.a[k].aidx;
        uid = res->bb.a[k].hid>>1; urev = res->bb.a[k].hid&1; z = &(mg->u.a[uid]); 


        src = ms->a[uid].bb.a + res->bb.a[k].ts;
        des = res->bb.a + s; crn = e - s;
        if(!urev) {
            for(zi = 0; zi < crn; zi++) {
                des[zi] = src[zi]; 
                des[zi].qs += off; 
                des[zi].qe += off;
                des[zi].aidx = s;

                if(des[zi].qs >= res->bb.a[k].qe) des[zi].qs = res->bb.a[k].qe;
                if(des[zi].qs <= res->bb.a[k].qs) des[zi].qs = res->bb.a[k].qs;
                if(des[zi].qe >= res->bb.a[k].qe) des[zi].qe = res->bb.a[k].qe;
                if(des[zi].qe <= res->bb.a[k].qs) des[zi].qe = res->bb.a[k].qs;
                if(des[zi].qe <= des[zi].qs) des[zi].qe = des[zi].qs;
            }
        } else {
            for(zi = 0; zi < crn; zi++) {
                des[zi] = src[zi]; 
                des[zi].rev = 1 - des[zi].rev;
                des[zi].aidx = s;
                des[zi].qs = ((z->len>src[zi].qe)?(z->len-src[zi].qe):(0)); 
                des[zi].qe = ((z->len>src[zi].qs)?(z->len-src[zi].qs):(0)); 
                des[zi].qs += off; 
                des[zi].qe += off;
                des[zi].aidx = s;

                if(des[zi].qs >= res->bb.a[k].qe) des[zi].qs = res->bb.a[k].qe;
                if(des[zi].qs <= res->bb.a[k].qs) des[zi].qs = res->bb.a[k].qs;
                if(des[zi].qe >= res->bb.a[k].qe) des[zi].qe = res->bb.a[k].qe;
                if(des[zi].qe <= res->bb.a[k].qs) des[zi].qe = res->bb.a[k].qs;
                if(des[zi].qe <= des[zi].qs) des[zi].qe = des[zi].qs;
            }
        }
		res->bb.a[k].ts = s; res->bb.a[k].te = e;

        res->bb.a[k].hid = (uint32_t)-1;
	}
	assert(tt <= res->bb.m);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %rbx
movq %rcx, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, %r13
movq $0x0, 0x20(%r8)
movq 0x8(%rdi), %rax
xorl %esi, %esi
testq %rax, %rax
je 0xe02ea
movq 0x18(%rdi), %rcx
xorps %xmm2, %xmm2
xorl %r10d, %r10d
xorl %r14d, %r14d
movq $0x0, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
movq %r13, 0x30(%rsp)
movq (%rcx,%r10,8), %r11
movl %r11d, %edi
shrl %edi
leaq (%rdi,%rdi,4), %r15
shlq $0x3, %r15
movq 0x10(%rsp), %rdx
movq (%rdx), %rdx
imulq $0x58, %rdi, %r8
movq 0x8(%rsp), %rdi
addq 0x10(%rdi), %r15
movq %r15, (%rsp)
cmpq $0x0, 0x20(%rdx,%r8)
je 0xe02bd
movq %r10, 0x28(%rsp)
leaq (%rdx,%r8), %r12
addq $0x20, %r12
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rax
cmpq 0x28(%rbx), %rcx
jne 0xe01d6
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x28(%rbx)
shlq $0x2, %rdx
leaq (%rdx,%rdx,8), %rsi
movq %rax, %rdi
movq %r14, %r13
movq %rbx, %r14
movq %r11, %rbx
callq 0x2370
movq %rbx, %r11
movq %r14, %rbx
movq %r13, %r14
movq 0x30(%rsp), %r13
xorps %xmm2, %xmm2
xorl %esi, %esi
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
leaq (%rcx,%rcx,8), %rcx
leaq (%rax,%rcx,4), %rax
movups %xmm2, 0x10(%rax)
movups %xmm2, (%rax)
movl $0x0, 0x20(%rax)
movq 0x10(%r12), %rcx
movups (%rcx,%r15), %xmm0
movups 0x10(%rcx,%r15), %xmm1
movups %xmm1, 0x10(%rax)
movl 0x20(%rcx,%r15), %ecx
movl %ecx, 0x20(%rax)
movups %xmm0, (%rax)
movl %r14d, 0x1c(%rax)
movq 0x10(%r12), %rdi
movl 0x4(%rdi,%r15), %ecx
movl %ecx, 0x4(%rax)
movl 0x8(%rdi,%r15), %edx
movl %edx, 0x8(%rax)
testb $0x1, %r11b
je 0xe0258
movq (%rsp), %r9
movl (%r9), %ecx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %ecx
subl %edx, %ecx
cmovbl %esi, %ecx
movl %ecx, 0x4(%rax)
movl (%r9), %edx
andl %r8d, %edx
subl 0x4(%rdi,%r15), %edx
cmovbl %esi, %edx
addl %r14d, %ecx
movl %ecx, 0x4(%rax)
addl %r14d, %edx
movl %edx, 0x8(%rax)
movl %r11d, (%rax)
cmpq %r13, %rcx
jb 0xe0273
movl %r13d, 0x4(%rax)
movl %r13d, %ecx
movl %edx, %edi
cmpq %r13, %rdi
jb 0xe0281
movl %r13d, 0x8(%rax)
movl %r13d, %edx
cmpl %ecx, %edx
jbe 0xe0292
movl 0x10(%rax), %ecx
subl 0xc(%rax), %ecx
addq %rcx, 0x18(%rsp)
jmp 0xe0296
decq 0x20(%rbx)
incq %rbp
addq $0x24, %r15
cmpq (%r12), %rbp
jb 0xe0180
movq 0x20(%rsp), %r9
movq 0x8(%r9), %rax
movq 0x18(%r9), %rcx
movq 0x28(%rsp), %r10
movq (%rcx,%r10,8), %r11
movq (%rsp), %rdx
movl (%rdx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
shrq $0x20, %r11
addq %r11, %r14
addq %rdx, %r14
incq %r10
cmpq %rax, %r10
jb 0xe0138
movq 0x20(%rbx), %rsi
movq 0x18(%rsp), %r14
jmp 0xe02ed
xorl %r14d, %r14d
movq 0x30(%rbx), %rdi
callq 0x1b08a2
movq 0x20(%rbx), %rcx
leaq (%rcx,%r14), %rdx
movq 0x30(%rbx), %rax
cmpq %rdx, 0x28(%rbx)
jae 0xe0360
decq %rdx
movq %rdx, %rcx
shrq %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x2, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x4, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x8, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x10, %rcx
orq %rdx, %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x28(%rbx)
leaq 0x4(,%rcx,4), %rcx
leaq (%rcx,%rcx,8), %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rcx
leaq (%rcx,%rcx,8), %rcx
leaq (%rax,%rcx,4), %rdi
shlq $0x2, %r14
leaq (%r14,%r14,8), %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0xe05ce
movq 0x30(%rbx), %r10
xorl %r13d, %r13d
movq %rax, %rsi
leaq (,%r13,8), %rdi
addq %r13, %rdi
movl 0x10(%r10,%rdi,4), %eax
addq %rsi, %rax
movl (%r10,%rdi,4), %edx
movl 0xc(%r10,%rdi,4), %ecx
subq %rcx, %rax
movl 0x1c(%r10,%rdi,4), %r8d
movl %edx, %r11d
shrl %r11d
movq 0x10(%rsp), %r9
movq (%r9), %r9
imulq $0x58, %r11, %r14
movq 0x30(%r9,%r14), %r15
leaq (%rcx,%rcx,8), %r12
leaq (%rsi,%rsi,8), %rcx
movq %rax, %r9
subq %rsi, %r9
testb $0x1, %dl
movq %r13, (%rsp)
jne 0xe04a5
cmpq %rsi, %rax
je 0xe05a5
leaq (%r15,%r12,4), %r11
leaq (%r10,%rcx,4), %r14
cmpq $0x1, %r9
adcq $0x0, %r9
xorl %r12d, %r12d
movups (%r11,%r12), %xmm0
movups 0x10(%r11,%r12), %xmm1
movups %xmm0, (%r14,%r12)
movups %xmm1, 0x10(%r14,%r12)
movl 0x20(%r11,%r12), %ecx
movl %ecx, 0x20(%r14,%r12)
movl 0x4(%r14,%r12), %ebp
addl %r8d, %ebp
movl %ebp, 0x4(%r14,%r12)
movl 0x8(%r14,%r12), %r15d
addl %r8d, %r15d
movl %r15d, 0x8(%r14,%r12)
movl %esi, 0x1c(%r14,%r12)
movq 0x30(%rbx), %r10
movl 0x8(%r10,%rdi,4), %r13d
cmpl %r13d, %ebp
jb 0xe0455
movl %r13d, 0x4(%r14,%r12)
movl %r13d, %ebp
movl 0x4(%r10,%rdi,4), %ecx
cmpl %ecx, %ebp
ja 0xe0465
movl %ecx, 0x4(%r14,%r12)
movl %ecx, %ebp
cmpl %r13d, %r15d
jb 0xe0472
movl %r13d, 0x8(%r14,%r12)
movl %r13d, %r15d
leaq (%r10,%rdi,4), %rcx
addq $0x4, %rcx
movl (%rcx), %ecx
cmpl %ecx, %r15d
cmoval %r15d, %ecx
jbe 0xe0489
cmpl %ebp, %ecx
ja 0xe0493
cmpl %ebp, %ecx
cmoval %ecx, %ebp
movl %ebp, 0x8(%r14,%r12)
addq $0x24, %r12
decq %r9
jne 0xe0400
jmp 0xe05a5
cmpq %rsi, %rax
je 0xe05a5
leaq (%r11,%r11,4), %r11
shlq $0x3, %r11
movq 0x8(%rsp), %rdx
addq 0x10(%rdx), %r11
cmpq $0x1, %r9
adcq $0x0, %r9
leaq (%r10,%rcx,4), %r14
addq $0x20, %r14
leaq (%r15,%r12,4), %r12
addq $0x8, %r12
xorl %r13d, %r13d
movl 0x18(%r12,%r13), %ecx
movl %ecx, (%r14,%r13)
movups -0x8(%r12,%r13), %xmm0
movups 0x8(%r12,%r13), %xmm1
movups %xmm1, -0x10(%r14,%r13)
movups %xmm0, -0x20(%r14,%r13)
xorb $0x20, %cl
movb %cl, (%r14,%r13)
movl %esi, -0x4(%r14,%r13)
movl (%r11), %ebp
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ebp
subl (%r12,%r13), %ebp
movl $0x0, %ecx
cmovbl %ecx, %ebp
movl %ebp, -0x1c(%r14,%r13)
movl (%r11), %r15d
andl %edx, %r15d
subl -0x4(%r12,%r13), %r15d
cmovbl %ecx, %r15d
addl %r8d, %ebp
movl %ebp, -0x1c(%r14,%r13)
addl %r8d, %r15d
movl %r15d, -0x18(%r14,%r13)
movl %esi, -0x4(%r14,%r13)
movq 0x30(%rbx), %r10
movl 0x8(%r10,%rdi,4), %ecx
cmpl %ecx, %ebp
jb 0xe055a
movl %ecx, -0x1c(%r14,%r13)
movl %ecx, %ebp
movl 0x4(%r10,%rdi,4), %edx
cmpl %edx, %ebp
ja 0xe056a
movl %edx, -0x1c(%r14,%r13)
movl %edx, %ebp
cmpl %ecx, %r15d
jb 0xe0577
movl %ecx, -0x18(%r14,%r13)
movl %ecx, %r15d
leaq (%r10,%rdi,4), %rcx
addq $0x4, %rcx
movl (%rcx), %ecx
cmpl %ecx, %r15d
cmoval %r15d, %ecx
jbe 0xe058e
cmpl %ebp, %ecx
ja 0xe0598
cmpl %ebp, %ecx
cmoval %ecx, %ebp
movl %ebp, -0x18(%r14,%r13)
addq $0x24, %r13
decq %r9
jne 0xe04da
movl %esi, 0xc(%r10,%rdi,4)
movl %eax, 0x10(%r10,%rdi,4)
movl $0xffffffff, (%r10,%rdi,4) # imm = 0xFFFFFFFF
movq (%rsp), %r13
incq %r13
cmpq 0x20(%rbx), %r13
jb 0xe038b
cmpq 0x28(%rbx), %rax
ja 0xe05dd
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x15769c(%rip), %rdi    # 0x237c80
leaq 0x1558a6(%rip), %rsi    # 0x235e91
leaq 0x15769e(%rip), %rcx    # 0x237c90
movl $0x5a3e, %edx           # imm = 0x5A3E
callq 0x2110