// File: /workspace/github_cpp_makefile_star2_O1/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 $0x0, 0x20(%r8)
cmpq $0x0, 0x8(%rdi)
je 0xdef69
movq %rsi, %r10
xorps %xmm2, %xmm2
xorl %edx, %edx
xorl %ebp, %ebp
movq $0x0, (%rsp)
movq %rdi, 0x20(%rsp)
movq 0x18(%rdi), %rax
movq %rdx, 0x28(%rsp)
movq (%rax,%rdx,8), %r12
movl %r12d, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rsi
shlq $0x3, %rsi
movq 0x10(%rsp), %rax
movq (%rax), %rax
imulq $0x58, %rcx, %rcx
movq 0x8(%rsp), %rdx
addq 0x10(%rdx), %rsi
movq %rsi, 0x18(%rsp)
cmpq $0x0, 0x20(%rax,%rcx)
je 0xdef36
leaq (%rax,%rcx), %r14
addq $0x20, %r14
xorl %r15d, %r15d
xorl %r13d, %r13d
movq %r14, 0x30(%rsp)
movq 0x28(%rbx), %rax
cmpq %rax, 0x20(%rbx)
jne 0xdee57
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x28(%rbx)
movq 0x30(%rbx), %rdi
shlq $0x2, %rcx
leaq (%rcx,%rcx,8), %rsi
movq %rbx, %r14
movq %r10, %rbx
callq 0x2370
movq %rbx, %r10
movq %r14, %rbx
movq 0x30(%rsp), %r14
xorps %xmm2, %xmm2
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rax
movq 0x30(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x20(%rbx)
leaq (%rax,%rax,8), %rax
leaq (%rcx,%rax,4), %rax
movups %xmm2, 0x10(%rax)
movups %xmm2, (%rax)
movl $0x0, 0x20(%rax)
movq 0x10(%r14), %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 %ebp, 0x1c(%rax)
movq 0x10(%r14), %rcx
movl 0x4(%rcx,%r15), %edx
movl %edx, 0x4(%rax)
movl 0x8(%rcx,%r15), %edx
movl %edx, 0x8(%rax)
testb $0x1, %r12b
je 0xdeee7
movq 0x18(%rsp), %r8
movl (%r8), %edx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %edx
subl 0x8(%rcx,%r15), %edx
movl $0x0, %esi
cmovbl %esi, %edx
movl %edx, 0x4(%rax)
movl (%r8), %edx
andl %edi, %edx
subl 0x4(%rcx,%r15), %edx
cmovbl %esi, %edx
movl %edx, 0x8(%rax)
movl 0x4(%rax), %edx
addl %ebp, %edx
movl %edx, 0x4(%rax)
movl 0x8(%rax), %ecx
addl %ebp, %ecx
movl %ecx, 0x8(%rax)
movl %r12d, (%rax)
cmpq %r10, %rdx
jb 0xdef03
movl %r10d, 0x4(%rax)
movl %ecx, %ecx
cmpq %r10, %rcx
jb 0xdef0e
movl %r10d, 0x8(%rax)
movl 0x8(%rax), %ecx
cmpl 0x4(%rax), %ecx
jbe 0xdef22
movl 0x10(%rax), %ecx
subl 0xc(%rax), %ecx
addq %rcx, (%rsp)
jmp 0xdef26
decq 0x20(%rbx)
incq %r13
addq $0x24, %r15
cmpq (%r14), %r13
jb 0xdee10
movq 0x18(%rsp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x20(%rsp), %rdi
movq 0x18(%rdi), %rcx
movq 0x28(%rsp), %rdx
movl 0x4(%rcx,%rdx,8), %ecx
addq %rcx, %rbp
addq %rax, %rbp
incq %rdx
cmpq 0x8(%rdi), %rdx
jb 0xdedbd
jmp 0xdef71
movq $0x0, (%rsp)
movq 0x20(%rbx), %rsi
movq 0x30(%rbx), %rdi
callq 0x1a7654
movq 0x20(%rbx), %rax
movq (%rsp), %r14
addq %r14, %rax
cmpq %rax, 0x28(%rbx)
jae 0xdefe4
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x28(%rbx)
movq 0x30(%rbx), %rdi
leaq 0x4(,%rcx,4), %rax
leaq (%rax,%rax,8), %rsi
callq 0x2370
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rax
leaq (%rax,%rax,8), %rdi
shlq $0x2, %rdi
addq 0x30(%rbx), %rdi
shlq $0x2, %r14
leaq (%r14,%r14,8), %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0xdf248
xorl %edx, %edx
movq %rax, %rsi
movq 0x30(%rbx), %rcx
leaq (%rdx,%rdx,8), %rdi
movl 0x10(%rcx,%rdi,4), %eax
addq %rsi, %rax
movl (%rcx,%rdi,4), %ebp
movl 0xc(%rcx,%rdi,4), %r9d
subq %r9, %rax
movl 0x1c(%rcx,%rdi,4), %r8d
movl %ebp, %r11d
shrl %r11d
movq 0x10(%rsp), %r10
movq (%r10), %r10
imulq $0x58, %r11, %r14
movq 0x30(%r10,%r14), %r14
leaq (%r9,%r9,8), %r15
leaq (%rsi,%rsi,8), %r9
leaq (%rcx,%r9,4), %r9
movq %rax, %r10
subq %rsi, %r10
testb $0x1, %bpl
jne 0xdf125
cmpq %rsi, %rax
je 0xdf228
leaq (%r14,%r15,4), %r11
cmpq $0x1, %r10
adcq $0x0, %r10
xorl %r14d, %r14d
movups (%r11,%r14), %xmm0
movups 0x10(%r11,%r14), %xmm1
movups %xmm0, (%r9,%r14)
movups %xmm1, 0x10(%r9,%r14)
movl 0x20(%r11,%r14), %ecx
movl %ecx, 0x20(%r9,%r14)
movl 0x4(%r9,%r14), %ecx
addl %r8d, %ecx
movl %ecx, 0x4(%r9,%r14)
movl 0x8(%r9,%r14), %ebp
addl %r8d, %ebp
movl %ebp, 0x8(%r9,%r14)
movl %esi, 0x1c(%r9,%r14)
movq 0x30(%rbx), %r15
movl 0x8(%r15,%rdi,4), %r13d
cmpl %r13d, %ecx
jb 0xdf0d1
movl %r13d, 0x4(%r9,%r14)
movl 0x4(%r15,%rdi,4), %r12d
cmpl %r12d, 0x4(%r9,%r14)
ja 0xdf0e2
movl %r12d, 0x4(%r9,%r14)
cmpl %r13d, %ebp
jb 0xdf0ec
movl %r13d, 0x8(%r9,%r14)
leaq (%r15,%rdi,4), %rcx
addq $0x4, %rcx
movl (%rcx), %ebp
cmpl %ebp, 0x8(%r9,%r14)
ja 0xdf102
movl %ebp, 0x8(%r9,%r14)
movl 0x4(%r9,%r14), %ebp
cmpl %ebp, 0x8(%r9,%r14)
ja 0xdf113
movl %ebp, 0x8(%r9,%r14)
addq $0x24, %r14
decq %r10
jne 0xdf07f
jmp 0xdf228
cmpq %rsi, %rax
je 0xdf228
leaq (%r11,%r11,4), %r11
shlq $0x3, %r11
movq 0x8(%rsp), %rcx
addq 0x10(%rcx), %r11
cmpq $0x1, %r10
adcq $0x0, %r10
leaq (%r14,%r15,4), %r14
addq $0x8, %r14
xorl %r15d, %r15d
movl 0x18(%r14,%r15), %ecx
movl %ecx, 0x20(%r9,%r15)
movups -0x8(%r14,%r15), %xmm0
movups 0x8(%r14,%r15), %xmm1
movups %xmm1, 0x10(%r9,%r15)
movups %xmm0, (%r9,%r15)
xorb $0x20, %cl
movb %cl, 0x20(%r9,%r15)
movl %esi, 0x1c(%r9,%r15)
movl (%r11), %ecx
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
andl %r13d, %ecx
subl (%r14,%r15), %ecx
movl $0x0, %r12d
cmovbl %r12d, %ecx
movl %ecx, 0x4(%r9,%r15)
movl (%r11), %ebp
andl %r13d, %ebp
subl -0x4(%r14,%r15), %ebp
cmovbl %r12d, %ebp
addl %r8d, %ecx
movl %ecx, 0x4(%r9,%r15)
addl %r8d, %ebp
movl %ebp, 0x8(%r9,%r15)
movl %esi, 0x1c(%r9,%r15)
movq 0x30(%rbx), %r13
movl 0x8(%r13,%rdi,4), %r12d
cmpl %r12d, %ecx
jb 0xdf1d6
movl %r12d, 0x4(%r9,%r15)
movl 0x4(%r13,%rdi,4), %ecx
cmpl %ecx, 0x4(%r9,%r15)
ja 0xdf1e7
movl %ecx, 0x4(%r9,%r15)
cmpl %r12d, %ebp
jb 0xdf1f1
movl %r12d, 0x8(%r9,%r15)
leaq 0x4(,%rdi,4), %rcx
addq %r13, %rcx
movl (%rcx), %ecx
cmpl %ecx, 0x8(%r9,%r15)
ja 0xdf20a
movl %ecx, 0x8(%r9,%r15)
movl 0x4(%r9,%r15), %ecx
cmpl %ecx, 0x8(%r9,%r15)
ja 0xdf21b
movl %ecx, 0x8(%r9,%r15)
addq $0x24, %r15
decq %r10
jne 0xdf152
movq 0x30(%rbx), %rcx
movl %esi, 0xc(%rcx,%rdi,4)
movl %eax, 0x10(%rcx,%rdi,4)
movl $0xffffffff, (%rcx,%rdi,4) # imm = 0xFFFFFFFF
incq %rdx
cmpq 0x20(%rbx), %rdx
jb 0xdf012
cmpq 0x28(%rbx), %rax
ja 0xdf25d
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14c82c(%rip), %rdi    # 0x22ba90
leaq 0x14aa36(%rip), %rsi    # 0x229ca1
leaq 0x14c82e(%rip), %rcx    # 0x22baa0
movl $0x5a3e, %edx           # imm = 0x5A3E
callq 0x2110