// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_sub_ov_adv(ul_idx_t const*, overlap_region*, double, utg_ct_t*, unsigned long, kv_ul_ov_t*)
uint64_t gen_sub_ov_adv(const ul_idx_t *udb, overlap_region* o, double o_rate, utg_ct_t *ct_a, uint64_t ct_n, kv_ul_ov_t *res)
{
	uint64_t ts, te, i, l, rn = res->n, rev = o->y_pos_strand, s, e, rid, t[2]; ul_ov_t z;
	ma_utg_t *u = &(udb->ug->u.a[o->y_id]); int64_t k;
    if(!rev){
        ts = o->y_pos_s; te = o->y_pos_e + 1; k = 0;
    } else {
        ts = u->len - (o->y_pos_e+1); te = u->len - o->y_pos_s; 
        k = ((int64_t)o->w_list.n)-1; if(k < 0) k = 0;
    }

    if(!ct_a) {    
        for (i = l = 0; i < u->n; i++) {
            rid = u->a[i]>>33; 
            s = l; e = l + Get_READ_LENGTH(R_INF, rid);
            l += (uint32_t)u->a[i];
            if(e <= ts) continue; 
            if(s >= te) break;
            t[0] = (rev?(u->len-e):(s)); t[1] = (rev?(u->len-s):(e));
            if(extract_subov(t[0], t[1], o, o_rate, &k, &z)) {
                ///[ts, te) -> whole interval rid at the unitig adjusted by the reverse 
                // z.ts = t[0]; z.te = t[1]; 
                ///the strand of rid at the unitig
                z.rev = rev; 
                ///non-contained read at the unitg
                z.el = 0; 
                ///rid
                z.tn = rid;
                ///i-th read at the unitig
                z.qn = i;
                kv_push(ul_ov_t, *res, z);
            }
        }
    } else {
        for (i = 0; i < ct_n; i++) {
            rid = ct_a[i].x>>1; s = ct_a[i].s; e = ct_a[i].e;
            if(e <= ts) continue; 
            if(s >= te) break;
            t[0] = (rev?(u->len-e):(s)); t[1] = (rev?(u->len-s):(e));
            if(extract_subov(t[0], t[1], o, o_rate, &k, &z)) {
                ///[ts, te) -> whole interval rid at the unitig adjusted by the reverse 
                // z.ts = t[0]; z.te = t[1]; 
                ///the strand of rid at the unitig
                z.rev = rev; 
                ///contained read at the unitg
                z.el = 1; 
                ///rid
                z.tn = rid;
                ///i-th read at the unitig
                z.qn = i;
                kv_push(ul_ov_t, *res, z);
            }
        }
    }
    return res->n-rn;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %rbx
movq %rcx, %r15
movq %rdx, %r12
movsd %xmm0, 0x40(%rsp)
movl 0x1c(%rsi), %edx
movq (%rdi), %rax
movl 0x10(%rsi), %ecx
leaq (%rcx,%rcx,4), %rbp
shlq $0x3, %rbp
addq 0x10(%rax), %rbp
movl 0x18(%rsi), %eax
movl %edx, 0xc(%rsp)
testl %edx, %edx
je 0x7cf6b
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl (%rbp), %edi
notl %eax
movl %eax, %ecx
addl %edi, %ecx
subl 0x14(%rsi), %edi
movq 0x48(%rsi), %rax
cmpq $0x2, %rax
movl $0x1, %edx
cmovgeq %rax, %rdx
decq %rdx
movl %edi, %eax
jmp 0x7cf72
movl 0x14(%rsi), %ecx
incl %eax
xorl %edx, %edx
movq %rsi, 0x38(%rsp)
movq 0x8(%rbx), %rsi
movq %rsi, 0x60(%rsp)
movl %ecx, %ecx
movq %rcx, 0x50(%rsp)
movl %eax, %eax
movq %rax, 0x48(%rsp)
movq %rdx, 0x58(%rsp)
testq %r12, %r12
je 0x7d0b9
testq %r15, %r15
je 0x7d1dd
movl 0xc(%rsp), %eax
shll $0x1f, %eax
movq %rax, 0x10(%rsp)
addq $0x8, %r12
xorl %r14d, %r14d
movl (%r12), %esi
cmpl 0x50(%rsp), %esi
jbe 0x7d0a4
movl -0x4(%r12), %edi
cmpl 0x48(%rsp), %edi
jae 0x7d1dd
cmpl $0x0, 0xc(%rsp)
je 0x7cff3
movl (%rbp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, %rcx
subq %rsi, %rcx
subq %rdi, %rax
movq %rcx, %rdi
movq %rax, %rsi
movl -0x8(%r12), %r13d
movq 0x38(%rsp), %rdx
movsd 0x40(%rsp), %xmm0
leaq 0x58(%rsp), %rcx
leaq 0x1c(%rsp), %r8
callq 0x7ccdc
testq %rax, %rax
je 0x7d0a4
shrl %r13d
movl 0x34(%rsp), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
movq 0x10(%rsp), %rcx
addl %ecx, %eax
addl $0x40000000, %eax       # imm = 0x40000000
movl %eax, 0x34(%rsp)
movl %r13d, 0x28(%rsp)
movl %r14d, 0x1c(%rsp)
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x7d07e
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movups 0x1c(%rsp), %xmm0
movups 0x28(%rsp), %xmm1
movups %xmm1, 0xc(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
incq %r14
addq $0xc, %r12
cmpq %r14, %r15
jne 0x7cfb8
jmp 0x7d1dd
cmpl $0x0, 0x10(%rbp)
je 0x7d1dd
movl 0xc(%rsp), %eax
shll $0x1f, %eax
movl %eax, 0x10(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
movq 0x18(%rbp), %rax
movq (%rax,%r12,8), %r13
movq %r13, %r15
shrq $0x21, %r15
leaq 0x219146(%rip), %rax    # 0x296230
movq 0x18(%rax), %rax
movq (%rax,%r15,8), %rsi
addq %r14, %rsi
cmpq 0x50(%rsp), %rsi
jbe 0x7d1c8
cmpq 0x48(%rsp), %r14
jae 0x7d1dd
cmpl $0x0, 0xc(%rsp)
je 0x7d128
movl (%rbp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, %rdi
subq %rsi, %rdi
subq %r14, %rax
movq %rax, %rsi
jmp 0x7d12b
movq %r14, %rdi
movq 0x38(%rsp), %rdx
movsd 0x40(%rsp), %xmm0
leaq 0x58(%rsp), %rcx
leaq 0x1c(%rsp), %r8
callq 0x7ccdc
testq %rax, %rax
je 0x7d1c8
movl 0x34(%rsp), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
orl 0x10(%rsp), %eax
movl %eax, 0x34(%rsp)
movl %r15d, 0x28(%rsp)
movl %r12d, 0x1c(%rsp)
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x7d1a2
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movups 0x1c(%rsp), %xmm0
movups 0x28(%rsp), %xmm1
movups %xmm1, 0xc(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
movl %r13d, %eax
addq %rax, %r14
incq %r12
movl 0x10(%rbp), %eax
cmpq %rax, %r12
jb 0x7d0d4
movq 0x8(%rbx), %rax
subq 0x60(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq