// File: /workspace/github_cpp_makefile_star2_O2/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, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, %r12
andq $0x0, 0x20(%r8)
xorps %xmm2, %xmm2
movq $0x0, (%rsp)
xorl %ebp, %ebp
xorl %ecx, %ecx
movq %rdi, 0x28(%rsp)
cmpq 0x8(%rdi), %rcx
jae 0xa2a01
movq 0x18(%rdi), %rax
movq %rcx, 0x30(%rsp)
movq (%rax,%rcx,8), %r9
movl %r9d, %eax
shrl %eax
imulq $0x28, %rax, %rdx
movq 0x18(%rsp), %rcx
addq 0x10(%rcx), %rdx
movq %rdx, 0x8(%rsp)
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
imulq $0x58, %rax, %rax
leaq (%rcx,%rax), %r14
addq $0x20, %r14
xorl %r13d, %r13d
xorl %r15d, %r15d
cmpq (%r14), %r15
jae 0xa29d5
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rax
cmpq 0x28(%rbx), %rcx
jne 0xa28fa
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x28(%rbx)
imulq $0x24, %rdx, %rsi
movq %rax, %rdi
movq %r15, 0x10(%rsp)
movq %r12, %r15
movq %r14, %r12
movq %rbx, %r14
movq %r9, %rbx
callq 0x2370
movq %rbx, %r9
movq %r14, %rbx
movq %r12, %r14
movq %r15, %r12
movq 0x10(%rsp), %r15
xorps %xmm2, %xmm2
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
imulq $0x24, %rcx, %r8
leaq (%rax,%r8), %rcx
andl $0x0, 0x20(%rax,%r8)
movups %xmm2, 0x10(%rax,%r8)
movups %xmm2, (%rax,%r8)
movq 0x10(%r14), %rdx
movups (%rdx,%r13), %xmm0
movups 0x10(%rdx,%r13), %xmm1
movups %xmm1, 0x10(%rax,%r8)
movl 0x20(%rdx,%r13), %edx
movl %edx, 0x20(%rax,%r8)
movups %xmm0, (%rax,%r8)
movl %ebp, 0x1c(%rax,%r8)
movq 0x10(%r14), %rdi
movl 0x4(%rdi,%r13), %edx
movl %edx, 0x4(%rax,%r8)
movl 0x8(%rdi,%r13), %esi
movl %esi, 0x8(%rax,%r8)
testb $0x1, %r9b
je 0xa298e
movq 0x8(%rsp), %r11
movl (%r11), %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edx
subl %esi, %edx
movl $0x0, %eax
cmovbl %eax, %edx
movl %edx, 0x4(%rcx)
movl (%r11), %esi
andl %r8d, %esi
subl 0x4(%rdi,%r13), %esi
cmovbl %eax, %esi
addl %ebp, %edx
movl %edx, 0x4(%rcx)
addl %ebp, %esi
movl %esi, 0x8(%rcx)
movl %r9d, (%rcx)
cmpq %r12, %rdx
jb 0xa29a7
movl %r12d, 0x4(%rcx)
movl %r12d, %edx
movl %esi, %eax
cmpq %r12, %rax
jb 0xa29b5
movl %r12d, 0x8(%rcx)
movl %r12d, %esi
cmpl %edx, %esi
jbe 0xa29c5
movl 0x10(%rcx), %eax
subl 0xc(%rcx), %eax
addq %rax, (%rsp)
jmp 0xa29c9
decq 0x20(%rbx)
incq %r15
addq $0x24, %r13
jmp 0xa2898
movq 0x8(%rsp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, %rbp
movq 0x28(%rsp), %rdi
movq 0x18(%rdi), %rax
movq 0x30(%rsp), %rcx
movl 0x4(%rax,%rcx,8), %eax
addq %rax, %rbp
incq %rcx
jmp 0xa2850
movq 0x20(%rbx), %rsi
movq 0x30(%rbx), %rdi
callq 0x167b92
movq 0x20(%rbx), %rcx
movq (%rsp), %r14
leaq (%rcx,%r14), %rdx
movq 0x30(%rbx), %rax
cmpq %rdx, 0x28(%rbx)
jae 0xa2a73
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
incq %rcx
movq %rcx, 0x28(%rbx)
imulq $0x24, %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rcx
imulq $0x24, %rcx, %rdi
addq %rax, %rdi
imulq $0x24, %r14, %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x20(%rbx), %rcx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rcx, %rdx
xorl %esi, %esi
cmpq %rcx, %rsi
jae 0xa2cc2
movq 0x30(%rbx), %r11
movq %rsi, 0x10(%rsp)
imulq $0x24, %rsi, %rsi
movl 0x10(%r11,%rsi), %r14d
movl (%r11,%rsi), %r8d
movl 0xc(%r11,%rsi), %r12d
movl 0x1c(%r11,%rsi), %r9d
movl %r8d, %ecx
shrl %ecx
movq 0x20(%rsp), %rdi
movq (%rdi), %rdi
imulq $0x58, %rcx, %r10
movq 0x30(%rdi,%r10), %r15
imulq $0x24, %r12, %rdi
imulq $0x24, %rdx, %r13
movq %r14, 0x8(%rsp)
movq %r14, %r10
movq %r12, (%rsp)
subq %r12, %r10
testb $0x1, %r8b
jne 0xa2ba5
addq %rdi, %r15
addq %r11, %r13
xorl %r14d, %r14d
subq $0x1, %r10
jb 0xa2c99
movups (%r15,%r14), %xmm0
movups 0x10(%r15,%r14), %xmm1
movups %xmm0, (%r13,%r14)
movups %xmm1, 0x10(%r13,%r14)
movl 0x20(%r15,%r14), %ecx
movl %ecx, 0x20(%r13,%r14)
movl 0x4(%r13,%r14), %edi
addl %r9d, %edi
movl %edi, 0x4(%r13,%r14)
movl 0x8(%r13,%r14), %ecx
addl %r9d, %ecx
movl %ecx, 0x8(%r13,%r14)
movl %edx, 0x1c(%r13,%r14)
movq 0x30(%rbx), %r11
movl 0x8(%r11,%rsi), %ebp
cmpl %ebp, %edi
jb 0xa2b5a
movl %ebp, 0x4(%r13,%r14)
movl %ebp, %edi
movl 0x4(%r11,%rsi), %r8d
cmpl %r8d, %edi
ja 0xa2b6c
movl %r8d, 0x4(%r13,%r14)
movl %r8d, %edi
cmpl %ebp, %ecx
jb 0xa2b77
movl %ebp, 0x8(%r13,%r14)
movl %ebp, %ecx
leaq (%r11,%rsi), %r8
addq $0x4, %r8
movl (%r8), %r8d
cmpl %r8d, %ecx
cmoval %ecx, %r8d
jbe 0xa2b90
cmpl %edi, %r8d
ja 0xa2b9c
cmpl %edi, %r8d
cmoval %r8d, %edi
movl %edi, 0x8(%r13,%r14)
addq $0x24, %r14
jmp 0xa2afc
imulq $0x28, %rcx, %r14
movq 0x18(%rsp), %rcx
addq 0x10(%rcx), %r14
addq %rdi, %r15
addq $0x8, %r15
addq %r11, %r13
addq $0x20, %r13
xorl %ebp, %ebp
subq $0x1, %r10
jb 0xa2c99
movl 0x18(%r15,%rbp), %ecx
movl %ecx, (%r13,%rbp)
movups -0x8(%r15,%rbp), %xmm0
movups 0x8(%r15,%rbp), %xmm1
movups %xmm1, -0x10(%r13,%rbp)
movups %xmm0, -0x20(%r13,%rbp)
xorb $0x20, %cl
movb %cl, (%r13,%rbp)
movl %edx, -0x4(%r13,%rbp)
movl (%r14), %edi
andl %eax, %edi
subl (%r15,%rbp), %edi
movl $0x0, %r8d
cmovbl %r8d, %edi
movl %edi, -0x1c(%r13,%rbp)
movl (%r14), %ecx
andl %eax, %ecx
subl -0x4(%r15,%rbp), %ecx
cmovbl %r8d, %ecx
addl %r9d, %edi
movl %edi, -0x1c(%r13,%rbp)
addl %r9d, %ecx
movl %ecx, -0x18(%r13,%rbp)
movl %edx, -0x4(%r13,%rbp)
movq 0x30(%rbx), %r11
movl 0x8(%r11,%rsi), %r8d
cmpl %r8d, %edi
jb 0xa2c4c
movl %r8d, -0x1c(%r13,%rbp)
movl %r8d, %edi
movl 0x4(%r11,%rsi), %r12d
cmpl %r12d, %edi
ja 0xa2c5e
movl %r12d, -0x1c(%r13,%rbp)
movl %r12d, %edi
cmpl %r8d, %ecx
jb 0xa2c6b
movl %r8d, -0x18(%r13,%rbp)
movl %r8d, %ecx
leaq (%r11,%rsi), %r8
addq $0x4, %r8
movl (%r8), %r8d
cmpl %r8d, %ecx
cmoval %ecx, %r8d
jbe 0xa2c84
cmpl %edi, %r8d
ja 0xa2c90
cmpl %edi, %r8d
cmoval %r8d, %edi
movl %edi, -0x18(%r13,%rbp)
addq $0x24, %rbp
jmp 0xa2bc2
movl %edx, 0xc(%r11,%rsi)
addq 0x8(%rsp), %rdx
subq (%rsp), %rdx
movl %edx, 0x10(%r11,%rsi)
orl $-0x1, (%r11,%rsi)
movq 0x10(%rsp), %rsi
incq %rsi
movq 0x20(%rbx), %rcx
jmp 0xa2a93
cmpq 0x28(%rbx), %rdx
ja 0xa2cd7
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x146ea2(%rip), %rdi    # 0x1e9b80
leaq 0x1450ac(%rip), %rsi    # 0x1e7d91
leaq 0x146ea4(%rip), %rcx    # 0x1e9b90
movl $0x5a3e, %edx           # imm = 0x5A3E
callq 0x2110