// File: /workspace/github_cpp_makefile_star2_O1/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 0x7d4e2
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 0x7d4e9
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 0x7d63b
testq %r15, %r15
je 0x7d768
movl 0xc(%rsp), %eax
shll $0x1f, %eax
movq %rax, 0x10(%rsp)
addq $0x8, %r12
xorl %r13d, %r13d
movl (%r12), %esi
cmpl 0x50(%rsp), %esi
jbe 0x7d626
movl -0x4(%r12), %eax
cmpl 0x48(%rsp), %eax
jae 0x7d768
movq %rax, %rdi
cmpl $0x0, 0xc(%rsp)
je 0x7d562
movl (%rbp), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
subq %rsi, %rdi
cmpl $0x0, 0xc(%rsp)
je 0x7d575
movl (%rbp), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
subq %rax, %rsi
movl -0x8(%r12), %r14d
movq 0x38(%rsp), %rdx
movsd 0x40(%rsp), %xmm0
leaq 0x58(%rsp), %rcx
leaq 0x1c(%rsp), %r8
callq 0x7d216
testq %rax, %rax
je 0x7d626
shrl %r14d
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 %r14d, 0x28(%rsp)
movl %r13d, 0x1c(%rsp)
movq 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0x7d5f9
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rbx)
movq (%rbx), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
callq 0x2370
movq %rax, (%rbx)
movq (%rbx), %rax
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 %r13
addq $0xc, %r12
cmpq %r13, %r15
jne 0x7d52f
jmp 0x7d768
cmpl $0x0, 0x10(%rbp)
je 0x7d768
movl 0xc(%rsp), %eax
shll $0x1f, %eax
movl %eax, 0x10(%rsp)
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x18(%rbp), %rax
movq (%rax,%r12,8), %r14
movq %r14, %r15
shrq $0x21, %r15
leaq 0x20dbc4(%rip), %rax    # 0x28b230
movq 0x18(%rax), %rax
movq (%rax,%r15,8), %rsi
addq %r13, %rsi
cmpq 0x50(%rsp), %rsi
jbe 0x7d753
cmpq 0x48(%rsp), %r13
jae 0x7d768
movq %r13, %rdi
cmpl $0x0, 0xc(%rsp)
je 0x7d6a3
movl (%rbp), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
subq %rsi, %rdi
cmpl $0x0, 0xc(%rsp)
je 0x7d6b6
movl (%rbp), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
subq %r13, %rsi
movq 0x38(%rsp), %rdx
movsd 0x40(%rsp), %xmm0
leaq 0x58(%rsp), %rcx
leaq 0x1c(%rsp), %r8
callq 0x7d216
testq %rax, %rax
je 0x7d753
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 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0x7d726
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rbx)
movq (%rbx), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
callq 0x2370
movq %rax, (%rbx)
movq (%rbx), %rax
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 %r14d, %eax
addq %rax, %r13
incq %r12
movl 0x10(%rbp), %eax
cmpq %rax, %r12
jb 0x7d656
movq 0x8(%rbx), %rax
subq 0x60(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq