// File: /workspace/github_cpp_makefile_star2_O2/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
imulq $0x28, %rcx, %rbp
addq 0x10(%rax), %rbp
movl 0x18(%rsi), %eax
movl %edx, 0xc(%rsp)
testl %edx, %edx
je 0x49db0
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
pushq $0x1
popq %rdx
cmovgeq %rax, %rdx
decq %rdx
movl %edi, %eax
jmp 0x49db7
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 0x49ee1
movl 0xc(%rsp), %eax
shll $0x1f, %eax
movq %rax, 0x10(%rsp)
addq $0x8, %r12
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x49ff0
movl (%r12), %esi
cmpl 0x50(%rsp), %esi
jbe 0x49ed5
movl -0x4(%r12), %edi
cmpl 0x48(%rsp), %edi
jae 0x49ff0
cmpl $0x0, 0xc(%rsp)
je 0x49e38
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 0x49b3c
testq %rax, %rax
je 0x49ed5
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 0x49eb6
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
imulq $0x1c, %rcx, %rcx
movups 0x1c(%rsp), %xmm0
movups 0x28(%rsp), %xmm1
movups %xmm1, 0xc(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
incq %r14
addq $0xc, %r12
jmp 0x49df4
movl 0xc(%rsp), %eax
shll $0x1f, %eax
movl %eax, 0x10(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
movl 0x10(%rbp), %eax
cmpq %rax, %r14
jae 0x49ff0
movq 0x18(%rbp), %rax
movq (%rax,%r14,8), %r13
movq %r13, %r15
shrq $0x21, %r15
leaq 0x20a31c(%rip), %rax    # 0x254230
movq 0x18(%rax), %rax
movq (%rax,%r15,8), %rsi
addq %r12, %rsi
cmpq 0x50(%rsp), %rsi
jbe 0x49fe2
cmpq 0x48(%rsp), %r12
jae 0x49ff0
cmpl $0x0, 0xc(%rsp)
je 0x49f52
movl (%rbp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, %rdi
subq %rsi, %rdi
subq %r12, %rax
movq %rax, %rsi
jmp 0x49f55
movq %r12, %rdi
movq 0x38(%rsp), %rdx
movsd 0x40(%rsp), %xmm0
leaq 0x58(%rsp), %rcx
leaq 0x1c(%rsp), %r8
callq 0x49b3c
testq %rax, %rax
je 0x49fe2
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 %r14d, 0x1c(%rsp)
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x49fc3
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
imulq $0x1c, %rcx, %rcx
movups 0x1c(%rsp), %xmm0
movups 0x28(%rsp), %xmm1
movups %xmm1, 0xc(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
movl %r13d, %eax
addq %rax, %r12
incq %r14
jmp 0x49ef2
movq 0x8(%rbx), %rax
subq 0x60(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq